1 | <?php |
||
5 | class QueryEngine { |
||
6 | |||
7 | private $db; |
||
8 | |||
9 | 1 | public function setDriver($driver) { |
|
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) { |
|
91 | |||
92 | public function getCountQuery($parameters) { |
||
99 | |||
100 | public function getDeleteQuery($parameters) { |
||
107 | |||
108 | } |
||
109 |