Esempio n. 1
0
 public static function builder_call_nested_collection(Database_Query_Builder $builder, Jam_Event_Data $data, $groups = FALSE, $depth = 0)
 {
     $choices = array();
     if ($depth == 0) {
         $builder->root();
     }
     $items = $builder->order_by(':name_key', 'ASC');
     foreach ($items as $item) {
         if ($groups and $depth == 0) {
             $choices[$item->name()] = array();
             if ($item->children->count()) {
                 $choices[$item->name()] = self::builder_call_nested_collection($item->children->collection(), $data, $groups, $depth + 1);
             }
         } else {
             $choices[$item->id()] = str_repeat('   ', $depth) . $item->name();
             if ($item->children->count()) {
                 $choices = Arr::merge($choices, self::builder_call_nested_collection($item->children->collection(), $data, $groups, $depth + 1));
             }
         }
     }
     if ($depth == 0) {
         $data->return = $choices;
     } else {
         return $choices;
     }
 }
Esempio n. 2
0
 public function builder_call_where_in_scope(Database_Query_Builder $builder, Jam_Event_Data $data, Jam_Model $model)
 {
     if ($this->_scope) {
         foreach ((array) $this->_scope as $scope_field) {
             $builder->where($scope_field, '=', $model->{$scope_field});
         }
     }
 }
Esempio n. 3
0
 public function compile($db = NULL)
 {
     if (!is_object($db)) {
         $db = Database::instance($db);
     }
     $query = 'INSERT INTO ' . $db->quote_table($this->_table);
     $query .= ' (' . implode(', ', array_map(array($db, 'quote_column'), $this->_columns)) . ') ';
     if (is_array($this->_values)) {
         $quote = array($db, 'quote');
         $groups = array();
         foreach ($this->_values as $group) {
             foreach ($group as $offset => $value) {
                 if ((is_string($value) and array_key_exists($value, $this->_parameters)) === FALSE) {
                     $group[$offset] = $db->quote($value);
                 }
             }
             $groups[] = '(' . implode(', ', $group) . ')';
         }
         $query .= 'VALUES ' . implode(', ', $groups);
     } else {
         $query .= (string) $this->_values;
     }
     $this->_sql = $query;
     return parent::compile($db);
 }
Esempio n. 4
0
 public function compile(Database $db)
 {
     // Start with the basic syntax.
     $sql = 'CREATE TABLE ' . $db->quote_table($this->_table['name']);
     // You are allowed to create a table without any columns. Dont ask me why.
     if (count($this->_table['columns']) > 0) {
         // Get ready for the column data.
         $sql .= ' (';
         // Compile the columns in the normal way.
         foreach ($this->_table['columns'] as $name => $data) {
             $sql .= Database_Query_Builder::compile_column($data, $db) . ',';
         }
         // Compile constraints in a normal way
         foreach ($this->_table['constraints'] as $name => $data) {
             $sql .= Database_Query_Builder::compile_constraint($data, $db) . ',';
         }
         // Seperate the columns with commars, and add the table constraints at the end.
         $sql = rtrim($sql, ',') . ') ';
     }
     // Process table options
     foreach ($this->_table['options'] as $key => $option) {
         $sql .= Database_Query_Builder::compile_statement(array($key => $option)) . ' ';
     }
     // Remove the trailing space.
     return rtrim($sql, ' ') . ';';
 }
Esempio n. 5
0
 public function __construct($table)
 {
     // Set the table object.
     $this->_table = $table;
     // Because mummy says so.
     parent::__construct(Database::TRUNCATE, '');
 }
Esempio n. 6
0
 public function compile(Database $db)
 {
     $sql = 'CREATE TABLE ' . $db->quote_table($this->_table->name);
     if (count($this->_table->columns()) > 0) {
         $columns[] = Database_Query_Builder::compile_column($db, $column);
     }
     return $sql . ';';
 }
Esempio n. 7
0
 public function __construct($type, $name)
 {
     // Set the type of the object we're about to drop.
     $this->_drop_type = $type;
     // Set the object we're going to drop.
     $this->_name = $name;
     // Because mummy says so.
     parent::__construct(Database::DROP, '');
 }
Esempio n. 8
0
 /**
  * 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 ' . Database_Query_Builder::compile_conditions($db, $this->_where);
     }
     return $query;
 }
Esempio n. 9
0
 /**
  * Set the table and columns for an insert.
  *
  * @param   mixed  table name or array($table, $alias) or object
  * @param   array  column names
  * @return  void
  */
 public function __construct($table, array $columns = NULL)
 {
     // Set the inital table name
     $this->_table = $table;
     if (!empty($columns)) {
         // Set the column names
         $this->_columns = $columns;
     }
     // Start the query with no SQL
     return parent::__construct(Database::INSERT, '');
 }
