Newer
Older
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
277
278
279
280
281
<?php
// https://raw.githubusercontent.com/daveismyname/pdo-wrapper/master/database.php
require_once( __DIR__ .'/databaseTable.php' );
class DaveDatabase extends PDO
{
/**
* @var array Array of saved databases for reusing
*/
protected static $instances = array();
/**
* Static method get
*
* @param array $group
* @return \helpers\database
*/
public static function get($group = false)
{
// Determining if exists or it's not empty, then use default group defined in config
$group = !$group ? array (
'type' => DB_TYPE,
'host' => DB_HOST,
'name' => DB_NAME,
'user' => DB_USER,
'pass' => DB_PASS
) : $group;
// Group information
$type = $group['type'];
$host = $group['host'];
$name = $group['name'];
$user = $group['user'];
$pass = $group['pass'];
// ID for database based on the group information
$id = "$type.$host.$name.$user.$pass";
// Checking if the same
if (isset(self::$instances[$id])) {
return self::$instances[$id];
}
$instance = new Database("$type:host=$host;dbname=$name;charset=utf8", $user, $pass);
$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Setting Database into $instances to avoid duplication
self::$instances[$id] = $instance;
//return the pdo instance
return $instance;
}
/**
* run raw sql queries
* @param string $sql sql command
* @return none
*/
public function raw($sql)
{
$this->query($sql);
}
/**
* method for selecting records from a database
* @param string $sql sql query
* @param array $array named params
* @param object $fetchMode
* @param string $class class name
* @return array returns an array of records
*/
public function select($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $class = '')
{
// Append select if it isn't appended.
if (strtolower(substr($sql, 0, 7)) !== 'select ') {
$sql = "SELECT " . $sql;
}
$stmt = $this->prepare($sql);
foreach ($array as $key => $value) {
if (is_int($value)) {
$stmt->bindValue("$key", $value, PDO::PARAM_INT);
} else {
$stmt->bindValue("$key", $value);
}
}
$stmt->execute();
if ($fetchMode === PDO::FETCH_CLASS) {
return $stmt->fetchAll($fetchMode, $class);
} else {
return $stmt->fetchAll($fetchMode);
}
}
/**
* Count method
* @param string $table table name
* @param string $column optional
*/
public function count($table, $column= 'id') {
$stmt = $this->prepare("SELECT $column FROM $table");
$stmt->execute();
return $stmt->rowCount();
}
/**
* insert method
* @param string $table table name
* @param array $data array of columns and values
*/
public function insert($table, $data)
{
ksort($data);
$fieldNames = implode(',', array_keys($data));
$fieldValues = ':'.implode(', :', array_keys($data));
$stmt = $this->prepare("INSERT INTO $table ($fieldNames) VALUES ($fieldValues)");
foreach ($data as $key => $value) {
$stmt->bindValue(":$key", $value);
}
$stmt->execute();
return $this->lastInsertId();
}
/**
* update method
* @param string $table table name
* @param array $data array of columns and values
* @param array $where array of columns and values
*/
public function update($table, $data, $where)
{
ksort($data);
$fieldDetails = null;
foreach ($data as $key => $value) {
$fieldDetails .= "$key = :$key,";
}
$fieldDetails = rtrim($fieldDetails, ',');
$whereDetails = null;
$i = 0;
foreach ($where as $key => $value) {
if ($i == 0) {
$whereDetails .= "$key = :$key";
} else {
$whereDetails .= " AND $key = :$key";
}
$i++;
}
$whereDetails = ltrim($whereDetails, ' AND ');
$stmt = $this->prepare("UPDATE $table SET $fieldDetails WHERE $whereDetails");
foreach ($data as $key => $value) {
$stmt->bindValue(":$key", $value);
}
foreach ($where as $key => $value) {
$stmt->bindValue(":$key", $value);
}
$stmt->execute();
return $stmt->rowCount();
}
/**
* Delete method
* @param string $table table name
* @param array $data array of columns and values
* @param array $where array of columns and values
* @param integer $limit limit number of records
*/
public function delete($table, $where, $limit = 1)
{
ksort($where);
$whereDetails = null;
$i = 0;
foreach ($where as $key => $value) {
if ($i == 0) {
$whereDetails .= "$key = :$key";
} else {
$whereDetails .= " AND $key = :$key";
}
$i++;
}
$whereDetails = ltrim($whereDetails, ' AND ');
//if limit is a number use a limit on the query
if (is_numeric($limit)) {
$uselimit = "LIMIT $limit";
}
$stmt = $this->prepare("DELETE FROM $table WHERE $whereDetails $uselimit");
foreach ($where as $key => $value) {
$stmt->bindValue(":$key", $value);
}
$stmt->execute();
return $stmt->rowCount();
}
/**
* truncate table
* @param string $table table name
*/
public function truncate($table)
{
return $this->exec("TRUNCATE TABLE $table");
}
}
class Database extends DaveDatabase
{
private $table_objects = array();
public function getTable( $table_name )
{
if( !isset($this->table_objects[$table_name]) )
{
$this->loadTableObject( $table_name );
}
return ( isset($this->table_objects[$table_name]) ) ? $this->table_objects[$table_name] : false;
}
private function loadTableObject( $table_name )
{
$result = false;
$filepath = __DIR__ .'/database/'.$table_name.'.ext.php';
if( !file_exists( $filepath ) )
{
$this->tryCreateTableDefinition( $table_name );
}
if( file_exists( $filepath ) )
{
require_once( $filepath );
$class_name = 'Table'. $table_name;
if( class_exists( $class_name ) )
{
$this->table_objects[$table_name] = new $class_name();
$result = true;
}
}
return $result;
}
private function tryCreateTableDefinition( $table_name )
{
$table_class = new DatabaseTable($this, $table_name);
$table_class->getColumnMeta();
var_dump($table_class->fields);
var_dump($table_class->field_meta);
var_dump($table_class->primary_key);
}
/*
* $db->select("`username` FROM `members` WHERE `memberID` = :id and `email` = :email", array(':id' => 1, ':email' => 'someone@domain.com'));
* $db->selectAll("members", "WHERE `memberID` = :id and `email` = :email", array(':id' => 1, ':email' => 'someone@domain.com'));
*/
public function selectAll($sql, $array = array(), $fetchMode = PDO::FETCH_OBJ, $class = '')
{
}
}