-
Notifications
You must be signed in to change notification settings - Fork 2
/
DB.php
276 lines (179 loc) · 6.14 KB
/
DB.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
class Wave_DB {
private static $num_databases = 0;
private static $instances = array();
private static $default;
private $connection;
private $config;
const NS_SEPARATOR = '_';
public function __construct($config){
$this->connection = new Wave_DB_Connection($config);
$this->config = $config;
if(Wave_Core::$_MODE == Wave_Core::MODE_DEVELOPMENT)
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function from($table, $fields = null){
return Wave_DB_Query::create($this)->from($table, $fields);
}
public static function save($object){
if($object->_isLoaded()){
if($object->_isDirty()){
return Wave_DB::update($object);
}
} else {
return Wave_DB::insert($object);
}
}
//CRUDE IMPLEMENTATION TO BE REPLACED vvvvvvvv (euphemism?)
public static function insert($object){
$schema = $object::_getSchemaName();
$db = self::get($schema)->getConnection();
$table = $object::_getTableName();
$data = $object->_getDataArray();
$values = '';
foreach($data as $key => $value){
if($value === null)
$values .= 'NULL,';
elseif($value instanceof DateTime)
$values .= '"'.$value->format('Y-m-d H:i:s').'",';
else
$values .= '"'.addslashes($value).'",';
}
$fields = '`'.implode('`,`', array_keys($data)).'`';
$values = trim($values,',');
$sql = "INSERT INTO `$table` ($fields) VALUES ($values)";
// WHAT THE HELL, SOMEONE PUT A GROSS ON HERE
$db->exec($sql);
$liid = $db->lastInsertId();
if($liid !== 0){
$keys = $object::_getKeys(Wave_DB_Column::INDEX_PRIMARY);
if(count($keys) === 1){
$object->{$keys[0]} = $liid;
$object->_setLoaded();
}
}
return true;
}
public static function update($object){
$schema = $object::_getSchemaName();
$db = self::get($schema)->getConnection();
$table = $object::_getTableName();
$data = $object->_getDataArray();
$dirty = $object->_getDirtyArray();
$keys = $object::_getKeys(Wave_DB_Column::INDEX_PRIMARY);
$sql = "UPDATE $table SET ";
foreach($dirty as $key => $value){
if(!isset($data[$key])) continue;
if($data[$key] === null)
$sql .= "`$key` = NULL,";
elseif($data[$key] instanceof DateTime)
$sql .= "`$key` = '".$data[$key]->format('Y-m-d H:i:s')."',";
elseif(!is_object($data[$key]) && !is_array($data[$key]))
$sql .= "`$key` = '".addslashes($data[$key])."',";
}
//remove comma
$sql = substr($sql, 0, strlen($sql)-1);
$sql .= " WHERE ";
foreach($keys as $key){
$sql .= "`$key` = '{$object->$key}',";
}
//remove comma
$sql = substr($sql, 0, strlen($sql)-1);
return $db->exec($sql);
}
public function rawQuery($sql){
$db = $this->getConnection();
$db->exec($sql);
}
//FOR BOTH OF THESE ^^^^^^^^^^
/**
* Function to return the results of a basic query
*/
public function basicQuery($sql, $params = array()){
if(!is_array($params))
$params = array($params);
$statement = $this->connection->prepare($sql);
$statement->execute($params);
//$statement->debugDumpParams();
return $statement->fetchAll();
}
public function getConnection(){
return $this->connection;
}
public function getName(){
return $this->config->database;
}
public function getNamespace(){
return $this->config->namespace;
}
public function isDefault(){
return isset($database->default) && $database->default == true;
}
public static function init($config){
$databases = $config->databases;
$installed_drivers = Wave_DB_Connection::getAvailableDrivers();
foreach($databases as $database){
$driver_class = self::getDriverClass($database->driver);
//Check PDO driver is installed on system
if(!in_array($driver_class::getDriverName(), $installed_drivers))
throw new Wave_DB_Exception(sprintf('PDO::%s driver not installed for %s.', $driver_class::getDriverName(), $driver_class));
self::$instances[$database->namespace] = new self($database);
/*
* Define default database if it is either first or flagged as default.
* First db is always flagged as default to avoid the case where there is no default,
* (it'll be overwritten by any db with the default flag).
*/
if(self::$num_databases == 0 || isset($database->default) && $database->default == true)
self::$default = $database->namespace;
self::$num_databases++;
}
}
public static function get($database = null){
//if no db spec, return default
if($database === null)
$database = self::$default;
return isset(self::$instances[$database]) ? self::$instances[$database] : null;
}
public static function getNumDatabases(){
return self::$num_databases;
}
public static function getAllDatabases(){
return self::$instances;
}
public static function tableNameToClass($table_name){
$class_name = '';
$parts = explode('_', $table_name);
foreach($parts as $part)
$class_name .= ucfirst($part);
return $class_name;
}
public static function columnToRelationName($key, $target_table = ''){
$column_name = $key['table_name'] == $key['referenced_table_name'] ? $key['column_name'] : $key['table_name'];
$column_name = $target_table === '' ? $column_name : $target_table;
$relation_name = '';
$parts = explode('_', $column_name);
foreach($parts as $part)
$relation_name .= $part == 'id' ? '' : ucfirst($part);
return $relation_name;
}
public static function getDriverClass($driver){
return 'Wave_DB_Driver_'.$driver;
}
public static function getClassNameForTable($table, $database = null, $raw_table = false){
if($raw_table)
$table = self::tableNameToClass($table);
if(strpos($table, self::NS_SEPARATOR) !== false)
return $table;
if(is_null($database))
$database = self::get();
$class_name = $database->getNameSpace().self::NS_SEPARATOR.$table;
if(!class_exists($class_name))
throw new Wave_DB_Exception("Class does not exist: $table");
return $class_name;
}
public static function getFieldsForTable($table){
$class_name = self::getClassNameForTable($table);
$fields = $class_name::_getFields();
return $fields;
}
}