From 23c1cf0e5b95d17f74446d0423d994fdbf7fcced Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 31 Mar 2014 12:58:43 -0400 Subject: [PATCH] Improvements for scrutinizer --- classes/db_util.php | 66 +++++++++++++++++++++++----- common.php | 7 ++- drivers/firebird/firebird_result.php | 2 +- drivers/firebird/firebird_util.php | 60 ------------------------- drivers/pgsql/pgsql_util.php | 59 ------------------------- 5 files changed, 62 insertions(+), 132 deletions(-) diff --git a/classes/db_util.php b/classes/db_util.php index b674413..f712b17 100644 --- a/classes/db_util.php +++ b/classes/db_util.php @@ -25,7 +25,7 @@ abstract class DB_Util { * Reference to the current connection object */ private $conn; - + /** * Save a reference to the connection object for later use * @@ -33,11 +33,11 @@ abstract class DB_Util { */ public function __construct($conn) { - $this->conn = $conn; + $this->conn = $conn; } - + // -------------------------------------------------------------------------- - + /** * Enable calling driver methods * @@ -49,22 +49,68 @@ abstract class DB_Util { { return call_user_func_array(array($this->conn, $method), $args); } - + // -------------------------------------------------------------------------- // ! Abstract Methods // -------------------------------------------------------------------------- /** - * Get database-specific sql to create a new table + * Convienience public function to generate sql for creating a db table * - * @abstract * @param string $name - * @param array $columns + * @param array $fields * @param array $constraints * @param array $indexes + * * @return string */ - abstract public function create_table($name, $columns, array $constraints=array(), array $indexes=array()); + public function create_table($name, $fields, array $constraints=array(), array $indexes=array()) + { + $column_array = array(); + + // Reorganize into an array indexed with column information + // Eg $column_array[$colname] = array( + // 'type' => ..., + // 'constraint' => ..., + // 'index' => ..., + // ) + foreach($fields as $colname => $type) + { + if(is_numeric($colname)) + { + $colname = $type; + } + + $column_array[$colname] = array(); + $column_array[$colname]['type'] = ($type !== $colname) ? $type : ''; + } + + if( ! empty($constraints)) + { + foreach($constraints as $col => $const) + { + $column_array[$col]['constraint'] = $const; + } + } + + // Join column definitons together + $columns = array(); + foreach($column_array as $n => $props) + { + $str = '"'.$n.'"'; + $str .= (isset($props['type'])) ? " {$props['type']}" : ""; + $str .= (isset($props['constraint'])) ? " {$props['constraint']}" : ""; + + $columns[] = $str; + } + + // Generate the sql for the creation of the table + $sql = 'CREATE TABLE "'.$name.'" ('; + $sql .= implode(', ', $columns); + $sql .= ')'; + + return $sql; + } /** * Get database-specific sql to drop a table @@ -74,7 +120,7 @@ abstract class DB_Util { * @return string */ abstract public function delete_table($name); - + /** * Return an SQL file with the database table structure * diff --git a/common.php b/common.php index 925ac4c..133c646 100644 --- a/common.php +++ b/common.php @@ -88,9 +88,12 @@ function Query($params = '') { return $cmanager->get_connection($params); } + elseif ( ! is_scalar($params)) + { + // Otherwise, return a new connection + return $cmanager->connect($params); + } - // Otherwise, return a new connection - return $cmanager->connect($params); } // End of common.php \ No newline at end of file diff --git a/drivers/firebird/firebird_result.php b/drivers/firebird/firebird_result.php index 8f344bb..8555492 100644 --- a/drivers/firebird/firebird_result.php +++ b/drivers/firebird/firebird_result.php @@ -61,7 +61,7 @@ class Firebird_Result extends PDOStatement { // but we only want "interbase result" types when attempting to fetch data if (is_resource($link) && get_resource_type($link) === "interbase result") { - while($row = ibase_fetch_assoc($link, IBASE_FETCH_BLOBS)) + while($row = fbird_fetch_assoc($link, IBASE_FETCH_BLOBS)) { $this->result[] = $row; } diff --git a/drivers/firebird/firebird_util.php b/drivers/firebird/firebird_util.php index 27d967c..1451e2a 100644 --- a/drivers/firebird/firebird_util.php +++ b/drivers/firebird/firebird_util.php @@ -24,66 +24,6 @@ */ class Firebird_Util extends DB_Util { - /** - * Convienience public function to generate sql for creating a db table - * - * @param string $name - * @param array $fields - * @param array $constraints - * @param array $indexes - * - * @return string - */ - public function create_table($name, $fields, array $constraints=array(), array $indexes=array()) - { - $column_array = array(); - - // Reorganize into an array indexed with column information - // Eg $column_array[$colname] = array( - // 'type' => ..., - // 'constraint' => ..., - // 'index' => ..., - // ) - foreach($fields as $colname => $type) - { - if(is_numeric($colname)) - { - $colname = $type; - } - - $column_array[$colname] = array(); - $column_array[$colname]['type'] = ($type !== $colname) ? $type : ''; - } - - if( ! empty($constraints)) - { - foreach($constraints as $col => $const) - { - $column_array[$col]['constraint'] = $const; - } - } - - // Join column definitons together - $columns = array(); - foreach($column_array as $n => $props) - { - $str = '"'.$n.'"'; - $str .= (isset($props['type'])) ? " {$props['type']}" : ""; - $str .= (isset($props['constraint'])) ? " {$props['constraint']}" : ""; - - $columns[] = $str; - } - - // Generate the sql for the creation of the table - $sql = 'CREATE TABLE "'.$name.'" ('; - $sql .= implode(', ', $columns); - $sql .= ')'; - - return $sql; - } - - // -------------------------------------------------------------------------- - /** * Drop the selected table * diff --git a/drivers/pgsql/pgsql_util.php b/drivers/pgsql/pgsql_util.php index 10b9aaa..6c31e55 100644 --- a/drivers/pgsql/pgsql_util.php +++ b/drivers/pgsql/pgsql_util.php @@ -23,65 +23,6 @@ */ class PgSQL_Util extends DB_Util { - /** - * Database-specific method to create a new table - * - * @param string $name - * @param array $columns - * @param array $constraints - * @param array $indexes - * @return string - */ - public function create_table($name, $columns, array $constraints=array(), array $indexes=array()) - { - $column_array = array(); - - // Reorganize into an array indexed with column information - // Eg $column_array[$colname] = array( - // 'type' => ..., - // 'constraint' => ..., - // 'index' => ..., - // ) - foreach($columns as $colname => $type) - { - if(is_numeric($colname)) - { - $colname = $type; - } - - $column_array[$colname] = array(); - $column_array[$colname]['type'] = ($type !== $colname) ? $type : ''; - } - - if( ! empty($constraints)) - { - foreach($constraints as $col => $const) - { - $column_array[$col]['constraint'] = $const; - } - } - - // Join column definitons together - $columns = array(); - foreach($column_array as $n => $props) - { - $str = "{$n} "; - $str .= (isset($props['type'])) ? "{$props['type']} " : ""; - $str .= (isset($props['constraint'])) ? $props['constraint'] : ""; - - $columns[] = $str; - } - - // Generate the sql for the creation of the table - $sql = "CREATE TABLE \"{$name}\" ("; - $sql .= implode(", ", $columns); - $sql .= ")"; - - return $sql; - } - - // -------------------------------------------------------------------------- - /** * Database-specific SQL for dropping a table *