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;
 }
 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);
 }
Exemplo n.º 3
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;
     }
 }