Пример #1
0
 public function schema(TableSchema $schema, $isSubSchema = false)
 {
     if (!$isSubSchema) {
         $this->schemas[$schema->getSymbol()] = $schema;
     } else {
         $this->subQuerySchemas[$schema->getSymbol()] = $schema;
     }
 }
 public function toQueryString(QueryContext $context)
 {
     if (empty($this->conditions)) {
         throw new DataTableException($this->schema->getSymbol(), "Can't execute DELETE query without conditions.");
     }
     $statements = array("DELETE FROM", $this->schema->getSymbol(), "WHERE", ConditionQueryBuilder::toString($context, $this->conditions));
     return implode(' ', $statements);
 }
 public function toQueryString(QueryContext $context)
 {
     $updatetion = UpdatetionQueryBuilder::toString($this->schema, $context, $this->updates);
     $condition = ConditionQueryBuilder::toString($context, $this->conditions);
     $statements = array();
     $statements[] = "UPDATE " . $this->schema->getSymbol();
     $statements[] = $updatetion;
     $statements[] = "WHERE " . $condition;
     return implode(' ', $statements);
 }
 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);
 }
Пример #6
0
 public function getQueryString(QueryContext $context)
 {
     if (!$this->subQuery) {
         if ($this->subQueryLimit) {
             throw new QueryException("Must be sub-query in [" . ConditionQueryBuilder::toString($context, $this->conditions) . "]");
         }
         return ConditionQueryBuilder::toString($context, $this->conditions);
     } else {
         $context->schema($this->subQueryTableSchema);
         if (!$this->softDeleteLess && $this->subQueryTableSchema->getSoftDelete()) {
             $this->addCondition(ConditionInfo::make(ConditionInfo::CONDITION_AND, WhereConditionBuilder::makeNormal($this->subQueryTableSchema->getFieldSymbol(TableSchema::SOFT_DELETE_FIELD, false), '=', 0)));
         }
         $statements = array();
         $statements[] = SelectionQueryBuilder::toString($context, $this->selectFields);
         $statements[] = 'FROM ' . $this->subQueryTableSchema->getSymbol();
         $condition = ConditionQueryBuilder::toString($context, $this->conditions);
         if (isset($condition)) {
             $statements[] = "WHERE " . $condition;
         }
         return implode(' ', $statements);
     }
 }