Example #1
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) . ' 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;
 }
Example #2
0
 /**
  * Compiles the current object into a string.
  *
  * @param	string	The column name.
  * @param	string	The operator used in the conditional statement.
  * @param	object	The value to compare the column with.
  * @return	Database_Constraint_Check	The current object.
  */
 protected function _compile_check(array $data, Database $db)
 {
     // We have a keyword
     if (is_array(reset($data))) {
         // AND or OR
         $keyword = key(reset($data));
         // Compile the check params into a single string
         return $keyword . ' ' . $db->quote_identifier($data[0]) . ' ' . $data[1] . ' ' . $db->quote($data[2]);
     } else {
         // Compile the check params into a single string
         return $db->quote_identifier($data[0]) . ' ' . $data[1] . ' ' . $db->quote($data[2]);
     }
 }
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) . ' 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 ..."
     $sql .= '(' . implode(' AND ', $conditions) . ')';
     return $sql;
 }
Example #4
0
 public function compile(Database $db)
 {
     // If the asshole hasnt given us a name, we'll generate one
     if (!isset($this->name)) {
         $this->name = 'key_' . $this->_key;
     }
     // We assume that the constraint is created when it is compiled.
     $this->_loaded = TRUE;
     // Return the DBForge constraint array.
     return array('name' => $this->name, 'params' => array('unique' => $db->quote_identifier($this->_key)));
 }
Example #5
0
 /**
  * Compiles an array of ORDER BY statements into an SQL partial.
  *
  * @param   object  Database instance
  * @param   array   sorting columns
  * @return  string
  */
 public static function compile_order_by(Database $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);
 }
Example #6
0
 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;
 }
Example #7
0
 /**
  * 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;
 }
Example #8
0
 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;
     }
 }
Example #9
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 #10
0
 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;
 }
Example #11
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_identifier($column);
			}

			$sort[] = trim($column.' '.$direction);
		}

		return 'ORDER BY '.implode(', ', $sort);
	}
Example #12
0
 /**
  * Compiles a column array into SQL syntax.
  * 
  * @param	Database	The active database instance.
  * @param   array   The column array.
  * @return  string	The SQL syntax.
  */
 public static function compile_column(array $column, Database $db)
 {
     // Start with the column name
     $sql = $db->quote_identifier($column['name']) . ' ';
     // Compile the datatype
     $sql .= self::compile_method($column['datatype'], array($db, 'quote')) . ' ';
     // Compile the column constraints
     foreach ($column['constraints'] as $name => $data) {
         // Use the compile statement method to compile the statement
         $sql .= Database_Query_Builder::compile_statement(array($name => $data), ' ') . ' ';
     }
     // Remove the trailing space
     $sql = rtrim($sql, ' ');
     // Return the SQL as is.
     return $sql;
 }
Example #13
0
 /**
  * Produce SQL code from this object.
  *
  * Processes this expression and returns the resulting SQL
  * string.
  *
  * @return string
  */
 public function sql_string()
 {
     return Database::quote_identifier($this->identifier);
 }