Example #1
0
 /**
  * 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 $db, array $columns)
 {
     $sort = array();
     foreach ($columns as $group) {
         list($column, $direction) = $group;
         if (is_array($column)) {
             // Use the column alias
             $column = $db->quote_identifier(end($column));
         } else {
             // Apply proper quoting to the column
             $column = $db->quote_column($column);
         }
         if ($direction) {
             // Make the direction uppercase
             $direction = ' ' . strtoupper($direction);
         }
         $sort[] = $column . $direction;
     }
     return 'ORDER BY ' . implode(', ', $sort);
 }
Example #2
0
 /**
  * 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(DB::expr('COUNT(' . $this->_db->quote_column($this->_object_name . '.' . $this->_primary_key) . ')'), '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 (int) $records;
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * 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 $db, array $columns)
 {
     $sort = array();
     foreach ($columns as $group) {
         list($column, $direction) = $group;
         if ($direction) {
             // Make the direction uppercase
             $direction = strtoupper($direction);
         }
         if ($column) {
             // Quote the column, if it has a value
             $column = $db->quote_column($column);
         }
         $sort[] = trim($column . ' ' . $direction);
     }
     return 'ORDER BY ' . implode(', ', $sort);
 }