Esempio n. 1
0
 /**
  * Run a query.
  *
  * @param DB\Query $query Current query object.
  *
  * @return mixed
  */
 public function run(DB\Query $query)
 {
     $query->appendTablesPrefix(Core\Config()->DB['tables_prefix']);
     $sql = $this->buildSql($query);
     $query_hash = md5(serialize(array('query' => $sql, 'bind_params' => $query->bind_params)));
     $query_cache_name = implode(',', array($query->table, implode(',', array_map(function ($item) {
         return $item['table'];
     }, $query->join))));
     if (array_key_exists($query_hash, Core\DbCache()->getCache($query_cache_name))) {
         return Core\DbCache()->getCache($query_cache_name, $query_hash);
     }
     $this->storeQueries($sql, $query->bind_params);
     if ($query->type === 'select') {
         $res = $this->query($sql, $query->bind_params);
         Core\DbCache()->setCache($query_cache_name, $query_hash, $res);
         return $res;
     }
     foreach (Core\DbCache()->cache as $table => $value) {
         if (in_array($query->table, explode(',', $table), true)) {
             Core\DbCache()->clearCache($table);
         }
     }
     return $this->execute($sql, $query->bind_params);
 }
Esempio n. 2
0
 /**
  * Drop columns.
  *
  * @param string $tableName Table name.
  * @param array  $columns   Array of column names.
  *
  * @return void
  */
 protected function dropColumns($tableName, array $columns)
 {
     $query = new Query();
     $query->dropColumns($tableName)->columns($columns)->run();
 }
Esempio n. 3
0
 /**
  * Checks if a key-value pair exists.
  *
  * @param string $key Cache key.
  *
  * @return boolean
  */
 public function exists($key)
 {
     $query = new Query();
     $exists = $query->select('all')->from($this->tableName)->where("{$this->fields[0]} = ?", array(md5($key)))->exists();
     return $exists;
 }
Esempio n. 4
0
 /**
  * Assigns filtering of the results.
  *
  * @param DB\Query $query  Current query object instance of BaseModel or its children.
  * @param array    $params Query params to format filtering criteria.
  *
  * @access private
  * @static
  * @uses   Core\DB()
  * @uses   Core\DbCache()
  * @uses   Core\Helpers\SQL
  *
  * @return Core\Modules\DB\Query
  */
 private static function assignFilter(DB\Query $query, array $params)
 {
     if (isset($params['filtering']) && !empty($params['filtering']) && is_array($params['filtering'])) {
         $model_fields = $query->getObject()->getSchema();
         if ($query->getObject()->hasAndBelongsToMany) {
             $model_fields = array_merge($model_fields, $query->getObject()->hasAndBelongsToMany);
         }
         foreach ($params['filtering'] as $field => $value) {
             if ($value && isset($model_fields[$field])) {
                 if (is_array($value)) {
                     if (isset($value['start'], $value['end']) && !empty($value['start']) && !empty($value['end'])) {
                         if (in_array($model_fields[$field]['type'], array('date', 'datetime'), true)) {
                             $decorator = 'Core\\Modules\\DB\\Decorators\\Interfaces\\TimezoneAwareness';
                             if (is_subclass_of($query->getObject(), $decorator)) {
                                 $value['start'] = Core\Helpers\DateTime::formatGmt($value['start'] . date(' H:i:s'), 'Y-m-d');
                                 $value['end'] = Core\Helpers\DateTime::formatGmt($value['end'] . date(' H:i:s'), 'Y-m-d');
                             }
                             $query = $query->where("(DATE({$field}) BETWEEN " . Core\DB()->escapeString($value['start']) . ' AND ' . Core\DB()->escapeString($value['end']) . ')');
                         } else {
                             $query = $query->where("{$field} BETWEEN " . Core\DB()->escapeString($value['start']) . ' AND ' . Core\DB()->escapeString($value['end']));
                         }
                     } else {
                         if (isset($query->getObject()->hasAndBelongsToMany[$field]) && $query->getObject()->hasAndBelongsToMany[$field]) {
                             $related = $query->getObject()->hasAndBelongsToMany[$field];
                             $obj = $query->getObject();
                             $primaryKey = $obj->primaryKeyField();
                             $prefix = Core\Config()->DB['tables_prefix'];
                             foreach ($value as $v) {
                                 $query = $query->join("{$related['table']} as {$related['table']}{$v}", "{$prefix}{$obj::$tableName}.{$primaryKey} = " . "{$related['table']}{$v}.{$related['key']}" . ' AND ' . "{$related['table']}{$v}.{$related['relative_key']} = {$v}");
                             }
                         }
                     }
                 } elseif (strlen($value)) {
                     if ($model_fields[$field]['type'] === 'string') {
                         $value_to_match = trim(Core\DB()->escapeString($value), "'");
                         $query = $query->where("{$field} LIKE \"%{$value_to_match}%\"");
                     } else {
                         $query = $query->where($field . ' = ' . Core\DB()->escapeString($value));
                     }
                 }
             }
         }
     }
     return $query;
 }
Esempio n. 5
0
 /**
  * Rollback method.
  *
  * @param integer $step Count of steps to rollback.
  *
  * @return void
  */
 public static function rollback($step = 1)
 {
     $query = new Query();
     $migrations_to_execute = $query->select('*')->from('migrations')->order('version', 'desc')->limit($step)->all();
     foreach ($migrations_to_execute as $item) {
         DB\Migrate::down($item['version']);
     }
 }
Esempio n. 6
0
 /**
  * Get object/objects from the Database.
  *
  * @param string $fields List of fields to return (optional).
  *
  * @access public
  * @final
  * @static
  *
  * @return DB\Query
  */
 public static final function find($fields = 'all')
 {
     $query = new DB\Query(get_called_class());
     if (static::$isI18n) {
         if (!static::$i18nLocale) {
             $_locale = Core\Registry()->get('locale');
             static::$i18nLocale = isset(Core\Config()->I18N['locales'][$_locale]) ? $_locale : Core\Config()->I18N['default'];
         }
         if (!static::$i18nTableName) {
             static::$i18nTableName = static::$tableName . static::$i18nTableNameSuffix;
         }
         $prefix = Core\Config()->DB['tables_prefix'];
         return $query->select($fields)->from(static::$tableName)->join(static::$i18nTableName, $prefix . static::$tableName . '.' . static::$primaryKeyField . ' = ' . $prefix . static::$i18nTableName . '.' . static::$i18nForeignKeyField . ' AND ' . $prefix . static::$i18nTableName . '.' . static::$i18nLocaleField . ' = "' . static::$i18nLocale . '"');
     }
     return $query->select($fields)->from(static::$tableName);
 }
Esempio n. 7
0
 /**
  * @covers Core\Modules\Cache\Adapters\Database::store
  */
 public function testStoringKeyValuePair()
 {
     $query = new Query();
     $query->remove()->from($this->tableName)->where("{$this->fields[0]} = ?", array(md5($this->key)))->run();
     $this->assertTrue($this->databaseCache->store($this->key, $this->value));
 }
Esempio n. 8
0
 /**
  * Remove Ownership Data for an Owner.
  *
  * @param \Core\Base\Model|null $owner Owner instance
  *
  * @return void
  */
 public static function resetOwner(Base\Model $owner = null)
 {
     if (!$owner) {
         $owner = Core\Registry()->get('current_cms_user');
     }
     $query = new Core\Modules\DB\Query();
     Core\DB()->run($query->remove()->from('cms_ownership')->where('owner_id = ? AND model = ?', array($owner->getPrimaryKeyValue(), get_class($resource))));
 }