-
Notifications
You must be signed in to change notification settings - Fork 4
/
Database.php
217 lines (172 loc) · 5.24 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
class Database {
/*
* Static methods
*
*/
static $pool = array('r'=>null, 'w'=>null);
static $db;
static $table_prefix = "";
static function openDatabase($mode='r', $dsn=null, $username=null, $password=null){
// Is this a correct mode? (read/write)
if(!in_array($mode, array('r', 'w', 'rw', 'wr'))) return false;
// array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") - contains the command to PDO to use UNICODE MODE
try {
$tmp = new PDO($dsn, $username, $password);
$tmp->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Put the connection in its relevant pool
if($mode=='rw' || $mode='wr'){
self::$pool['r'] = self::$pool['w'] = $tmp;
} else {
self::$pool[$mode] = $tmp;
}
} catch(PDOException $e){
var_dump($e); exit;
$tmp = false;
}
return $tmp;
}
static function switchDatabase($mode){
self::$db = self::$pool[$mode];
}
static function query($sql, $data=array(),$type=array()){
$cmd = strtoupper(strtok($sql, ' '));
$updates = array('UPDATE', 'INSERT', 'CREATE', 'DELETE', 'DROP', 'GRANT');
if(in_array($cmd, $updates)){
self::switchDatabase('w');
$write = true;
} else {
self::switchDatabase('r');
$write = false;
}
// Are we connected to a database?
if(!self::$db) return false;
// Add the table prefixes, if any
$sql_parsed = str_replace('tbl:', self::$table_prefix, $sql);
#try {
$stmt = self::$db->prepare($sql_parsed);
//We must set each value as 'execute($data)' does not work
// this is since it always place 'quotes' around each value
// hence causes error in cases like "LIMIT ?" -> "LIMIT '1'" (which fails in mysql)
$i_data=0;
foreach($data as $key => $value){
if(!empty($type[$key])){
switch($type[$key]){
case 'INT':
$valueType=PDO::PARAM_INT;
break;
case 'BOOL':
$valueType=PDO::PARAM_BOOL;
break;
case 'LARGEOBJECT':
$valueType=PDO::PARAM_LOB;
break;
default:
case 'STR':
$valueType=PDO::PARAM_STR;
break;
}
}else{$valueType=PDO::PARAM_STR;}
$stmt->bindValue($i_data+1, $value, $valueType);
$i_data++;
}
$stmt->execute();
//var_dump($sql_parsed);
//$stmt->execute($data);
if($write){
$rs = true;
} else {
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
//$rs = $rs->fetchAll();
#} catch (Exception $e){
# return false;
#}
return $rs;
}
/*
* $table: Table name to insert into
* $data: Array of rows to be inserted.
* If inserted rows > 1, return true on success. insert_id if 1 inserted row.
* Returns false on error.
*/
static function insert($table, $data, $type=array() ){
// Make sure we actually have data to insert
if(!is_array($data)) return false;
if(count($data) == 0) return true;
// If only one row has been specified, make sure the data is set as a row in an array
if(!isset($data[0]) || !is_array($data[0])) $data = array($data);
self::switchDatabase('w');
// More than 1 insert? Start a transaction to speed it up
self::$db->beginTransaction();
// Build the statement
$table = str_replace('tbl:', self::$table_prefix, $table);
$sql = "INSERT INTO $table (`".join('`, `', array_keys($data[0]))."`) VALUES ";
$insert_row = array_fill(0, count($data[0]), '?');
$insert_row = '('.join(', ', $insert_row).')';
$insert_rows = array_fill(0, count($data), $insert_row);
$sql .= join(', ', $insert_rows);
//var_dump($sql);
//var_dump($data);
//var_dump($type);
try {
/*echo $sql;
echo "<pre>";
print_r($data[0]);
exit;*/
$stmt = self::$db->prepare($sql);
//We must set each value as 'execute($data)' does not work
// this is since it always place 'quotes' around each value
// hence causes error in cases like "LIMIT ?" -> "LIMIT '1'" (which fails in mysql)
$i_data=0;
foreach(array_values($data[0]) as $key => $value){
if(!empty($type[$key])){
switch($type[$key]){
case 'INT':
$valueType=PDO::PARAM_INT;
break;
case 'BOOL':
$valueType=PDO::PARAM_BOOL;
break;
case 'LARGEOBJECT':
$valueType=PDO::PARAM_LOB;
break;
default:
case 'STR':
$valueType=PDO::PARAM_STR;
break;
}
}else{$valueType=PDO::PARAM_STR;}
$stmt->bindValue($i_data+1, $value, $valueType);
$i_data++;
}
//echo "loop".$i_data."times";
$stmt->execute();
//$stmt->execute(array_values($data[0]));
$ret = self::$db->lastInsertId();
self::$db->commit();
} catch(PDOException $e){
echo $e."<br/>";
self::$db->rollBack();
$ret = false;
}
self::switchDatabase('r');
return $ret;
}
static function escape($input){
if(!self::$db) return $input;
$ret = self::$db->quote($input);
return $ret;
}
static function getDataBaseType(){
// makes sure $db is set to read
self::switchDatabase('r');
//check that $db actually exist
if(self::$db){
//If exist, then show the attribute
return self::$db->getAttribute(PDO::ATTR_DRIVER_NAME);
}else{
return false;
}
}
}