1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spiral Framework. |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author Anton Titov (Wolfy-J) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Spiral\Database\Drivers\SQLite; |
10
|
|
|
|
11
|
|
|
use Spiral\Database\Entities\QueryCompiler as AbstractCompiler; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* SQLite specific syntax compiler. |
15
|
|
|
*/ |
16
|
|
|
class SQLiteCompiler extends AbstractCompiler |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function compileInsert(string $table, array $columns, array $rowsets): string |
22
|
|
|
{ |
23
|
|
|
if (count($rowsets) == 1) { |
24
|
|
|
return parent::compileInsert($table, $columns, $rowsets); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
//SQLite uses alternative syntax |
28
|
|
|
$statement = []; |
29
|
|
|
$statement[] = "INSERT INTO {$this->quote($table, true)} ({$this->prepareColumns($columns)})"; |
30
|
|
|
|
31
|
|
|
foreach ($rowsets as $rowset) { |
32
|
|
|
if (count($statement) == 1) { |
33
|
|
|
$selectColumns = []; |
34
|
|
|
foreach ($columns as $column) { |
35
|
|
|
$selectColumns[] = "? AS {$this->quote($column)}"; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$statement[] = 'SELECT ' . implode(', ', $selectColumns); |
39
|
|
|
} else { |
40
|
|
|
$statement[] = 'UNION SELECT ' . trim(str_repeat('?, ', count($columns)), ', '); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return implode("\n", $statement); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
* |
50
|
|
|
* @link http://stackoverflow.com/questions/10491492/sqllite-with-skip-offset-only-not-limit |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
protected function compileLimit(int $limit, int $offset): string |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
if (empty($limit) && empty($offset)) { |
55
|
|
|
return ''; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$statement = ''; |
59
|
|
|
|
60
|
|
|
if (!empty($limit) || !empty($offset)) { |
61
|
|
|
$statement = 'LIMIT ' . ($limit ?: '-1') . ' '; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!empty($offset)) { |
65
|
|
|
$statement .= "OFFSET {$offset}"; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return trim($statement); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.