cvs: pear /MDB2 package_mssql.php /MDB2/MDB2/Driver/Reverse mssql.php

From: Date: Sun, 04 Mar 2007 17:19:05 +0000
Subject: cvs: pear /MDB2 package_mssql.php /MDB2/MDB2/Driver/Reverse mssql.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-44943@lists.php.net to get a copy of this message
quipo Sun Mar 4 17:19:05 2007 UTC Modified files: /pear/MDB2/MDB2/Driver/Reverse mssql.php /pear/MDB2 package_mssql.php Log: implemented getTableConstraintDefinition() in the Reverse module http://cvs.php.net/viewvc.cgi/pear/MDB2/MDB2/Driver/Reverse/mssql.php?r1=1.33&r2=1.34&diff_format=u Index: pear/MDB2/MDB2/Driver/Reverse/mssql.php diff -u pear/MDB2/MDB2/Driver/Reverse/mssql.php:1.33 pear/MDB2/MDB2/Driver/Reverse/mssql.php:1.34 --- pear/MDB2/MDB2/Driver/Reverse/mssql.php:1.33 Sun Feb 25 11:14:34 2007 +++ pear/MDB2/MDB2/Driver/Reverse/mssql.php Sun Mar 4 17:19:04 2007 @@ -43,7 +43,7 @@ // | Lorenzo Alberton <l.alberton@quipo.it> | // +----------------------------------------------------------------------+ // -// $Id: mssql.php,v 1.33 2007/02/25 11:14:34 quipo Exp $ +// $Id: mssql.php,v 1.34 2007/03/04 17:19:04 quipo Exp $ // require_once 'MDB2/Driver/Reverse/Common.php'; @@ -251,6 +251,81 @@ } // }}} + // {{{ getTableConstraintDefinition() + + /** + * Get the structure 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 = $db->quoteIdentifier($table, true); + $query = "SELECT k.table_name, + k.column_name field_name, + CASE c.constraint_type WHEN 'PRIMARY KEY' THEN 1 ELSE 0 END 'primary', + CASE c.constraint_type WHEN 'UNIQUE' THEN 1 ELSE 0 END 'unique', + CASE c.constraint_type WHEN 'FOREIGN KEY' THEN 1 ELSE 0 END 'foreign', + CASE c.constraint_type WHEN 'CHECK' THEN 1 ELSE 0 END 'check' + FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k + LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS c + ON k.table_name = c.table_name + AND k.constraint_name = c.constraint_name + WHERE k.constraint_catalog = DB_NAME() + AND k.table_name = '$table' + AND k.constraint_name = '%s' + ORDER BY k.constraint_name, + k.ordinal_position"; + + $index_name_mdb2 = $db->getIndexName($index_name); + $result = $db->queryRow(sprintf($query, $index_name_mdb2)); + if (!PEAR::isError($result) && !is_null($result)) { + // apply 'idxname_format' only if the query succeeded, otherwise + // fallback to the given $index_name, without transformation + $index_name = $index_name_mdb2; + } + $result = $db->query(sprintf($query, $index_name)); + if (PEAR::isError($result)) { + return $result; + } + + $definition = array(); + while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) { + $column_name = $row['field_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 (!empty($row['collation'])) { + $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC' + ? 'ascending' : 'descending'); + } + $definition['primary'] = $row['primary']; + $definition['unique'] = $row['unique']; + $definition['foreign'] = $row['foreign']; + $definition['check'] = $row['check']; + } + $result->free(); + if (empty($definition['fields'])) { + return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null, + 'it was not specified an existing table index', __FUNCTION__); + } + return $definition; + } + + // }}} // {{{ getTriggerDefinition() /** http://cvs.php.net/viewvc.cgi/pear/MDB2/package_mssql.php?r1=1.76&r2=1.77&diff_format=u Index: pear/MDB2/package_mssql.php diff -u pear/MDB2/package_mssql.php:1.76 pear/MDB2/package_mssql.php:1.77 --- pear/MDB2/package_mssql.php:1.76 Wed Feb 28 16:05:45 2007 +++ pear/MDB2/package_mssql.php Sun Mar 4 17:19:04 2007 @@ -40,6 +40,7 @@ - migrated to package.xml version 2 - implemented getTableFieldDefinition() in the Reverse module - implemented getTableIndexDefinition() in the Reverse module +- implemented getTableConstraintDefinition() in the Reverse module - implemented getTriggerDefinition() in the Reverse module - implemented guid() in the Function module [globally unique identifier] - implemented a fallback mechanism within getTableIndexDefinition() in the Reverse

« previous php.pear.cvs (#44943) next »