/** * Set the table for a update. * * @param mixed table name or array($table, $alias) or object * @return void */ public function __construct($table) { // Set the inital table name $this->_table = $table; // Start the query with no SQL return parent::__construct(Database::UPDATE, ''); }
/** * Sets the initial columns to select from. * * @param array column list * @return void */ public function __construct(array $columns = NULL) { if (!empty($columns)) { // Set the initial columns $this->_select = $columns; } // Start the query with no actual SQL statement parent::__construct(\DB::SELECT, ''); }
/** * Set the table for a delete. * * @param mixed $table * table name or array($table, $alias) or object */ public function __construct($table = null) { if ($table) { // Set the inital table name $this->_table = $table; } // Start the query with no SQL return parent::__construct('', \DB::DELETE); }
/** * Set the table for a update. * * @param mixed $table table name or array($table, $alias) or object * * @return void */ public function __construct($table = NULL) { if ($table) { // Set the inital table name $this->_table = $table; } // Start the query with no SQL parent::__construct('', \DB::UPDATE); }
public function compile($db = NULL) { if (!is_object($db)) { $db = Database::instance($db); } $query = 'DELETE FROM ' . $db->quote_table($this->_table); if (!empty($this->_where)) { $query .= ' WHERE ' . $this->_compile_conditions($db, $this->_where); } if (!empty($this->_order_by)) { $query .= ' ' . $this->_compile_order_by($db, $this->_order_by); } if ($this->_limit !== NULL) { $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 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 mixed $db Database instance or name of instance * * @return string */ public function compile($db = null) { if (!is_object($db)) { // Get the database instance $db = Database::instance($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 mixed $db Database instance or name of instance * @return string */ public function compile($db = NULL) { if (!is_object($db)) { // Get the database instance $db = Database::instance($db); } // Callback to quote columns $quote_column = array($db, 'quote_column'); // Callback to quote tables $quote_table = array($db, 'quote_table'); // Start a selection query $query = 'SELECT '; if ($this->_distinct === TRUE) { // Select only unique results $query .= 'DISTINCT '; } if (empty($this->_select)) { // Select all columns $query .= '*'; } else { // Select all columns $query .= implode(', ', array_unique(array_map($quote_column, $this->_select))); } if (!empty($this->_from)) { // Set tables to select from $query .= ' FROM ' . implode(', ', array_unique(array_map($quote_table, $this->_from))); } if (!empty($this->_join)) { // Add tables to join $query .= ' ' . $this->_compile_join($db, $this->_join); } if (!empty($this->_where)) { // Add selection conditions $query .= ' WHERE ' . $this->_compile_conditions($db, $this->_where); } if (!empty($this->_group_by)) { // Add grouping $query .= ' ' . $this->_compile_group_by($db, $this->_group_by); } if (!empty($this->_having)) { // Add filtering conditions $query .= ' HAVING ' . $this->_compile_conditions($db, $this->_having); } 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; } if ($this->_offset !== NULL) { // Add offsets $query .= ' OFFSET ' . $this->_offset; } if (!empty($this->_union)) { foreach ($this->_union as $u) { $query .= ' UNION '; if ($u['all'] === TRUE) { $query .= 'ALL '; } $query .= $u['select']->compile($db); } } $this->_sql = $query; return parent::compile($db); }
/** * Count the number of records in the table. * * @return integer */ public function count_all() { $selects = array(); foreach ($this->_db_pending as $key => $method) { if ($method['name'] == 'select') { // Ignore any selected columns for now $selects[] = $method; unset($this->_db_pending[$key]); } } if (!empty($this->_load_with)) { foreach ($this->_load_with as $alias) { // Bind relationship $this->with($alias); } } $this->_build(Database::SELECT); $records = $this->_db_builder->from(array($this->_table_name, $this->_object_name))->select(array('COUNT("*")', 'records_found'))->execute($this->_db)->get('records_found'); // Add back in selected columns $this->_db_pending += $selects; $this->reset(); // Return the total number of records in a table return $records; }
/** * Compile the SQL query and return it. * * @param mixed $db Database instance or name of instance * @return string */ public function compile($db = NULL) { if (!is_object($db)) { // Get the database instance $db = Database::instance($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; } $this->_sql = $query; return parent::compile($db); }
public function compile($db = NULL) { if (!is_object($db)) { $db = Database::instance($db); } $quote_column = array($db, 'quote_column'); $quote_table = array($db, 'quote_table'); $query = 'SELECT '; if ($this->_distinct === TRUE) { $query .= 'DISTINCT '; } if (empty($this->_select)) { $query .= '*'; } else { $query .= implode(', ', array_unique(array_map($quote_column, $this->_select))); } if (!empty($this->_from)) { $query .= ' FROM ' . implode(', ', array_unique(array_map($quote_table, $this->_from))); } if (!empty($this->_join)) { $query .= ' ' . $this->_compile_join($db, $this->_join); } if (!empty($this->_where)) { $query .= ' WHERE ' . $this->_compile_conditions($db, $this->_where); } if (!empty($this->_group_by)) { $query .= ' ' . $this->_compile_group_by($db, $this->_group_by); } if (!empty($this->_having)) { $query .= ' HAVING ' . $this->_compile_conditions($db, $this->_having); } if (!empty($this->_order_by)) { $query .= ' ' . $this->_compile_order_by($db, $this->_order_by); } if ($this->_limit !== NULL) { $query .= ' LIMIT ' . $this->_limit; } if ($this->_offset !== NULL) { $query .= ' OFFSET ' . $this->_offset; } if (!empty($this->_union)) { $query = '(' . $query . ')'; foreach ($this->_union as $u) { $query .= ' UNION '; if ($u['all'] === TRUE) { $query .= 'ALL '; } $query .= '(' . $u['select']->compile($db) . ')'; } } $this->_sql = $query; return parent::compile($db); }