59 public function isInt($fieldType) {
64 $this->nameProtection =
'`';
65 $this->createDatabaseMask =
'CREATE DATABASE {name}';
66 $this->selectDbMask =
"USE {name}";
67 $this->createTableMask =
'CREATE TABLE {name} ({fields}) {attributes}';
68 $this->fieldMask =
'{name} {type} {extra}';
69 $this->alterTableMask =
'ALTER TABLE {tableName} {alter}';
70 $this->foreignKeyMask =
'ALTER TABLE {tableName} ADD CONSTRAINT {fkName} FOREIGN KEY ({fkFieldName}) REFERENCES {referencesTableName} ({referencesFieldName}) ON DELETE {onDelete% ON UPDATE {onUpdate}';
71 $this->alterTableAddKey =
'ALTER TABLE {tableName} ADD {type} KEY ({pkFields})';
72 $this->autoIncMask =
'ALTER TABLE {tableName} MODIFY {fieldInfos} AUTO_INCREMENT, AUTO_INCREMENT={value}';
73 $this->addFieldMask=
'ALTER TABLE {tableName} ADD {fieldName} {attributes}';
74 $this->modifyFieldMask=
'ALTER TABLE {tableName} MODIFY {fieldName} {attributes}';
80 $this->nameProtection=$wrapper->quote;
95 $this->tablesToCreate = $tables;
99 $script = $this->
replaceMask(
'name', $name, $this->createDatabaseMask);
100 return $this->
addScript(
'head', $script);
104 $script = $this->
replaceMask(
'name', $name, $this->selectDbMask);
105 return $this->
addScript(
'head', $script);
108 public function createTable($name, $fieldsAttributes, $attributes = []) {
110 $attributes = \implode(
" ", $attributes);
114 'attributes' => $attributes
115 ], $this->createTableMask);
116 return $this->
addScript(
'body', $script);
119 public function addKey($tableName, $fieldNames, $type =
'PRIMARY') {
121 foreach ($fieldNames as $fieldName) {
125 'tableName' => $tableName,
126 'pkFields' => \implode(
",", $pks),
128 ], $this->alterTableAddKey);
129 return $this->
addScript(
'before-constraints', $script);
132 public function addForeignKey($tableName, $fkFieldName, $referencesTableName, $referencesFieldName, $fkName =
null, $onDelete =
'CASCADE', $onUpdate =
'NO ACTION') {
133 if (! isset($fkName)) {
137 'tableName' => $tableName,
139 'fkFieldName' => $fkFieldName,
140 'referencesTableName' => $referencesTableName,
141 'referencesFieldName' => $referencesFieldName,
142 'onDelete' => $onDelete,
143 'onUpdate' => $onUpdate
144 ], $this->foreignKeyMask);
145 return $this->
addScript(
'constraints', $script);
148 public function addAutoInc($tableName, $fieldName,$fieldInfos, $value = 1) {
150 'tableName' => $tableName,
151 'fieldInfos' => $fieldInfos,
152 'fieldName'=>$fieldName,
153 'seqName'=>
'seq_'.$tableName,
155 ], $this->autoIncMask);
156 return $this->
addScript(
'before-constraints', $script);
159 public function addField($tableName,$fieldName,$fieldAttributes){
160 $this->
addOrUpdateField($tableName,$fieldName,$fieldAttributes,
'addFieldMask');
163 public function modifyField($tableName,$fieldName,$fieldAttributes){
164 $this->
addOrUpdateField($tableName,$fieldName,$fieldAttributes,
'modifyFieldMask');
168 if (! isset($this->sqlScript[$key])) {
169 $this->sqlScript[$key] = [];
171 $this->sqlScript[$key][] = $script;
175 protected function addOrUpdateField($tableName,$fieldName,$fieldAttributes,$part=
'addFieldMask'){
178 'tableName' => $tableName,
179 'fieldName' => $fieldName,
180 'attributes' => \implode(
" ", $fieldAttributes)
182 return $this->
addScript(
'body', $script);
186 if (\array_search($name, $this->constraintNames)) {
188 if (\preg_match(
'@([\s\S]*?)((?:\d)+)$@', $name, $matches)) {
189 if (isset($matches[2])) {
190 $nb = \intval($matches[2]) + 1;
191 $name = $matches[1] . $nb;
197 $this->constraintNames[] = $name;
207 $result = $fieldAttributes;
208 $type = $fieldAttributes[
'type'];
209 $existingType =
false;
211 if (isset($strType)) {
212 if (isset($this->fieldTypes[$strType])) {
213 if (! $forPk && (! isset($fieldAttributes[
'extra']) || $fieldAttributes[
'extra'] ==
'')) {
214 $result[
'extra'] =
'DEFAULT ' . $this->fieldTypes[$strType];
216 $existingType =
true;
219 if (! $existingType) {
227 foreach ($fieldsAttributes as $fieldAttribute) {
230 return \implode(
",", $result);
234 if (\strstr(\strtolower($key),
'name'))
236 return \str_replace(
'{'.$key.
'}', $value, $mask);
240 foreach ($keyValues as $key => $value) {
251 $jointable=$jointableInfos[
'name'];
252 if (! isset($this->manyToManys[$jointable])) {
253 $this->manyToManys[$jointable] = [];
255 $this->manyToManys[$jointable][] = [
'targetEntity'=>$targetEntity,
'jointableInfos'=>$jointableInfos];
259 foreach ($this->manyToManys as $joinTable => $infos) {
267 if (\is_array($this->tablesToCreate)) {
268 return \array_search($table, $this->tablesToCreate) !==
false;
277 $invertedJoinColumns = [];
278 foreach ($infos as $info) {
279 $targetEntity=$info[
'targetEntity'];
280 $joinTableInfos=$info[
'jointableInfos'];
281 $pk = OrmUtils::getFirstKey($targetEntity);
282 $shortClassName = ClassUtils::getClassSimpleName($targetEntity);
283 $fieldName = $joinTableInfos[
'inverseJoinColumns'][
'name']??($pk . \ucfirst($shortClassName));
284 $fields[] = $fieldName;
285 $type = OrmUtils::getFieldType($targetEntity, $pk);
287 $memberName = \lcfirst($shortClassName);
288 $manyToOnes[] = $memberName;
289 $invertedJoinColumns[$fieldName] = [
290 "member" => $memberName,
291 "className" => $targetEntity
295 '#tableName' => $joinTable,
296 '#primaryKeys' => \array_combine($fields, $fields),
298 '#notSerializable' => [],
300 '#manyToOne' => $manyToOnes,
301 '#invertedJoinColumn' => $invertedJoinColumns,
305 '#fieldNames' => $fields,
306 '#memberNames' => $fields
310 $tableGenerator->init($metas);
311 $tableGenerator->generateSQL($this);
315 $scripts = \array_merge($this->sqlScript[
'head']??[], $this->sqlScript[
'body']??[]);
316 if (isset($this->sqlScript[
'before-constraints'])) {
317 $scripts = \array_merge($scripts, $this->sqlScript[
'before-constraints']);
319 if (isset($this->sqlScript[
'constraints'])) {
320 $scripts = \array_merge($scripts, $this->sqlScript[
'constraints']);
326 $scripts = $this->getScript();
327 if(\count($scripts)>0) {
328 return \implode(
";\n", $scripts) .
';';
337 $this->migrationMode = $migrationMode;
Manipulates class and namespace names Ubiquity\cache$ClassUtils This class is part of Ubiquity.
Ubiquity Generic database class.
Ubiquity\db\providers$AbstractDbWrapper This class is part of Ubiquity.
migrateOperation(string $operation)
Returns the SQL string for a migration operation.
replaceMask($key, $value, $mask)
createTable($name, $fieldsAttributes, $attributes=[])
generateFields($fieldsAttributes)
setDatabaseWrapper(AbstractDbWrapper $wrapper)
addField($tableName, $fieldName, $fieldAttributes)
addAutoInc($tableName, $fieldName, $fieldInfos, $value=1)
generateManyToMany($joinTable, $infos)
generateField($fieldAttributes, $forPk=false)
addManyToMany($jointableInfos, $targetEntity)
checkConstraintName($name)
setTablesToCreate(array $tables)
checkFieldAttributes($fieldAttributes, $forPk=false)
hasToCreateTable(string $table)
modifyField($tableName, $fieldName, $fieldAttributes)
addKey($tableName, $fieldNames, $type='PRIMARY')
addOrUpdateField($tableName, $fieldName, $fieldAttributes, $part='addFieldMask')
replaceArrayMask($keyValues, $mask)
addForeignKey($tableName, $fkFieldName, $referencesTableName, $referencesFieldName, $fkName=null, $onDelete='CASCADE', $onUpdate='NO ACTION')
setMigrationMode($migrationMode)
Object/relational mapping utilities.