Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
SqlServerGrammar | |
0.00% |
0 / 34 |
|
0.00% |
0 / 3 |
210 | |
0.00% |
0 / 1 |
compileSelectTables | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
compileSelectFields | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
compileCreateFields | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
156 |
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 SqlServerGrammar 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('table_name') |
60 | ->from('sys.tables') |
61 | ->innerJoin('sys.schemas') |
62 | ->on('sys.tables.schema_id', '=', 'sys.schemas.schema_id'); |
63 | |
64 | return \rtrim($builder->toSql(), ';'); |
65 | } |
66 | |
67 | /** |
68 | * Compile from. |
69 | * |
70 | * @param SchemaBuilder $query Builder |
71 | * @param string $table Tables |
72 | * |
73 | * @return string |
74 | * |
75 | * @since 1.0.0 |
76 | */ |
77 | protected function compileSelectFields(SchemaBuilder $query, string $table) : string |
78 | { |
79 | $builder = new Builder($query->getConnection()); |
80 | $builder->select('*') |
81 | ->from('information_schema.columns') |
82 | ->where('table_schema', '=', $query->getConnection()->getDatabase()) |
83 | ->andWhere('table_name', '=', $table); |
84 | |
85 | return \rtrim($builder->toSql(), ';'); |
86 | } |
87 | |
88 | /** |
89 | * Compile create table fields query. |
90 | * |
91 | * @param SchemaBuilder $query Query |
92 | * @param array $fields Fields to create |
93 | * |
94 | * @return string |
95 | * |
96 | * @since 1.0.0 |
97 | */ |
98 | protected function compileCreateFields(SchemaBuilder $query, array $fields) : string |
99 | { |
100 | $fieldQuery = ''; |
101 | $keys = ''; |
102 | |
103 | foreach ($fields as $name => $field) { |
104 | $fieldQuery .= ' ' . $this->expressionizeTableColumn([$name]) . ' ' . $field['type']; |
105 | |
106 | if (isset($field['default']) || ($field['default'] === null && ($field['null'] ?? false))) { |
107 | $fieldQuery .= ' DEFAULT ' . $this->compileValue($query, $field['default']); |
108 | } |
109 | |
110 | if ($field['null'] ?? false) { |
111 | $fieldQuery .= ' ' . ($field['null'] ? '' : 'NOT ') . 'NULL'; |
112 | } |
113 | |
114 | if ($field['autoincrement'] ?? false) { |
115 | $fieldQuery .= ' AUTO_INCREMENT'; |
116 | } |
117 | |
118 | $fieldQuery .= ','; |
119 | |
120 | if ($field['primary'] ?? false) { |
121 | $keys .= ' PRIMARY KEY (' . $this->expressionizeTableColumn([$name]) . '),'; |
122 | } |
123 | |
124 | if ($field['unique'] ?? false) { |
125 | $keys .= ' UNIQUE KEY (' . $this->expressionizeTableColumn([$name]) . '),'; |
126 | } |
127 | |
128 | if (isset($field['foreignTable'], $field['foreignKey'])) { |
129 | $keys .= ' FOREIGN KEY (' . $this->expressionizeTableColumn([$name]) . ') REFERENCES ' |
130 | . $this->expressionizeTableColumn([$field['foreignTable']]) |
131 | . ' (' . $this->expressionizeTableColumn([$field['foreignKey']]) . '),'; |
132 | } |
133 | |
134 | if (isset($field['meta']['multi_autoincrement'])) { |
135 | $query->hasPostQuery = true; |
136 | } |
137 | } |
138 | |
139 | return '(' . \ltrim(\rtrim($fieldQuery . $keys, ','), ' ') . ')'; |
140 | } |
141 | } |