added numerous database components
authorpaulr <me@pjr.cc>
Tue, 1 Nov 2011 09:37:13 +0000 (20:37 +1100)
committerpaulr <me@pjr.cc>
Tue, 1 Nov 2011 09:37:13 +0000 (20:37 +1100)
added a unit test for database setup/control

db/design-sqlite.db [new file with mode: 0644]
gwvplib/gwvpdatabase.php
gwvplib/gwvplib.php
unittests/sqlitedb.php [new file with mode: 0644]

diff --git a/db/design-sqlite.db b/db/design-sqlite.db
new file mode 100644 (file)
index 0000000..8c354ed
Binary files /dev/null and b/db/design-sqlite.db differ
index 507fdfc..1f0282f 100644 (file)
@@ -9,9 +9,61 @@ function gwvp_dbCreateMysqlStructure()
        
 }
 
-function gwvp_dbCreateSQLiteStructure()
+function gwvp_dbCreateSQLiteStructure($dbloc)
 {
+       $usersql = '
+               CREATE TABLE "users" (
+           "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+           "user_full_name" TEXT,
+           "user_password" TEXT,
+           "user_nickname" TEXT,
+           "user_email" TEXT,
+           "user_desc" TEXT,
+           "user_status" INTEGER
+               )';
        
+       $groupsql = '
+               CREATE TABLE groups (
+           "groups_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+           "groups_name" TEXT,
+           "groups_is_admin" INTEGER,
+               "groups_owner_id" INTEGER
+               )';
+       
+       $reposql = '
+               CREATE TABLE "repos" (
+           "repos_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+           "repos_name" TEXT,
+           "repos_description" TEXT,
+           "repos_owner" INTEGER
+               )';
+       
+       $configsql = '
+               CREATE TABLE "config" (
+           "config_name" TEXT,
+           "config_value" TEXT
+               )';
+       
+       $groupmemsql = '
+               CREATE TABLE "group_membership" (
+           "groupmember_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+           "groupmember_groupid" INTEGER,
+           "groupmember_userid" INTEGER
+               )';
+       
+       try {
+               $DB_CONNECTION = new PDO("sqlite:$dbloc");
+       } catch(PDOException $exep) {
+               error_log("execpt on db open");
+               return false;
+       }
+       
+       
+       $DB_CONNECTION->query($usersql);
+       $DB_CONNECTION->query($groupsql);
+       $DB_CONNECTION->query($reposql);
+       $DB_CONNECTION->query($configsql);
+       $DB_CONNECTION->query($groupmemsql);
 }
 
 function gwvp_isDBSetup()
@@ -30,7 +82,7 @@ function gwvp_isDBSetup()
 
 function gwvp_ConnectDB()
 {
-       global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_url;
+       global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_url, $DB_CONNECTION;
        
        // first check if $DB_CONNECTION IS live
        if($DB_CONNECTION != false) return $DB_CONNECTION;
@@ -44,6 +96,145 @@ function gwvp_ConnectDB()
        }
 }
 
