cvs: pear /MDB2_Schema/MDB2 Schema.php /MDB2_Schema/MDB2/Schema Parser.php Writer.php

From: Date: Fri, 10 Jun 2005 15:55:58 +0000
Subject: cvs: pear /MDB2_Schema/MDB2 Schema.php /MDB2_Schema/MDB2/Schema Parser.php Writer.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-32465@lists.php.net to get a copy of this message
dufuz Fri Jun 10 11:55:58 2005 EDT Modified files: /pear/MDB2_Schema/MDB2 Schema.php /pear/MDB2_Schema/MDB2/Schema Parser.php Writer.php Log: Implement auto increment and primary key support, the given database has to support those so it works, primar key falls back to unique + notnull'ing the fields in question, auto increment fallbacks to using the good old _seq tables.

http://cvs.php.net/diff.php/pear/MDB2_Schema/MDB2/Schema.php?r1=1.25&r2=1.26&ty=u Index: pear/MDB2_Schema/MDB2/Schema.php diff -u pear/MDB2_Schema/MDB2/Schema.php:1.25 pear/MDB2_Schema/MDB2/Schema.php:1.26 --- pear/MDB2_Schema/MDB2/Schema.php:1.25 Mon Jun 6 07:40:29 2005 +++ pear/MDB2_Schema/MDB2/Schema.php Fri Jun 10 11:55:57 2005 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@backendmedia.com> | // +----------------------------------------------------------------------+ // -// $Id: Schema.php,v 1.25 2005/06/06 11:40:29 lsmith Exp $ +// $Id: Schema.php,v 1.26 2005/06/10 15:55:57 dufuz Exp $ // require_once 'MDB2.php'; @@ -327,6 +327,7 @@ if (PEAR::isError($parser->error)) { return $parser->error; } + return $parser->database_definition; } @@ -452,12 +453,14 @@ overwritten if it already exists * @access public */ - function createTableIndexes($table_name, $indexes, $overwrite) + function createTableIndexes($table_name, $indexes, $overwrite = false) { if (!$this->db->supports('indexes')) { $this->db->debug('Indexes are not supported'); return MDB2_OK; } + + $supports_primary_key = $this->db->supports('primary_key'); foreach ($indexes as $index_name => $index) { $errorcodes = array(MDB2_ERROR_UNSUPPORTED, MDB2_ERROR_NOT_CAPABLE); $this->db->expectError($errorcodes); @@ -478,6 +481,31 @@ } $this->db->debug('Overwritting index: '.$index_name); } + + // check if primary is being used and if it's supported + if (isset($index['primary']) && !$supports_primary_key) { + /** + * Primary not supported so we fallback to UNIQUE + * and making the field NOT NULL + */ + unset($index['primary']); + $index['unique'] = true; + $fields = $index['fields']; + + $this->db->loadModule('Reverse'); + $changes = array(); + foreach ($fields as $field => $empty) { + $field_info = $this->db->reverse->getTableFieldDefinition($table_name, $field); + if (PEAR::isError($field_info)) { + return $field_info; + } + + $changes['changed_fields'][$field] = $field_info[0][0]; + $changes['changed_fields'][$field]['notnull'] = true; + } + $this->db->manager->alterTable($table_name, $changes, false); + } + $result = $this->db->manager->createIndex($table_name, $index_name, $index); if (PEAR::isError($result)) { return $result; @@ -523,18 +551,21 @@ $this->db->debug('Overwritting table: '.$table_name); } } + if ($create) { $result = $this->db->manager->createTable($table_name, $table['fields']); if (PEAR::isError($result)) { return $result; } } + if (isset($table['initialization']) && is_array($table['initialization'])) { $result = $this->initializeTable($table_name, $table); if (PEAR::isError($result)) { return $result; } } + if (isset($table['indexes']) && is_array($table['indexes'])) { $result = $this->createTableIndexes($table_name, $table['indexes'], $overwrite); if (PEAR::isError($result)) { @@ -623,6 +654,7 @@ $this->db->debug('Sequence already exists: '.$sequence_name); return MDB2_OK; } + $result = $this->db->manager->dropSequence($sequence_name); if (PEAR::isError($result)) { return $result; @@ -632,8 +664,16 @@ $start = 1; if (isset($sequence['on'])) { + if (isset($sequence['on']['autoincrement'])) { + if (!$this->db->supports('auto_increment')) { + $this->db->debug('Auto increment is not supported'); + return MDB2_OK; + } + $autoinc = $sequence['on']['autoincrement']; + } $table = $sequence['on']['table']; $field = $sequence['on']['field']; + $errorcodes = array(MDB2_ERROR_UNSUPPORTED, MDB2_ERROR_NOT_CAPABLE); $this->db->expectError($errorcodes); $tables = $this->db->manager->listTables(); @@ -641,6 +681,7 @@ if (PEAR::isError($tables) && !MDB2::isError($tables, $errorcodes)) { return $tables; } + if (!PEAR::isError($tables) && is_array($tables) && in_array($table, $tables) ) { @@ -659,9 +700,10 @@ } } elseif (isset($sequence['start']) && is_numeric($sequence['start'])) { $start = $sequence['start']; + $table = ''; } - $result = $this->db->manager->createSequence($sequence_name, $start); + $result = $this->db->manager->createSequence($sequence_name, $start, $autoinc, $field); if (PEAR::isError($result)) { return $result; } @@ -884,13 +926,9 @@ } if (isset($previous_definition[$was_field_name])) { if ($was_field_name != $field_name) { - $declaration = $this->db->getDeclaration($field['type'], $field_name, $field); - if (PEAR::isError($declaration)) { - return $declaration; - } + $changes['renamed_fields'][$was_field_name] = array( 'name' => $field_name, - 'declaration' => $declaration, ); } if (isset($defined_fields[$was_field_name])) { @@ -904,11 +942,7 @@ return $change; } if (!empty($change)) { - $declaration = $this->db->getDeclaration($field['type'], $field_name, $field); - if (PEAR::isError($declaration)) { - return $declaration; - } - $change['declaration'] = $declaration; + $change['definition'] = $field; $changes['changed_fields'][$field_name] = $change; } @@ -919,11 +953,7 @@ $was_field_name.'") for field "'.$field_name.'" of table "'. $table_name.'" that does not exist'); } - $declaration = $this->db->getDeclaration($field['type'], $field_name, $field); - if (PEAR::isError($declaration)) { - return $declaration; - } - $change['declaration'] = $declaration; + $changes['added_fields'][$field_name] = $change; } } http://cvs.php.net/diff.php/pear/MDB2_Schema/MDB2/Schema/Parser.php?r1=1.9&r2=1.10&ty=u Index: pear/MDB2_Schema/MDB2/Schema/Parser.php diff -u pear/MDB2_Schema/MDB2/Schema/Parser.php:1.9 pear/MDB2_Schema/MDB2/Schema/Parser.php:1.10 --- pear/MDB2_Schema/MDB2/Schema/Parser.php:1.9 Thu May 19 00:44:52 2005 +++ pear/MDB2_Schema/MDB2/Schema/Parser.php Fri Jun 10 11:55:57 2005 @@ -42,7 +42,7 @@ // | Author: Christian Dickmann <dickmann@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Parser.php,v 1.9 2005/05/19 04:44:52 lsmith Exp $ +// $Id: Parser.php,v 1.10 2005/06/10 15:55:57 dufuz Exp $ // require_once 'XML/Parser.php'; @@ -254,6 +254,7 @@ if (isset($this->field['unsigned']) && !$this->isBoolean($this->field['unsigned'])) { $this->raiseError('field "notnull" has to be a boolean value', null, $xp); } + $this->table['fields'][$this->field_name] = $this->field; if (isset($this->field['default'])) { if ($this->field['type'] == 'clob' || $this->field['type'] == 'blob') { @@ -279,6 +280,11 @@ if (isset($this->index['unique']) && !$this->isBoolean($this->index['unique'])) { $this->raiseError('field "unique" has to be a boolean value', null, $xp); } + + if (isset($this->index['primary']) && !$this->isBoolean($this->index['primary'])) { + $this->raiseError('field "primary" has to be a boolean value', null, $xp); + } + if (!isset($this->index['was'])) { $this->index['was'] = $this->index_name; } @@ -308,10 +314,17 @@ if (isset($this->database_definition['sequences'][$this->seq_name])) { $this->raiseError('sequence "'.$this->seq_name.'" already exists', null, $xp); } + if (!isset($this->seq['was'])) { $this->seq['was'] = $this->seq_name; } if (isset($this->seq['on'])) { + if (isset($this->seq['on']['autoincrement']) && + (!$this->isBoolean($this->seq['on']['autoincrement']) && $this->seq['on']['autoincrement'] != 'force') + ) { + $this->raiseError('Auto increment has to be either a boolean value or "force"', null, $xp); + } + if ((!isset($this->seq['on']['table']) || !$this->seq['on']['table']) || (!isset($this->seq['on']['field']) || !$this->seq['on']['field']) ) { @@ -613,6 +626,13 @@ $this->index_name = $data; } break; + case 'database-table-declaration-index-primary': + if (isset($this->index['primary'])) { + $this->index['primary'] .= $data; + } else { + $this->index['primary'] = $data; + } + break; case 'database-table-declaration-index-unique': if (isset($this->index['unique'])) { $this->index['unique'] .= $data; @@ -664,6 +684,13 @@ $this->seq['start'] = $data; } break; + case 'database-sequence-on-autoincrement': + if (isset($this->seq['on']['autoincrement'])) { + $this->seq['on']['autoincrement'] .= $data; + } else { + $this->seq['on']['autoincrement'] = $data; + } + break; case 'database-sequence-on-table': if (isset($this->seq['on']['table'])) { $this->seq['on']['table'] .= $data; http://cvs.php.net/diff.php/pear/MDB2_Schema/MDB2/Schema/Writer.php?r1=1.7&r2=1.8&ty=u Index: pear/MDB2_Schema/MDB2/Schema/Writer.php diff -u pear/MDB2_Schema/MDB2/Schema/Writer.php:1.7 pear/MDB2_Schema/MDB2/Schema/Writer.php:1.8 --- pear/MDB2_Schema/MDB2/Schema/Writer.php:1.7 Thu Apr 21 12:59:10 2005 +++ pear/MDB2_Schema/MDB2/Schema/Writer.php Fri Jun 10 11:55:58 2005 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@backendmedia.com> | // +----------------------------------------------------------------------+ // -// $Id: Writer.php,v 1.7 2005/04/21 16:59:10 lsmith Exp $ +// $Id: Writer.php,v 1.8 2005/06/10 15:55:58 dufuz Exp $ // /** @@ -150,12 +150,19 @@ $buffer .= " <start>$start</start>$eol"; } } + if (isset($sequence_definition['on'])) { - $buffer .= " <on>$eol <table>".$sequence_definition['on']['table']. + $buffer .= " <on>$eol"; + if (isset($sequence_definition['on']['autoincrement'])) { + $buffer .= " <autoincrement>" . $sequence_definition['autoincrement'] . + "</autoincrement>$eol" + } + $buffer .= " <table>".$sequence_definition['on']['table']. "</table>$eol <field>".$sequence_definition['on']['field']. "</field>$eol </on>$eol"; } $buffer .= " </sequence>$eol"; + return $buffer; } @@ -277,7 +284,7 @@ '" is not yet supported'); } if (isset($field['notnull']) && $field['notnull']) { - $buffer .=(" <notnull>1</notnull>$eol"); + $buffer .=(" <notnull>true</notnull>$eol"); } if (isset($field['default'])) { $buffer .=(' <default>'.$this->_escapeSpecialChars($field['default']) @@ -290,8 +297,13 @@ foreach ($table['indexes'] as $index_name => $index) { $buffer .=("$eol <index>$eol <name>$index_name</name>$eol"); if (isset($index['unique'])) { - $buffer .=(" <unique>1</unique>$eol"); + $buffer .=(" <unique>true</unique>$eol"); } + + if (isset($index['primary'])) { + $buffer .=(" <primary>true</primary>$eol"); + } + foreach ($index['fields'] as $field_name => $field) { $buffer .=(" <field>$eol <name>$field_name</name>$eol"); if (is_array($field) && isset($field['sorting'])) {
« previous php.pear.cvs (#32465) next »