Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
93.75% |
30 / 32 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
SQLiteGrammar | |
93.75% |
30 / 32 |
|
66.67% |
2 / 3 |
14.05 | |
0.00% |
0 / 1 |
compileSelectTables | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
compileSelectFields | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
compileCreateFields | |
90.91% |
20 / 22 |
|
0.00% |
0 / 1 |
12.11 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\DataStorage\Database\Schema\Grammar |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\DataStorage\Database\Schema\Grammar; |
16 | |
17 | use phpOMS\DataStorage\Database\Query\Builder; |
18 | use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder; |
19 | |
20 | /** |
21 | * Database query grammar. |
22 | * |
23 | * @package phpOMS\DataStorage\Database\Schema\Grammar |
24 | * @license OMS License 2.0 |
25 | * @link https://jingga.app |
26 | * @since 1.0.0 |
27 | */ |
28 | class SQLiteGrammar extends Grammar |
29 | { |
30 | /** |
31 | * System identifier. |
32 | * |
33 | * @var string |
34 | * @since 1.0.0 |
35 | */ |
36 | public string $systemIdentifierStart = '`'; |
37 | |
38 | /** |
39 | * System identifier. |
40 | * |
41 | * @var string |
42 | * @since 1.0.0 |
43 | */ |
44 | public string $systemIdentifierEnd = '`'; |
45 | |
46 | /** |
47 | * Compile from. |
48 | * |
49 | * @param SchemaBuilder $query Builder |
50 | * @param array $table Tables |
51 | * |
52 | * @return string |
53 | * |
54 | * @since 1.0.0 |
55 | */ |
56 | protected function compileSelectTables(SchemaBuilder $query, array $table) : string |
57 | { |
58 | $builder = new Builder($query->getConnection()); |
59 | $builder->select('name') |
60 | ->from('sqlite_master') |
61 | ->where('type', '=', 'table'); |
62 | |
63 | return \rtrim($builder->toSql(), ';'); |
64 | } |
65 | |
66 | /** |
67 | * Compile from. |
68 | * |
69 | * @param SchemaBuilder $query Builder |
70 | * @param string $table Tables |
71 | * |
72 | * @return string |
73 | * |
74 | * @since 1.0.0 |
75 | */ |
76 | protected function compileSelectFields(SchemaBuilder $query, string $table) : string |
77 | { |
78 | $builder = new Builder($query->getConnection()); |
79 | $builder->select('*') |
80 | ->from('pragma_table_info(\'' . $table . '\')') |
81 | ->where('pragma_table_info(\'' . $table . '\')', '=', $table); |
82 | |
83 | return \rtrim($builder->toSql(), ';'); |
84 | } |
85 | |
86 | /** |
87 | * Compile create table fields query. |
88 | * |
89 | * @param SchemaBuilder $query Query |
90 | * @param array $fields Fields to create |
91 | * |
92 | * @return string |
93 | * |
94 | * @since 1.0.0 |
95 | */ |
96 | protected function compileCreateFields(SchemaBuilder $query, array $fields) : string |
97 | { |
98 | $fieldQuery = ''; |
99 | $keys = ''; |
100 | |
101 | foreach ($fields as $name => $field) { |
102 | $fieldQuery .= ' ' . $this->expressionizeTableColumn([$name]) . ' ' . $field['type']; |
103 | |
104 | if (isset($field['default']) || ($field['default'] === null && ($field['null'] ?? false))) { |
105 | $fieldQuery .= ' DEFAULT ' . $this->compileValue($query, $field['default']); |
106 | } |
107 | |
108 | if ($field['null'] ?? false) { |
109 | $fieldQuery .= ' ' . ($field['null'] ? '' : 'NOT ') . 'NULL'; |
110 | } |
111 | |
112 | if ($field['autoincrement'] ?? false) { |
113 | $fieldQuery .= ' AUTO_INCREMENT'; |
114 | } |
115 | |
116 | $fieldQuery .= ','; |
117 | |
118 | if ($field['primary'] ?? false) { |
119 | $keys .= ' PRIMARY KEY (' . $this->expressionizeTableColumn([$name]) . '),'; |
120 | } |
121 | |
122 | if ($field['unique'] ?? false) { |
123 | $keys .= ' UNIQUE KEY (' . $this->expressionizeTableColumn([$name]) . '),'; |
124 | } |
125 | |
126 | if (isset($field['foreignTable'], $field['foreignKey'])) { |
127 | $keys .= ' FOREIGN KEY (' . $this->expressionizeTableColumn([$name]) . ') REFERENCES ' |
128 | . $this->expressionizeTableColumn([$field['foreignTable']]) |
129 | . ' (' . $this->expressionizeTableColumn([$field['foreignKey']]) . '),'; |
130 | } |
131 | |
132 | if (isset($field['meta']['multi_autoincrement'])) { |
133 | $query->hasPostQuery = true; |
134 | } |
135 | } |
136 | |
137 | return '(' . \ltrim(\rtrim($fieldQuery . $keys, ','), ' ') . ')'; |
138 | } |
139 | } |