From 12f53c5fd5cd441d287539287e272e59034bc941 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 18 Apr 2012 15:53:06 -0400 Subject: [PATCH] Split off db_util classes for each driver --- classes/db_pdo.php | 16 ++- classes/db_sql.php | 37 ------ classes/db_util.php | 71 ++++++++++ classes/settings.php | 2 + drivers/firebird/firebird_driver.php | 14 +- drivers/firebird/firebird_sql.php | 157 +--------------------- drivers/firebird/firebird_util.php | 180 +++++++++++++++++++++++++ drivers/mysql/mysql_driver.php | 3 - drivers/mysql/mysql_sql.php | 111 +--------------- drivers/mysql/mysql_util.php | 132 +++++++++++++++++++ drivers/odbc/odbc_driver.php | 3 - drivers/odbc/odbc_sql.php | 47 ------- drivers/odbc/odbc_util.php | 72 ++++++++++ drivers/pgsql/pgsql_driver.php | 4 - drivers/pgsql/pgsql_sql.php | 98 -------------- drivers/pgsql/pgsql_util.php | 121 +++++++++++++++++ drivers/sqlite/sqlite_driver.php | 3 - drivers/sqlite/sqlite_sql.php | 165 +---------------------- drivers/sqlite/sqlite_util.php | 188 +++++++++++++++++++++++++++ tests/databases/mysql/mysql.php | 4 +- tests/databases/pgsql/pgsql.php | 4 +- tests/databases/sqlite/sqlite.php | 4 +- tests/db_files/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 23 files changed, 802 insertions(+), 634 deletions(-) create mode 100644 classes/db_util.php create mode 100644 drivers/firebird/firebird_util.php create mode 100644 drivers/mysql/mysql_util.php create mode 100644 drivers/odbc/odbc_util.php create mode 100644 drivers/pgsql/pgsql_util.php create mode 100644 drivers/sqlite/sqlite_util.php diff --git a/classes/db_pdo.php b/classes/db_pdo.php index 90f8560..3c4f240 100644 --- a/classes/db_pdo.php +++ b/classes/db_pdo.php @@ -21,9 +21,15 @@ */ abstract class DB_PDO extends PDO { - public $manip; + // Reference to last query protected $statement; + + // Character to escape identifiers protected $escape_char = '"'; + + // References to sub-classes + public $sql, + $util; /** * PDO constructor wrapper @@ -36,6 +42,14 @@ abstract class DB_PDO extends PDO { public function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array()) { parent::__construct($dsn, $username, $password, $driver_options); + + // Load the sql class for the driver + $class = get_class($this)."_sql"; + $this->sql = new $class(); + + // Load the util class for the driver + $class = get_class($this)."_util"; + $this->util = new $class($this); $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } diff --git a/classes/db_sql.php b/classes/db_sql.php index 30ea1e8..ec4f551 100644 --- a/classes/db_sql.php +++ b/classes/db_sql.php @@ -83,27 +83,6 @@ abstract class DB_SQL { // ! Abstract Methods // -------------------------------------------------------------------------- - /** - * Get database-specific sql to create a new table - * - * @abstract - * @param string $name - * @param array $columns - * @param array $constraints - * @param array $indexes - * @return string - */ - abstract public function create_table($name, $columns, array $constraints=array(), array $indexes=array()); - - /** - * Get database-specific sql to drop a table - * - * @abstract - * @param string $name - * @return string - */ - abstract public function delete_table($name); - /** * Get database specific sql for limit clause * @@ -122,22 +101,6 @@ abstract class DB_SQL { * @return string */ abstract public function random(); - - /** - * Return an SQL file with the database table structure - * - * @abstract - * @return string - */ - abstract public function backup_structure(); - - /** - * Return an SQL file with the database data as insert statements - * - * @abstract - * @return string - */ - abstract public function backup_data(); /** * Returns sql to list other databases diff --git a/classes/db_util.php b/classes/db_util.php new file mode 100644 index 0000000..d6503a4 --- /dev/null +++ b/classes/db_util.php @@ -0,0 +1,71 @@ +conn =& $conn; + } + + // -------------------------------------------------------------------------- + // ! Abstract Methods + // -------------------------------------------------------------------------- + + /** + * Get database-specific sql to create a new table + * + * @abstract + * @param string $name + * @param array $columns + * @param array $constraints + * @param array $indexes + * @return string + */ + abstract public function create_table($name, $columns, array $constraints=array(), array $indexes=array()); + + /** + * Get database-specific sql to drop a table + * + * @abstract + * @param string $name + * @return string + */ + abstract public function delete_table($name); + + /** + * Return an SQL file with the database table structure + * + * @abstract + * @return string + */ + abstract public function backup_structure(); + + /** + * Return an SQL file with the database data as insert statements + * + * @abstract + * @return string + */ + abstract public function backup_data(); + +} \ No newline at end of file diff --git a/classes/settings.php b/classes/settings.php index f505049..54a15b9 100644 --- a/classes/settings.php +++ b/classes/settings.php @@ -39,6 +39,8 @@ class Settings { */ private function __construct() { + // For testing and use outside of OpenSQLManager, + // define a different SETTINGS_DIR if ( ! defined('SETTINGS_DIR')) { define('SETTINGS_DIR', '.'); diff --git a/drivers/firebird/firebird_driver.php b/drivers/firebird/firebird_driver.php index 9cc070a..2a68a35 100644 --- a/drivers/firebird/firebird_driver.php +++ b/drivers/firebird/firebird_driver.php @@ -43,9 +43,19 @@ class Firebird extends DB_PDO { throw new PDOException(fbird_errmsg()); die(); } - + + // Load these classes here because this + // driver does not call the constructor + // of DB_PDO, which defines these two + // class variables for the other drivers + + // Load the sql class $class = __CLASS__."_sql"; - $this->sql = new $class; + $this->sql = new $class(); + + // Load the util class + $class = __CLASS__."_util"; + $this->util = new $class($this); } // -------------------------------------------------------------------------- diff --git a/drivers/firebird/firebird_sql.php b/drivers/firebird/firebird_sql.php index 66491a1..903bc6e 100644 --- a/drivers/firebird/firebird_sql.php +++ b/drivers/firebird/firebird_sql.php @@ -17,79 +17,6 @@ */ class Firebird_SQL extends DB_SQL { - /** - * Convienience public function to generate sql for creating a db table - * - * @param string $name - * @param array $fields - * @param array $constraints=array() - * @param array $indexes=array() - * - * @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 - * - * @param string $name - * @return string - */ - public function delete_table($name) - { - return 'DROP TABLE "'.$name.'"'; - } - - // -------------------------------------------------------------------------- - /** * Limit clause * @@ -127,89 +54,7 @@ class Firebird_SQL extends DB_SQL { return FALSE; } - // -------------------------------------------------------------------------- - - /** - * Create an SQL backup file for the current database's structure - * - * @return string - */ - public function backup_structure() - { - // @todo Implement Backup structure function - return ''; - } - - // -------------------------------------------------------------------------- - - /** - * Create an SQL backup file for the current database's data - * - * @param array $exclude - * @param bool $system_tables - * @return string - */ - public function backup_data($exclude=array(), $system_tables=FALSE) - { - // Determine which tables to use - if($system_tables == TRUE) - { - $tables = array_merge($this->get_system_tables(), $this->get_tables()); - } - else - { - $tables = $this->get_tables(); - } - - // Filter out the tables you don't want - if( ! empty($exclude)) - { - $tables = array_diff($tables, $exclude); - } - - $output_sql = ''; - - // Get the data for each object - foreach($tables as $t) - { - $sql = 'SELECT * FROM "'.trim($t).'"'; - $res = $this->query($sql); - $obj_res = $this->fetchAll(PDO::FETCH_ASSOC); - - unset($res); - - // Nab the column names by getting the keys of the first row - $columns = @array_keys($obj_res[0]); - - $insert_rows = array(); - - // Create the insert statements - foreach($obj_res as $row) - { - $row = array_values($row); - - // Quote values as needed by type - if(stripos($t, 'RDB$') === FALSE) - { - $row = array_map(array(&$this, 'quote'), $row); - $row = array_map('trim', $row); - } - - $row_string = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; - - unset($row); - - $insert_rows[] = $row_string; - } - - unset($obj_res); - - $output_sql .= "\n\nSET TRANSACTION;\n".implode("\n", $insert_rows)."\nCOMMIT;"; - } - - return $output_sql; - } - + // -------------------------------------------------------------------------- /** diff --git a/drivers/firebird/firebird_util.php b/drivers/firebird/firebird_util.php new file mode 100644 index 0000000..804e749 --- /dev/null +++ b/drivers/firebird/firebird_util.php @@ -0,0 +1,180 @@ + ..., + // '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 + * + * @param string $name + * @return string + */ + public function delete_table($name) + { + return 'DROP TABLE "'.$name.'"'; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // @todo Implement Backup structure function + return ''; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @param array $exclude + * @param bool $system_tables + * @return string + */ + public function backup_data($exclude=array(), $system_tables=FALSE) + { + // Determine which tables to use + if($system_tables == TRUE) + { + $tables = array_merge($this->get_system_tables(), $this->get_tables()); + } + else + { + $tables = $this->get_tables(); + } + + // Filter out the tables you don't want + if( ! empty($exclude)) + { + $tables = array_diff($tables, $exclude); + } + + $output_sql = ''; + + // Get the data for each object + foreach($tables as $t) + { + $sql = 'SELECT * FROM "'.trim($t).'"'; + $res = $this->query($sql); + $obj_res = $this->fetchAll(PDO::FETCH_ASSOC); + + unset($res); + + // Nab the column names by getting the keys of the first row + $columns = @array_keys($obj_res[0]); + + $insert_rows = array(); + + // Create the insert statements + foreach($obj_res as $row) + { + $row = array_values($row); + + // Quote values as needed by type + if(stripos($t, 'RDB$') === FALSE) + { + $row = array_map(array(&$this, 'quote'), $row); + $row = array_map('trim', $row); + } + + $row_string = 'INSERT INTO "'.trim($t).'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; + + unset($row); + + $insert_rows[] = $row_string; + } + + unset($obj_res); + + $output_sql .= "\n\nSET TRANSACTION;\n".implode("\n", $insert_rows)."\nCOMMIT;"; + } + + return $output_sql; + } + +} +// End of firebird_util.php \ No newline at end of file diff --git a/drivers/mysql/mysql_driver.php b/drivers/mysql/mysql_driver.php index 5df149f..0e90aee 100644 --- a/drivers/mysql/mysql_driver.php +++ b/drivers/mysql/mysql_driver.php @@ -37,9 +37,6 @@ class MySQL extends DB_PDO { )); parent::__construct("mysql:$dsn", $username, $password, $options); - - $class = __CLASS__.'_sql'; - $this->sql = new $class; } // -------------------------------------------------------------------------- diff --git a/drivers/mysql/mysql_sql.php b/drivers/mysql/mysql_sql.php index cd7baa4..396c0fb 100644 --- a/drivers/mysql/mysql_sql.php +++ b/drivers/mysql/mysql_sql.php @@ -15,90 +15,7 @@ /** * MySQL specifc SQL */ -class MySQL_SQL extends DB_SQL{ - - /** - * Convienience public function for creating a new MySQL table - * - * @param string $name - * @param array $columns - * @param array $constraints=array() - * @param array $indexes=array() - * - * @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} ({$col})"; - } - } - - // Join column definitons together - $columns = array(); - foreach($column_array as $n => $props) - { - $n = trim($n, '`'); - - $str = "`{$n}` "; - $str .= (isset($props['type'])) ? "{$props['type']} " : ""; - - $columns[] = $str; - } - - // Add constraints - foreach($column_array as $n => $props) - { - if (isset($props['constraint'])) - { - $columns[] = $props['constraint']; - } - } - - // Generate the sql for the creation of the table - $sql = "CREATE TABLE IF NOT EXISTS `{$name}` ("; - $sql .= implode(", ", $columns); - $sql .= ")"; - - return $sql; - } - - // -------------------------------------------------------------------------- - - /** - * Convience public function for droping a MySQL table - * - * @param string $name - * @return string - */ - public function delete_table($name) - { - return "DROP TABLE `{$name}`"; - } - - // -------------------------------------------------------------------------- +class MySQL_SQL extends DB_SQL { /** * Limit clause @@ -132,32 +49,6 @@ class MySQL_SQL extends DB_SQL{ // -------------------------------------------------------------------------- - /** - * Create an SQL backup file for the current database's structure - * - * @return string - */ - public function backup_structure() - { - // @todo Implement Backup function - return ''; - } - - // -------------------------------------------------------------------------- - - /** - * Create an SQL backup file for the current database's data - * - * @return string - */ - public function backup_data() - { - // @todo Implement Backup function - return ''; - } - - // -------------------------------------------------------------------------- - /** * Returns sql to list other databases * diff --git a/drivers/mysql/mysql_util.php b/drivers/mysql/mysql_util.php new file mode 100644 index 0000000..283ae8c --- /dev/null +++ b/drivers/mysql/mysql_util.php @@ -0,0 +1,132 @@ + ..., + // '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} ({$col})"; + } + } + + // Join column definitons together + $columns = array(); + foreach($column_array as $n => $props) + { + $n = trim($n, '`'); + + $str = "`{$n}` "; + $str .= (isset($props['type'])) ? "{$props['type']} " : ""; + + $columns[] = $str; + } + + // Add constraints + foreach($column_array as $n => $props) + { + if (isset($props['constraint'])) + { + $columns[] = $props['constraint']; + } + } + + // Generate the sql for the creation of the table + $sql = "CREATE TABLE IF NOT EXISTS `{$name}` ("; + $sql .= implode(", ", $columns); + $sql .= ")"; + + return $sql; + } + + // -------------------------------------------------------------------------- + + /** + * Convience public function for droping a MySQL table + * + * @param string $name + * @return string + */ + public function delete_table($name) + { + return "DROP TABLE `{$name}`"; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // @todo Implement Backup function + return ''; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @return string + */ + public function backup_data() + { + // @todo Implement Backup function + return ''; + } +} +// End of mysql_util.php \ No newline at end of file diff --git a/drivers/odbc/odbc_driver.php b/drivers/odbc/odbc_driver.php index 0c4d689..aab1d1a 100644 --- a/drivers/odbc/odbc_driver.php +++ b/drivers/odbc/odbc_driver.php @@ -35,9 +35,6 @@ class ODBC extends DB_PDO { public function __construct($dsn, $username=null, $password=null, $options=array()) { parent::__construct("odbc:$dsn", $username, $password, $options); - - $class = __CLASS__.'_sql'; - $this->sql = new $class; } // -------------------------------------------------------------------------- diff --git a/drivers/odbc/odbc_sql.php b/drivers/odbc/odbc_sql.php index 3713472..66b27aa 100644 --- a/drivers/odbc/odbc_sql.php +++ b/drivers/odbc/odbc_sql.php @@ -17,27 +17,6 @@ */ class ODBC_SQL extends DB_SQL { - public function create_table($name, $columns, array $constraints=array(), array $indexes=array()) - { - //ODBC can't know how to create a table - return FALSE; - } - - // -------------------------------------------------------------------------- - - /** - * Remove a table from the database - * - * @param string $name - * @return string - */ - public function delete_table($name) - { - return "DROP TABLE {$name}"; - } - - // -------------------------------------------------------------------------- - /** * Limit clause * @@ -65,32 +44,6 @@ class ODBC_SQL extends DB_SQL { // -------------------------------------------------------------------------- - /** - * Create an SQL backup file for the current database's structure - * - * @return string - */ - public function backup_structure() - { - // Not applicable to ODBC - return ''; - } - - // -------------------------------------------------------------------------- - - /** - * Create an SQL backup file for the current database's data - * - * @return string - */ - public function backup_data() - { - // Not applicable to ODBC - return ''; - } - - // -------------------------------------------------------------------------- - /** * Returns sql to list other databases * diff --git a/drivers/odbc/odbc_util.php b/drivers/odbc/odbc_util.php new file mode 100644 index 0000000..cb38a88 --- /dev/null +++ b/drivers/odbc/odbc_util.php @@ -0,0 +1,72 @@ +sql = new $class; } // -------------------------------------------------------------------------- diff --git a/drivers/pgsql/pgsql_sql.php b/drivers/pgsql/pgsql_sql.php index b7254ec..24d820d 100644 --- a/drivers/pgsql/pgsql_sql.php +++ b/drivers/pgsql/pgsql_sql.php @@ -17,78 +17,6 @@ */ class pgSQL_SQL extends DB_SQL { - /** - * 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 - * - * @param string $name - * @return string - */ - public function delete_table($name) - { - return 'DROP TABLE "'.$name.'"'; - } - - // -------------------------------------------------------------------------- - /** * Limit clause * @@ -123,32 +51,6 @@ class pgSQL_SQL extends DB_SQL { // -------------------------------------------------------------------------- - /** - * Create an SQL backup file for the current database's structure - * - * @return string - */ - public function backup_structure() - { - // @todo Implement Backup function - return ''; - } - - // -------------------------------------------------------------------------- - - /** - * Create an SQL backup file for the current database's data - * - * @return string - */ - public function backup_data() - { - // @todo Implement Backup function - return ''; - } - - // -------------------------------------------------------------------------- - /** * Returns sql to list other databases * diff --git a/drivers/pgsql/pgsql_util.php b/drivers/pgsql/pgsql_util.php new file mode 100644 index 0000000..6e35960 --- /dev/null +++ b/drivers/pgsql/pgsql_util.php @@ -0,0 +1,121 @@ + ..., + // '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 + * + * @param string $name + * @return string + */ + public function delete_table($name) + { + return 'DROP TABLE "'.$name.'"'; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // @todo Implement Backup function + return ''; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @return string + */ + public function backup_data() + { + // @todo Implement Backup function + return ''; + } +} +// End of pgsql_util.php \ No newline at end of file diff --git a/drivers/sqlite/sqlite_driver.php b/drivers/sqlite/sqlite_driver.php index 4364b63..e67f0f5 100644 --- a/drivers/sqlite/sqlite_driver.php +++ b/drivers/sqlite/sqlite_driver.php @@ -30,9 +30,6 @@ class SQLite extends DB_PDO { { // DSN is simply `sqlite:/path/to/db` parent::__construct("sqlite:{$dsn}", $user, $pass); - - $class = __CLASS__."_sql"; - $this->sql = new $class; } // -------------------------------------------------------------------------- diff --git a/drivers/sqlite/sqlite_sql.php b/drivers/sqlite/sqlite_sql.php index 022ea45..429f701 100644 --- a/drivers/sqlite/sqlite_sql.php +++ b/drivers/sqlite/sqlite_sql.php @@ -17,78 +17,6 @@ */ class SQLite_SQL extends DB_SQL { - /** - * Convenience public function to create a new table - * - * @param string $name //Name of the table - * @param array $columns //columns as straight array and/or column => type pairs - * @param array $constraints // column => constraint pairs - * @param array $indexes // column => index pairs - * @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 IF NOT EXISTS \"{$name}\" ("; - $sql .= implode(", ", $columns); - $sql .= ")"; - - return $sql; - } - - // -------------------------------------------------------------------------- - - /** - * SQL to drop the specified table - * - * @param string $name - * @return string - */ - public function delete_table($name) - { - return 'DROP TABLE IF EXISTS "'.$name.'"'; - } - - // -------------------------------------------------------------------------- - /** * Limit clause * @@ -118,98 +46,7 @@ class SQLite_SQL extends DB_SQL { { return ' RANDOM()'; } - - // -------------------------------------------------------------------------- - - /** - * Create an SQL backup file for the current database's data - * - * @param array $excluded - * @return string - */ - public function backup_data($excluded=array()) - { - // Get a list of all the objects - $sql = 'SELECT "name" FROM "sqlite_master"'; - - if( ! empty($excluded)) - { - $sql .= ' WHERE NOT IN("'.implode('","', $excluded).'")'; - } - - $res = $this->query($sql); - $result = $res->fetchAll(PDO::FETCH_ASSOC); - - unset($res); - - $output_sql = ''; - - // Get the data for each object - foreach($result as $r) - { - $sql = 'SELECT * FROM "'.$r['name'].'"'; - $res = $this->query($sql); - $obj_res = $res->fetchAll(PDO::FETCH_ASSOC); - - unset($res); - - // Nab the column names by getting the keys of the first row - $columns = array_keys($obj_res[0]); - - $insert_rows = array(); - - // Create the insert statements - foreach($obj_res as $row) - { - $row = array_values($row); - - // Quote values as needed by type - for($i=0, $icount=count($row); $i<$icount; $i++) - { - $row[$i] = (is_numeric($row[$i])) ? $row[$i] : $this->quote($row[$i]); - } - - $row_string = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; - - unset($row); - - $insert_rows[] = $row_string; - } - - unset($obj_res); - - $output_sql .= "\n\n".implode("\n", $insert_rows); - } - - return $output_sql; - } - - // -------------------------------------------------------------------------- - - /** - * Create an SQL backup file for the current database's structure - * - * @return string - */ - public function backup_structure() - { - // Fairly easy for SQLite...just query the master table - $sql = 'SELECT "sql" FROM "sqlite_master"'; - $res = $this->query($sql); - $result = $res->fetchAll(PDO::FETCH_ASSOC); - - $sql_array = array(); - - foreach($result as $r) - { - $sql_array[] = $r['sql']; - } - - $sql_structure = implode("\n\n", $sql_array); - - return $sql_structure; - } - + // -------------------------------------------------------------------------- /** diff --git a/drivers/sqlite/sqlite_util.php b/drivers/sqlite/sqlite_util.php new file mode 100644 index 0000000..07a5d80 --- /dev/null +++ b/drivers/sqlite/sqlite_util.php @@ -0,0 +1,188 @@ + type pairs + * @param array $constraints // column => constraint pairs + * @param array $indexes // column => index pairs + * @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 IF NOT EXISTS \"{$name}\" ("; + $sql .= implode(", ", $columns); + $sql .= ")"; + + return $sql; + } + + // -------------------------------------------------------------------------- + + /** + * SQL to drop the specified table + * + * @param string $name + * @return string + */ + public function delete_table($name) + { + return 'DROP TABLE IF EXISTS "'.$name.'"'; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's data + * + * @param array $excluded + * @return string + */ + public function backup_data($excluded=array()) + { + // Get a list of all the objects + $sql = 'SELECT "name" FROM "sqlite_master"'; + + if( ! empty($excluded)) + { + $sql .= ' WHERE NOT IN("'.implode('","', $excluded).'")'; + } + + $res = $this->query($sql); + $result = $res->fetchAll(PDO::FETCH_ASSOC); + + unset($res); + + $output_sql = ''; + + // Get the data for each object + foreach($result as $r) + { + $sql = 'SELECT * FROM "'.$r['name'].'"'; + $res = $this->query($sql); + $obj_res = $res->fetchAll(PDO::FETCH_ASSOC); + + unset($res); + + // Nab the column names by getting the keys of the first row + $columns = array_keys($obj_res[0]); + + $insert_rows = array(); + + // Create the insert statements + foreach($obj_res as $row) + { + $row = array_values($row); + + // Quote values as needed by type + for($i=0, $icount=count($row); $i<$icount; $i++) + { + $row[$i] = (is_numeric($row[$i])) ? $row[$i] : $this->quote($row[$i]); + } + + $row_string = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');'; + + unset($row); + + $insert_rows[] = $row_string; + } + + unset($obj_res); + + $output_sql .= "\n\n".implode("\n", $insert_rows); + } + + return $output_sql; + } + + // -------------------------------------------------------------------------- + + /** + * Create an SQL backup file for the current database's structure + * + * @return string + */ + public function backup_structure() + { + // Fairly easy for SQLite...just query the master table + $sql = 'SELECT "sql" FROM "sqlite_master"'; + $res = $this->query($sql); + $result = $res->fetchAll(PDO::FETCH_ASSOC); + + $sql_array = array(); + + foreach($result as $r) + { + $sql_array[] = $r['sql']; + } + + $sql_structure = implode("\n\n", $sql_array); + + return $sql_structure; + } +} +// End of sqlite_util.php \ No newline at end of file diff --git a/tests/databases/mysql/mysql.php b/tests/databases/mysql/mysql.php index e77e591..77ab7b2 100644 --- a/tests/databases/mysql/mysql.php +++ b/tests/databases/mysql/mysql.php @@ -52,7 +52,7 @@ class MySQLTest extends DBTest { if (empty($this->db)) return; //Attempt to create the table - $sql = $this->db->sql->create_table('create_test', + $sql = $this->db->util->create_table('create_test', array( 'id' => 'int(10)', 'key' => 'TEXT', @@ -66,7 +66,7 @@ class MySQLTest extends DBTest { $this->db->query($sql); //Attempt to create the table - $sql = $this->db->sql->create_table('create_join', + $sql = $this->db->util->create_table('create_join', array( 'id' => 'int(10)', 'key' => 'TEXT', diff --git a/tests/databases/pgsql/pgsql.php b/tests/databases/pgsql/pgsql.php index f7005f4..5cd529b 100644 --- a/tests/databases/pgsql/pgsql.php +++ b/tests/databases/pgsql/pgsql.php @@ -64,7 +64,7 @@ class PgTest extends DBTest { //Attempt to create the table - $sql = $this->db->sql->create_table('create_test', + $sql = $this->db->util->create_table('create_test', array( 'id' => 'integer', 'key' => 'TEXT', @@ -78,7 +78,7 @@ class PgTest extends DBTest { $this->db->query($sql); //Attempt to create the table - $sql = $this->db->sql->create_table('create_join', + $sql = $this->db->util->create_table('create_join', array( 'id' => 'integer', 'key' => 'TEXT', diff --git a/tests/databases/sqlite/sqlite.php b/tests/databases/sqlite/sqlite.php index e2ffdfa..78e766c 100644 --- a/tests/databases/sqlite/sqlite.php +++ b/tests/databases/sqlite/sqlite.php @@ -62,7 +62,7 @@ class SQLiteTest extends UnitTestCase { function TestCreateTable() { //Attempt to create the table - $sql = $this->db->sql->create_table('create_test', + $sql = $this->db->util->create_table('create_test', array( 'id' => 'INTEGER', 'key' => 'TEXT', @@ -75,7 +75,7 @@ class SQLiteTest extends UnitTestCase { $this->db->query($sql); //Attempt to create the table - $sql = $this->db->sql->create_table('create_join', + $sql = $this->db->util->create_table('create_join', array( 'id' => 'INTEGER', 'key' => 'TEXT', diff --git a/tests/db_files/FB_TEST_DB.FDB b/tests/db_files/FB_TEST_DB.FDB index bf5a51239670427d41dc9a592a0b5b0e74920807..a7da6afd7787ab75bf5870d68d651a93c78f2c15 100644 GIT binary patch delta 689 zcmZ9IO-LI-6vy8r>wZ1N5iL{;CVqr?Oh6D_>?&Ic8fZ(A;z^|7&0~A>un5jgp&$4t zUOafot<=_BN>BDy@h%7^YEz0ydeu|kx)};P!@mFiAM<~|*$u;D7#5!jm@2%>!v7^P zOm|$Qr3}C#KL9DQcLuODd79sP7$1<3;=Our1lLmLaCr4|&!$vTD49|+t)!u3my)KE zmXfxT&XIHtDazHZtb1;~pOr?no_7arRdA0AXna~3vEtdfz7&0MZ05jra+f# z{dN|Pohid0L zfEeP7)7Kh46D4}kz<}lB1(sPVEtUl;)-agU5e~$W&WLovr#e1JAc7bX(2w7Y2xy_s zvjf_0W1ha%aZJopt8)S+HK>b=(=lk*TCuOdK*7H{|9;aip~H4 delta 693 zcmZ9IK}Z`x6o%hyRul8s?5GsdF1ktAW#cV51yc}gpio*=L_A6mym_?HOUqJZPI?Fk z@6cOMEqD-ZlS}EXN2zCNAsekNqVeLTx4yU;l%3_xzyC1*`*;(El`yQ#3z#XtEx_H9 z7^ViUuT>YI#sy%A_a^|$)gQr|e0)KI!C&Ln2;QsiVX<*wzmrM|B@HE0N}5UzDLJfU zTFH!(BQ2RVrFhaFDrA49+vh>{QKs22RXN)#qq)C2mX2HNeMOAJmgU1w`!RgBeb}~3 zuwfVAC0B{h^0oyxIS-DxKITfW#k!fZV3DdZjS|s zNq~U9M7SUVdhiG9EdNgN1cx2-c0n>95=ynV~$I&E|}xSL%H{cDpK=|IXb5~ z+G~#X_JHdoI8MxQ$iHNcL!W;0JWkAcc+QVELE;MQMV-^{q`l@kw0?z0^r3^#=yeZk V)a&3=y6NF8)gr95H?DDa@jpZeiFyD4