Example #1
0
 /**
  * {@inheritdoc}
  */
 public function toSql()
 {
     $insertParts = $this->values;
     if (array_values($insertParts) !== $insertParts) {
         $insertParts = [$insertParts];
     }
     $columns = array_keys($insertParts[0]);
     $valueSqlPart = Helper::stringRepeat(', ', '?', count($columns), '(', ')');
     return "INSERT INTO `{$this->table}`(`" . implode("`, `", array_values($columns)) . "`)" . " VALUES " . Helper::stringRepeat(', ', $valueSqlPart, count($insertParts));
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function toSql()
 {
     if (count($this->orders) === 0) {
         return '';
     }
     $parts = [];
     foreach ($this->orders as $order) {
         $parts[] = "`{$order[0]}`" . ($order[1] ? '' : ' DESC');
     }
     return 'ORDER BY ' . Helper::arrayImplode(", ", $parts);
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function toSql()
 {
     $parts = ['UPDATE `' . $this->table . '`'];
     if (count($this->attributes)) {
         $columns = array_keys($this->attributes);
         $parts[] = "SET " . Helper::arrayImplode(', ', $columns, "`", "` = ?");
     }
     if ($part = parent::toSql()) {
         $parts[] = $part;
     }
     return implode(' ', $parts);
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function toSql()
 {
     $sqlToReturn = "REFERENCES `{$this->table}`";
     $sqlToReturn .= '(' . Helper::arrayImplode(', ', $this->columns, '`', '`') . ')';
     if (isset($this->attributes['on_delete'])) {
         switch ($this->attributes['on_delete']) {
             case static::OPTION_NO_ACTION:
                 $sqlToReturn .= ' ON DELETE NO ACTION';
                 break;
             case static::OPTION_RESTRICT:
                 $sqlToReturn .= ' ON DELETE RESTRICT';
                 break;
             case static::OPTION_CASCADE:
                 $sqlToReturn .= ' ON DELETE CASCADE';
                 break;
             case static::OPTION_SET_NULL:
                 $sqlToReturn .= ' ON DELETE SET NULL';
                 break;
         }
     }
     if (isset($this->attributes['on_update'])) {
         switch ($this->attributes['on_update']) {
             case static::OPTION_NO_ACTION:
                 $sqlToReturn .= ' ON UPDATE NO ACTION';
                 break;
             case static::OPTION_RESTRICT:
                 $sqlToReturn .= ' ON UPDATE RESTRICT';
                 break;
             case static::OPTION_CASCADE:
                 $sqlToReturn .= ' ON UPDATE CASCADE';
                 break;
             case static::OPTION_SET_NULL:
                 $sqlToReturn .= ' ON UPDATE SET NULL';
                 break;
         }
     }
     return $sqlToReturn;
 }
Example #5
0
 protected function getTypeString()
 {
     $stringToReturn = strtoupper($this->type);
     $lowerType = strtolower($this->type);
     $enableLength = in_array($lowerType, static::$typesHavingLength) && isset($this->attributes['length']);
     $enableDecimal = in_array($lowerType, static::$typesHavingDecimal) && isset($this->attributes['decimal']);
     $enableFsp = in_array($lowerType, static::$typesHavingFsp) && isset($this->attributes['fsp']);
     $enableValues = in_array($lowerType, static::$typesHavingValues) && isset($this->attributes['values']);
     if ($enableLength || $enableDecimal || $enableFsp || $enableValues) {
         $stringToReturn .= '(';
         if ($enableLength && $enableDecimal) {
             $stringToReturn .= $this->attributes['length'] . ", " . $this->attributes['decimal'];
         } elseif ($enableLength) {
             $stringToReturn .= $this->attributes['length'];
         } elseif ($enableFsp) {
             $stringToReturn .= $this->attributes['fsp'];
         } elseif ($enableValues) {
             $stringToReturn .= Helper::arrayImplode(", ", $this->attributes['values'], '\'', '\'');
         }
         $stringToReturn .= ')';
     }
     if (in_array($lowerType, static::$typesHavingUnsigned) && isset($this->attributes['unsigned'])) {
         $stringToReturn .= " UNSIGNED";
     }
     if (in_array($lowerType, static::$typesHavingBinary)) {
         if (isset($this->attributes['binary'])) {
             $stringToReturn .= " BINARY";
         }
         if (isset($this->attributes['charset'])) {
             $stringToReturn .= " CHARACTER SET {$this->attributes['charset']}";
         }
         if (isset($this->attributes['collation'])) {
             $stringToReturn .= " COLLATE {$this->attributes['collation']}";
         }
     }
     if (isset($this->attributes['first']) && $this->attributes['first']) {
         $stringToReturn .= ' FIRST';
     }
     if (isset($this->attributes['after'])) {
         $stringToReturn .= " AFTER `{$this->attributes['after']}`";
     }
     return $stringToReturn;
 }
Example #6
0
 /**
  * @example
  *          KEY [index_name] [index_type] (index_col_name,...)
  *  PRIMARY KEY              [index_type] (index_col_name,...)
  *  UNIQUE  KEY [index_name] [index_type] (index_col_name,...)
  *  FOREIGN KEY [index_name]              (index_col_name,...) ReferenceExpression
  * 
  * {@inheritdoc}
  */
 public function toSql()
 {
     $stringToReturn = '';
     $keyType = isset($this->attributes['key_type']) ? $this->attributes['key_type'] : static::KEY_TYPE_NONE;
     switch ($keyType) {
         case static::KEY_TYPE_PRIMARY:
             $stringToReturn .= 'PRIMARY ';
             break;
         case static::KEY_TYPE_UNIQUE:
             $stringToReturn .= 'UNIQUE ';
             break;
         case static::KEY_TYPE_FOREIGN:
             $stringToReturn .= 'FOREIGN ';
             break;
         default:
             $keyType = static::KEY_TYPE_NONE;
             // normalize
     }
     $stringToReturn .= 'KEY';
     if ($keyType !== static::KEY_TYPE_PRIMARY && $this->name) {
         $stringToReturn .= " `{$this->name}`";
     }
     if ($keyType !== static::KEY_TYPE_FOREIGN && isset($this->attributes['using'])) {
         switch ($this->attributes['using']) {
             case static::INDEX_TYPE_BTREE:
                 $stringToReturn .= ' USING BTREE';
                 break;
             case static::INDEX_TYPE_HASH:
                 $stringToReturn .= ' USING HASH';
                 break;
         }
     }
     $stringToReturn .= ' (' . Helper::arrayImplode(', ', $this->columns, '`', '`') . ')';
     if ($keyType === static::KEY_TYPE_FOREIGN && isset($this->reference)) {
         $referenceString = $this->reference->toSql();
         if ($referenceString) {
             $stringToReturn .= " {$referenceString}";
         }
     }
     return $stringToReturn;
 }