From 13ff38acb4cd1af5a69c5cbb3a3c7299328cd023 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 13 Feb 2012 13:10:48 -0500 Subject: [PATCH] DB class updates, changed some SQL statements to heredoc form. --- DEV_README.md | 1 + src/databases/pgsql.php | 18 +++++++++++---- src/databases/sqlite.php | 50 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/DEV_README.md b/DEV_README.md index 7f68a48..3b842cf 100644 --- a/DEV_README.md +++ b/DEV_README.md @@ -7,6 +7,7 @@ Follow the CodeIgniter [Style Guide](https://github.com/timw4mail/CodeIgniter/bl * Do not use `global`, `eval` * Do not use the error suppressor `@` * Add a docblock to every method +* Use [heredoc](http://us2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc) string syntax for SQL statements to minimize PHP escape characters ## PHP-Gtk Resources * [Reference](http://gtk.php.net/manual/en/reference.php) diff --git a/src/databases/pgsql.php b/src/databases/pgsql.php index b532736..b001079 100644 --- a/src/databases/pgsql.php +++ b/src/databases/pgsql.php @@ -54,7 +54,11 @@ class pgSQL extends DB_PDO { */ function get_dbs() { - $sql = "SELECT datname FROM pg_database WHERE datname NOT IN ('template0','template1') ORDER BY 1"; + $sql = <<< SQL + SELECT "datname" FROM "pg_database" + WHERE "datname" NOT IN ('template0','template1') + ORDER BY 1 +SQL; $res = $this->query($sql); $dbs = $res->fetchAll(PDO::FETCH_ASSOC); @@ -110,12 +114,16 @@ class pgSQL extends DB_PDO { { if($database === "") { - $sql = 'SELECT DISTINCT "schemaname" FROM pg_tables - WHERE "schemaname" NOT LIKE \'pg\_%\''; + $sql = <<< SQL + SELECT DISTINCT "schemaname" FROM "pg_tables" + WHERE "schemaname" NOT LIKE 'pg\_%' +SQL; } - $sql = 'SELECT "nspname" FROM pg_namespace - WHERE "nspname" NOT LIKE \'pg\_%\''; + $sql = <<< SQL + SELECT "nspname" FROM pg_namespace + WHERE "nspname" NOT LIKE 'pg\_%' +SQL; $res = $this->query($sql); $schemas = $res->fetchAll(PDO::FETCH_ASSOC); diff --git a/src/databases/sqlite.php b/src/databases/sqlite.php index 5842da8..9830e87 100644 --- a/src/databases/sqlite.php +++ b/src/databases/sqlite.php @@ -42,7 +42,8 @@ class SQLite extends DB_PDO { { // SQLite has a TRUNCATE optimization, // but no support for the actual command. - $sql = "DELETE FROM {$table}"; + $sql = <<query($sql); } @@ -54,7 +55,9 @@ class SQLite extends DB_PDO { function get_tables() { $tables = array(); - $res = $this->query("SELECT \"name\", \"sql\" FROM sqlite_master WHERE type='table'"); + $sql = <<query($sql); $result = $res->fetchAll(PDO::FETCH_ASSOC); foreach($result as $r) @@ -65,9 +68,50 @@ class SQLite extends DB_PDO { return $tables; } + /** + * List system tables for the current database + * + * @return array + */ function get_system_tables() { - + $sql= <<< SQL + SELECT "name", "type" + FROM sqlite_master + WHERE "type" IN ('table', 'view') + AND "name" NOT LIKE 'sqlite?_%' escape '?' +SQL; + + $res = $this->query($sql); + $result = $res->fetchAll(PDO::FETCH_ASSOC); + + return $result; + } + + /** + * Load a database for the current connection + * + * @param string $db + * @param string $name + */ + function load_database($db, $name) + { + $sql = <<< SQL + ATTACH DATABASE '{$db}' AS "{$name}" +SQL; + $this->query($sql); + } + + /** + * Unload a database from the current connection + * + * @param string $name + */ + function unload_database($name) + { + $sql = <<< SQL DETACH DATABASE "{$name}"" +SQL; + $this->query($sql); } /**