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());
 }
Пример #2
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());
 }
Пример #3
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);
     }
 }
Пример #4
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();
 }
Пример #5
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;
     }
 }
Пример #6
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;
 }