/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(\Database_Connection $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_Connection $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 partial for a JOIN statement and return it. * * @param object Database instance * @return string */ public function compile(\Database_Connection $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; }
/** * Compile the SQL query and return it. * * @param object Database instance * @return string */ public function compile(\Database_Connection $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; }