Esempio n. 10
0
 /**
  * Set the table and columns for an insert.
  *
  * @param   mixed  table name or array($table, $alias) or object
  * @param   array  column names
  * @return  void
  */
 public function __construct($table = NULL, array $columns = NULL)
 {
     if ($table) {
         // Set the inital table name
         $this->_table = $table;
     }
     if ($columns) {
         // Set the column names
         $this->_columns = $columns;
     }
     // Start the query with no SQL
     return parent::__construct('', \Database::INSERT);
 }
Esempio n. 11
0
 /**
  * Set the table and columns for an insert.
  *
  * @param   mixed $table   table name or array($table, $alias) or object
  * @param   array $columns column names
  */
 public function __construct($table = null, array $columns = null)
 {
     if ($table) {
         // Set the inital table name
         $this->_table = $table;
     }
     if ($columns) {
         // Set the column names
         $this->_columns = $columns;
     }
     // Start the query with no SQL
     parent::__construct('', \DB::INSERT);
 }
Esempio n. 12
0
 /**
  * Compile the SQL query and return it.
  *
  * @param   object  Database instance
  * @return  string
  */
 public function compile(Database $db)
 {
     $query = 'ALTER TABLE ' . $db->quote_table($this->_table) . ' ';
     $lines = array();
     if ($this->_name !== NULL) {
         $lines[] = 'RENAME TO ' . $db->quote_table($this->_name) . '; ';
     }
     if (count($this->_add_columns) > 0) {
         $columns = array();
         $sql = $query . 'ADD(';
         foreach ($this->_add_columns as $name => $params) {
             $columns[] = Database_Query_Builder::compile_column($name, $params);
         }
         $sql .= implode($columns, ',') . '); ';
         $lines[] = $sql;
     }
     if (count($this->_modify_columns) > 0) {
         $columns = array();
         $sql = $query . 'MODIFY(';
         foreach ($this->_modify_columns as $name => $params) {
             $columns[] = Database_Query_Builder::compile_column($name, $params);
         }
         $sql .= implode($columns, ',') . '); ';
         $lines[] = $sql;
     }
     if (count($this->_drop_columns) > 0) {
         foreach ($this->_drop_columns as $name) {
             $drop = new Database_Query_Builder_Drop('column', $name);
             $lines[] = $drop->compile() . ';';
         }
     }
 }
Esempio n. 13
0
 public static function builder_call_slugs_children(Database_Query_Builder $builder, Jam_Event_Data $data, $slugs)
 {
     $builder->join(array('parent', 'parent'), 'LEFT')->where_open()->or_where('term.slug', 'IN', (array) $slugs)->or_where('parent.slug', 'IN', (array) $slugs)->where_close();
 }
Esempio n. 14
0
 /**
  * Compile the SQL query and return it.
  *
  * @param   Database  Database instance.
  * @return  string	The SQL query.
  */
 public function compile(Database $db)
 {
     // Initiate the alter statement
     $sql = 'ALTER TABLE ' . $db->quote_table($this->_original_name) . ' ';
     // If we have a name set, rename the table.
     if (isset($this->_new_name)) {
         // Prepare the rename SQL
         $sql .= 'RENAME TO ' . $db->quote_table($this->_new_name) . '; ';
         // Update the new name
         $this->_original_name = $this->_new_name;
     } elseif (!empty($this->_add_columns) or !empty($this->_add_constraints)) {
         // If we have more then one constraint or column, we need brackets
         $multi = count($this->_modify_columns) + count($this->_add_constraints) > 1;
         // Add the brackets and begin the statement
         $sql .= 'ADD ' . ($multi ? '(' : '');
         // Loop through each column, compiling it where necessary
         foreach ($this->_add_columns as $column) {
             $sql .= Database_Query_Builder::compile_column($column, $db) . ',';
         }
         // Loop through each constraint, compiling it where necessary
         foreach ($this->_add_constraints as $constraint) {
             $sql .= Database_Query_Builder::compile_constraint($column, $db) . ',';
         }
         // Remove the trailing commar and append a closing bracket where necessary
         $sql = rtrim($sql, ',') . ($multi ? ')' : '') . ';';
     } elseif (!empty($this->_modify_columns)) {
         // Check to see if we have more then one column
         $multi = count($this->_modify_columns) > 1;
         // Begin the modify statement, if we have multiple columns, add them as methods.
         $sql .= 'MODIFY ' . ($multi ? '(' : '');
         // Loop through each column, compile it, then add it to the sql string.
         foreach ($this->_modify_columns as $column) {
             $sql .= Database_Query_Builder::compile_column($column, $db) . ',';
         }
         // Return the sql statement with any closing brackets where necessary
         $sql = rtrim($sql, ',') . ($multi ? ')' : '') . ';';
     } elseif (!empty($this->_drop_columns) or !empty($this->_drop_constraints)) {
         // Reset the sql string, multiple drop methods cannot be put in the same query.
         $sql = '';
         // Foreach drop column, get the SQL and create a statement for it.
         foreach ($this->_drop_columns as $column) {
             // Start each drop statement as a new query
             $sql .= 'ALTER TABLE ' . $db->quote_table($this->_original_name) . ' ' . DB::drop('column', $column)->compile($db) . ';';
         }
         // Foreach drop constraint
         foreach ($this->_drop_constraints as $constraint => $type) {
             // Start each drop statement as a new query
             $sql .= 'ALTER TABLE ' . $db->quote_table($this->_original_name) . ' ' . DB::drop($type, $constraint)->compile($db) . ';';
         }
     }
     // return the SQL.
     return $sql;
 }
