From bdc6417e64fc4a48c1364dc4773d68205f88b015 Mon Sep 17 00:00:00 2001 From: paulr Date: Tue, 19 Apr 2011 03:23:35 +1000 Subject: [PATCH] rudementry functioning db functions --- lib/config.php | 3 +- lib/plugins/db.php | 87 +++++++++++++++++++++++++++++++++++++++++++++++++- unittests/dbtest.php | 76 +++++++++++++++++++++++++++++++++++++++++++ var/config.php | 2 +- 4 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 unittests/dbtest.php diff --git a/lib/config.php b/lib/config.php index ad4a503..272e641 100644 --- a/lib/config.php +++ b/lib/config.php @@ -1,7 +1,8 @@ query($sql); +} + +function db_insertData($tablename) +{ + + $db = db_getDB(); + $sql = "insert into \"$tablename\" values (NULL"; + for($i=1; $i < func_num_args(); $i++) { + $sql .= ",'".func_get_arg($i)."'"; + } + $sql .=")"; + + $db->query($sql); } -function db_createTable($tablename) +function db_selectData($tablename, $column, $value) { + $db = db_getDB(); + + $sql = "select * from \"$tablename\" where column_$column like '%$value%'"; + $res = $db->query($sql); + $data = $res->fetchAll(); + return $data; +} + +function db_deleteData($tablename, $column, $value) +{ + $db = db_getDB(); + + $sql = "delete from \"$tablename\" where column_$column like '%$value%'"; + return $db->query($sql); + +} + +function db_deleteTable($tablename) +{ + $db = db_getDB(); + + $sql = "drop table $tablename"; + + return $db->query($sql); +} + +function db_tableExists($tablename) +{ + $db = db_getDB(); + + $sql = "select count(*) from sqlite_master where type='table' and name='$tablename'"; + $res = $db->query($sql); + + $value = $res->fetchColumn(); + + if($value == 1) return true; + else return false; } function db_getTables() { + $db = db_getDB(); + + $sql = "select name from sqlite_master where type='table' and name not like 'sqlite_%'"; + $res = $db->query($sql); + return $res->fetchAll(); } ?> \ No newline at end of file diff --git a/unittests/dbtest.php b/unittests/dbtest.php new file mode 100644 index 0000000..20491c9 --- /dev/null +++ b/unittests/dbtest.php @@ -0,0 +1,76 @@ + \ No newline at end of file diff --git a/var/config.php b/var/config.php index 52bceef..8e63d25 100644 --- a/var/config.php +++ b/var/config.php @@ -2,7 +2,7 @@ // statically set for now $GLOBAL_BASE_URL="/src/eclipse-workspace/glcas/www/"; -$DB_URL="sqlite:" +$DB_URL="sqlite:$BASE_DIR/var/ds_store.db"; global $GLOBAL_BASE_URL; -- 1.7.0.4