lots of changed, though it'll probably be all backed-out because
[configmanager.git] / lib / db.php
1 <?php
2
3 function db_getDB()
4 {
5         global $DB_URL, $IS_WEB_REQUEST;
6         
7         if(!isset($IS_WEB_REQUEST)) {
8                 // only the web requests are allowed to create dbs
9                 echo "no web request, no db\n";
10                 return false;
11         }
12         
13         
14         $dbobject = false;
15     global $BASE_DIR, $DB_HANDLE;
16     if($DB_HANDLE != false) return $DB_HANDLE;
17         try {
18                 $dbobject = new PDO("$DB_URL");
19         } catch(PDOException $exep) {
20                 error_log("execpt on db open");
21                 return false;
22         }
23         
24         $DB_HANDLE = $dbobject;
25
26         return $dbobject;
27 }
28
29 function db_createDB()
30 {
31         // theres not much to do yet with only sqlite support
32 }
33
34 // all columns end up as text
35 function db_createTable($tablename)
36 {
37         $db = db_getDB();
38         
39         if(!$db) return false;
40         
41         if(db_tableExists($tablename)) return true;
42         
43         $sql = "create table \"$tablename\" (\"".$tablename."_id\" INTEGER PRIMARY KEY AUTOINCREMENT";
44         for($i=1; $i < func_num_args(); $i++) {
45                 $colname = func_get_arg($i);
46                 $sql .= ", $colname text";
47         }
48         $sql .= ")";
49         
50         //echo "sql: $sql\n";
51         $db->query($sql);
52 }
53
54 function db_insertData($tablename)
55 {
56
57         $db = db_getDB();
58         
59         if(!$db) return false;
60         
61         $sql = "insert into \"$tablename\" values (NULL";
62         for($i=1; $i < func_num_args(); $i++) {
63                 $sql .= ",'".func_get_arg($i)."'";
64         }
65         $sql .=")";
66         
67         //echo "sql is $sql\n";
68         
69         $db->query($sql);
70 }
71
72 function db_getMaxValue($tablename, $column, $columnsel="", $wheresel="")
73 {
74         $db = db_getDB();
75         
76         if(!$db) return false;
77         
78         if($columnsel != "") {
79                 $extra = " where $columnsel = '$wheresel'";
80         }
81         
82         $sql = "select max($column) from \"$tablename\"$extra";
83         $res = $db->query($sql);
84         if(!$res) return false;
85         $data = $res->fetchAll();
86         
87         return $data;
88 }
89
90
91 function db_selectData($tablename, $column="", $value="", $orderby = "")
92 {
93         $db = db_getDB();
94         
95         if(!$db) return false;
96         
97         
98         if($column != "") $extra = " where $column like '%$value%'";
99         else $extra = "";
100         if($orderby != "") $extra .= " order by $orderby";
101         $sql = "select * from \"$tablename\"$extra";
102         $res = $db->query($sql);
103         if(!$res) return false;
104         $data = $res->fetchAll();
105         
106         return $data;
107 }
108
109 function db_deleteData($tablename, $column, $value)
110 {
111         $db = db_getDB();
112         
113         if(!$db) return false;
114         
115         $sql = "delete from \"$tablename\" where $column like '%$value%'";
116         //echo "Sql is $sql\n";
117         return $db->query($sql);
118         
119 }
120
121 function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
122 {
123         $db = db_getDB();
124         
125         if(!$db) return false;
126         
127         if($exact) $sql = "update \"$tablename\" set $column='$newdata' where $wherecol='$wheredata'";
128         else $sql = "update \"$tablename\" set $column='$newdata' where $wherecol like '$wheredata'";
129         return $db->query($sql);
130 }
131
132 function db_deleteTable($tablename)
133 {
134         $db = db_getDB();
135         
136         if(!$db) return false;
137         
138         $sql = "drop table $tablename";
139         
140         return $db->query($sql);
141 }
142
143 function db_tableExists($tablename)
144 {
145         $db = db_getDB();
146         
147         if(!$db) return false;
148         
149         $sql = "select count(*) from sqlite_master where type='table' and name='$tablename'";
150         $res = $db->query($sql);
151         
152         $value = $res->fetchColumn();
153         
154         if($value == 1) return true;
155         else return false;
156 }
157
158 function db_getTables()
159 {
160         $db = db_getDB();
161         
162         if(!$db) return false;
163         
164         $sql = "select name from sqlite_master where type='table' and name not like 'sqlite_%'";
165         $res = $db->query($sql);
166         
167         return $res->fetchAll();
168 }
169 ?>