1
|
|
|
<?php |
2
|
|
|
namespace Data; |
3
|
|
|
|
4
|
|
|
class SQLDataSet extends DataSet |
5
|
|
|
{ |
6
|
|
|
protected $pdo; |
7
|
|
|
|
8
|
|
|
public function __construct($params) |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
if(isset($params['user'])) |
11
|
|
|
{ |
12
|
|
|
$this->pdo = new \PDO($params['dsn'], $params['user'], $params['pass']); |
13
|
|
|
} |
14
|
|
|
else |
15
|
|
|
{ |
16
|
|
|
$this->pdo = new \PDO($params['dsn']); |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get the number of rows affected by the query |
22
|
|
|
* |
23
|
|
|
* @param string $sql The SQL string |
24
|
|
|
* |
25
|
|
|
* @return integer The number of rows affected by the query |
26
|
|
|
*/ |
27
|
|
|
private function _get_row_count_for_query($sql) |
28
|
|
|
{ |
29
|
|
|
$stmt = $this->pdo->query($sql); |
30
|
|
|
if($stmt === false) |
31
|
|
|
{ |
32
|
|
|
return 0; |
33
|
|
|
} |
34
|
|
|
$count = $stmt->rowCount(); |
35
|
|
|
if($count === 0) |
36
|
|
|
{ |
37
|
|
|
$array = $stmt->fetchAll(); |
38
|
|
|
$count = count($array); |
39
|
|
|
} |
40
|
|
|
return $count; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
function _tableExistsNoPrefix($name) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote($name)) > 0) |
46
|
|
|
{ |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote($name)) > 0) |
50
|
|
|
{ |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
function _tableExists($name) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('tbl'.$name)) > 0) |
59
|
|
|
{ |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('tbl'.$name)) > 0) |
63
|
|
|
{ |
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
function _viewExists($name) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if($this->_get_row_count_for_query('SHOW TABLES LIKE '.$this->pdo->quote('v'.$name)) > 0) |
72
|
|
|
{ |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
else if($this->_get_row_count_for_query('SELECT * FROM sqlite_master WHERE name LIKE '.$this->pdo->quote('v'.$name)) > 0) |
76
|
|
|
{ |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function tableExists($name) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
if($this->_tableExists($name)) |
85
|
|
|
{ |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
if($this->_tableExistsNoPrefix($name)) |
89
|
|
|
{ |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
if($this->_viewExists($name)) |
93
|
|
|
{ |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
function getTable($name) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if($this->_tableExists($name)) |
102
|
|
|
{ |
103
|
|
|
return new SQLDataTable($this, 'tbl'.$name); |
104
|
|
|
} |
105
|
|
|
if($this->_viewExists($name)) |
106
|
|
|
{ |
107
|
|
|
return new SQLDataTable($this, 'v'.$name); |
108
|
|
|
} |
109
|
|
|
if($this->_tableExistsNoPrefix($name)) |
110
|
|
|
{ |
111
|
|
|
return new SQLDataTable($this, $name); |
112
|
|
|
} |
113
|
|
|
throw new \Exception('No such table '.$name); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $sort The array to sort by or false to not sort |
118
|
|
|
*/ |
119
|
|
|
private function getOrderByClause($sort) |
120
|
|
|
{ |
121
|
|
|
if(empty($sort)) |
122
|
|
|
{ |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
$sql = ' ORDER BY '; |
126
|
|
|
$tmp = array(); |
127
|
|
|
foreach($sort as $sort_col=>$dir) |
128
|
|
|
{ |
129
|
|
|
array_push($tmp, $sort_col.' '.($dir === 1 ? 'ASC' : 'DESC')); |
130
|
|
|
} |
131
|
|
|
$sql .= implode($tmp, ','); |
132
|
|
|
return $sql; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function read($tablename, $where = false, $select = '*', $count = false, $skip = false, $sort = false) |
136
|
|
|
{ |
137
|
|
|
if($select === false) |
138
|
|
|
{ |
139
|
|
|
$select = '*'; |
140
|
|
|
} |
141
|
|
|
$sql = "SELECT $select FROM $tablename"; |
142
|
|
|
if($where !== false) |
143
|
|
|
{ |
144
|
|
|
$sql .= ' WHERE '.$where; |
145
|
|
|
} |
146
|
|
|
if($count !== false) |
147
|
|
|
{ |
148
|
|
|
if($skip === false) |
149
|
|
|
{ |
150
|
|
|
$sql .= ' LIMIT '.(int)$count; |
151
|
|
|
} |
152
|
|
|
else |
153
|
|
|
{ |
154
|
|
|
$sql .= " LIMIT $skip, $count"; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
$tmp = $this->getOrderByClause($sort); |
|
|
|
|
158
|
|
|
if($tmp !== false) |
159
|
|
|
{ |
160
|
|
|
$sql .= $tmp; |
161
|
|
|
} |
162
|
|
|
$stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC); |
163
|
|
|
if($stmt === false) |
164
|
|
|
{ |
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
$ret = $stmt->fetchAll(); |
168
|
|
|
if($ret === false || empty($ret)) |
169
|
|
|
{ |
170
|
|
|
return false; |
171
|
|
|
} |
172
|
|
|
return $ret; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
View Code Duplication |
function update($tablename, $where, $data) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$set = array(); |
178
|
|
|
if(is_object($data)) |
179
|
|
|
{ |
180
|
|
|
$data = (array)$data; |
181
|
|
|
} |
182
|
|
|
$cols = array_keys($data); |
183
|
|
|
$count = count($cols); |
184
|
|
|
for($i = 0; $i < $count; $i++) |
185
|
|
|
{ |
186
|
|
|
array_push($set, $cols[$i].'='.$this->pdo->quote($data[$cols[$i]])); |
187
|
|
|
} |
188
|
|
|
$set = implode(',', $set); |
189
|
|
|
$sql = "UPDATE $tablename SET $set WHERE $where"; |
190
|
|
|
if($this->pdo->exec($sql) === false) |
191
|
|
|
{ |
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
View Code Duplication |
function create($tablename, $data) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$set = array(); |
200
|
|
|
if(is_object($data)) |
201
|
|
|
{ |
202
|
|
|
$data = (array)$data; |
203
|
|
|
} |
204
|
|
|
$cols = array_keys($data); |
205
|
|
|
$count = count($cols); |
206
|
|
|
for($i = 0; $i < $count; $i++) |
207
|
|
|
{ |
208
|
|
|
array_push($set, $this->pdo->quote($data[$cols[$i]])); |
209
|
|
|
} |
210
|
|
|
$cols = implode(',', $cols); |
211
|
|
|
$set = implode(',', $set); |
212
|
|
|
$sql = "INSERT INTO $tablename ($cols) VALUES ($set);"; |
213
|
|
|
if($this->pdo->exec($sql) === false) |
214
|
|
|
{ |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
return true; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
function delete($tablename, $where) |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
$sql = "DELETE FROM $tablename WHERE $where"; |
223
|
|
|
if($this->pdo->exec($sql) === false) |
224
|
|
|
{ |
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
return true; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
function raw_query($sql) |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
$stmt = $this->pdo->query($sql, \PDO::FETCH_ASSOC); |
233
|
|
|
if($stmt === false) |
234
|
|
|
{ |
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
$ret = $stmt->fetchAll(); |
238
|
|
|
return $ret; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
242
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.