/**
  * @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;
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function toSql()
 {
     $stringToReturn = "`{$this->name}` " . $this->getTypeString();
     if (isset($this->attributes['nullable']) && $this->attributes['nullable']) {
         $stringToReturn .= ' NULL';
     } else {
         $stringToReturn .= ' NOT NULL';
     }
     if (isset($this->attributes['default'])) {
         $stringToReturn .= " DEFAULT ";
         if ($this->attributes['default'] instanceof ExpressionInterface) {
             $stringToReturn .= $this->attributes['default']->toSql();
         } else {
             $stringToReturn .= "'" . addslashes($this->attributes['default']) . "'";
         }
     }
     if (isset($this->attributes['auto_increment'])) {
         $stringToReturn .= " AUTO_INCREMENT";
     }
     if (isset($this->attributes['unique'])) {
         $stringToReturn .= ' UNIQUE KEY';
     }
     if (isset($this->attributes['primary'])) {
         $stringToReturn .= ' PRIMARY KEY';
     }
     if (isset($this->reference)) {
         $referenceString = $this->reference->toSql();
         if ($referenceString) {
             $stringToReturn .= " {$referenceString}";
         }
     }
     return $stringToReturn;
 }