cvs: pear /MDB2/MDB2/Driver/Manager oci8.php pgsql.php sqlite.php

From: Date: Fri, 14 Oct 2005 16:33:21 +0000
Subject: cvs: pear /MDB2/MDB2/Driver/Manager oci8.php pgsql.php sqlite.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-35536@lists.php.net to get a copy of this message
lsmith Fri Oct 14 12:33:21 2005 EDT Modified files: /pear/MDB2/MDB2/Driver/Manager oci8.php pgsql.php sqlite.php Log: - split index and contraint handling

http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Manager/oci8.php?r1=1.43&r2=1.44&ty=u Index: pear/MDB2/MDB2/Driver/Manager/oci8.php diff -u pear/MDB2/MDB2/Driver/Manager/oci8.php:1.43 pear/MDB2/MDB2/Driver/Manager/oci8.php:1.44 --- pear/MDB2/MDB2/Driver/Manager/oci8.php:1.43 Thu Oct 13 10:23:36 2005 +++ pear/MDB2/MDB2/Driver/Manager/oci8.php Fri Oct 14 12:33:20 2005 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@pooteeweet.org> | // +----------------------------------------------------------------------+ -// $Id: oci8.php,v 1.43 2005/10/13 14:23:36 lsmith Exp $ +// $Id: oci8.php,v 1.44 2005/10/14 16:33:20 lsmith Exp $ require_once 'MDB2/Driver/Manager/Common.php'; @@ -144,7 +144,7 @@ 'primary' => true, 'fields' => array($name), ); - $result = $db->manager->createIndex($table, $index_name, $definition); + $result = $db->manager->createConstraint($table, $index_name, $definition); if (PEAR::isError($result)) { return $db->raiseError(MDB2_ERROR, null, null, '_makeAutoincrement: primary key for autoincrement PK could not be created'); @@ -201,6 +201,13 @@ return $db->raiseError(MDB2_ERROR, null, null, '_dropAutoincrement: sequence for autoincrement PK could not be dropped'); } + + $index_name = $table . '_AUTOINCREMENT_PK'; + $result = $db->manager->dropConstraint($table, $index_name); + if (PEAR::isError($result)) { + return $db->raiseError(MDB2_ERROR, null, null, + '_dropAutoincrement: primary key for autoincrement PK could not be dropped'); + } } return MDB2_OK; @@ -602,79 +609,54 @@ } return $result; } + // }}} - // {{{ createIndex() + // {{{ listTableIndexes() /** - * get the stucture of a field into an array + * list all indexes in a table * - * @param string $table name of the table on which the index is to be created - * @param string $name name of the index to be created - * @param array $definition associative array that defines properties of the index to be created. - * Currently, only one property named FIELDS is supported. This property - * is also an associative with the names of the index fields as array - * indexes. Each entry of this array is set to another type of associative - * array that specifies properties of the index that are specific to - * each field. - * - * Currently, only the sorting property is supported. It should be used - * to define the sorting direction of the index. It may be set to either - * ascending or descending. - * - * Not all DBMS support index sorting direction configuration. The DBMS - * drivers of those that do not support it ignore this property. Use the - * function supports() to determine whether the DBMS driver can manage indexes. - * - * Example - * array( - * 'fields' => array( - * 'user_name' => array( - * 'sorting' => 'ascending' - * ), - * 'last_login' => array() - * ) - * ) - * @return mixed MDB2_OK on success, a MDB2 error on failure + * @param string $table name of table that should be used in method + * @return mixed data array on success, a MDB2 error on failure * @access public */ - function createIndex($table, $name, $definition) + function listTableIndexes($table) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } - if (array_key_exists('primary', $definition) && $definition['primary']) { - $query = "ALTER TABLE $table ADD CONSTRAINT $name PRIMARY KEY ("; - } else { - $query = 'CREATE'; - if (array_key_exists('unique', $definition) && $definition['unique']) { - $query.= ' UNIQUE'; - } - $query .= " INDEX $name ON $table ("; + $query = "SELECT index_name name FROM user_indexes WHERE table_name='$table'"; + $result = $db->queryCol($query); + if (PEAR::isError($result)) { + return $result; } - $query .= implode(', ', array_keys($definition['fields'])) . ')'; - - return $db->query($query); + if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE + && $db->options['field_case'] == CASE_LOWER + ) { + $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result); + } + return $result; } // }}} - // {{{ listTableIndexes() + // {{{ listTableConstraints() /** - * list all indexes in a table + * list all sonstraints in a table * - * @param string $table name of table that should be used in method + * @param string $table name of table that should be used in method * @return mixed data array on success, a MDB2 error on failure * @access public */ - function listTableIndexes($table) + function listTableConstraints($table) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } - $query = "SELECT contraint_name name FROM user_contraints WHERE contraint_type = 'P' AND table_name='$table'" - $query.= "UNION SELECT index_name name FROM user_indexes WHERE table_name='$table'"; + $table = strtoupper($table); + $query = "SELECT index_name name FROM user_constraints WHERE table_name='$table'"; $result = $db->queryCol($query); if (PEAR::isError($result)) { return $result; http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Manager/pgsql.php?r1=1.33&r2=1.34&ty=u Index: pear/MDB2/MDB2/Driver/Manager/pgsql.php diff -u pear/MDB2/MDB2/Driver/Manager/pgsql.php:1.33 pear/MDB2/MDB2/Driver/Manager/pgsql.php:1.34 --- pear/MDB2/MDB2/Driver/Manager/pgsql.php:1.33 Thu Oct 13 10:23:36 2005 +++ pear/MDB2/MDB2/Driver/Manager/pgsql.php Fri Oct 14 12:33:20 2005 @@ -42,7 +42,7 @@ // | Author: Paul Cooper <pgc@ucecom.com> | // +----------------------------------------------------------------------+ // -// $Id: pgsql.php,v 1.33 2005/10/13 14:23:36 lsmith Exp $ +// $Id: pgsql.php,v 1.34 2005/10/14 16:33:20 lsmith Exp $ require_once 'MDB2/Driver/Manager/Common.php'; @@ -453,82 +453,54 @@ } // }}} - // {{{ createIndex() + // {{{ listTableIndexes() /** - * get the stucture of a field into an array + * list all indexes in a table * - * @param string $table name of the table on which the index is to be created - * @param string $name name of the index to be created - * @param array $definition associative array that defines properties of the index to be created. - * Currently, only one property named FIELDS is supported. This property - * is also an associative with the names of the index fields as array - * indexes. Each entry of this array is set to another type of associative - * array that specifies properties of the index that are specific to - * each field. - * - * Currently, only the sorting property is supported. It should be used - * to define the sorting direction of the index. It may be set to either - * ascending or descending. - * - * Not all DBMS support index sorting direction configuration. The DBMS - * drivers of those that do not support it ignore this property. Use the - * function supports() to determine whether the DBMS driver can manage indexes. - * - * Example - * array( - * 'fields' => array( - * 'user_name' => array( - * 'sorting' => 'ascending' - * ), - * 'last_login' => array() - * ) - * ) - * @return mixed MDB2_OK on success, a MDB2 error on failure + * @param string $table name of table that should be used in method + * @return mixed data array on success, a MDB2 error on failure * @access public */ - function createIndex($table, $name, $definition) + function listTableIndexes($table) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } - if (array_key_exists('primary', $definition) && $definition['primary']) { - $query = "ALTER TABLE $table ADD CONSTRAINT $name PRIMARY KEY ("; - } else { - $query = 'CREATE'; - if (array_key_exists('unique', $definition) && $definition['unique']) { - $query.= ' UNIQUE'; - } - $query.= " INDEX $name ON $table ("; + $subquery = "SELECT indexrelid FROM pg_index, pg_class"; + $subquery.= " WHERE (pg_class.relname='$table') AND (pg_class.oid=pg_index.indrelid)"; + $result = $db->queryCol("SELECT relname FROM pg_class WHERE oid IN ($subquery)"); + if (PEAR::isError($result)) { + return $result; } - $query.= implode(', ', array_keys($definition['fields'])); - $query.= ')'; - - return $db->query($query); + if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { + $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result); + } + return $result; } // }}} - // {{{ listTableIndexes() + // {{{ listTableConstraints() /** - * list all indexes in a table + * list all sonstraints in a table * * @param string $table name of table that should be used in method * @return mixed data array on success, a MDB2 error on failure * @access public */ - function listTableIndexes($table) + function listTableConstraints($table) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } - $subquery = "SELECT indexrelid FROM pg_index, pg_class"; - $subquery.= " WHERE (pg_class.relname='$table') AND (pg_class.oid=pg_index.indrelid)"; - $result = $db->queryCol("SELECT relname FROM pg_class WHERE oid IN ($subquery)"); + $query = "SELECT DISTINCT conname FROM pg_constraint, pg_class"; + $query.= " WHERE (pg_class.relname='$table') AND (pg_class.oid=pg_constraint.conrelid)"; + $result = $db->queryCol($query); if (PEAR::isError($result)) { return $result; } http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Manager/sqlite.php?r1=1.27&r2=1.28&ty=u Index: pear/MDB2/MDB2/Driver/Manager/sqlite.php diff -u pear/MDB2/MDB2/Driver/Manager/sqlite.php:1.27 pear/MDB2/MDB2/Driver/Manager/sqlite.php:1.28 --- pear/MDB2/MDB2/Driver/Manager/sqlite.php:1.27 Fri Oct 14 05:42:44 2005 +++ pear/MDB2/MDB2/Driver/Manager/sqlite.php Fri Oct 14 12:33:20 2005 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@pooteeweet.org> | // +----------------------------------------------------------------------+ // -// $Id: sqlite.php,v 1.27 2005/10/14 09:42:44 lsmith Exp $ +// $Id: sqlite.php,v 1.28 2005/10/14 16:33:20 lsmith Exp $ // require_once 'MDB2/Driver/Manager/Common.php'; @@ -262,16 +262,8 @@ return $db; } - if (strtolower($name) == 'primary') { - return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, - 'Index may not be named primary'); - } - $query = 'CREATE'; - if (array_key_exists('primary', $definition) && $definition['primary']) { - return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, - 'Primary Key creation must be done during table creation'); - } elseif (array_key_exists('unique', $definition) && $definition['unique']) { + if (array_key_exists('unique', $definition) && $definition['unique']) { $query.= ' UNIQUE'; } $query .= " INDEX $name ON $table ("; @@ -296,57 +288,111 @@ } // }}} - // {{{ dropIndex() + // {{{ listTableIndexes() /** - * drop existing index + * list all indexes in a table * - * @param string $table name of table that should be used in method - * @param string $name name of the index to be dropped + * @param string $table name of table that should be used in method + * @return mixed data array on success, a MDB2 error on failure + * @access public + */ + function listTableIndexes($table) + { + $db =& $this->getDBInstance(); + if (PEAR::isError($db)) { + return $db; + } + + $query = "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='$table' AND sql NOT NULL ORDER BY name"; + $result = $db->queryCol($query); + if (PEAR::isError($result)) { + return $result; + } + if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { + $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result); + } + return $result; + } + + + // }}} + // {{{ createConstraint() + + /** + * create a constraint on a table + * + * @param string $table name of the table on which the constraint is to be created + * @param string $name name of the constraint to be created + * @param array $definition associative array that defines properties of the constraint to be created. + * Currently, only one property named FIELDS is supported. This property + * is also an associative with the names of the constraint fields as array + * constraints. Each entry of this array is set to another type of associative + * array that specifies properties of the constraint that are specific to + * each field. + * + * Example + * array( + * 'fields' => array( + * 'user_name' => array(), + * 'last_login' => array() + * ) + * ) * @return mixed MDB2_OK on success, a MDB2 error on failure * @access public */ - function dropIndex($table, $name) + function createConstraint($table, $name, $definition) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } - if (strtolower($name) == 'primary') { - return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, - 'Index may not be named primary'); + return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, + 'createConstraint: Creating Constraints is not supported'); + } + + // }}} + // {{{ dropConstraint() + + /** + * drop existing constraint + * + * @param string $table name of table that should be used in method + * @param string $name name of the constraint to be dropped + * @return mixed MDB2_OK on success, a MDB2 error on failure + * @access public + */ + function dropConstraint($table, $name) + { + $db =& $this->getDBInstance(); + if (PEAR::isError($db)) { + return $db; } - return $db->query("DROP INDEX $name"); + return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, + 'dropConstraints: Drop Constraints is not supported'); } // }}} - // {{{ listTableIndexes() + // {{{ listTableConstraints() /** - * list all indexes in a table + * list all sonstraints in a table * * @param string $table name of table that should be used in method * @return mixed data array on success, a MDB2 error on failure * @access public */ - function listTableIndexes($table) + function listTableConstraints($table) { $db =& $this->getDBInstance(); if (PEAR::isError($db)) { return $db; } - $query = "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='$table' AND sql NOT NULL ORDER BY name"; - $result = $db->queryCol($query); - if (PEAR::isError($result)) { - return $result; - } - if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { - $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result); - } - return $result; + return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, + 'listTableConstraints: List Constraints is not supported'); } // }}}
« previous php.pear.cvs (#35536) next »