|
1
|
|
|
<?php
|
|
2
|
|
|
/**
|
|
3
|
|
|
* This file is part of the O2System Framework package.
|
|
4
|
|
|
*
|
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
6
|
|
|
* file that was distributed with this source code.
|
|
7
|
|
|
*
|
|
8
|
|
|
* @author Steeve Andrian Salim
|
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
|
10
|
|
|
*/
|
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Database\Sql\Drivers\Sqlite;
|
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
|
17
|
|
|
|
|
18
|
|
|
use O2System\Database\Sql\Abstracts\AbstractQueryBuilder;
|
|
19
|
|
|
|
|
20
|
|
|
/**
|
|
21
|
|
|
* Class QueryBuilder
|
|
22
|
|
|
*
|
|
23
|
|
|
* @package O2System\Database\Sql\Drivers\Sqlite
|
|
24
|
|
|
*/
|
|
25
|
|
|
class QueryBuilder extends AbstractQueryBuilder
|
|
26
|
|
|
{
|
|
27
|
|
|
/**
|
|
28
|
|
|
* Platform independent LIKE statement builder.
|
|
29
|
|
|
*
|
|
30
|
|
|
* @param string|null $prefix
|
|
31
|
|
|
* @param string $column
|
|
32
|
|
|
* @param string|null $not
|
|
33
|
|
|
* @param string $bind
|
|
34
|
|
|
* @param bool $caseSensitive
|
|
35
|
|
|
*
|
|
36
|
|
|
* @return string
|
|
37
|
|
|
*/
|
|
38
|
|
|
protected function platformPrepareLikeStatement(
|
|
39
|
|
|
$prefix = null,
|
|
40
|
|
|
$column,
|
|
41
|
|
|
$not = null,
|
|
42
|
|
|
$bind,
|
|
43
|
|
|
$caseSensitive = false
|
|
44
|
|
|
) {
|
|
45
|
|
|
$likeStatement = "{$prefix} {$column} {$not} LIKE :{$bind}";
|
|
46
|
|
|
|
|
47
|
|
|
if ($caseSensitive === true) {
|
|
48
|
|
|
$likeStatement = "{$prefix} LOWER({$column}) {$not} LIKE :{$bind}";
|
|
49
|
|
|
}
|
|
50
|
|
|
|
|
51
|
|
|
return $likeStatement;
|
|
52
|
|
|
}
|
|
53
|
|
|
|
|
54
|
|
|
//--------------------------------------------------------------------
|
|
55
|
|
|
|
|
56
|
|
|
/**
|
|
57
|
|
|
* QueryBuilder::platformInsertStatement
|
|
58
|
|
|
*
|
|
59
|
|
|
* Generates a platform-specific insert string from the supplied data.
|
|
60
|
|
|
*
|
|
61
|
|
|
* @param string $table Table name.
|
|
62
|
|
|
* @param array $keys Insert keys.
|
|
63
|
|
|
* @param array $values Insert values.
|
|
64
|
|
|
*
|
|
65
|
|
|
* @return string
|
|
66
|
|
|
*/
|
|
67
|
|
|
protected function platformInsertStatement($table, array $keys, array $values)
|
|
68
|
|
|
{
|
|
69
|
|
|
return 'INSERT INTO '
|
|
70
|
|
|
. $table
|
|
71
|
|
|
. ' ('
|
|
72
|
|
|
. implode(', ', $keys)
|
|
73
|
|
|
. ') VALUES ('
|
|
74
|
|
|
. implode(', ', $values)
|
|
75
|
|
|
. ')';
|
|
76
|
|
|
}
|
|
77
|
|
|
|
|
78
|
|
|
//--------------------------------------------------------------------
|
|
79
|
|
|
|
|
80
|
|
|
/**
|
|
81
|
|
|
* QueryBuilder::platformReplaceStatement
|
|
82
|
|
|
*
|
|
83
|
|
|
* Generates a platform-specific update string from the supplied data.
|
|
84
|
|
|
*
|
|
85
|
|
|
* @param string $table Table name.
|
|
86
|
|
|
* @param array $keys Insert keys.
|
|
87
|
|
|
* @param array $values Insert values.
|
|
88
|
|
|
*
|
|
89
|
|
|
* @return string
|
|
90
|
|
|
*/
|
|
91
|
|
|
protected function platformReplaceStatement($table, array $keys, array $values)
|
|
92
|
|
|
{
|
|
93
|
|
|
return 'REPLACE INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES (' . implode(', ', $values) . ')';
|
|
94
|
|
|
}
|
|
95
|
|
|
|
|
96
|
|
|
//--------------------------------------------------------------------
|
|
97
|
|
|
|
|
98
|
|
|
/**
|
|
99
|
|
|
* QueryBuilder::platformUpdateStatement
|
|
100
|
|
|
*
|
|
101
|
|
|
* Generates a platform-specific update string from the supplied data.
|
|
102
|
|
|
*
|
|
103
|
|
|
* @param string $table Table name.
|
|
104
|
|
|
* @param array $sets An associative array of set values.
|
|
105
|
|
|
* sets[][field => value]
|
|
106
|
|
|
*
|
|
107
|
|
|
* @return string
|
|
108
|
|
|
*/
|
|
109
|
|
|
protected function platformUpdateStatement($table, array $sets)
|
|
110
|
|
|
{
|
|
111
|
|
|
$columns = [];
|
|
112
|
|
|
|
|
113
|
|
|
foreach ($sets as $key => $val) {
|
|
114
|
|
|
$columns[] = $key . ' = ' . $val;
|
|
115
|
|
|
}
|
|
116
|
|
|
|
|
117
|
|
|
return 'UPDATE ' . $table . ' SET ' . implode(', ', $columns)
|
|
118
|
|
|
. $this->compileWhereHavingStatement('where')
|
|
119
|
|
|
. $this->compileOrderByStatement()
|
|
120
|
|
|
. $this->compileLimitStatement();
|
|
121
|
|
|
}
|
|
122
|
|
|
|
|
123
|
|
|
//--------------------------------------------------------------------
|
|
124
|
|
|
|
|
125
|
|
|
/**
|
|
126
|
|
|
* QueryBuilder::platformUpdateBatchStatement
|
|
127
|
|
|
*
|
|
128
|
|
|
* Generates a platform-specific batch update string from the supplied data.
|
|
129
|
|
|
*
|
|
130
|
|
|
* @param string $table Table name
|
|
131
|
|
|
* @param array $values Update data
|
|
132
|
|
|
* @param string $index WHERE key
|
|
133
|
|
|
*
|
|
134
|
|
|
* @return string
|
|
135
|
|
|
*/
|
|
136
|
|
|
protected function platformUpdateBatchStatement($table, $values, $index)
|
|
137
|
|
|
{
|
|
138
|
|
|
$ids = [];
|
|
139
|
|
|
$columns = [];
|
|
140
|
|
|
|
|
141
|
|
|
foreach ($values as $key => $value) {
|
|
142
|
|
|
$ids[] = $value[ $index ];
|
|
143
|
|
|
|
|
144
|
|
|
foreach (array_keys($value) as $field) {
|
|
145
|
|
|
if ($field !== $index) {
|
|
146
|
|
|
$columns[ $field ][] = 'WHEN ' . $index . ' = ' . $value[ $index ] . ' THEN ' . $value[ $field ];
|
|
147
|
|
|
}
|
|
148
|
|
|
}
|
|
149
|
|
|
}
|
|
150
|
|
|
|
|
151
|
|
|
$cases = '';
|
|
152
|
|
|
foreach ($columns as $key => $value) {
|
|
153
|
|
|
$cases .= $key . " = CASE \n"
|
|
154
|
|
|
. implode("\n", $value) . "\n"
|
|
155
|
|
|
. 'ELSE ' . $key . ' END, ';
|
|
156
|
|
|
}
|
|
157
|
|
|
|
|
158
|
|
|
$this->where($index . ' IN(' . implode(',', $ids) . ')', null, false);
|
|
159
|
|
|
|
|
160
|
|
|
return 'UPDATE ' . $table . ' SET ' . substr($cases, 0, -2) . $this->compileWhereHavingStatement('where');
|
|
161
|
|
|
}
|
|
162
|
|
|
|
|
163
|
|
|
//--------------------------------------------------------------------
|
|
164
|
|
|
|
|
165
|
|
|
/**
|
|
166
|
|
|
* QueryBuilder::platformDeleteStatement
|
|
167
|
|
|
*
|
|
168
|
|
|
* Generates a platform-specific delete string from the supplied data
|
|
169
|
|
|
*
|
|
170
|
|
|
* @param string $table The table name.
|
|
171
|
|
|
*
|
|
172
|
|
|
* @return string
|
|
173
|
|
|
*/
|
|
174
|
|
|
protected function platformDeleteStatement($table)
|
|
175
|
|
|
{
|
|
176
|
|
|
return 'DELETE FROM ' . $table
|
|
177
|
|
|
. $this->compileWhereHavingStatement('where')
|
|
178
|
|
|
. $this->compileLimitStatement();
|
|
179
|
|
|
}
|
|
180
|
|
|
|
|
181
|
|
|
//--------------------------------------------------------------------
|
|
182
|
|
|
|
|
183
|
|
|
/**
|
|
184
|
|
|
* QueryBuilder::platformTruncateStatement
|
|
185
|
|
|
*
|
|
186
|
|
|
* Generates a platform-specific truncate statement.
|
|
187
|
|
|
*
|
|
188
|
|
|
* @param string $table The table name.
|
|
189
|
|
|
*
|
|
190
|
|
|
* @return string
|
|
191
|
|
|
*/
|
|
192
|
|
|
protected function platformTruncateStatement($table)
|
|
193
|
|
|
{
|
|
194
|
|
|
return 'TRUNCATE ' . $table;
|
|
195
|
|
|
}
|
|
196
|
|
|
|
|
197
|
|
|
//--------------------------------------------------------------------
|
|
198
|
|
|
|
|
199
|
|
|
/**
|
|
200
|
|
|
* QueryBuilder::platformInsertBatchStatement
|
|
201
|
|
|
*
|
|
202
|
|
|
* @param string $table
|
|
203
|
|
|
* @param array $keys
|
|
204
|
|
|
* @param array $values
|
|
205
|
|
|
*
|
|
206
|
|
|
* @return mixed
|
|
207
|
|
|
*/
|
|
208
|
|
|
protected function platformInsertBatchStatement($table, array $keys, array $values)
|
|
209
|
|
|
{
|
|
210
|
|
|
return 'INSERT INTO '
|
|
211
|
|
|
. $table
|
|
212
|
|
|
. ' ('
|
|
213
|
|
|
. implode(', ', $keys)
|
|
214
|
|
|
. ') VALUES '
|
|
215
|
|
|
. implode(', ', $values);
|
|
216
|
|
|
}
|
|
217
|
|
|
}
|
|
218
|
|
|
|