From: Andrey Kutejko Date: Sat, 27 Jul 2013 06:39:26 +0000 (+0300) Subject: delete buggy sequences X-Git-Tag: 0.6~75 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=39f5ad30e3046c8ee90a896ff1a191c8a03033f4;p=ipf-legacy-orm.git delete buggy sequences --- diff --git a/ipf/orm.php b/ipf/orm.php index 43bbc49..a216def 100644 --- a/ipf/orm.php +++ b/ipf/orm.php @@ -101,8 +101,6 @@ final class IPF_ORM 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; @@ -118,7 +116,6 @@ final class IPF_ORM 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; @@ -167,7 +164,6 @@ final class IPF_ORM 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; diff --git a/ipf/orm/configurable.php b/ipf/orm/configurable.php index b629ec0..ce12e94 100644 --- a/ipf/orm/configurable.php +++ b/ipf/orm/configurable.php @@ -67,7 +67,6 @@ abstract class IPF_ORM_Configurable 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: @@ -78,21 +77,15 @@ abstract class IPF_ORM_Configurable 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; diff --git a/ipf/orm/connection.php b/ipf/orm/connection.php index 06c5306..1107bad 100644 --- a/ipf/orm/connection.php +++ b/ipf/orm/connection.php @@ -15,7 +15,6 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta 'dataDict' => false, 'export' => false, 'import' => false, - 'sequence' => false, 'unitOfWork' => false, 'formatter' => false, 'util' => false, @@ -682,7 +681,7 @@ abstract class IPF_ORM_Connection extends IPF_ORM_Configurable implements Counta public function lastInsertId($table = null, $field = null) { - return $this->sequence->lastInsertId($table, $field); + return $this->getDbh()->lastInsertId(); } public function beginTransaction($savepoint = null) diff --git a/ipf/orm/connection/unitofwork.php b/ipf/orm/connection/unitofwork.php index ad76646..07fa260 100644 --- a/ipf/orm/connection/unitofwork.php +++ b/ipf/orm/connection/unitofwork.php @@ -397,16 +397,6 @@ class IPF_ORM_Connection_UnitOfWork extends IPF_ORM_Connection_Module $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() && @@ -415,7 +405,7 @@ class IPF_ORM_Connection_UnitOfWork extends IPF_ORM_Connection_Module $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."); diff --git a/ipf/orm/export.php b/ipf/orm/export.php index 66c813c..005aab6 100644 --- a/ipf/orm/export.php +++ b/ipf/orm/export.php @@ -62,16 +62,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module 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)); @@ -128,16 +118,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module 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); @@ -444,7 +424,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module if ( ! isset($connections[$connectionName])) { $connections[$connectionName] = array( 'create_tables' => array(), - 'create_sequences' => array(), 'create_indexes' => array(), 'alters' => array() ); @@ -463,14 +442,6 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module 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; @@ -492,7 +463,7 @@ class IPF_ORM_Export extends IPF_ORM_Connection_Module // 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) { diff --git a/ipf/orm/export/mysql.php b/ipf/orm/export/mysql.php index a5cabf8..fb90b11 100644 --- a/ipf/orm/export/mysql.php +++ b/ipf/orm/export/mysql.php @@ -237,68 +237,6 @@ class IPF_ORM_Export_Mysql extends IPF_ORM_Export 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; @@ -444,4 +382,4 @@ class IPF_ORM_Export_Mysql extends IPF_ORM_Export return $this->conn->exec('ALTER TABLE ' . $table . ' DROP FOREIGN KEY ' . $name); } -} \ No newline at end of file +} diff --git a/ipf/orm/formatter.php b/ipf/orm/formatter.php index 078f655..456fb2a 100644 --- a/ipf/orm/formatter.php +++ b/ipf/orm/formatter.php @@ -89,17 +89,6 @@ class IPF_ORM_Formatter extends IPF_ORM_Connection_Module } } - 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'; @@ -110,12 +99,6 @@ class IPF_ORM_Formatter extends IPF_ORM_Connection_Module 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), diff --git a/ipf/orm/import/schema.php b/ipf/orm/import/schema.php index 163db9f..551b9fd 100644 --- a/ipf/orm/import/schema.php +++ b/ipf/orm/import/schema.php @@ -35,7 +35,6 @@ class IPF_ORM_Import_Schema 'scale', 'values', 'comment', - 'sequence', 'protected', 'zerofill', 'owner', @@ -190,7 +189,6 @@ class IPF_ORM_Import_Schema $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 diff --git a/ipf/orm/manager.php b/ipf/orm/manager.php index 74d68fe..e790f11 100644 --- a/ipf/orm/manager.php +++ b/ipf/orm/manager.php @@ -14,10 +14,8 @@ class IPF_ORM_Manager extends IPF_ORM_Configurable implements Countable, Iterato $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, diff --git a/ipf/orm/record.php b/ipf/orm/record.php index 57b9f27..a89718d 100644 --- a/ipf/orm/record.php +++ b/ipf/orm/record.php @@ -203,7 +203,6 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab { 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)) { diff --git a/ipf/orm/relation/parser.php b/ipf/orm/relation/parser.php index 6f3b460..a5e36d6 100644 --- a/ipf/orm/relation/parser.php +++ b/ipf/orm/relation/parser.php @@ -369,7 +369,6 @@ class IPF_ORM_Relation_Parser unset($col['type']); unset($col['length']); unset($col['autoincrement']); - unset($col['sequence']); unset($col['primary']); $def['table']->setColumn($column, $type, $length, $col); diff --git a/ipf/orm/sequence.php b/ipf/orm/sequence.php deleted file mode 100644 index 77ce93b..0000000 --- a/ipf/orm/sequence.php +++ /dev/null @@ -1,21 +0,0 @@ -warnings[] = 'database does not support getting current - sequence value, the sequence value was incremented'; - return $this->nextId($seqName); - } -} \ No newline at end of file diff --git a/ipf/orm/sequence/mysql.php b/ipf/orm/sequence/mysql.php deleted file mode 100644 index 4293095..0000000 --- a/ipf/orm/sequence/mysql.php +++ /dev/null @@ -1,52 +0,0 @@ -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 diff --git a/ipf/orm/table.php b/ipf/orm/table.php index 325c90d..90834c5 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -20,7 +20,6 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable protected $_options = array('name' => null, 'tableName' => null, - 'sequenceName' => null, 'inheritanceMap' => array(), 'enumMap' => array(), 'type' => null, @@ -130,21 +129,6 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable $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)) { @@ -1080,7 +1064,6 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable || $name == 'autoincrement' || $name == 'default' || $name == 'values' - || $name == 'sequence' || $name == 'zerofill' || $name == 'owner' || $name == 'scale'