From d31646570a19d6fb66d2f648cd282e2b2e3e7033 Mon Sep 17 00:00:00 2001 From: paulr Date: Tue, 1 Nov 2011 20:37:13 +1100 Subject: [PATCH] added numerous database components added a unit test for database setup/control --- db/design-sqlite.db | Bin 0 -> 8192 bytes gwvplib/gwvpdatabase.php | 195 +++++++++++++++++++++++++++++++++++++++++++++- gwvplib/gwvplib.php | 1 + unittests/sqlitedb.php | 107 +++++++++++++++++++++++++ 4 files changed, 301 insertions(+), 2 deletions(-) create mode 100644 db/design-sqlite.db create mode 100644 unittests/sqlitedb.php diff --git a/db/design-sqlite.db b/db/design-sqlite.db new file mode 100644 index 0000000000000000000000000000000000000000..8c354ed213b03850ee1fe1f0b8b1bc3fe69a4a7f GIT binary patch literal 8192 zcmeI1&2G~`5XZe~64OYCqt#wm`BW>3M8yL@s4NsU2_+bboGd5V6bpaVYm*ZPhu7fg zxbYUOck{8fDTs^Z!&tK8nc12B`!_qMe0V=`6M{Ek>^lj5sk9Y@l-C$5igM2GC+t34 zx!pP#>=#MzkG-BN!=IO(Jlf_<( z4j@oJ0hs^#nZu4ipeY1k{+rSX&;bPMCjj$bKXcd-2sDKN%zsll0Xl#{{RHy)?<&`d za*g`vTWiw#b@t=0DzYt7HBCd`r-`%lNJ>de`J;6+H%FF#y`)%tz3;NMLjJodk-P02$_f)y#5X9X6e z;8iYez%q?&(pI(M5bbVx&HFe^Bikqbk}>`4MrFHGsH&I4N7y_d%KR5{Igh_f{n~d$ zUXpiMTG2DI%Z{pDT%g^v)8|m0LAwR%!gn|u!jhHBo6&0j4++$E*1j8@S~S1{3bn&+ zKw`P1k-g~hbGv)R#l<8FDSxyJ(sb@SqU4rb=bz_RWK_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 -- 1.7.0.4