Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
PostgresGrammar | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
210 | |
0.00% |
0 / 1 |
compileSelectTables | |
0.00% |
0 / 5 |
|
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 PostgresGrammar extends Grammar |
29 | { |
30 | /** |
31 | * Compile from. |
32 | * |
33 | * @param SchemaBuilder $query Builder |
34 | * @param array $table Tables |
35 | * |
36 | * @return string |
37 | * |
38 | * @since 1.0.0 |
39 | */ |
40 | protected function compileSelectTables(SchemaBuilder $query, array $table) : string |
41 | { |
42 | $builder = new Builder($query->getConnection()); |
43 | $builder->select('table_name') |
44 | ->from('information_schema.tables') |
45 | ->where('table_schema', '=', $query->getConnection()->getDatabase()); |
46 | |
47 | return \rtrim($builder->toSql(), ';'); |
48 | } |
49 | |
50 | /** |
51 | * Compile from. |
52 | * |
53 | * @param SchemaBuilder $query Builder |
54 | * @param string $table Tables |
55 | * |
56 | * @return string |
57 | * |
58 | * @since 1.0.0 |
59 | */ |
60 | protected function compileSelectFields(SchemaBuilder $query, string $table) : string |
61 | { |
62 | $builder = new Builder($query->getConnection()); |
63 | $builder->select('*') |
64 | ->from('information_schema.columns') |
65 | ->where('table_schema', '=', $query->getConnection()->getDatabase()) |
66 | ->andWhere('table_name', '=', $table); |
67 | |
68 | return \rtrim($builder->toSql(), ';'); |
69 | } |
70 | |
71 | /** |
72 | * Compile create table fields query. |
73 | * |
74 | * @param SchemaBuilder $query Query |
75 | * @param array $fields Fields to create |
76 | * |
77 | * @return string |
78 | * |
79 | * @since 1.0.0 |
80 | */ |
81 | protected function compileCreateFields(SchemaBuilder $query, array $fields) : string |
82 | { |
83 | $fieldQuery = ''; |
84 | $keys = ''; |
85 | |
86 | foreach ($fields as $name => $field) { |
87 | $fieldQuery .= ' ' . $this->expressionizeTableColumn([$name]) . ' ' . $field['type']; |
88 | |
89 | if (isset($field['default']) || ($field['default'] === null && ($field['null'] ?? false))) { |
90 | $fieldQuery .= ' DEFAULT ' . $this->compileValue($query, $field['default']); |
91 | } |
92 | |
93 | if ($field['null'] ?? false) { |
94 | $fieldQuery .= ' ' . ($field['null'] ? '' : 'NOT ') . 'NULL'; |
95 | } |
96 | |
97 | if ($field['autoincrement'] ?? false) { |
98 | $fieldQuery .= ' AUTO_INCREMENT'; |
99 | } |
100 | |
101 | $fieldQuery .= ','; |
102 | |
103 | if ($field['primary'] ?? false) { |
104 | $keys .= ' PRIMARY KEY (' . $this->expressionizeTableColumn([$name]) . '),'; |
105 | } |
106 | |
107 | if ($field['unique'] ?? false) { |
108 | $keys .= ' UNIQUE KEY (' . $this->expressionizeTableColumn([$name]) . '),'; |
109 | } |
110 | |
111 | if (isset($field['foreignTable'], $field['foreignKey'])) { |
112 | $keys .= ' FOREIGN KEY (' . $this->expressionizeTableColumn([$name]) . ') REFERENCES ' |
113 | . $this->expressionizeTableColumn([$field['foreignTable']]) |
114 | . ' (' . $this->expressionizeTableColumn([$field['foreignKey']]) . '),'; |
115 | } |
116 | |
117 | if (isset($field['meta']['multi_autoincrement'])) { |
118 | $query->hasPostQuery = true; |
119 | } |
120 | } |
121 | |
122 | return '(' . \ltrim(\rtrim($fieldQuery . $keys, ','), ' ') . ')'; |
123 | } |
124 | } |