/** * 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; }
/** * Compiles an array of ORDER BY statements into an SQL partial. * * @param object Database instance * @param array sorting columns * @return string */ protected function _compile_order_by(\Database_Connection $db, array $columns) { $sort = array(); foreach ($columns as $group) { list($column, $direction) = $group; if (!empty($direction)) { // Make the direction uppercase $direction = ' ' . strtoupper($direction); } $sort[] = $db->quote_identifier($column) . $direction; } return 'ORDER BY ' . implode(', ', $sort); }