175 [$sql, $binds, $offset, $limit] = self::parseQueryFunctionArgs(func_get_args());
179 $binds1 = $binds2 =
"";
180 foreach ($binds as
$key => $value)
191 $binds2 .=
":" .
$key;
197 $sql .=
" RETURNING " . $binds1 .
" INTO " . $binds2;
201 return parent::query($sql, $binds, $offset, $limit);
360 $sql =
'CREATE TABLE ' . $this->getSqlHelper()->quote($tableName) .
' (';
363 foreach (
$fields as $columnName => $field)
368 'Field `%s` should be an Entity\ScalarField instance', $columnName
372 $realColumnName = $field->getColumnName();
374 $sqlFields[] = $this->getSqlHelper()->quote($realColumnName)
375 .
' ' . $this->getSqlHelper()->getColumnTypeByField($field)
376 .
' ' . (in_array($columnName, $primary,
true) ?
'NOT NULL' :
'NULL');
379 $sql .= join(
', ', $sqlFields);
381 if (!empty($primary))
383 foreach ($primary as &$primaryColumn)
385 $realColumnName =
$fields[$primaryColumn]->getColumnName();
386 $primaryColumn = $this->getSqlHelper()->quote($realColumnName);
389 $sql .=
', PRIMARY KEY(' . join(
', ', $primary) .
')';
397 if (!empty($autoincrement))
399 foreach ($autoincrement as $autoincrementColumn)
401 $autoincrementColumn =
$fields[$autoincrementColumn]->getColumnName();
403 if ($autoincrementColumn ==
'ID')
406 $aiName = $tableName;
410 $aiName = $tableName .
'_' . $autoincrementColumn;
413 $this->
query(
'CREATE SEQUENCE ' . $this->getSqlHelper()->quote(
'sq_' . $aiName));
415 $this->
query(
'CREATE OR REPLACE TRIGGER ' . $this->getSqlHelper()->quote($aiName .
'_insert') .
'
417 ON ' . $this->getSqlHelper()->quote($tableName) .
'
420 IF :NEW.' . $this->getSqlHelper()->quote($autoincrementColumn) .
' IS NULL THEN
421 SELECT ' . $this->getSqlHelper()->quote(
'sq_' . $aiName) .
'.NEXTVAL
422 INTO :NEW.' . $this->getSqlHelper()->quote($autoincrementColumn) .
' FROM dual;
435 $this->
query(
'RENAME ' . $this->getSqlHelper()->quote($currentName) .
' TO ' . $this->getSqlHelper()->quote($newName));
439 $aiName = $currentName;
441 if ($this->queryScalar(
"SELECT 1 FROM user_sequences WHERE sequence_name=upper('" . $this->getSqlHelper()->forSql(
'sq_' . $aiName) .
"')"))
444 $newAiName = $newName;
447 $this->
query(
'RENAME ' . $this->getSqlHelper()->quote(
'sq_' . $aiName) .
' TO ' . $this->getSqlHelper()->quote(
'sq_' . $newAiName));
450 $this->
query(
'DROP TRIGGER ' . $this->getSqlHelper()->quote($aiName .
'_insert'));
452 $this->
query(
'CREATE OR REPLACE TRIGGER ' . $this->getSqlHelper()->quote($newAiName .
'_insert') .
'
454 ON ' . $this->getSqlHelper()->quote($newName) .
'
457 IF :NEW.' . $this->getSqlHelper()->quote(
'ID') .
' IS NULL THEN
458 SELECT ' . $this->getSqlHelper()->quote(
'sq_' . $newAiName) .
'.NEXTVAL
459 INTO :NEW.' . $this->getSqlHelper()->quote(
'ID') .
' FROM dual;