public function compile(Database $db) { // Start with the basic syntax. $sql = 'CREATE TABLE ' . $db->quote_table($this->_table['name']); // You are allowed to create a table without any columns. Dont ask me why. if (count($this->_table['columns']) > 0) { // Get ready for the column data. $sql .= ' ('; // Compile the columns in the normal way. foreach ($this->_table['columns'] as $name => $data) { $sql .= Database_Query_Builder::compile_column($data, $db) . ','; } // Compile constraints in a normal way foreach ($this->_table['constraints'] as $name => $data) { $sql .= Database_Query_Builder::compile_constraint($data, $db) . ','; } // Seperate the columns with commars, and add the table constraints at the end. $sql = rtrim($sql, ',') . ') '; } // Process table options foreach ($this->_table['options'] as $key => $option) { $sql .= Database_Query_Builder::compile_statement(array($key => $option)) . ' '; } // Remove the trailing space. return rtrim($sql, ' ') . ';'; }
public function compile(Database $db) { $sql = 'CREATE TABLE ' . $db->quote_table($this->_table->name); if (count($this->_table->columns()) > 0) { $columns[] = Database_Query_Builder::compile_column($db, $column); } return $sql . ';'; }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { $query = 'ALTER TABLE ' . $db->quote_table($this->_table) . ' '; $lines = array(); if ($this->_name !== NULL) { $lines[] = 'RENAME TO ' . $db->quote_table($this->_name) . '; '; } if (count($this->_add_columns) > 0) { $columns = array(); $sql = $query . 'ADD('; foreach ($this->_add_columns as $name => $params) { $columns[] = Database_Query_Builder::compile_column($name, $params); } $sql .= implode($columns, ',') . '); '; $lines[] = $sql; } if (count($this->_modify_columns) > 0) { $columns = array(); $sql = $query . 'MODIFY('; foreach ($this->_modify_columns as $name => $params) { $columns[] = Database_Query_Builder::compile_column($name, $params); } $sql .= implode($columns, ',') . '); '; $lines[] = $sql; } if (count($this->_drop_columns) > 0) { foreach ($this->_drop_columns as $name) { $drop = new Database_Query_Builder_Drop('column', $name); $lines[] = $drop->compile() . ';'; } } }
/** * Compile the SQL query and return it. * * @param Database Database instance. * @return string The SQL query. */ public function compile(Database $db) { // Initiate the alter statement $sql = 'ALTER TABLE ' . $db->quote_table($this->_original_name) . ' '; // If we have a name set, rename the table. if (isset($this->_new_name)) { // Prepare the rename SQL $sql .= 'RENAME TO ' . $db->quote_table($this->_new_name) . '; '; // Update the new name $this->_original_name = $this->_new_name; } elseif (!empty($this->_add_columns) or !empty($this->_add_constraints)) { // If we have more then one constraint or column, we need brackets $multi = count($this->_modify_columns) + count($this->_add_constraints) > 1; // Add the brackets and begin the statement $sql .= 'ADD ' . ($multi ? '(' : ''); // Loop through each column, compiling it where necessary foreach ($this->_add_columns as $column) { $sql .= Database_Query_Builder::compile_column($column, $db) . ','; } // Loop through each constraint, compiling it where necessary foreach ($this->_add_constraints as $constraint) { $sql .= Database_Query_Builder::compile_constraint($column, $db) . ','; } // Remove the trailing commar and append a closing bracket where necessary $sql = rtrim($sql, ',') . ($multi ? ')' : '') . ';'; } elseif (!empty($this->_modify_columns)) { // Check to see if we have more then one column $multi = count($this->_modify_columns) > 1; // Begin the modify statement, if we have multiple columns, add them as methods. $sql .= 'MODIFY ' . ($multi ? '(' : ''); // Loop through each column, compile it, then add it to the sql string. foreach ($this->_modify_columns as $column) { $sql .= Database_Query_Builder::compile_column($column, $db) . ','; } // Return the sql statement with any closing brackets where necessary $sql = rtrim($sql, ',') . ($multi ? ')' : '') . ';'; } elseif (!empty($this->_drop_columns) or !empty($this->_drop_constraints)) { // Reset the sql string, multiple drop methods cannot be put in the same query. $sql = ''; // Foreach drop column, get the SQL and create a statement for it. foreach ($this->_drop_columns as $column) { // Start each drop statement as a new query $sql .= 'ALTER TABLE ' . $db->quote_table($this->_original_name) . ' ' . DB::drop('column', $column)->compile($db) . ';'; } // Foreach drop constraint foreach ($this->_drop_constraints as $constraint => $type) { // Start each drop statement as a new query $sql .= 'ALTER TABLE ' . $db->quote_table($this->_original_name) . ' ' . DB::drop($type, $constraint)->compile($db) . ';'; } } // return the SQL. return $sql; }