public static function toString(TableSchema $schema, QueryContext $context, array $updates)
 {
     if (empty($updates)) {
         throw new DataTableException($schema->getSymbol(), "What do you want to update?");
     }
     if ($schema->existsField(TableSchema::RESERVED_FIELD_UPDATE_AT) && !array_key_exists(TableSchema::RESERVED_FIELD_UPDATE_AT, $updates)) {
         $updates[TableSchema::RESERVED_FIELD_UPDATE_AT] = time();
     }
     $statements = array();
     foreach ($updates as $key => $value) {
         if (!$schema->existsField($key)) {
             throw new FieldValidateException($schema->getSymbol(), $key, "Not exists field [{$key}] in table [" . $schema->getSymbol() . "].");
         }
         $field = $schema->field($key);
         if ($field->autoIncrement) {
             throw new FieldValidateException($schema->getSymbol(), $key, "Can't update auto-increment field [{$key}].");
         }
         if (ColumnInfo::FIELD_TYPE_ENUM === $field->type) {
             if (false === array_search($value, $field->enums)) {
                 throw new FieldValidateException($schema->getSymbol(), $key, "Not exists enum value [{$value}] in insert query. " . "The value must be one of (" . implode(',', $field->enums) . ").");
             }
         }
         if (isset($field->validation) && !$field->validation->valid($value)) {
             throw new FieldValidateException($schema->getSymbol(), $key, "Field value validation error. [{$key}]");
         }
         $statements[] = "`{$key}` = ?";
         $context->param($value);
     }
     return !empty($statements) ? "SET " . implode(',', $statements) : null;
 }
 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 #3
0
 private function parseTableQuery(QueryContext $context)
 {
     $context->schema($this->schema);
     switch ($this->queryType) {
         case self::QUERY_SELECT:
             return TableQueryExecutor::makeSelect($this->schema, $this->softDeleteLess, $this->selectFields, $this->conditions, $this->joins, $this->orders, $this->groups, $this->havings, $this->selectQuerySettings);
         case self::QUERY_UPDATE:
             return TableQueryExecutor::makeUpdate($this->schema, $this->updates, $this->conditions);
         case self::QUERY_DELETE:
             return TableQueryExecutor::makeDelete($this->schema, $this->conditions, $this->forceDelete);
         case self::QUERY_INSERT:
             return TableQueryExecutor::makeInsert($this->schema, $this->inserts, $this->insertGetId);
     }
     return null;
 }
 public function toQueryString(QueryContext $context)
 {
     if (empty($this->inserts)) {
         throw new DataTableException($this->schema->getSymbol(), "Can't execute INSERT without inserts.");
     }
     if ($this->schema->existsField(TableSchema::RESERVED_FIELD_CREATE_AT) && !array_key_exists(TableSchema::RESERVED_FIELD_CREATE_AT, $this->inserts)) {
         $this->inserts[TableSchema::RESERVED_FIELD_CREATE_AT] = time();
     }
     if ($this->schema->existsField(TableSchema::RESERVED_FIELD_UPDATE_AT) && !array_key_exists(TableSchema::RESERVED_FIELD_UPDATE_AT, $this->inserts)) {
         $this->inserts[TableSchema::RESERVED_FIELD_UPDATE_AT] = time();
     }
     $columns = $this->schema->columns();
     foreach ($columns as $item) {
         if (!array_key_exists($item, $this->inserts) && !$this->schema->field($item)->nullable && !$this->schema->field($item)->autoIncrement) {
             throw new FieldValidateException($this->schema->getSymbol(), $item, "Field [{$item}] can't be null.");
         }
     }
     $fields = array();
     $values = array();
     foreach ($this->inserts as $key => $value) {
         if (!$this->schema->existsField($key)) {
             throw new FieldValidateException($this->schema->getSymbol(), $key, "Not exists field [{$key}] in table [" . $this->schema->getSymbol() . "].");
         }
         $field = $this->schema->field($key);
         if ($field->autoIncrement) {
             throw new FieldValidateException($this->schema->getSymbol(), $key, "Can't set auto-increment field [{$key}] in insert query.");
         }
         if (ColumnInfo::FIELD_TYPE_ENUM === $field->type) {
             if (false === array_search($value, $field->enums)) {
                 throw new FieldValidateException($this->schema->getSymbol(), $key, "Not exists enum value [{$value}] in insert query. " . "The value must be one of (" . implode(',', $field->enums) . ").");
             }
         }
         if (isset($field->validation) && !$field->validation->valid($value)) {
             throw new FieldValidateException($this->schema->getSymbol(), $key, "Field value validation error. [{$key}]");
         }
         $fields[] = "`{$key}`";
         $values[] = "?";
         $context->param($value);
     }
     $statements = array("INSERT INTO", $this->schema->getSymbol(), "(", implode(',', $fields), ")VALUES(", implode(',', $values), ")");
     return implode(' ', $statements);
 }