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