public function execute(QueryContext $context)
 {
     if (!$this->forceDelete && $this->schema->getSoftDelete()) {
         $executor = TableQueryExecutor::makeUpdate($this->schema, array(TableSchema::SOFT_DELETE_FIELD => time()), $this->conditions);
         return $executor->execute($context);
     }
     $query = $this->toQueryString($context);
     return DB::delete($query, $context->getParams());
 }
 public function execute(QueryContext $context)
 {
     $query = $this->toQueryString($context);
     if (true === $this->insertGetId) {
         if (true === DB::insert($query, $context->getParams())) {
             return DB::table($this->schema->getName())->orderBy($this->schema->getAutoIncrementField(), 'DESC')->pluck($this->schema->getAutoIncrementField())->execute();
         }
     }
     return DB::insert($query, $context->getParams());
 }
Example #3
0
 private function clearTableSchema($name)
 {
     if (preg_match('#m[\\d]+(Create|Alter)(?<table>[a-zA-Z][a-zA-Z0-9\\_]+)Table#i', $name, $matches)) {
         $table = strtolower(Strings::camelToSnake($matches['table']));
         $path = App::path('cache') . "/schema/" . DB::getDatabaseName() . "/{$table}.dat";
         if (file_exists($path)) {
             unlink($path);
         }
     }
 }
Example #4
0
 public function test_error()
 {
     $query = \Xaircraft\DB::table('user')->whereIn('id', function (\Xaircraft\Database\WhereQuery $whereQuery) {
         $whereQuery->from('user')->select('id');
     });
     $query1 = $query;
     $list = $query->select()->execute();
     $list = $query1->select()->execute();
     var_dump($list);
     var_dump(\Xaircraft\DB::getQueryLog());
 }
 private function getQueryResult(QueryContext $context, $query)
 {
     $result = DB::select($query, $context->getParams());
     if (!empty($this->formats)) {
         $formatResult = array();
         foreach ($result as $row) {
             $item = array();
             foreach ($row as $key => $value) {
                 $item[$key] = array_key_exists($key, $this->formats) ? $this->formatConvert($row, $this->getFieldValue($key, $value), $this->formats[$key]) : $this->getFieldValue($key, $value);
             }
             $formatResult[] = $item;
         }
     } else {
         $formatResult = array();
         foreach ($result as $row) {
             $item = array();
             foreach ($row as $key => $value) {
                 $item[$key] = $this->getFieldValue($key, $value);
             }
             $formatResult[] = $item;
         }
     }
     if (!empty($formatResult)) {
         if ($this->pluck) {
             /** @var FieldInfo $field */
             $field = $this->selectFields[0];
             $alias = $field->getAlias();
             if (isset($alias)) {
                 return $formatResult[0][$alias];
             }
             return $formatResult[0][$field->getField()];
         }
         if ($this->singleField) {
             $result = array();
             foreach ($formatResult as $row) {
                 if (!empty($row)) {
                     foreach ($row as $key => $value) {
                         $result[] = $value;
                         break;
                     }
                 }
             }
             return $result;
         }
         if ($this->detail) {
             return isset($formatResult[0]) ? $formatResult[0] : null;
         }
     } else {
         if ($this->pluck || $this->detail) {
             return null;
         }
     }
     return $formatResult;
 }
Example #6
0
 private static function getParent($id, array &$traces, TableQuery $query = null)
 {
     /** @var Model $model */
     $model = DI::get(__CLASS__);
     if (!isset($query)) {
         $realQuery = DB::table($model->getSchema()->getName());
     } else {
         $realQuery = clone $query;
     }
     $current = $realQuery->where('id', $id)->select()->detail()->execute();
     if (!isset($traces)) {
         $traces = array();
     }
     $traces[] = $current['name'];
     $parentID = $current[$model->getParentIDField()];
     if ($parentID > 0) {
         self::getParent($parentID, $traces, $query);
     }
 }
Example #7
0
 public function test_order()
 {
     $query = DB::table('user')->orderBy('id', \Xaircraft\Database\OrderInfo::SORT_ASC)->select(array("id", "name", "project_id" => function (WhereQuery $whereQuery) {
         $whereQuery->from('project')->select('id')->top();
     }))->execute();
 }
 public function execute(QueryContext $context)
 {
     $query = $this->toQueryString($context);
     return DB::update($query, $context->getParams());
 }
Example #9
0
 private function initializeColumns()
 {
     $result = DB::query("SHOW FULL COLUMNS FROM `{$this->table}`");
     if (false === $result) {
         throw new DataTableException($this->table, "Table not exists - [{$this->table}]");
     }
     $this->columns = array();
     foreach ($result as $row) {
         $column = $this->parseColumn($row);
         $this->columns[$column->name] = $column;
     }
     $this->writeCache();
 }
Example #10
0
 private function setField($field, $value)
 {
     if (!$this->schema->existsField($field)) {
         throw new EntityException("Can't find field [{$field}] in table [" . $this->schema->getName());
     }
     if ($value != $this->shadows[$field]) {
         if ($this->autoIncrementField === $field) {
             if (!$this->exists) {
                 if (DB::table($this->schema->getName())->where($field, $value)->count()->execute() > 0) {
                     $this->exists = true;
                 }
                 $this->query = DB::table($this->schema->getName())->where($field, $value);
             }
         }
         $this->fields[$field] = $value;
         if ($this->autoIncrementField !== $field) {
             $this->updates[$field] = $value;
         }
         $this->shadows[$field] = $value;
     }
 }
Example #11
0
 public static function find($arg)
 {
     $model = self::model();
     if ($arg instanceof TableQuery) {
         if ($arg->getTableSchema()->getName() !== $model->schema->getName()) {
             throw new ModelException("TableQuery must be table [" . $model->schema->getName() . "]'s query.");
         }
         $query = $arg;
     } else {
         if (is_numeric($arg)) {
             $query = DB::table($model->schema->getName())->where($model->schema->getAutoIncrementField(), $arg)->select();
         } else {
             throw new ModelException("What do you want to find?");
         }
     }
     $model->loadData($query);
     if (is_numeric($arg) && !$model->isExists()) {
         throw new ModelException("Record not exists.");
     }
     return $model;
 }
 public function getDatabase()
 {
     return DB::getDatabaseName();
 }