51 public function getIndexName($tableName, array $columns, $strict =
false)
58 $tableName = preg_replace(
"/[^a-z0-9_]+/i",
"", $tableName);
59 $tableName = trim($tableName);
61 $rs = $this->query(
"SHOW INDEX FROM `".$this->getSqlHelper()->forSql($tableName).
"`");
68 while ($ar = $rs->fetch())
70 $indexes[$ar[
"Key_name"]][$ar[
"Seq_in_index"] - 1] = $ar[
"Column_name"];
73 return static::findIndex($indexes, $columns, $strict);
81 if (!isset($this->tableColumnsCache[$tableName]))
85 $sqlTableName = ($tableName[0] ===
'(')
86 ? $tableName.
' AS xyz'
87 : $this->getSqlHelper()->quote($tableName);
89 $query = $this->queryInternal(
"SELECT * FROM {$sqlTableName} LIMIT 0");
91 $result = $this->createResult($query);
93 $this->tableColumnsCache[$tableName] = $result->getFields();
95 return $this->tableColumnsCache[$tableName];
101 public function createTable($tableName, $fields, $primary = array(), $autoincrement = array())
103 $sql =
'CREATE TABLE '.$this->getSqlHelper()->quote($tableName).
' (';
104 $sqlFields = array();
106 foreach ($fields as $columnName => $field)
111 'Field `%s` should be an Entity\ScalarField instance', $columnName
115 $realColumnName = $field->getColumnName();
117 $sqlFields[] = $this->getSqlHelper()->quote($realColumnName)
118 .
' ' . $this->getSqlHelper()->getColumnTypeByField($field)
119 . ($field->isNullable() ?
'' :
' NOT NULL')
120 . (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).
')';
141 $sql .=
' Engine='.$this->engine;
150 public function createIndex($tableName, $indexName, $columnNames, $columnLengths =
null, $indexType =
null)
152 if (!is_array($columnNames))
154 $columnNames = array($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).
')';
190 return $this->query($sql);