Esempio n. 15
0
 /**
  * Compile the SQL query and return it.
  *
  * @param   object  Database instance
  * @return  string
  */
 public function compile(Database $db)
 {
     // Callback to quote identifiers
     $quote_ident = array($db, 'quote_identifier');
     // 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_map($quote_ident, $this->_select));
     }
     if (!empty($this->_from)) {
         // Set tables to select from
         $query .= ' FROM ' . implode(', ', array_map(array($db, 'quote_table'), $this->_from));
     }
     if (!empty($this->_join)) {
         // Add tables to join
         $query .= ' ' . Database_Query_Builder::compile_join($db, $this->_join);
     }
     if (!empty($this->_where)) {
         // Add selection conditions
         $query .= ' WHERE ' . Database_Query_Builder::compile_conditions($db, $this->_where);
     }
     if (!empty($this->_group_by)) {
         // Add sorting
         $query .= ' GROUP BY ' . implode(', ', array_map($quote_ident, $this->_group_by));
     }
     if (!empty($this->_having)) {
         // Add filtering conditions
         $query .= ' HAVING ' . Database_Query_Builder::compile_conditions($db, $this->_having);
     }
     if (!empty($this->_order_by)) {
         // Add sorting
         $query .= ' ' . Database_Query_Builder::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;
     }
     return $query;
 }
Esempio n. 16
0
 /**
  * Apply std builder conditions
  *
  * @param    Database_Query_Builder  $builder   Instance of builder
  */
 private function _position_apply_builder_conditions($field, $builder)
 {
     $config = $this->_position_fields[$field];
     $model = $this->_orm;
     foreach ($config['group_by'] as $gb_field) {
         $builder->where($gb_field, '=', $model->{$gb_field});
     }
     $deleted_field = $this->deleted_field();
     if (array_key_exists($deleted_field, $model->table_columns())) {
         $builder->where($deleted_field, '=', 0);
     }
 }
Esempio n. 17
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;
 }
Esempio n. 18
0
 /**
  * Applies WHERE conditions to a query.
  *
  *     // Create a new SELECT query
  *     $query = DB::select();
  *
  *     // Apply model conditions
  *     $model->query_conditions($query);
  *
  * @param   object  query builder
  * @return  object
  */
 public function query_conditions(Database_Query_Builder $query)
 {
     // Import meta data
     $meta = static::meta($this);
     foreach ($meta->fields as $name => $field) {
         if (array_key_exists($name, $this->__changed)) {
             $query->where($meta->column($name), '=', $this->__changed[$name]);
         } elseif ($field->unique and $this->__data[$name]) {
             $query->where($meta->column($name), '=', $this->__data[$name]);
         }
     }
     return $query;
 }
