104 $sql =
'CREATE TABLE IF NOT EXISTS ' . $this->getSqlHelper()->quote($tableName) .
' (';
107 foreach (
$fields as $columnName => $field)
112 'Field `%s` should be an Entity\ScalarField instance', $columnName
116 $realColumnName = $field->getColumnName();
118 $sqlFields[] = $this->getSqlHelper()->quote($realColumnName)
119 .
' ' . $this->getSqlHelper()->getColumnTypeByField($field)
120 . ($field->isNullable() ?
'' :
' NOT NULL')
121 . (in_array($columnName, $autoincrement,
true) ?
' AUTO_INCREMENT' :
'');
124 $sql .= join(
', ', $sqlFields);
126 if (!empty($primary))
128 foreach ($primary as &$primaryColumn)
130 $realColumnName =
$fields[$primaryColumn]->getColumnName();
131 $primaryColumn = $this->getSqlHelper()->quote($realColumnName);
134 $sql .=
', PRIMARY KEY(' . join(
', ', $primary) .
')';
150 public function createIndex($tableName, $indexName, $columnNames, $columnLengths =
null, $indexType =
null)
152 if (!is_array($columnNames))
154 $columnNames = [$columnNames];
157 $sqlHelper = $this->getSqlHelper();
159 foreach ($columnNames as &$columnName)
161 if (is_array($columnLengths) && isset($columnLengths[$columnName]) && $columnLengths[$columnName] > 0)
163 $maxLength = intval($columnLengths[$columnName]);
170 $columnName = $sqlHelper->quote($columnName);
173 $columnName .=
'(' . $maxLength .
')';
180 if ($indexType !==
null
181 && in_array(strtoupper($indexType), [static::INDEX_UNIQUE, static::INDEX_FULLTEXT, static::INDEX_SPATIAL],
true)
184 $indexTypeSql = strtoupper($indexType);
187 $sql =
'CREATE ' . $indexTypeSql .
' INDEX ' . $sqlHelper->quote($indexName) .
' ON ' . $sqlHelper->quote($tableName)
188 .
' (' . join(
', ', $columnNames) .
')';
197 if ($this->getErrorCode() != 1061)