From: paulr Date: Tue, 1 Nov 2011 09:37:13 +0000 (+1100) Subject: added numerous database components X-Git-Url: http://git.pjr.cc/?a=commitdiff_plain;h=d31646570a19d6fb66d2f648cd282e2b2e3e7033;p=gwvp.git added numerous database components added a unit test for database setup/control --- diff --git a/db/design-sqlite.db b/db/design-sqlite.db new file mode 100644 index 0000000..8c354ed Binary files /dev/null and b/db/design-sqlite.db differ diff --git a/gwvplib/gwvpdatabase.php b/gwvplib/gwvpdatabase.php index 507fdfc..1f0282f 100644 --- a/gwvplib/gwvpdatabase.php +++ b/gwvplib/gwvpdatabase.php @@ -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) diff --git a/gwvplib/gwvplib.php b/gwvplib/gwvplib.php index afd06d8..e23ece7 100644 --- a/gwvplib/gwvplib.php +++ b/gwvplib/gwvplib.php @@ -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 index 0000000..21de42a --- /dev/null +++ b/unittests/sqlitedb.php @@ -0,0 +1,107 @@ + \ No newline at end of file