+function gwvp_createUser($email, $fullname, $password, $nick, $desc, $status)
+{
+       $conn = gwvp_ConnectDB();
+       
+       // TODO: change from sha1
+       $shapass = sha1($password);
+       $conn->query("insert into users values (null, '$fullname', '$shapass', '$nick', '$email', '$desc', '$status')");
+       /*
+        *          "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+           "user_full_name" TEXT,
+           "user_password" TEXT,
+           "user_nickname" TEXT,
+           "user_email" TEXT,
+           "user_desc" TEXT,
+           "user_status" INTEGER
+
+        */
+}
+
+function gwvp_getUsers()
+{
+       $conn = gwvp_ConnectDB();
+       
+       $res = $conn->query("select * from users");
+       
+       $returns = false;
+       $rn = 0;
+       foreach($res as $u_res) {
+               $returns[$rn]["id"] = $u_res["users_id"];
+               $returns[$rn]["fullname"] = $u_res["user_full_name"];
+               $returns[$rn]["password"] = $u_res["user_password"];
+               $returns[$rn]["nickname"] = $u_res["user_nickname"];
+               $returns[$rn]["email"] = $u_res["user_email"];
+               $returns[$rn]["desc"] = $u_res["user_desc"];
+               $returns[$rn]["status"] = $u_res["user_status"];
+               $rn++;
+       }
+       
+       return $returns;
+}
+
+function gwvp_deleteUser($email)
+{
+       $conn = gwvp_ConnectDB();
+       
+       $sql = "delete from users where user_email='$email'";
+       
+       $conn->query($sql);
+}
+
+function gwvp_createGroup($group_name, $is_admin, $owner_id)
+{
+       $conn = gwvp_ConnectDB();
+       
+       /*
+        *              CREATE TABLE groups (
+           "groups_id" INTEGER,
+           "groups_name" TEXT,
+           "groups_is_admin" INTEGER,
+               "groups_owner_id" INTEGER
+               )';
+
+        */
+       if($is_admin) {
+               $is_admin_t = 1;
+       } else {
+               $is_admin_t = 0;
+       }
+       $sql = "insert into groups values( null, '$group_name', '$is_admin_t', '$owner_id')";
+       
+       $conn->query($sql);
+       
+}
+
+function gwvp_deleteGroup($groupname)
+{
+       $conn = gwvp_ConnectDB();
+       
+       $sql = "delete from groups where groups_name='$groupname'";
+       
+       $conn->query($sql);
+}
+
+
+function gwvp_getGroups()
+{
+       $conn = gwvp_ConnectDB();
+       
+       $res = $conn->query("select * from groups");
+       
+       $returns = false;
+       $rn = 0;
+       foreach($res as $u_res) {
+               $returns[$rn]["id"] = $u_res["groups_id"];
+               $returns[$rn]["name"] = $u_res["groups_name"];
+               if($u_res["groups_is_admin"]=="1") $return[$rn]["admin"] = true;
+               else $return[$rn]["admin"] = false;
+               $returns[$rn]["admin"] = $u_res["groups_is_admin"];
+               $returns[$rn]["ownerid"] = $u_res["groups_owner_id"];
+               $rn++;
+       }
+       
+       return $returns;
+}
+
+function gwvp_getGroupId($groupname)
+{
+       $conn = gwvp_ConnectDB();
+       
+       $sql = "select groups_id from groups where groups_name='$groupname'";
+       
+       $res = $conn->query($sql);
+       $return = false;
+       foreach($res as $u_res) {
+               $return = $u_res["groups_id"];
+       }
+       
+       return $return;
+}
+
+function gwvp_getUserId($useremail)
+{
+       $conn = gwvp_ConnectDB();
+       
+       $sql = "select users_id from users where user_email='$useremail'";
+       
+       $res = $conn->query($sql);
+       $return = false;
+       foreach($res as $u_res) {
+               $return = $u_res["users_id"];
+       }
+       
+       return $return;
+}
+
+function gwvp_addGroupMember($email, $groupname)
+{
+       
+}
 /* functions we'll need to access data:
  * 
  * getUsers(pattern)
index afd06d8..e23ece7 100644 (file)
@@ -6,6 +6,7 @@ require_once("gwvpuseradmin.php");
 require_once("gwvprepoadmin.php");
 require_once("gwvpauth.php");
 require_once("gwvpsetup.php");
+require_once("gwvpdatabase.php");
 
 // only enable this if you need it:
 require_once("gwvpdebug.php");
diff --git a/unittests/sqlitedb.php b/unittests/sqlitedb.php
new file mode 100644 (file)
index 0000000..21de42a
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+$lib_base = "../gwvplib/";
+
+// initial http file
+$WEB_ROOT_FS = realpath(dirname(__FILE__));
+$BASE_URL = dirname($_SERVER["PHP_SELF"]);
+
+global $WEB_ROOT_FS, $BASE_URL, $repo_base, $data_directory, $db_type, $db_name, $db_username, $db_password;
+
+// add gwvplib as if it were a path in ../gwvplib
+if(file_exists($lib_base)) {
+       $path = realpath($lib_base);
+       set_include_path(get_include_path().PATH_SEPARATOR.$path);
+}
+
+require_once("gwvplib.php");
+global $db_type, $db_url;
+
+$db_type = "sqlite";
+$db_url = "/tmp/unittestdb.db";
+
+gwvp_dbCreateSQLiteStructure("$db_url");
+
+gwvp_ConnectDB();
+
+// function gwvp_createUser($email, $fullname, $password, $nick, $desc, $status)
+// test users creation
+gwvp_createUser("test@test.com", "test user", "password", "nick", "desc", "0");
+gwvp_createUser("test2@test.com", "test2 user", "password", "nick2", "desc2", "0");
+$users = gwvp_getUsers();
+echo "Users\n";
+echo "\tCreate test@test.com\n";
+if($users[0]["email"] != "test@test.com") {
+       echo "\t\tUser 0 isnt correct: ".$users[0]["email"]."\n";
+} else {
+       echo "\t\tUser 0 correct\n";
+}
+if($users[1]["email"] != "test2@test.com") {
+       echo "\t\tUser 1 isnt correct: ".$users[0]["email"]."\n";
+} else {
+       echo "\t\tUser 1 correct\n";
+}
+echo "\tCheck User It\n";
+$uid1 = gwvp_getUserId("test@test.com");
+$uid2 = gwvp_getUserId("test2@test.com");
+if($uid1 != 1) {
+       echo "\t\tUser 1 id not correct\n";
+} else {
+       echo "\t\tUser 1 id correct\n";
+}
+if($uid2 != 2) {
+       echo "\t\tUser 2 id not correct\n";
+} else {
+       echo "\t\tUser 2 id correct\n";
+}
+
+echo "\tDelete test@test.com\n";
+gwvp_deleteUser("test@test.com");
+$users = gwvp_getUsers();
+if($users[0]["email"] != "test2@test.com") {
+       echo "\t\tUser 0 isnt correct: ".$users[0]["email"]."\n";
+} else {
+       echo "\t\tUser 0 is correct\n";
+}
+
+// test group creation
+// function gwvp_createGroup($group_name, $is_admin, $owner_id)
+echo "\n\nGroups\n";
+echo "\tCreate testgroups\n";
+gwvp_createGroup("testgroup", true, "1");
+gwvp_createGroup("testgroup2", true, "1");
+$groups = gwvp_getGroups();
+if($groups[0]["name"] != "testgroup") {
+       echo "\t\tGroup 0 isnt correct: ".$groups[0]["name"]."\n";
+} else {
+       echo "\t\tGroup 0 correct\n";
+}
+if($groups[1]["name"] != "testgroup2") {
+       echo "\t\tGroup 1 isnt correct: ".$groups[1]["name"]."\n";
+} else {
+       echo "\t\tGroup 1 correct\n";
+}
+echo "\tCheck group id\n";
+$gid1 = gwvp_getGroupId("testgroup");
+$gid2 = gwvp_getGroupId("testgroup2");
+if($gid1 != 1) {
+       echo "\t\tGroup 1 ID not correct\n";
+} else {
+       echo "\t\tGroup 1 ID correct\n";
+}
+if($gid2 != 2) {
+       echo "\t\tGroup 2 ID not correct\n";
+} else {
+       echo "\t\tGroup 2 ID correct\n";
+}
+
+echo "\tNow delete group\n";
+gwvp_deleteGroup("testgroup");
+$groups = gwvp_getGroups();
+if($groups[0]["name"] != "testgroup2") {
+       echo "\t\tGroup 0 isnt correct: ".$groups[0]["name"]."\n";
+} else {
+       echo "\t\tGroup 0 correct\n";
+}
+
+unlink($db_url);
+?>
\ No newline at end of file