|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
|
4
|
|
|
|
|
5
|
|
|
class QueryEngine |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
private $db; |
|
9
|
|
|
|
|
10
|
32 |
|
public function setDriver($driver) |
|
11
|
|
|
{ |
|
12
|
32 |
|
$this->db = $driver; |
|
13
|
32 |
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Generates an SQL insert query string for the model based on the fields |
|
17
|
|
|
* currently stored in the model. |
|
18
|
|
|
* |
|
19
|
|
|
* @param RecordWrapper $model |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getInsertQuery($model) |
|
23
|
|
|
{ |
|
24
|
|
|
$data = $model->getData(); |
|
25
|
|
|
$table = $model->getDBStoreInformation()['quoted_table']; |
|
26
|
|
|
$fields = array_keys($data[0]); |
|
27
|
|
|
$quotedFields = []; |
|
28
|
|
|
$valueFields = []; |
|
29
|
|
|
|
|
30
|
|
|
foreach ($fields as $field) { |
|
31
|
|
|
$quotedFields[] = $this->db->quoteIdentifier($field); |
|
32
|
|
|
$valueFields[] = ":{$field}"; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return "INSERT INTO " . $table . |
|
36
|
|
|
" (" . implode(", ", $quotedFields) . ") VALUES (" . implode(', ', $valueFields) . ")"; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
6 |
|
public function getBulkUpdateQuery($data, $parameters) |
|
40
|
|
|
{ |
|
41
|
6 |
|
$updateData = []; |
|
42
|
6 |
|
foreach ($data as $field => $value) { |
|
43
|
6 |
|
$updateData[] = "{$this->db->quoteIdentifier($field)} = :$field"; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
6 |
|
return sprintf( |
|
47
|
6 |
|
"UPDATE %s SET %s %s", |
|
48
|
6 |
|
$parameters->getTable(), |
|
49
|
6 |
|
implode(', ', $updateData), |
|
50
|
6 |
|
$parameters->getWhereClause() |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Generates an SQL update query string for the model based on the data |
|
56
|
|
|
* currently stored in the model. |
|
57
|
|
|
* |
|
58
|
|
|
* @param RecordWrapper $model |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function getUpdateQuery($model) |
|
62
|
|
|
{ |
|
63
|
2 |
|
$data = $model->getData(); |
|
64
|
2 |
|
$fields = array_keys($data[0]); |
|
65
|
2 |
|
$valueFields = []; |
|
66
|
2 |
|
$conditions = []; |
|
67
|
2 |
|
$primaryKey = $model->getDescription()->getPrimaryKey(); |
|
68
|
|
|
|
|
69
|
2 |
|
foreach ($fields as $field) { |
|
70
|
2 |
|
$quotedField = $this->db->quoteIdentifier($field); |
|
71
|
|
|
|
|
72
|
2 |
|
if (array_search($field, $primaryKey) !== false) { |
|
73
|
2 |
|
$conditions[] = "{$quotedField} = :{$field}"; |
|
74
|
|
|
} else { |
|
75
|
2 |
|
$valueFields[] = "{$quotedField} = :{$field}"; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return "UPDATE " . |
|
80
|
2 |
|
$model->getDBStoreInformation()['quoted_table'] . |
|
81
|
2 |
|
" SET " . implode(', ', $valueFields) . |
|
82
|
2 |
|
" WHERE " . implode(' AND ', $conditions); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
24 |
|
public function getSelectQuery($parameters) |
|
86
|
|
|
{ |
|
87
|
24 |
|
return sprintf( |
|
88
|
24 |
|
"SELECT %s FROM %s%s%s%s%s", |
|
89
|
24 |
|
$parameters->getFields(), |
|
90
|
24 |
|
$parameters->getTable(), |
|
91
|
24 |
|
$parameters->getWhereClause(), |
|
92
|
24 |
|
$parameters->getSorts(), |
|
93
|
24 |
|
$parameters->getLimit(), |
|
94
|
24 |
|
$parameters->getOffset() |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
public function getCountQuery($parameters) |
|
99
|
|
|
{ |
|
100
|
4 |
|
return sprintf( |
|
101
|
4 |
|
"SELECT count(*) as count FROM %s%s", |
|
102
|
4 |
|
$parameters->getTable(), |
|
103
|
4 |
|
$parameters->getWhereClause() |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
2 |
|
public function getDeleteQuery($parameters) |
|
108
|
|
|
{ |
|
109
|
2 |
|
return sprintf( |
|
110
|
2 |
|
"DELETE FROM %s%s", |
|
111
|
2 |
|
$parameters->getTable(), |
|
112
|
2 |
|
$parameters->getWhereClause() |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|