Esempio n. 19
0
 /**
  * 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 insertion query
     $query = 'INSERT INTO ' . $db->quote_table($this->_table);
     // Add the column names
     $query .= ' (' . implode(', ', array_map(array($db, 'quote_column'), $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 $offset => $value) {
                 if ((is_string($value) and array_key_exists($value, $this->_parameters)) === FALSE) {
                     // Quote the value, it is not a parameter
                     $group[$offset] = $db->quote($value);
                 }
             }
             $groups[] = '(' . implode(', ', $group) . ')';
         }
         // Add the values
         $query .= 'VALUES ' . implode(', ', $groups);
     } else {
         // Add the sub-query
         $query .= (string) $this->_values;
     }
     $this->_sql = $query;
     return parent::compile($db);
 }
Esempio n. 20
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;
 }
Esempio n. 21
0
 /**
  * Формирование Database_Query_Builder в части филтрации документов
  * 
  * @param array $filters
  * @param array $t
  * @param Database_Query_Builder $query
  * @return Database_Query_Builder
  */
 protected function _fetch_filters(array $filters, &$t, &$query)
 {
     if (empty($filters)) {
         return $query;
     }
     $field_names = array_flip($this->get_field_names());
     $ds_fields = $this->get_fields();
     $sys_fields = $this->get_system_fields();
     foreach ($filters as $pos => $data) {
         $params = array();
         $field = $data['field'];
         if (!empty($data['params'])) {
             parse_str($data['params'], $params);
         }
         $condition = $data['condition'];
         $type = $data['type'];
         $invert = !empty($data['invert']);
         $value = Arr::get($data, 'value');
         if ($value !== NULL and $type != self::VALUE_PLAIN) {
             $ctx = Context::instance();
             $request = Request::initial();
             switch ($type) {
                 case self::VALUE_BEHAVIOR:
                     if ($ctx->behavior_router() instanceof Behavior_Route) {
                         $value = $ctx->behavior_router()->param($value);
                         break;
                     }
                 case self::VALUE_GET:
                     $value = $request->query($value);
                     break;
                 case self::VALUE_POST:
                     $value = $request->post($value);
                     break;
                 default:
                     $value = $ctx->get($value);
                     break;
             }
         }
         if ($value === NULL) {
             continue;
         }
         $field_id = strpos($field, '$') == 1 ? Context::instance()->get(substr($field, 1)) : $field;
         if (isset($sys_fields[$field_id])) {
             $field = $sys_fields[$field_id];
         } else {
             if (isset($ds_fields[$field_id])) {
                 $field = $ds_fields[$field_id];
             } else {
                 if (isset($field_names[$field_id])) {
                     $field = $ds_fields[$field_names[$field_id]];
                 } else {
                     $field = NULL;
                 }
             }
         }
         if (!$field instanceof DataSource_Hybrid_Field) {
             continue;
         }
         if (!isset($t[$field->ds_id])) {
             $query->join('dshybrid_' . $field->ds_id, 'dfilter' . $pos)->on('dfilter' . $pos . '.id', '=', 'ds.id');
             $t[$field->ds_id] = TRUE;
         }
         $in = FALSE;
         switch ($condition) {
             case self::COND_EQ:
                 $value = explode(',', $value);
                 if ($value[0] == '*') {
                     break;
                 } elseif (count($value) > 1) {
                     $in = TRUE;
                 } else {
                     $value = $value[0];
                 }
                 break;
             case self::COND_CONTAINS:
                 $value = explode(',', $value);
                 $in = TRUE;
                 break;
             case self::COND_BTW:
                 $value = explode(',', $value, 2);
                 if (count($value) != 2) {
                     break;
                 }
                 break;
             default:
                 $value = $value;
         }
         $in = $in === TRUE ? 'IN' : '=';
         if (is_array($value)) {
             foreach ($value as $i => $v) {
                 if ($this->is_db_function($value)) {
                     $value[$i] = DB::expr($v);
                 }
             }
         } else {
             if ($this->is_db_function($value)) {
                 $value = DB::expr($value);
             }
         }
         if (isset($params['db_function']) and !$this->is_db_function($params['db_function'])) {
             unset($params['db_function']);
         }
         $conditions = array($in, 'BETWEEN', '>', '<', '>=', '<=', 'IN', 'LIKE');
         $condition = Arr::get($conditions, $condition, '=');
         if ($invert === TRUE) {
             switch ($condition) {
                 case '>':
                     $condition = '<=';
                     break;
                 case '<':
                     $condition = '>=';
                     break;
                 case '=':
                     $condition = '!=';
                     break;
                 case 'IN':
                 case 'LIKE':
                 case 'BETWEEN':
                     $condition = 'NOT ' . $condition;
                     break;
                 case '>=':
                     $condition = '<';
                     break;
                 case '<=':
                     $condition = '>';
                     break;
             }
         }
         $type = NULL;
         $fid = NULL;
         foreach ($ds_fields as $id => $f) {
             if ($f->key == $field->key) {
                 $type = $f->type;
                 $fid = $id;
             }
         }
         $field->filter_condition($query, $condition, $value, $params);
     }
     unset($field_names, $ds_fields, $sys_fields, $filters);
     return $query;
 }
Esempio n. 22
0
 public function __construct($type, $object)
 {
     $this->_drop_type = $type;
     $this->_object = $object;
     parent::__construct(Database_Query_Type::DROP, '');
 }