const ATTR_QUOTE_IDENTIFIER = 101;
const ATTR_FIELD_CASE = 102;
const ATTR_IDXNAME_FORMAT = 103;
- const ATTR_SEQNAME_FORMAT = 104;
- const ATTR_SEQCOL_NAME = 105;
const ATTR_CMPNAME_FORMAT = 118;
const ATTR_DBNAME_FORMAT = 117;
const ATTR_TBLCLASS_FORMAT = 119;
const ATTR_DEF_TABLESPACE = 115;
const ATTR_EMULATE_DATABASE = 116;
const ATTR_USE_NATIVE_ENUM = 117;
- const ATTR_DEFAULT_SEQUENCE = 133;
//const ATTR_FETCHMODE = 118;
const ATTR_NAME_PREFIX = 121;
const HYDRATE_NONE = 4;
const IDENTIFIER_AUTOINC = 1;
- const IDENTIFIER_SEQUENCE = 2;
const IDENTIFIER_NATURAL = 3;
const IDENTIFIER_COMPOSITE = 4;
const MODEL_LOADING_AGGRESSIVE = 1;
case IPF_ORM::ATTR_DEFAULT_TABLE_TYPE:
case IPF_ORM::ATTR_EMULATE_DATABASE:
case IPF_ORM::ATTR_USE_NATIVE_ENUM:
- case IPF_ORM::ATTR_DEFAULT_SEQUENCE:
case IPF_ORM::ATTR_EXPORT:
case IPF_ORM::ATTR_DECIMAL_PLACES:
case IPF_ORM::ATTR_LOAD_REFERENCES:
case IPF_ORM::ATTR_RECURSIVE_MERGE_FIXTURES;
case IPF_ORM::ATTR_SINGULARIZE_IMPORT;
- break;
- case IPF_ORM::ATTR_SEQCOL_NAME:
- if ( ! is_string($value)) {
- throw new IPF_ORM_Exception('Sequence column name attribute only accepts string values');
- }
break;
case IPF_ORM::ATTR_FIELD_CASE:
if ($value != 0 && $value != CASE_LOWER && $value != CASE_UPPER)
throw new IPF_ORM_Exception('Field case attribute should be either 0, CASE_LOWER or CASE_UPPER constant.');
break;
- case IPF_ORM::ATTR_SEQNAME_FORMAT:
case IPF_ORM::ATTR_IDXNAME_FORMAT:
case IPF_ORM::ATTR_TBLNAME_FORMAT:
if ($this instanceof IPF_ORM_Table) {
- throw new IPF_ORM_Exception('Sequence / index name format attributes cannot be set'
+ throw new IPF_ORM_Exception('index name format attributes cannot be set'
. 'at table level (only at connection or global level).');
}
break;
'dataDict' => false,
'export' => false,
'import' => false,
- 'sequence' => false,
'unitOfWork' => false,
'formatter' => false,
'util' => false,
public function lastInsertId($table = null, $field = null)
{
- return $this->sequence->lastInsertId($table, $field);
+ return $this->getDbh()->lastInsertId();
}
public function beginTransaction($savepoint = null)
$identifier = (array) $table->getIdentifier();
- $seq = $record->getTable()->sequenceName;
-
- if ( ! empty($seq)) {
- $id = $this->conn->sequence->nextId($seq);
- $seqName = $table->getIdentifier();
- $fields[$seqName] = $id;
-
- $record->assignIdentifier($id);
- }
-
$this->conn->insert($table, $fields);
if (empty($seq) && count($identifier) == 1 && $identifier[0] == $table->getIdentifier() &&
$seq = $table->getTableName() . '_' . $identifier[0];
}
- $id = $this->conn->sequence->lastInsertId($seq);
+ $id = $this->conn->lastInsertId($seq);
if ( ! $id) {
throw new IPF_ORM_Exception("Couldn't get last insert identifier.");
return $this->dropConstraint($table, $name);
}
- public function dropSequence($sequenceName)
- {
- $this->conn->exec($this->dropSequenceSql($sequenceName));
- }
-
- public function dropSequenceSql($sequenceName)
- {
- throw new IPF_ORM_Exception('Drop sequence not supported by this driver.');
- }
-
public function createDatabase($database)
{
$this->conn->execute($this->createDatabaseSql($database));
return $sql;
}
- public function createSequence($seqName, $start = 1, array $options = array())
- {
- return $this->conn->execute($this->createSequenceSql($seqName, $start = 1, $options));
- }
-
- public function createSequenceSql($seqName, $start = 1, array $options = array())
- {
- throw new IPF_ORM_Exception('Create sequence not supported by this driver.');
- }
-
public function createConstraint($table, $name, $definition)
{
$sql = $this->createConstraintSql($table, $name, $definition);
if ( ! isset($connections[$connectionName])) {
$connections[$connectionName] = array(
'create_tables' => array(),
- 'create_sequences' => array(),
'create_indexes' => array(),
'alters' => array()
);
continue;
}
- // If create sequence statement
- if (substr($query, 0, strlen('CREATE SEQUENCE')) == 'CREATE SEQUENCE') {
- $connections[$connectionName]['create_sequences'][] = $query;
-
- unset($sql[$key]);
- continue;
- }
-
// If create index statement
if (preg_grep("/CREATE .* INDEX/", array($query))) {
$connections[$connectionName]['create_indexes'][] = $query;
// Loop over all the sql again to merge everything together so it is in the correct order
$build = array();
foreach ($connections as $connectionName => $sql) {
- $build[$connectionName] = array_merge($sql['create_tables'], $sql['create_sequences'], $sql['create_indexes'], $sql['alters']);
+ $build[$connectionName] = array_merge($sql['create_tables'], $sql['create_indexes'], $sql['alters']);
}
if ( ! $groupByConnection) {
return 'ALTER TABLE ' . $name . ' ' . $query;
}
- public function createSequence($sequenceName, $start = 1, array $options = array())
- {
- $sequenceName = $this->conn->quoteIdentifier($sequenceName, true);
- $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(IPF_ORM::ATTR_SEQCOL_NAME), true);
-
- $optionsStrings = array();
-
- if (isset($options['comment']) && ! empty($options['comment'])) {
- $optionsStrings['comment'] = 'COMMENT = ' . $this->conn->quote($options['comment'], 'string');
- }
-
- if (isset($options['charset']) && ! empty($options['charset'])) {
- $optionsStrings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
-
- if (isset($options['collate'])) {
- $optionsStrings['charset'] .= ' COLLATE ' . $options['collate'];
- }
- }
-
- $type = false;
-
- if (isset($options['type'])) {
- $type = $options['type'];
- } else {
- $type = $this->conn->getAttribute(IPF_ORM::ATTR_DEFAULT_TABLE_TYPE);
- }
- if ($type) {
- $optionsStrings[] = 'ENGINE = ' . $type;
- }
-
-
- try {
- $query = 'CREATE TABLE ' . $sequenceName
- . ' (' . $seqcolName . ' BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
- . $seqcolName . ')) ' . implode($optionsStrings, ' ');
-
- $res = $this->conn->exec($query);
- } catch(IPF_ORM_Exception $e) {
- throw new IPF_ORM_Exception('could not create sequence table');
- }
-
- if ($start == 1 && $res == 1)
- return true;
-
- $query = 'INSERT INTO ' . $sequenceName
- . ' (' . $seqcolName . ') VALUES (' . ($start - 1) . ')';
-
- $res = $this->conn->exec($query);
-
- if ($res == 1)
- return true;
-
- // Handle error
- try {
- $result = $this->conn->exec('DROP TABLE ' . $sequenceName);
- } catch(IPF_ORM_Exception $e) {
- throw new IPF_ORM_Exception('could not drop inconsistent sequence table');
- }
-
-
- }
-
public function createIndexSql($table, $name, array $definition)
{
$table = $table;
return $this->conn->exec('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $name);
}
-}
\ No newline at end of file
+}
}
}
- public function fixSequenceName($sqn)
- {
- $seqPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(IPF_ORM::ATTR_SEQNAME_FORMAT)).'$/i';
- $seqName = preg_replace($seqPattern, '\\1', $sqn);
-
- if ($seqName && ! strcasecmp($sqn, $this->getSequenceName($seqName))) {
- return $seqName;
- }
- return $sqn;
- }
-
public function fixIndexName($idx)
{
$indexPattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $this->conn->getAttribute(IPF_ORM::ATTR_IDXNAME_FORMAT)).'$/i';
return $idx;
}
- public function getSequenceName($sqn)
- {
- return sprintf($this->conn->getAttribute(IPF_ORM::ATTR_SEQNAME_FORMAT),
- preg_replace('/[^a-z0-9_\$.]/i', '_', $sqn));
- }
-
public function getIndexName($idx)
{
return sprintf($this->conn->getAttribute(IPF_ORM::ATTR_IDXNAME_FORMAT),
'scale',
'values',
'comment',
- 'sequence',
'protected',
'zerofill',
'owner',
$colDesc['primary'] = isset($field['primary']) ? (bool) (isset($field['primary']) && $field['primary']):null;
$colDesc['default'] = isset($field['default']) ? $field['default']:null;
$colDesc['autoincrement'] = isset($field['autoincrement']) ? (bool) (isset($field['autoincrement']) && $field['autoincrement']):null;
- $colDesc['sequence'] = isset($field['sequence']) ? (string) $field['sequence']:null;
$colDesc['values'] = isset($field['values']) ? (array) $field['values']:null;
// Include all the specified and valid validators in the colDesc
$this->attributes = array(
IPF_ORM::ATTR_LOAD_REFERENCES => true,
IPF_ORM::ATTR_IDXNAME_FORMAT => "%s_idx",
- IPF_ORM::ATTR_SEQNAME_FORMAT => "%s_seq",
IPF_ORM::ATTR_TBLNAME_FORMAT => "%s",
IPF_ORM::ATTR_QUOTE_IDENTIFIER => false,
- IPF_ORM::ATTR_SEQCOL_NAME => 'id',
IPF_ORM::ATTR_PORTABILITY => IPF_ORM::PORTABILITY_ALL,
IPF_ORM::ATTR_EXPORT => IPF_ORM::EXPORT_ALL,
IPF_ORM::ATTR_DECIMAL_PLACES => 2,
{
switch ($this->_table->getIdentifierType()) {
case IPF_ORM::IDENTIFIER_AUTOINC:
- case IPF_ORM::IDENTIFIER_SEQUENCE:
case IPF_ORM::IDENTIFIER_NATURAL:
$name = $this->_table->getIdentifier();
if (is_array($name)) {
unset($col['type']);
unset($col['length']);
unset($col['autoincrement']);
- unset($col['sequence']);
unset($col['primary']);
$def['table']->setColumn($column, $type, $length, $col);
+++ /dev/null
-<?php
-
-class IPF_ORM_Sequence extends IPF_ORM_Connection_Module
-{
- public function nextId($seqName, $ondemand = true)
- {
- throw new IPF_ORM_Sequence_Exception('method not implemented');
- }
-
- public function lastInsertId($table = null, $field = null)
- {
- throw new IPF_ORM_Exception('method not implemented');
- }
-
- public function currId($seqName)
- {
- $this->warnings[] = 'database does not support getting current
- sequence value, the sequence value was incremented';
- return $this->nextId($seqName);
- }
-}
\ No newline at end of file
+++ /dev/null
-<?php
-
-class IPF_ORM_Sequence_Mysql extends IPF_ORM_Sequence
-{
- public function nextId($seqName, $onDemand = true)
- {
- $sequenceName = $this->conn->quoteIdentifier($seqName, true);
- $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(IPF_ORM::ATTR_SEQCOL_NAME), true);
- $query = 'INSERT INTO ' . $sequenceName . ' (' . $seqcolName . ') VALUES (NULL)';
-
- try {
-
- $this->conn->exec($query);
-
- } catch(IPF_ORM_Exception $e) {
- if ($onDemand && $e->getPortableCode() == IPF_ORM::ERR_NOSUCHTABLE) {
- // Since we are creating the sequence on demand
- // we know the first id = 1 so initialize the
- // sequence at 2
- try {
- $result = $this->conn->export->createSequence($seqName, 2);
- } catch(IPF_ORM_Exception $e) {
- throw new IPF_ORM_Exception('on demand sequence ' . $seqName . ' could not be created');
- }
- // First ID of a newly created sequence is 1
- return 1;
- }
- throw $e;
- }
-
- $value = $this->lastInsertId();
-
- if (is_numeric($value)) {
- $query = 'DELETE FROM ' . $sequenceName . ' WHERE ' . $seqcolName . ' < ' . $value;
- $this->conn->exec($query);
- }
- return $value;
- }
-
- public function lastInsertId($table = null, $field = null)
- {
- return $this->conn->getDbh()->lastInsertId();
- }
-
- public function currId($seqName)
- {
- $sequenceName = $this->conn->quoteIdentifier($seqName, true);
- $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(IPF_ORM::ATTR_SEQCOL_NAME), true);
- $query = 'SELECT MAX(' . $seqcolName . ') FROM ' . $sequenceName;
- return (int) $this->conn->fetchOne($query);
- }
-}
\ No newline at end of file
protected $_options = array('name' => null,
'tableName' => null,
- 'sequenceName' => null,
'inheritanceMap' => array(),
'enumMap' => array(),
'type' => null,
$found = true;
}
break;
- case 'seq':
- case 'sequence':
- $this->_identifierType = IPF_ORM::IDENTIFIER_SEQUENCE;
- $found = true;
-
- if (is_string($value)) {
- $this->_options['sequenceName'] = $value;
- } else {
- if (($sequence = $this->getAttribute(IPF_ORM::ATTR_DEFAULT_SEQUENCE)) !== null) {
- $this->_options['sequenceName'] = $sequence;
- } else {
- $this->_options['sequenceName'] = $this->_conn->formatter->getSequenceName($this->_options['tableName']);
- }
- }
- break;
}
}
if ( ! isset($this->_identifierType)) {
|| $name == 'autoincrement'
|| $name == 'default'
|| $name == 'values'
- || $name == 'sequence'
|| $name == 'zerofill'
|| $name == 'owner'
|| $name == 'scale'