cvs: pear /MDB2/MDB2/Driver/Reverse Common.php ibase.php mysql.php mysqli.php pgsql.php sqlite.php

From: Date: Sun, 16 Oct 2005 09:39:54 +0000
Subject: cvs: pear /MDB2/MDB2/Driver/Reverse Common.php ibase.php mysql.php mysqli.php pgsql.php sqlite.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-35587@lists.php.net to get a copy of this message
lsmith Sun Oct 16 05:39:54 2005 EDT Modified files: /pear/MDB2/MDB2/Driver/Reverse Common.php ibase.php mysql.php mysqli.php pgsql.php sqlite.php Log: - split index and constraint handling

http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Reverse/Common.php?r1=1.14&r2=1.15&ty=u Index: pear/MDB2/MDB2/Driver/Reverse/Common.php diff -u pear/MDB2/MDB2/Driver/Reverse/Common.php:1.14 pear/MDB2/MDB2/Driver/Reverse/Common.php:1.15 --- pear/MDB2/MDB2/Driver/Reverse/Common.php:1.14 Fri Oct 14 19:07:14 2005 +++ pear/MDB2/MDB2/Driver/Reverse/Common.php Sun Oct 16 05:39:50 2005 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@pooteeweet.org> | // +----------------------------------------------------------------------+ // -// $Id: Common.php,v 1.14 2005/10/14 23:07:14 lsmith Exp $ +// $Id: Common.php,v 1.15 2005/10/16 09:39:50 lsmith Exp $ // /** @@ -132,7 +132,7 @@ } return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, - 'getTableIndexDefinition: getting index definition is not supported'); + 'getTableConstraintDefinition: getting index definition is not supported'); } // }}} http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Reverse/ibase.php?r1=1.20&r2=1.21&ty=u Index: pear/MDB2/MDB2/Driver/Reverse/ibase.php diff -u pear/MDB2/MDB2/Driver/Reverse/ibase.php:1.20 pear/MDB2/MDB2/Driver/Reverse/ibase.php:1.21 --- pear/MDB2/MDB2/Driver/Reverse/ibase.php:1.20 Fri Oct 14 17:57:07 2005 +++ pear/MDB2/MDB2/Driver/Reverse/ibase.php Sun Oct 16 05:39:50 2005 @@ -42,7 +42,7 @@ // | Author: Lorenzo Alberton <l.alberton@quipo.it> | // +----------------------------------------------------------------------+ // -// $Id: ibase.php,v 1.20 2005/10/14 21:57:07 quipo Exp $ +// $Id: ibase.php,v 1.21 2005/10/16 09:39:50 lsmith Exp $ // require_once 'MDB2/Driver/Reverse/Common.php'; @@ -258,8 +258,80 @@ $definition['unique'] = true; } if ($index['constraint_type'] == 'PRIMARY KEY') { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableIndexDefinition: it was not specified an existing table index'); + } + foreach ($fields as $field) { + $definition['fields'][$field] = array(); + //collation?!? + /* + if (array_key_exists('collation', $row)) { + $definition['fields'][$field]['sorting'] = ($row['collation'] == 'A' + ? 'ascending' : 'descending'); + } + */ + } + return $definition; + } + + // }}} + // {{{ getTableConstraintDefinition() + + /** + * get the stucture of a constraint into an array + * + * @param string $table name of table that should be used in method + * @param string $index_name name of index that should be used in method + * @return mixed data array on success, a MDB2 error on failure + * @access public + */ + function getTableConstraintDefinition($table, $index_name) + { + $db =& $this->getDBInstance(); + if (PEAR::isError($db)) { + return $db; + } + $table = strtoupper($table); + $index_name = strtoupper($index_name); + $query = "SELECT RDB\$INDEX_SEGMENTS.RDB\$FIELD_NAME AS field_name, + RDB\$INDICES.RDB\$UNIQUE_FLAG AS unique_flag, + RDB\$INDICES.RDB\$FOREIGN_KEY AS foreign_key, + RDB\$INDICES.RDB\$DESCRIPTION AS description, + RDB\$RELATION_CONSTRAINTS.RDB\$CONSTRAINT_TYPE as constraint_type + FROM RDB\$INDEX_SEGMENTS + LEFT JOIN RDB\$INDICES ON RDB\$INDICES.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME + LEFT JOIN RDB\$RELATION_CONSTRAINTS ON RDB\$RELATION_CONSTRAINTS.RDB\$INDEX_NAME = RDB\$INDEX_SEGMENTS.RDB\$INDEX_NAME + WHERE UPPER(RDB\$INDICES.RDB\$RELATION_NAME)='$table' + AND UPPER(RDB\$INDICES.RDB\$INDEX_NAME)='$index_name' + ORDER BY RDB\$INDEX_SEGMENTS.RDB\$FIELD_POSITION;"; + $result = $db->query($query); + if (PEAR::isError($result)) { + return $result; + } + + $index = $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC); + if (empty($index)) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); + } + $fields = array(); + do { + $fields[] = $row['field_name']; + } while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))); + $result->free(); + + if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { + $fields = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $fields); + } + + $definition = array(); + if ($index['constraint_type'] == 'PRIMARY KEY') { $definition['primary'] = true; } + if ($index['unique_flag']) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); + } foreach ($fields as $field) { $definition['fields'][$field] = array(); //collation?!? http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Reverse/mysql.php?r1=1.33&r2=1.34&ty=u Index: pear/MDB2/MDB2/Driver/Reverse/mysql.php diff -u pear/MDB2/MDB2/Driver/Reverse/mysql.php:1.33 pear/MDB2/MDB2/Driver/Reverse/mysql.php:1.34 --- pear/MDB2/MDB2/Driver/Reverse/mysql.php:1.33 Fri Oct 14 12:31:19 2005 +++ pear/MDB2/MDB2/Driver/Reverse/mysql.php Sun Oct 16 05:39:50 2005 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@pooteeweet.org> | // +----------------------------------------------------------------------+ // -// $Id: mysql.php,v 1.33 2005/10/14 16:31:19 lsmith Exp $ +// $Id: mysql.php,v 1.34 2005/10/16 09:39:50 lsmith Exp $ // require_once 'MDB2/Driver/Reverse/Common.php'; @@ -171,8 +171,10 @@ } if ($index_name == $key_name) { if ($row['key_name'] == 'PRIMARY') { - $definition['primary'] = true; - } elseif (!$row['non_unique']) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableIndexDefinition: it was not specified an existing table index'); + } + if (!$row['non_unique']) { $definition['unique'] = true; } $column_name = $row['column_name']; @@ -199,6 +201,74 @@ } // }}} + // {{{ getTableConstraintDefinition() + + /** + * get the stucture of a constraint into an array + * + * @param string $table name of table that should be used in method + * @param string $index_name name of index that should be used in method + * @return mixed data array on success, a MDB2 error on failure + * @access public + */ + function getTableConstraintDefinition($table, $index_name) + { + $db =& $this->getDBInstance(); + if (PEAR::isError($db)) { + return $db; + } + + $result = $db->query("SHOW INDEX FROM $table"); + if (PEAR::isError($result)) { + return $result; + } + $definition = array(); + while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) { + if (!($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) + || $db->options['field_case'] != CASE_LOWER + ) { + $row = array_change_key_case($row, CASE_LOWER); + } + $key_name = $row['key_name']; + if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { + if ($db->options['field_case'] == CASE_LOWER) { + $key_name = strtolower($key_name); + } else { + $key_name = strtoupper($key_name); + } + } + if ($index_name == $key_name) { + if ($row['key_name'] == 'PRIMARY') { + $definition['primary'] = true; + } + if (!$row['non_unique']) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); + } + $column_name = $row['column_name']; + if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { + if ($db->options['field_case'] == CASE_LOWER) { + $column_name = strtolower($column_name); + } else { + $column_name = strtoupper($column_name); + } + } + $definition['fields'][$column_name] = array(); + if (array_key_exists('collation', $row)) { + $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' + ? 'ascending' : 'descending'); + } + } + } + $result->free(); + if (!array_key_exists('fields', $definition)) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); + } + return $definition; + } + + // }}} // {{{ tableInfo() /** http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Reverse/mysqli.php?r1=1.18&r2=1.19&ty=u Index: pear/MDB2/MDB2/Driver/Reverse/mysqli.php diff -u pear/MDB2/MDB2/Driver/Reverse/mysqli.php:1.18 pear/MDB2/MDB2/Driver/Reverse/mysqli.php:1.19 --- pear/MDB2/MDB2/Driver/Reverse/mysqli.php:1.18 Fri Oct 7 06:28:59 2005 +++ pear/MDB2/MDB2/Driver/Reverse/mysqli.php Sun Oct 16 05:39:50 2005 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@pooteeweet.org> | // +----------------------------------------------------------------------+ // -// $Id: mysqli.php,v 1.18 2005/10/07 10:28:59 lsmith Exp $ +// $Id: mysqli.php,v 1.19 2005/10/16 09:39:50 lsmith Exp $ // require_once 'MDB2/Driver/Reverse/Common.php'; @@ -226,8 +226,10 @@ } if ($index_name == $key_name) { if ($row['key_name'] == 'PRIMARY') { - $definition['primary'] = true; - } elseif (!$row['non_unique']) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableIndexDefinition: it was not specified an existing table index'); + } + if (!$row['non_unique']) { $definition['unique'] = true; } $column_name = $row['column_name']; @@ -254,6 +256,74 @@ } // }}} + // {{{ getTableConstraintDefinition() + + /** + * get the stucture of a constraint into an array + * + * @param string $table name of table that should be used in method + * @param string $index_name name of index that should be used in method + * @return mixed data array on success, a MDB2 error on failure + * @access public + */ + function getTableConstraintDefinition($table, $index_name) + { + $db =& $this->getDBInstance(); + if (PEAR::isError($db)) { + return $db; + } + + $result = $db->query("SHOW INDEX FROM $table"); + if (PEAR::isError($result)) { + return $result; + } + $definition = array(); + while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) { + if (!($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) + || $db->options['field_case'] != CASE_LOWER + ) { + $row = array_change_key_case($row, CASE_LOWER); + } + $key_name = $row['key_name']; + if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { + if ($db->options['field_case'] == CASE_LOWER) { + $key_name = strtolower($key_name); + } else { + $key_name = strtoupper($key_name); + } + } + if ($index_name == $key_name) { + if ($row['key_name'] == 'PRIMARY') { + $definition['primary'] = true; + } + if (!$row['non_unique']) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); + } + $column_name = $row['column_name']; + if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { + if ($db->options['field_case'] == CASE_LOWER) { + $column_name = strtolower($column_name); + } else { + $column_name = strtoupper($column_name); + } + } + $definition['fields'][$column_name] = array(); + if (array_key_exists('collation', $row)) { + $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A' + ? 'ascending' : 'descending'); + } + } + } + $result->free(); + if (!array_key_exists('fields', $definition)) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); + } + return $definition; + } + + // }}} // {{{ tableInfo() /** http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Reverse/pgsql.php?r1=1.26&r2=1.27&ty=u Index: pear/MDB2/MDB2/Driver/Reverse/pgsql.php diff -u pear/MDB2/MDB2/Driver/Reverse/pgsql.php:1.26 pear/MDB2/MDB2/Driver/Reverse/pgsql.php:1.27 --- pear/MDB2/MDB2/Driver/Reverse/pgsql.php:1.26 Fri Oct 14 12:31:19 2005 +++ pear/MDB2/MDB2/Driver/Reverse/pgsql.php Sun Oct 16 05:39:50 2005 @@ -42,7 +42,7 @@ // | Author: Paul Cooper <pgc@ucecom.com> | // +----------------------------------------------------------------------+ // -// $Id: pgsql.php,v 1.26 2005/10/14 16:31:19 lsmith Exp $ +// $Id: pgsql.php,v 1.27 2005/10/16 09:39:50 lsmith Exp $ require_once 'MDB2/Driver/Reverse/Common.php'; @@ -133,7 +133,6 @@ return $definition; } - // }}} // {{{ getTableIndexDefinition() /** @@ -167,8 +166,10 @@ $definition = array(); if ($row['indisprimary'] == 't') { - $definition['primary'] = true; - } elseif ($row['indisunique'] == 't') { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableIndexDefinition: it was not specified an existing table index'); + } + if ($row['indisunique'] == 't') { $definition['unique'] = true; } @@ -181,6 +182,54 @@ } // }}} + // {{{ getTableConstraintDefinition() + /** + * get the stucture of a constraint into an array + * + * @param string $table name of table that should be used in method + * @param string $index_name name of index that should be used in method + * @return mixed data array on success, a MDB2 error on failure + * @access public + */ + function getTableConstraintDefinition($table, $index_name) + { + $db =& $this->getDBInstance(); + if (PEAR::isError($db)) { + return $db; + } + + $query = "SELECT relname, indisunique, indisprimary, indkey FROM pg_index, pg_class + WHERE (pg_class.relname='$index_name') AND (pg_class.oid=pg_index.indexrelid)"; + $row = $db->queryRow($query, null, MDB2_FETCHMODE_ASSOC); + if (PEAR::isError($row)) { + return $row; + } + if ($row['relname'] != $index_name) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); + } + + $db->loadModule('Manager'); + $columns = $db->manager->listTableFields($table); + + $definition = array(); + if ($row['indisprimary'] == 't') { + $definition['primary'] = true; + } + if ($row['indisunique'] == 't') { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); + } + + $index_column_numbers = explode(' ', $row['indkey']); + + foreach ($index_column_numbers as $number) { + $definition['fields'][$columns[($number - 1)]] = array('sorting' => 'ascending'); + } + return $definition; + } + + // }}} // {{{ tableInfo() /** http://cvs.php.net/diff.php/pear/MDB2/MDB2/Driver/Reverse/sqlite.php?r1=1.25&r2=1.26&ty=u Index: pear/MDB2/MDB2/Driver/Reverse/sqlite.php diff -u pear/MDB2/MDB2/Driver/Reverse/sqlite.php:1.25 pear/MDB2/MDB2/Driver/Reverse/sqlite.php:1.26 --- pear/MDB2/MDB2/Driver/Reverse/sqlite.php:1.25 Fri Oct 14 05:43:10 2005 +++ pear/MDB2/MDB2/Driver/Reverse/sqlite.php Sun Oct 16 05:39:50 2005 @@ -42,7 +42,7 @@ // | Author: Lukas Smith <smith@pooteeweet.org> | // +----------------------------------------------------------------------+ // -// $Id: sqlite.php,v 1.25 2005/10/14 09:43:10 lsmith Exp $ +// $Id: sqlite.php,v 1.26 2005/10/16 09:39:50 lsmith Exp $ // require_once 'MDB2/Driver/Reverse/Common.php'; @@ -194,10 +194,71 @@ return $db; } - if ($index_name == 'PRIMARY') { + $query = "SELECT sql FROM sqlite_master WHERE type='index' AND name='$index_name' AND tbl_name='$table' AND sql NOT NULL ORDER BY name"; + $result = $db->query($query); + if (PEAR::isError($result)) { + return $result; + } + $columns = $result->getColumnNames(); + $column = 'sql'; + if (!isset($columns[$column])) { + $result->free(); + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableIndexDefinition: it was not specified an existing table index'); + } + + $sql = strtolower($result->fetchOne()); + $key_name = $index_name; + $start_pos = strpos($sql, '('); + $end_pos = strrpos($sql, ')'); + $column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1); + $column_names = split(',', $column_names); + + $definition = array(); + if (strstr($sql, ' primary ')) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableIndexDefinition: it was not specified an existing table index'); + } + if (strstr($sql, ' unique ')) { + $definition['unique'] = true; + } + $count = count($column_names); + for ($i=0; $i<$count; ++$i) { + $column_name = strtok($column_names[$i]," "); + $collation = strtok(" "); + $definition['fields'][$column_name] = array(); + if (!empty($collation)) { + $definition['fields'][$column_name]['sorting'] = + ($collation=='ASC' ? 'ascending' : 'descending'); + } + } + + $result->free(); + if (!array_key_exists('fields', $definition)) { return $db->raiseError(MDB2_ERROR, null, null, - 'getTableIndexDefinition: PRIMARY is an hidden index'); + 'getTableIndexDefinition: it was not specified an existing table index'); + } + return $definition; + } + + // }}} + // {{{ getTableConstraintDefinition() + + /** + * get the stucture of a constraint into an array + * + * @param string $table name of table that should be used in method + * @param string $index_name name of index that should be used in method + * @return mixed data array on success, a MDB2 error on failure + * @access public + */ + function getTableConstraintDefinition($table, $index_name) + { + $db =& $this->getDBInstance(); + if (PEAR::isError($db)) { + return $db; } + $query = "SELECT sql FROM sqlite_master WHERE type='index' AND name='$index_name' AND tbl_name='$table' AND sql NOT NULL ORDER BY name"; $result = $db->query($query); if (PEAR::isError($result)) { @@ -207,7 +268,8 @@ $column = 'sql'; if (!isset($columns[$column])) { $result->free(); - return $db->raiseError('getTableIndexDefinition: show index does not return the table creation sql'); + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); } $sql = strtolower($result->fetchOne()); @@ -220,8 +282,10 @@ $definition = array(); if (strstr($sql, ' primary ')) { $definition['primary'] = true; - } elseif (strstr($sql, ' unique ')) { - $definition['unique'] = true; + } + if (strstr($sql, ' unique ')) { + return $db->raiseError(MDB2_ERROR, null, null, + 'getTableConstraintDefinition: it was not specified an existing table index'); } $count = count($column_names); for ($i=0; $i<$count; ++$i) { @@ -237,7 +301,7 @@ $result->free(); if (!array_key_exists('fields', $definition)) { return $db->raiseError(MDB2_ERROR, null, null, - 'getTableIndexDefinition: it was not specified an existing table index'); + 'getTableConstraintDefinition: it was not specified an existing table index'); } return $definition; }
« previous php.pear.cvs (#35587) next »