/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { // Start a deletion query $query = 'DELETE FROM '.$db->quote_table($this->_table); if ( ! empty($this->_where)) { // Add deletion conditions $query .= ' WHERE '.$this->_compile_conditions($db, $this->_where); } if ( ! empty($this->_order_by)) { // Add sorting $query .= ' '.$this->_compile_order_by($db, $this->_order_by); } if ($this->_limit !== NULL && substr($db->_db_type, 0, 6) !== 'sqlite') { // Add limiting $query .= ' LIMIT '.$this->_limit; } return $query; }
/** * 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() . ';'; } } }
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, ' ') . ';'; }
/** * Compile the SQL partial for a JOIN statement and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { if ($this->_type) { $sql = strtoupper($this->_type) . ' JOIN'; } else { $sql = 'JOIN'; } // Quote the table name that is being joined $sql .= ' ' . $db->quote_table($this->_table); if (!empty($this->_using)) { // Quote and concat the columns $sql .= ' USING (' . implode(', ', array_map(array($db, 'quote_column'), $this->_using)) . ')'; } else { $conditions = array(); foreach ($this->_on as $condition) { // Split the condition list($c1, $op, $c2) = $condition; if ($op) { // Make the operator uppercase and spaced $op = ' ' . strtoupper($op); } // Quote each of the columns used for the condition $conditions[] = $db->quote_column($c1) . $op . ' ' . $db->quote_column($c2); } // Concat the conditions "... AND ..." $sql .= ' ON (' . implode(' AND ', $conditions) . ')'; } return $sql; }
/** * Compile the SQL partial for a JOIN statement and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { if ($this->_type) { $sql = strtoupper($this->_type) . ' JOIN'; } else { $sql = 'JOIN'; } // Quote the table name that is being joined $sql .= ' ' . $db->quote_table($this->_table) . ' ON '; $conditions = array(); foreach ($this->_on as $condition) { // Split the condition list($c1, $op, $c2) = $condition; // Quote each of the identifiers used for the condition $conditions[] = $db->quote_identifier($c1) . ' ' . strtoupper($op) . ' ' . $db->quote_identifier($c2); } // Concat the conditions "... AND ..." $conditions = implode(' AND ', $conditions); if (count($this->_on) > 1) { // Wrap the conditions in a group. Some databases (Postgre) will fail // when singular conditions are grouped like this. $sql .= '(' . $conditions . ')'; } else { $sql .= $conditions; } return $sql; }
/** * Compile the SQL partial for a JOIN statement and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { if ($this->_type) { $sql = strtoupper($this->_type).' JOIN'; } else { $sql = 'JOIN'; } // Quote the table name that is being joined $sql .= ' '.$db->quote_table($this->_table).' ON '; $conditions = array(); foreach ($this->_on as $condition) { // Split the condition list($c1, $op, $c2) = $condition; if ($op) { // Make the operator uppercase and spaced $op = ' '.strtoupper($op); } // Quote each of the identifiers used for the condition $conditions[] = $db->quote_identifier($c1).$op.' '.$db->quote_identifier($c2); } // Concat the conditions "... AND ..." $sql .= '('.implode(' AND ', $conditions).')'; return $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) { // Start a deletion query $query = 'DELETE FROM ' . $db->quote_table($this->_table); if (!empty($this->_where)) { // Add deletion conditions $query .= ' WHERE ' . Database_Query_Builder::compile_conditions($db, $this->_where); } return $query; }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { // Start an update query $query = 'UPDATE ' . $db->quote_table($this->_table); // Add the columns to update $query .= ' SET ' . $this->_compile_set($db, $this->_set); if (!empty($this->_where)) { // Add selection conditions $query .= ' WHERE ' . $this->_compile_conditions($db, $this->_where); } return $query; }
public function compile(Database $db) { switch ($this->_drop_type) { case 'database': return 'DROP DATABASE ' . $db->quote($this->_object->name); case 'table': return 'DROP TABLE ' . $db->quote_table($this->_object->name); case 'column': return 'DROP COLUMN ' . $db->quote_identifier($this->_object->name); default: throw new Database_Exception('Invalid drop object'); } return $query; }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { // Start an update query $query = 'UPDATE ' . $db->quote_table($this->_table); // Add the columns to update $query .= ' SET ' . $this->_compile_set($db, $this->_set); if (!empty($this->_where)) { // Add selection conditions $query .= ' WHERE ' . $this->_compile_conditions($db, $this->_where); } if ($this->_limit !== NULL && substr($db->_db_type, 0, 6) !== 'sqlite') { // Add limiting $query .= ' LIMIT ' . $this->_limit; } return $query; }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { // Start a deletion query $query = 'DELETE FROM ' . $db->quote_table($this->_table); if (!empty($this->_where)) { // Add deletion conditions $query .= ' WHERE ' . $this->_compile_conditions($db, $this->_where); } if (!empty($this->_order_by)) { // Add sorting $query .= ' ' . $this->_compile_order_by($db, $this->_order_by); } if ($this->_limit !== NULL) { // Add limiting $query .= ' LIMIT ' . $this->_limit; } $this->_sql = $query; return parent::compile($db); }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { // Start an update query $query = 'UPDATE ' . $db->quote_table($this->_table); $update = array(); foreach ($this->_set as $set) { // Split the set list($column, $value) = $set; // Quote the column name $column = $db->quote_identifier($column); $update[$column] = $column . ' = ' . $db->quote($value); } // Add the columns to update $query .= ' SET ' . implode(', ', $update); if (!empty($this->_where)) { // Add selection conditions $query .= ' WHERE ' . Database_Query_Builder::compile_conditions($db, $this->_where); } return $query; }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { // Start an update query $query = 'UPDATE ' . $db->quote_table($this->_table); // Add the columns to update $query .= ' SET ' . $this->_compile_set($db, $this->_set); if (!empty($this->_where)) { // Add selection conditions $query .= ' WHERE ' . $this->_compile_conditions($db, $this->_where); } if (!empty($this->_order_by)) { // Add sorting $query .= ' ' . $this->_compile_order_by($db, $this->_order_by); } if ($this->_limit !== NULL) { // Add limiting $query .= ' LIMIT ' . $this->_limit; } return $query; }
public function compile(Database $db) { // Lets identify the type switch (strtolower($this->_drop_type)) { // We're dropping an entire database! case 'database': return 'DROP DATABASE ' . $db->quote($this->_name); // Just a table to be dropped. // Just a table to be dropped. case 'table': return 'DROP TABLE ' . $db->quote_table($this->_name); // A column to be dropped. // A column to be dropped. case 'column': case 'constraint': case 'index': return 'DROP ' . strtoupper($this->_drop_type) . ' ' . $db->quote_identifier($this->_name); // Something we did not recognise. // Something we did not recognise. default: return 'DROP ' . strtoupper($this->_drop_type) . ' ' . $this->_name; } }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { // Start an insertion query $query = 'INSERT INTO ' . $db->quote_table($this->_table); // Add the column names $query .= ' (' . implode(', ', array_map(array($db, 'quote_column'), $this->_columns)) . ') '; if (is_array($this->_values)) { // Callback for quoting values $quote = array($db, 'quote'); $groups = array(); foreach ($this->_values as $group) { foreach ($group as $offset => $value) { if ((is_string($value) and array_key_exists($value, $this->_parameters)) === FALSE) { // Quote the value, it is not a parameter $group[$offset] = $db->quote($value); } } $groups[] = '(' . implode(', ', $group) . ')'; } // Add the values $query .= 'VALUES ' . implode(', ', $groups); } else { // Add the sub-query $query .= (string) $this->_values; } $this->_sql = $query; return parent::compile($db); }
public function compile(Database $db) { // Return the SQL, its straightforward. return 'TRUNCATE TABLE ' . $db->quote_table($this->_table); }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(Database $db) { // Start an insertion query $query = 'INSERT INTO ' . $db->quote_table($this->_table); // Add the column names $query .= ' (' . implode(', ', array_map(array($db, 'quote_identifier'), $this->_columns)) . ') '; if (is_array($this->_values)) { // Callback for quoting values $quote = array($db, 'quote'); $groups = array(); foreach ($this->_values as $group) { foreach ($group as $i => $value) { if (is_string($value) and isset($this->_parameters[$value])) { // Use the parameter value $group[$i] = $this->_parameters[$value]; } } $groups[] = '(' . implode(', ', array_map($quote, $group)) . ')'; } // Add the values $query .= 'VALUES ' . implode(', ', $groups); } else { // Add the sub-query $query .= (string) $this->_values; } return $query; }
/** * 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; }
public function compile(Database $db) { // Get the table and column names out of the references array. list($table, $column) = $this->_references; // If the bastards haven't set a name, we'll make one up. if (!isset($this->name)) { $this->name = 'fk_' . $this->_column . '_' . $table . '_' . $column; } // Compile a default array supported by the DBForge compiler. $result = array('name' => $this->name, 'params' => array('foreign key' => array($db->quote_identifier($this->_column)), 'references ' . $db->quote_table($table) => array($db->quote_identifier($column)))); // If we have an on_update action, add it to our magical array. if (isset($this->_on_update)) { $result['params'][] = 'on update ' . $this->_on_update; } // If we have an on_delete action, add it to the array. if (isset($this->_on_delete)) { $result['params'][] = 'on delete ' . $this->_on_delete; } // We assume that the constraint is created when it is compiled. $this->_loaded = TRUE; // Finally return our array. return $result; }