@@ -28,8 +28,8 @@ discard block |
||
28 | 28 | $table = $temp[1]; |
29 | 29 | } |
30 | 30 | |
31 | - if (isset($tables[$schema . '.' . $table])) { |
|
32 | - return $tables[$schema . '.' . $table]; |
|
31 | + if (isset($tables[$schema.'.'.$table])) { |
|
32 | + return $tables[$schema.'.'.$table]; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | static $comments = []; |
@@ -37,10 +37,10 @@ discard block |
||
37 | 37 | $comments[$schema] = Collection::from( |
38 | 38 | $this->query( |
39 | 39 | "SELECT TABLE_NAME, TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ?", |
40 | - [ $schema ] |
|
40 | + [$schema] |
|
41 | 41 | ) |
42 | 42 | ) |
43 | - ->mapKey(function ($v) { |
|
43 | + ->mapKey(function($v) { |
|
44 | 44 | return $v['TABLE_NAME']; |
45 | 45 | }) |
46 | 46 | ->pluck('TABLE_COMMENT') |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | WHERE |
61 | 61 | (TABLE_SCHEMA = ? OR REFERENCED_TABLE_SCHEMA = ?) AND |
62 | 62 | TABLE_NAME IS NOT NULL AND REFERENCED_TABLE_NAME IS NOT NULL", |
63 | - [ $main, $main ] |
|
63 | + [$main, $main] |
|
64 | 64 | ) |
65 | 65 | )->toArray(); |
66 | 66 | foreach ($col as $row) { |
@@ -70,8 +70,8 @@ discard block |
||
70 | 70 | if ($row['REFERENCED_TABLE_SCHEMA'] !== $main) { |
71 | 71 | $additional[] = $row['REFERENCED_TABLE_SCHEMA']; |
72 | 72 | } |
73 | - $relationsT[$row['TABLE_SCHEMA'] . '.' . $row['TABLE_NAME']][] = $row; |
|
74 | - $relationsR[$row['REFERENCED_TABLE_SCHEMA'] . '.' . $row['REFERENCED_TABLE_NAME']][] = $row; |
|
73 | + $relationsT[$row['TABLE_SCHEMA'].'.'.$row['TABLE_NAME']][] = $row; |
|
74 | + $relationsR[$row['REFERENCED_TABLE_SCHEMA'].'.'.$row['REFERENCED_TABLE_NAME']][] = $row; |
|
75 | 75 | } |
76 | 76 | foreach (array_filter(array_unique($additional)) as $s) { |
77 | 77 | $col = Collection::from( |
@@ -81,12 +81,12 @@ discard block |
||
81 | 81 | WHERE |
82 | 82 | TABLE_SCHEMA = ? AND REFERENCED_TABLE_SCHEMA = ? AND |
83 | 83 | TABLE_NAME IS NOT NULL AND REFERENCED_TABLE_NAME IS NOT NULL", |
84 | - [ $s, $s ] |
|
84 | + [$s, $s] |
|
85 | 85 | ) |
86 | 86 | )->toArray(); |
87 | 87 | foreach ($col as $row) { |
88 | - $relationsT[$row['TABLE_SCHEMA'] . '.' . $row['TABLE_NAME']][] = $row; |
|
89 | - $relationsR[$row['REFERENCED_TABLE_SCHEMA'] . '.' . $row['REFERENCED_TABLE_NAME']][] = $row; |
|
88 | + $relationsT[$row['TABLE_SCHEMA'].'.'.$row['TABLE_NAME']][] = $row; |
|
89 | + $relationsR[$row['REFERENCED_TABLE_SCHEMA'].'.'.$row['REFERENCED_TABLE_NAME']][] = $row; |
|
90 | 90 | } |
91 | 91 | } |
92 | 92 | } |
@@ -95,14 +95,14 @@ discard block |
||
95 | 95 | if (!count($columns)) { |
96 | 96 | throw new DBException('Table not found by name'); |
97 | 97 | } |
98 | - $tables[$schema . '.' . $table] = $definition = (new Table($table, $schema)) |
|
98 | + $tables[$schema.'.'.$table] = $definition = (new Table($table, $schema)) |
|
99 | 99 | ->addColumns( |
100 | 100 | $columns |
101 | 101 | ->clone() |
102 | - ->mapKey(function ($v) { |
|
102 | + ->mapKey(function($v) { |
|
103 | 103 | return $v['Field']; |
104 | 104 | }) |
105 | - ->map(function ($v) { |
|
105 | + ->map(function($v) { |
|
106 | 106 | $v['length'] = null; |
107 | 107 | if (!isset($v['Type'])) { |
108 | 108 | return $v; |
@@ -124,7 +124,7 @@ discard block |
||
124 | 124 | default: |
125 | 125 | if (strpos($type, 'char') !== false && strpos($type, '(') !== false) { |
126 | 126 | // extract length from varchar |
127 | - $v['length'] = (int)explode(')', explode('(', $type)[1])[0]; |
|
127 | + $v['length'] = (int) explode(')', explode('(', $type)[1])[0]; |
|
128 | 128 | $v['length'] = $v['length'] > 0 ? $v['length'] : null; |
129 | 129 | } |
130 | 130 | break; |
@@ -136,7 +136,7 @@ discard block |
||
136 | 136 | ->setPrimaryKey( |
137 | 137 | $columns |
138 | 138 | ->clone() |
139 | - ->filter(function ($v) { |
|
139 | + ->filter(function($v) { |
|
140 | 140 | return $v['Key'] === 'PRI'; |
141 | 141 | }) |
142 | 142 | ->pluck('Field') |
@@ -149,8 +149,8 @@ discard block |
||
149 | 149 | // assuming current table is on the "one" end having "many" records in the referencing table |
150 | 150 | // resulting in a "hasMany" or "manyToMany" relationship (if a pivot table is detected) |
151 | 151 | $relations = []; |
152 | - foreach ($relationsR[$schema . '.' . $table] ?? [] as $relation) { |
|
153 | - $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['TABLE_SCHEMA'] . '.' . $relation['TABLE_NAME']; |
|
152 | + foreach ($relationsR[$schema.'.'.$table] ?? [] as $relation) { |
|
153 | + $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['TABLE_SCHEMA'].'.'.$relation['TABLE_NAME']; |
|
154 | 154 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$relation['REFERENCED_COLUMN_NAME']] = |
155 | 155 | $relation['COLUMN_NAME']; |
156 | 156 | } |
@@ -166,10 +166,10 @@ discard block |
||
166 | 166 | $usedcol = []; |
167 | 167 | if (count($columns)) { |
168 | 168 | foreach (Collection::from($relationsT[$data['table']] ?? []) |
169 | - ->filter(function ($v) use ($columns) { |
|
169 | + ->filter(function($v) use ($columns) { |
|
170 | 170 | return in_array($v['COLUMN_NAME'], $columns); |
171 | 171 | }) |
172 | - ->map(function ($v) { |
|
172 | + ->map(function($v) { |
|
173 | 173 | $new = []; |
174 | 174 | foreach ($v as $kk => $vv) { |
175 | 175 | $new[strtoupper($kk)] = $vv; |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | return $new; |
178 | 178 | }) as $relation |
179 | 179 | ) { |
180 | - $foreign[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_SCHEMA'] . '.' . $relation['REFERENCED_TABLE_NAME']; |
|
180 | + $foreign[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_SCHEMA'].'.'.$relation['REFERENCED_TABLE_NAME']; |
|
181 | 181 | $foreign[$relation['CONSTRAINT_NAME']]['keymap'][$relation['COLUMN_NAME']] = |
182 | 182 | $relation['REFERENCED_COLUMN_NAME']; |
183 | 183 | $usedcol[] = $relation['COLUMN_NAME']; |
@@ -193,7 +193,7 @@ discard block |
||
193 | 193 | $orig = $relname; |
194 | 194 | $cntr = 1; |
195 | 195 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
196 | - $relname = $orig . '_' . (++ $cntr); |
|
196 | + $relname = $orig.'_'.(++$cntr); |
|
197 | 197 | } |
198 | 198 | $definition->addRelation( |
199 | 199 | new TableRelation( |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | $orig = $relname; |
215 | 215 | $cntr = 1; |
216 | 216 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
217 | - $relname = $orig . '_' . (++ $cntr); |
|
217 | + $relname = $orig.'_'.(++$cntr); |
|
218 | 218 | } |
219 | 219 | $definition->addRelation( |
220 | 220 | new TableRelation( |
@@ -230,8 +230,8 @@ discard block |
||
230 | 230 | // assuming current table is linked to "one" record in the referenced table |
231 | 231 | // resulting in a "belongsTo" relationship |
232 | 232 | $relations = []; |
233 | - foreach (Collection::from($relationsT[$schema . '.' . $table] ?? []) |
|
234 | - ->map(function ($v) { |
|
233 | + foreach (Collection::from($relationsT[$schema.'.'.$table] ?? []) |
|
234 | + ->map(function($v) { |
|
235 | 235 | $new = []; |
236 | 236 | foreach ($v as $kk => $vv) { |
237 | 237 | $new[strtoupper($kk)] = $vv; |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | return $new; |
240 | 240 | }) as $relation |
241 | 241 | ) { |
242 | - $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_SCHEMA'] . '.' . $relation['REFERENCED_TABLE_NAME']; |
|
242 | + $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_SCHEMA'].'.'.$relation['REFERENCED_TABLE_NAME']; |
|
243 | 243 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$relation['COLUMN_NAME']] = |
244 | 244 | $relation['REFERENCED_COLUMN_NAME']; |
245 | 245 | } |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | $orig = $relname; |
253 | 253 | $cntr = 1; |
254 | 254 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
255 | - $relname = $orig . '_' . (++ $cntr); |
|
255 | + $relname = $orig.'_'.(++$cntr); |
|
256 | 256 | } |
257 | 257 | $definition->addRelation( |
258 | 258 | new TableRelation( |
@@ -273,18 +273,18 @@ discard block |
||
273 | 273 | "SELECT table_name FROM information_schema.tables where table_schema = ?", |
274 | 274 | [$this->connection['opts']['schema'] ?? $this->connection['name']] |
275 | 275 | )) |
276 | - ->map(function ($v) { |
|
276 | + ->map(function($v) { |
|
277 | 277 | $new = []; |
278 | 278 | foreach ($v as $kk => $vv) { |
279 | 279 | $new[strtoupper($kk)] = $vv; |
280 | 280 | } |
281 | 281 | return $new; |
282 | 282 | }) |
283 | - ->mapKey(function ($v) { |
|
283 | + ->mapKey(function($v) { |
|
284 | 284 | return $v['TABLE_NAME']; |
285 | 285 | }) |
286 | 286 | ->pluck('TABLE_NAME') |
287 | - ->map(function ($v) { |
|
287 | + ->map(function($v) { |
|
288 | 288 | return $this->table($v); |
289 | 289 | }) |
290 | 290 | ->toArray(); |
@@ -29,8 +29,8 @@ discard block |
||
29 | 29 | $table = $temp[1]; |
30 | 30 | } |
31 | 31 | |
32 | - if (isset($tables[$schema . '.' . $table])) { |
|
33 | - return $tables[$schema . '.' . $table]; |
|
32 | + if (isset($tables[$schema.'.'.$table])) { |
|
33 | + return $tables[$schema.'.'.$table]; |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | static $relationsT = null; |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | (kc.table_schema = ? OR ct.table_schema = ?) AND |
62 | 62 | kc.table_name IS NOT NULL AND |
63 | 63 | kc.position_in_unique_constraint IS NOT NULL", |
64 | - [ $catalog, $main, $main ] |
|
64 | + [$catalog, $main, $main] |
|
65 | 65 | ) |
66 | 66 | )->toArray(); |
67 | 67 | foreach ($col as $row) { |
@@ -71,8 +71,8 @@ discard block |
||
71 | 71 | if ($row['referenced_table_schema'] !== $main) { |
72 | 72 | $additional[] = $row['referenced_table_schema']; |
73 | 73 | } |
74 | - $relationsT[$row['table_schema'] . '.' . $row['table_name']][] = $row; |
|
75 | - $relationsR[$row['referenced_table_schema'] . '.' . $row['referenced_table_name']][] = $row; |
|
74 | + $relationsT[$row['table_schema'].'.'.$row['table_name']][] = $row; |
|
75 | + $relationsR[$row['referenced_table_schema'].'.'.$row['referenced_table_name']][] = $row; |
|
76 | 76 | } |
77 | 77 | foreach (array_filter(array_unique($additional)) as $s) { |
78 | 78 | $col = Collection::from( |
@@ -97,12 +97,12 @@ discard block |
||
97 | 97 | (kc.table_schema = ? AND ct.table_schema = ?) AND |
98 | 98 | kc.table_name IS NOT NULL AND |
99 | 99 | kc.position_in_unique_constraint IS NOT NULL", |
100 | - [ $catalog, $s, $s ] |
|
100 | + [$catalog, $s, $s] |
|
101 | 101 | ) |
102 | 102 | )->toArray(); |
103 | 103 | foreach ($col as $row) { |
104 | - $relationsT[$row['table_schema'] . '.' . $row['table_name']][] = $row; |
|
105 | - $relationsR[$row['referenced_table_schema'] . '.' . $row['referenced_table_name']][] = $row; |
|
104 | + $relationsT[$row['table_schema'].'.'.$row['table_name']][] = $row; |
|
105 | + $relationsR[$row['referenced_table_schema'].'.'.$row['referenced_table_name']][] = $row; |
|
106 | 106 | } |
107 | 107 | } |
108 | 108 | } |
@@ -110,12 +110,12 @@ discard block |
||
110 | 110 | $columns = Collection::from($this |
111 | 111 | ->query( |
112 | 112 | "SELECT * FROM information_schema.columns WHERE table_name = ? AND table_schema = ? AND table_catalog = ?", |
113 | - [ $table, $schema, $catalog ] |
|
113 | + [$table, $schema, $catalog] |
|
114 | 114 | )) |
115 | - ->mapKey(function ($v) { |
|
115 | + ->mapKey(function($v) { |
|
116 | 116 | return $v['column_name']; |
117 | 117 | }) |
118 | - ->map(function ($v) { |
|
118 | + ->map(function($v) { |
|
119 | 119 | $v['length'] = null; |
120 | 120 | if (!isset($v['data_type'])) { |
121 | 121 | return $v; |
@@ -123,20 +123,20 @@ discard block |
||
123 | 123 | switch ($v['data_type']) { |
124 | 124 | case 'character': |
125 | 125 | case 'character varying': |
126 | - $v['length'] = (int)$v['character_maximum_length']; |
|
126 | + $v['length'] = (int) $v['character_maximum_length']; |
|
127 | 127 | break; |
128 | 128 | } |
129 | 129 | return $v; |
130 | 130 | }) |
131 | 131 | ->toArray(); |
132 | 132 | if (!count($columns)) { |
133 | - throw new DBException('Table not found by name: ' . implode('.', [$schema,$table])); |
|
133 | + throw new DBException('Table not found by name: '.implode('.', [$schema, $table])); |
|
134 | 134 | } |
135 | 135 | $pkname = Collection::from($this |
136 | 136 | ->query( |
137 | 137 | "SELECT constraint_name FROM information_schema.table_constraints |
138 | 138 | WHERE table_name = ? AND constraint_type = ? AND table_schema = ? AND table_catalog = ?", |
139 | - [ $table, 'PRIMARY KEY', $schema, $catalog ] |
|
139 | + [$table, 'PRIMARY KEY', $schema, $catalog] |
|
140 | 140 | )) |
141 | 141 | ->pluck('constraint_name') |
142 | 142 | ->value(); |
@@ -146,12 +146,12 @@ discard block |
||
146 | 146 | ->query( |
147 | 147 | "SELECT column_name FROM information_schema.constraint_column_usage |
148 | 148 | WHERE table_name = ? AND constraint_name = ? AND table_schema = ? AND table_catalog = ?", |
149 | - [ $table, $pkname, $schema, $catalog ] |
|
149 | + [$table, $pkname, $schema, $catalog] |
|
150 | 150 | )) |
151 | 151 | ->pluck('column_name') |
152 | 152 | ->toArray(); |
153 | 153 | } |
154 | - $tables[$schema . '.' .$table] = $definition = (new Table($table, $schema)) |
|
154 | + $tables[$schema.'.'.$table] = $definition = (new Table($table, $schema)) |
|
155 | 155 | ->addColumns($columns) |
156 | 156 | ->setPrimaryKey($primary) |
157 | 157 | ->setComment(''); |
@@ -161,8 +161,8 @@ discard block |
||
161 | 161 | // assuming current table is on the "one" end having "many" records in the referencing table |
162 | 162 | // resulting in a "hasMany" or "manyToMany" relationship (if a pivot table is detected) |
163 | 163 | $relations = []; |
164 | - foreach ($relationsR[$schema . '.' . $table] ?? [] as $relation) { |
|
165 | - $relations[$relation['constraint_name']]['table'] = $relation['table_schema'] . '.' . $relation['table_name']; |
|
164 | + foreach ($relationsR[$schema.'.'.$table] ?? [] as $relation) { |
|
165 | + $relations[$relation['constraint_name']]['table'] = $relation['table_schema'].'.'.$relation['table_name']; |
|
166 | 166 | $relations[$relation['constraint_name']]['keymap'][$relation['referenced_column_name']] = |
167 | 167 | $relation['column_name']; |
168 | 168 | } |
@@ -178,11 +178,11 @@ discard block |
||
178 | 178 | $usedcol = []; |
179 | 179 | if (count($columns)) { |
180 | 180 | foreach (Collection::from($relationsT[$data['table']] ?? []) |
181 | - ->filter(function ($v) use ($columns) { |
|
181 | + ->filter(function($v) use ($columns) { |
|
182 | 182 | return in_array($v['column_name'], $columns); |
183 | 183 | }) as $relation |
184 | 184 | ) { |
185 | - $foreign[$relation['constraint_name']]['table'] = $relation['referenced_table_schema'] . '.' . $relation['referenced_table_name']; |
|
185 | + $foreign[$relation['constraint_name']]['table'] = $relation['referenced_table_schema'].'.'.$relation['referenced_table_name']; |
|
186 | 186 | $foreign[$relation['constraint_name']]['keymap'][$relation['column_name']] = |
187 | 187 | $relation['referenced_column_name']; |
188 | 188 | $usedcol[] = $relation['column_name']; |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | $orig = $relname; |
199 | 199 | $cntr = 1; |
200 | 200 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
201 | - $relname = $orig . '_' . (++ $cntr); |
|
201 | + $relname = $orig.'_'.(++$cntr); |
|
202 | 202 | } |
203 | 203 | $definition->addRelation( |
204 | 204 | new TableRelation( |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | $orig = $relname; |
220 | 220 | $cntr = 1; |
221 | 221 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
222 | - $relname = $orig . '_' . (++ $cntr); |
|
222 | + $relname = $orig.'_'.(++$cntr); |
|
223 | 223 | } |
224 | 224 | $definition->addRelation( |
225 | 225 | new TableRelation( |
@@ -235,8 +235,8 @@ discard block |
||
235 | 235 | // assuming current table is linked to "one" record in the referenced table |
236 | 236 | // resulting in a "belongsTo" relationship |
237 | 237 | $relations = []; |
238 | - foreach ($relationsT[$schema . '.' . $table] ?? [] as $relation) { |
|
239 | - $relations[$relation['constraint_name']]['table'] = $relation['referenced_table_schema'] . '.' . $relation['referenced_table_name']; |
|
238 | + foreach ($relationsT[$schema.'.'.$table] ?? [] as $relation) { |
|
239 | + $relations[$relation['constraint_name']]['table'] = $relation['referenced_table_schema'].'.'.$relation['referenced_table_name']; |
|
240 | 240 | $relations[$relation['constraint_name']]['keymap'][$relation['column_name']] = |
241 | 241 | $relation['referenced_column_name']; |
242 | 242 | } |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | $orig = $relname; |
250 | 250 | $cntr = 1; |
251 | 251 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
252 | - $relname = $orig . '_' . (++ $cntr); |
|
252 | + $relname = $orig.'_'.(++$cntr); |
|
253 | 253 | } |
254 | 254 | $definition->addRelation( |
255 | 255 | new TableRelation( |
@@ -268,13 +268,13 @@ discard block |
||
268 | 268 | return Collection::from($this |
269 | 269 | ->query( |
270 | 270 | "SELECT table_name FROM information_schema.tables where table_schema = ? AND table_catalog = ?", |
271 | - [ $this->connection['opts']['schema'] ?? 'public', $this->connection['name'] ] |
|
271 | + [$this->connection['opts']['schema'] ?? 'public', $this->connection['name']] |
|
272 | 272 | )) |
273 | - ->mapKey(function ($v) { |
|
273 | + ->mapKey(function($v) { |
|
274 | 274 | return $v['table_name']; |
275 | 275 | }) |
276 | 276 | ->pluck('table_name') |
277 | - ->map(function ($v) { |
|
277 | + ->map(function($v) { |
|
278 | 278 | return $this->table($v); |
279 | 279 | }) |
280 | 280 | ->toArray(); |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | public function setPrimaryKey($column) : Table |
83 | 83 | { |
84 | 84 | if (!is_array($column)) { |
85 | - $column = [ $column ]; |
|
85 | + $column = [$column]; |
|
86 | 86 | } |
87 | 87 | $this->data['primary'] = array_values($column); |
88 | 88 | return $this; |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | */ |
110 | 110 | public function getFullName() |
111 | 111 | { |
112 | - return ($this->data['schema'] ? $this->data['schema'] . '.' : '') . $this->data['name']; |
|
112 | + return ($this->data['schema'] ? $this->data['schema'].'.' : '').$this->data['name']; |
|
113 | 113 | } |
114 | 114 | /** |
115 | 115 | * Get a column definition |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | } |
185 | 185 | |
186 | 186 | if (!isset($name)) { |
187 | - $name = $toTable->getName() . '_' . implode('_', array_keys($keymap)); |
|
187 | + $name = $toTable->getName().'_'.implode('_', array_keys($keymap)); |
|
188 | 188 | } |
189 | 189 | $this->addRelation(new TableRelation( |
190 | 190 | $name, |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | $fields = []; |
77 | 77 | $exists = false; |
78 | 78 | foreach ($relation->table->getColumns() as $column) { |
79 | - $nm = $name . static::SEP . $column; |
|
79 | + $nm = $name.static::SEP.$column; |
|
80 | 80 | if (isset($this->aliases[$nm])) { |
81 | 81 | $nm = $this->aliases[$nm]; |
82 | 82 | } |
@@ -90,17 +90,17 @@ discard block |
||
90 | 90 | $parts = explode(static::SEP, $name); |
91 | 91 | $name = array_pop($parts); |
92 | 92 | if (!$exists && !count($parts) && !isset($temp[$name])) { |
93 | - $temp[$name] = $relation->many ? [ '___clean' => true ] : null; |
|
93 | + $temp[$name] = $relation->many ? ['___clean' => true] : null; |
|
94 | 94 | } |
95 | 95 | if ($exists) { |
96 | - $full = ''; |
|
96 | + $full = ''; |
|
97 | 97 | foreach ($parts as $item) { |
98 | - $full = $full ? $full . static::SEP . $item : $item; |
|
98 | + $full = $full ? $full.static::SEP.$item : $item; |
|
99 | 99 | $temp = &$temp[$item]; |
100 | 100 | if ($this->relations[$full][0]->many) { |
101 | 101 | $rpk = []; |
102 | 102 | foreach ($this->relations[$full][0]->table->getPrimaryKey() as $pkey) { |
103 | - $nm = $full . static::SEP . $pkey; |
|
103 | + $nm = $full.static::SEP.$pkey; |
|
104 | 104 | if (isset($this->aliases[$nm])) { |
105 | 105 | $nm = $this->aliases[$nm]; |
106 | 106 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | } |
111 | 111 | } |
112 | 112 | if (!isset($temp[$name])) { |
113 | - $temp[$name] = $relation->many ? [ '___clean' => true ] : null; |
|
113 | + $temp[$name] = $relation->many ? ['___clean' => true] : null; |
|
114 | 114 | } |
115 | 115 | $temp = &$temp[$name]; |
116 | 116 | if ($relation->many) { |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | return; |
177 | 177 | } |
178 | 178 | } |
179 | - $this->fetched ++; |
|
179 | + $this->fetched++; |
|
180 | 180 | while ($this->result->valid()) { |
181 | 181 | $row = $this->result->current(); |
182 | 182 | $pk = []; |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | /** |
45 | 45 | * @var int[] |
46 | 46 | */ |
47 | - protected $li_of = [0,0,0]; |
|
47 | + protected $li_of = [0, 0, 0]; |
|
48 | 48 | /** |
49 | 49 | * @var array |
50 | 50 | */ |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | public function __construct(DBInterface $db, $table) |
75 | 75 | { |
76 | 76 | $this->db = $db; |
77 | - $this->definition = $table instanceof Table ? $table : $this->db->definition((string)$table); |
|
77 | + $this->definition = $table instanceof Table ? $table : $this->db->definition((string) $table); |
|
78 | 78 | $primary = $this->definition->getPrimaryKey(); |
79 | 79 | $columns = $this->definition->getColumns(); |
80 | 80 | $this->pkey = count($primary) ? $primary : $columns; |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | { |
98 | 98 | $column = explode('.', $column); |
99 | 99 | if (count($column) === 1) { |
100 | - $column = [ $this->definition->getFullName(), $column[0] ]; |
|
100 | + $column = [$this->definition->getFullName(), $column[0]]; |
|
101 | 101 | $col = $this->definition->getColumn($column[1]); |
102 | 102 | if (!$col) { |
103 | 103 | throw new DBException('Invalid column name in own table'); |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | throw new DBException('Invalid column name in related table'); |
121 | 121 | } |
122 | 122 | } else { |
123 | - throw new DBException('Invalid foreign table name: ' . implode(',', $column)); |
|
123 | + throw new DBException('Invalid foreign table name: '.implode(',', $column)); |
|
124 | 124 | } |
125 | 125 | } |
126 | 126 | } else { |
@@ -128,26 +128,26 @@ discard block |
||
128 | 128 | if ($this->definition->hasRelation(implode('.', $column))) { |
129 | 129 | $this->with(implode('.', $column), false); |
130 | 130 | $col = $this->definition->getRelation(implode('.', $column))->table->getColumn($name); |
131 | - $column = [ implode('.', $column), $name ]; |
|
131 | + $column = [implode('.', $column), $name]; |
|
132 | 132 | } else { |
133 | 133 | $this->with(implode('.', $column), false); |
134 | 134 | $table = $this->definition; |
135 | 135 | $table = array_reduce( |
136 | 136 | $column, |
137 | - function ($carry, $item) use (&$table) { |
|
137 | + function($carry, $item) use (&$table) { |
|
138 | 138 | $table = $table->getRelation($item)->table; |
139 | 139 | return $table; |
140 | 140 | } |
141 | 141 | ); |
142 | 142 | $col = $table->getColumn($name); |
143 | - $column = [ implode(static::SEP, $column), $name ]; |
|
143 | + $column = [implode(static::SEP, $column), $name]; |
|
144 | 144 | } |
145 | 145 | } |
146 | - return [ 'name' => implode('.', $column), 'data' => $col ]; |
|
146 | + return ['name' => implode('.', $column), 'data' => $col]; |
|
147 | 147 | } |
148 | 148 | protected function normalizeValue(TableColumn $col, $value) |
149 | 149 | { |
150 | - $strict = (int)$this->db->driverOption('strict', 0) > 0; |
|
150 | + $strict = (int) $this->db->driverOption('strict', 0) > 0; |
|
151 | 151 | if ($value === null && $col->isNullable()) { |
152 | 152 | return null; |
153 | 153 | } |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | $temp = strtotime($value); |
158 | 158 | if (!$temp) { |
159 | 159 | if ($strict) { |
160 | - throw new DBException('Invalid value for date column ' . $col->getName()); |
|
160 | + throw new DBException('Invalid value for date column '.$col->getName()); |
|
161 | 161 | } |
162 | 162 | return null; |
163 | 163 | } |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | return $value->format('Y-m-d'); |
171 | 171 | } |
172 | 172 | if ($strict) { |
173 | - throw new DBException('Invalid value (unknown data type) for date column ' . $col->getName()); |
|
173 | + throw new DBException('Invalid value (unknown data type) for date column '.$col->getName()); |
|
174 | 174 | } |
175 | 175 | return $value; |
176 | 176 | case 'datetime': |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | $temp = strtotime($value); |
179 | 179 | if (!$temp) { |
180 | 180 | if ($strict) { |
181 | - throw new DBException('Invalid value for datetime column ' . $col->getName()); |
|
181 | + throw new DBException('Invalid value for datetime column '.$col->getName()); |
|
182 | 182 | } |
183 | 183 | return null; |
184 | 184 | } |
@@ -191,7 +191,7 @@ discard block |
||
191 | 191 | return $value->format('Y-m-d H:i:s'); |
192 | 192 | } |
193 | 193 | if ($strict) { |
194 | - throw new DBException('Invalid value (unknown data type) for datetime column ' . $col->getName()); |
|
194 | + throw new DBException('Invalid value (unknown data type) for datetime column '.$col->getName()); |
|
195 | 195 | } |
196 | 196 | return $value; |
197 | 197 | case 'enum': |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | if (is_int($value)) { |
200 | 200 | if (!isset($values[$value])) { |
201 | 201 | if ($strict) { |
202 | - throw new DBException('Invalid value (using integer) for enum ' . $col->getName()); |
|
202 | + throw new DBException('Invalid value (using integer) for enum '.$col->getName()); |
|
203 | 203 | } |
204 | 204 | return $value; |
205 | 205 | } |
@@ -207,23 +207,23 @@ discard block |
||
207 | 207 | } |
208 | 208 | if (!in_array($value, $col->getValues())) { |
209 | 209 | if ($strict) { |
210 | - throw new DBException('Invalid value for enum ' . $col->getName()); |
|
210 | + throw new DBException('Invalid value for enum '.$col->getName()); |
|
211 | 211 | } |
212 | 212 | return 0; |
213 | 213 | } |
214 | 214 | return $value; |
215 | 215 | case 'int': |
216 | 216 | $temp = preg_replace('([^+\-0-9]+)', '', $value); |
217 | - return is_string($temp) ? (int)$temp : 0; |
|
217 | + return is_string($temp) ? (int) $temp : 0; |
|
218 | 218 | case 'float': |
219 | 219 | $temp = preg_replace('([^+\-0-9.]+)', '', str_replace(',', '.', $value)); |
220 | - return is_string($temp) ? (float)$temp : 0; |
|
220 | + return is_string($temp) ? (float) $temp : 0; |
|
221 | 221 | case 'text': |
222 | 222 | // check using strlen first, in order to avoid hitting mb_ functions which might be polyfilled |
223 | 223 | // because the polyfill is quite slow |
224 | 224 | if ($col->hasLength() && strlen($value) > $col->getLength() && mb_strlen($value) > $col->getLength()) { |
225 | 225 | if ($strict) { |
226 | - throw new DBException('Invalid value for text column ' . $col->getName()); |
|
226 | + throw new DBException('Invalid value for text column '.$col->getName()); |
|
227 | 227 | } |
228 | 228 | return mb_substr($value, 0, $col->getLength()); |
229 | 229 | } |
@@ -245,35 +245,32 @@ discard block |
||
245 | 245 | // str_replace(['%', '_'], ['\\%','\\_'], $q) |
246 | 246 | return $negate ? |
247 | 247 | [ |
248 | - $name . ' NOT LIKE ?', |
|
249 | - [ $this->normalizeValue($column, $value) ] |
|
250 | - ] : |
|
251 | - [ |
|
252 | - $name . ' LIKE ?', |
|
253 | - [ $this->normalizeValue($column, $value) ] |
|
248 | + $name.' NOT LIKE ?', |
|
249 | + [$this->normalizeValue($column, $value)] |
|
250 | + ] : [ |
|
251 | + $name.' LIKE ?', |
|
252 | + [$this->normalizeValue($column, $value)] |
|
254 | 253 | ]; |
255 | 254 | } |
256 | 255 | if (is_null($value)) { |
257 | 256 | return $negate ? |
258 | - [ $name . ' IS NOT NULL', [] ]: |
|
259 | - [ $name . ' IS NULL', [] ]; |
|
257 | + [$name.' IS NOT NULL', []] : [$name.' IS NULL', []]; |
|
260 | 258 | } |
261 | 259 | if (!is_array($value)) { |
262 | 260 | return $negate ? |
263 | 261 | [ |
264 | - $name . ' <> ?', |
|
265 | - [ $this->normalizeValue($column, $value) ] |
|
266 | - ] : |
|
267 | - [ |
|
268 | - $name . ' = ?', |
|
269 | - [ $this->normalizeValue($column, $value) ] |
|
262 | + $name.' <> ?', |
|
263 | + [$this->normalizeValue($column, $value)] |
|
264 | + ] : [ |
|
265 | + $name.' = ?', |
|
266 | + [$this->normalizeValue($column, $value)] |
|
270 | 267 | ]; |
271 | 268 | } |
272 | 269 | if (isset($value['beg']) && strlen($value['beg']) && (!isset($value['end']) || !strlen($value['end']))) { |
273 | - $value = [ 'gte' => $value['beg'] ]; |
|
270 | + $value = ['gte' => $value['beg']]; |
|
274 | 271 | } |
275 | 272 | if (isset($value['end']) && strlen($value['end']) && (!isset($value['beg']) || !strlen($value['beg']))) { |
276 | - $value = [ 'lte' => $value['end'] ]; |
|
273 | + $value = ['lte' => $value['end']]; |
|
277 | 274 | } |
278 | 275 | if (isset($value['beg']) && isset($value['end'])) { |
279 | 276 | return $negate ? |
@@ -283,8 +280,7 @@ discard block |
||
283 | 280 | $this->normalizeValue($column, $value['beg']), |
284 | 281 | $this->normalizeValue($column, $value['end']) |
285 | 282 | ] |
286 | - ] : |
|
287 | - [ |
|
283 | + ] : [ |
|
288 | 284 | $name.' BETWEEN ? AND ?', |
289 | 285 | [ |
290 | 286 | $this->normalizeValue($column, $value['beg']), |
@@ -296,42 +292,42 @@ discard block |
||
296 | 292 | $sql = []; |
297 | 293 | $par = []; |
298 | 294 | if (isset($value['gt'])) { |
299 | - $sql[] = $name. ' ' . ($negate ? '<=' : '>') . ' ?'; |
|
295 | + $sql[] = $name.' '.($negate ? '<=' : '>').' ?'; |
|
300 | 296 | $par[] = $this->normalizeValue($column, $value['gt']); |
301 | 297 | } |
302 | 298 | if (isset($value['gte'])) { |
303 | - $sql[] = $name. ' ' . ($negate ? '<' : '>=') . ' ?'; |
|
299 | + $sql[] = $name.' '.($negate ? '<' : '>=').' ?'; |
|
304 | 300 | $par[] = $this->normalizeValue($column, $value['gte']); |
305 | 301 | } |
306 | 302 | if (isset($value['lt'])) { |
307 | - $sql[] = $name. ' ' . ($negate ? '>=' : '<') . ' ?'; |
|
303 | + $sql[] = $name.' '.($negate ? '>=' : '<').' ?'; |
|
308 | 304 | $par[] = $this->normalizeValue($column, $value['lt']); |
309 | 305 | } |
310 | 306 | if (isset($value['lte'])) { |
311 | - $sql[] = $name. ' ' . ($negate ? '>' : '<=') . ' ?'; |
|
307 | + $sql[] = $name.' '.($negate ? '>' : '<=').' ?'; |
|
312 | 308 | $par[] = $this->normalizeValue($column, $value['lte']); |
313 | 309 | } |
314 | 310 | return [ |
315 | - '(' . implode(' AND ', $sql) . ')', |
|
311 | + '('.implode(' AND ', $sql).')', |
|
316 | 312 | $par |
317 | 313 | ]; |
318 | 314 | } |
319 | 315 | |
320 | - $value = array_values(array_map(function ($v) use ($column) { |
|
316 | + $value = array_values(array_map(function($v) use ($column) { |
|
321 | 317 | return $this->normalizeValue($column, $v); |
322 | 318 | }, $value)); |
323 | 319 | if ($this->db->driverName() === 'oracle') { |
324 | 320 | $sql = []; |
325 | 321 | $par = []; |
326 | 322 | for ($i = 0; $i < count($value); $i += 500) { |
327 | - $sql[] = $negate ? $name . ' NOT IN (??)' : $name . ' IN (??)'; |
|
323 | + $sql[] = $negate ? $name.' NOT IN (??)' : $name.' IN (??)'; |
|
328 | 324 | $par[] = array_slice($value, $i, 500); |
329 | 325 | } |
330 | - $sql = '(' . implode($negate ? ' AND ' : ' OR ', $sql) . ')'; |
|
331 | - return [ $sql, [$par] ]; |
|
326 | + $sql = '('.implode($negate ? ' AND ' : ' OR ', $sql).')'; |
|
327 | + return [$sql, [$par]]; |
|
332 | 328 | } |
333 | 329 | return [ |
334 | - $negate ? $name . ' NOT IN (??)' : $name . ' IN (??)', |
|
330 | + $negate ? $name.' NOT IN (??)' : $name.' IN (??)', |
|
335 | 331 | [$value] |
336 | 332 | ]; |
337 | 333 | } |
@@ -363,7 +359,7 @@ discard block |
||
363 | 359 | $par = array_merge($par, $temp[1]); |
364 | 360 | } |
365 | 361 | } |
366 | - return $this->where('(' . implode(' OR ', $sql) . ')', $par); |
|
362 | + return $this->where('('.implode(' OR ', $sql).')', $par); |
|
367 | 363 | } |
368 | 364 | /** |
369 | 365 | * Filter the results matching all of the criteria |
@@ -381,7 +377,7 @@ discard block |
||
381 | 377 | $par = array_merge($par, $temp[1]); |
382 | 378 | } |
383 | 379 | } |
384 | - return $this->where('(' . implode(' AND ', $sql) . ')', $par); |
|
380 | + return $this->where('('.implode(' AND ', $sql).')', $par); |
|
385 | 381 | } |
386 | 382 | /** |
387 | 383 | * Sort by a column |
@@ -391,7 +387,7 @@ discard block |
||
391 | 387 | */ |
392 | 388 | public function sort(string $column, bool $desc = false) : self |
393 | 389 | { |
394 | - return $this->order($this->getColumn($column)['name'] . ' ' . ($desc ? 'DESC' : 'ASC')); |
|
390 | + return $this->order($this->getColumn($column)['name'].' '.($desc ? 'DESC' : 'ASC')); |
|
395 | 391 | } |
396 | 392 | /** |
397 | 393 | * Group by a column (or columns) |
@@ -401,7 +397,7 @@ discard block |
||
401 | 397 | public function group($column) : self |
402 | 398 | { |
403 | 399 | if (!is_array($column)) { |
404 | - $column = [ $column ]; |
|
400 | + $column = [$column]; |
|
405 | 401 | } |
406 | 402 | foreach ($column as $k => $v) { |
407 | 403 | $column[$k] = $this->getColumn($v)['name']; |
@@ -443,7 +439,7 @@ discard block |
||
443 | 439 | $this->order = []; |
444 | 440 | $this->having = []; |
445 | 441 | $this->aliases = []; |
446 | - $this->li_of = [0,0,0]; |
|
442 | + $this->li_of = [0, 0, 0]; |
|
447 | 443 | $this->qiterator = null; |
448 | 444 | return $this; |
449 | 445 | } |
@@ -456,7 +452,7 @@ discard block |
||
456 | 452 | public function groupBy(string $sql, array $params = []) : self |
457 | 453 | { |
458 | 454 | $this->qiterator = null; |
459 | - $this->group = [ $sql, $params ]; |
|
455 | + $this->group = [$sql, $params]; |
|
460 | 456 | return $this; |
461 | 457 | } |
462 | 458 | /** |
@@ -469,7 +465,7 @@ discard block |
||
469 | 465 | */ |
470 | 466 | public function join($table, array $fields, string $name = null, bool $multiple = true) |
471 | 467 | { |
472 | - $table = $table instanceof Table ? $table : $this->db->definition((string)$table); |
|
468 | + $table = $table instanceof Table ? $table : $this->db->definition((string) $table); |
|
473 | 469 | $name = $name ?? $table->getName(); |
474 | 470 | if (isset($this->joins[$name]) || $this->definition->hasRelation($name)) { |
475 | 471 | throw new DBException('Alias / table name already in use'); |
@@ -478,7 +474,7 @@ discard block |
||
478 | 474 | foreach ($fields as $k => $v) { |
479 | 475 | $k = explode('.', $k, 2); |
480 | 476 | $k = count($k) == 2 ? $k[1] : $k[0]; |
481 | - $this->joins[$name]->keymap[$this->getColumn($name . '.' . $k)['name']] = $this->getColumn($v)['name']; |
|
477 | + $this->joins[$name]->keymap[$this->getColumn($name.'.'.$k)['name']] = $this->getColumn($v)['name']; |
|
482 | 478 | } |
483 | 479 | return $this; |
484 | 480 | } |
@@ -491,7 +487,7 @@ discard block |
||
491 | 487 | public function where(string $sql, array $params = []) : self |
492 | 488 | { |
493 | 489 | $this->qiterator = null; |
494 | - $this->where[] = [ $sql, $params ]; |
|
490 | + $this->where[] = [$sql, $params]; |
|
495 | 491 | return $this; |
496 | 492 | } |
497 | 493 | /** |
@@ -503,7 +499,7 @@ discard block |
||
503 | 499 | public function having(string $sql, array $params = []) : self |
504 | 500 | { |
505 | 501 | $this->qiterator = null; |
506 | - $this->having[] = [ $sql, $params ]; |
|
502 | + $this->having[] = [$sql, $params]; |
|
507 | 503 | return $this; |
508 | 504 | } |
509 | 505 | /** |
@@ -527,7 +523,7 @@ discard block |
||
527 | 523 | $name = null; |
528 | 524 | } |
529 | 525 | } |
530 | - $this->order = [ $sql, $params, $name ]; |
|
526 | + $this->order = [$sql, $params, $name]; |
|
531 | 527 | return $this; |
532 | 528 | } |
533 | 529 | /** |
@@ -539,7 +535,7 @@ discard block |
||
539 | 535 | public function limit(int $limit, int $offset = 0, bool $limitOnMainTable = false) : self |
540 | 536 | { |
541 | 537 | $this->qiterator = null; |
542 | - $this->li_of = [ $limit, $offset, $limitOnMainTable ? 1 : 0 ]; |
|
538 | + $this->li_of = [$limit, $offset, $limitOnMainTable ? 1 : 0]; |
|
543 | 539 | return $this; |
544 | 540 | } |
545 | 541 | /** |
@@ -549,9 +545,9 @@ discard block |
||
549 | 545 | public function count() : int |
550 | 546 | { |
551 | 547 | $aliases = []; |
552 | - $getAlias = function ($name) use (&$aliases) { |
|
548 | + $getAlias = function($name) use (&$aliases) { |
|
553 | 549 | // to bypass use: return $name; |
554 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
550 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
555 | 551 | }; |
556 | 552 | $table = $this->definition->getFullName(); |
557 | 553 | $sql = 'SELECT COUNT(DISTINCT '.$table.'.'.implode(', '.$table.'.', $this->pkey).') FROM '.$table.' '; |
@@ -565,35 +561,35 @@ discard block |
||
565 | 561 | $h = $this->having; |
566 | 562 | $o = $this->order; |
567 | 563 | $g = $this->group; |
568 | - $j = array_map(function ($v) { |
|
564 | + $j = array_map(function($v) { |
|
569 | 565 | return clone $v; |
570 | 566 | }, $this->joins); |
571 | 567 | foreach ($this->definition->getRelations() as $k => $v) { |
572 | 568 | foreach ($w as $kk => $vv) { |
573 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
574 | - $relations[$k] = [ $v, $table ]; |
|
575 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
569 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
570 | + $relations[$k] = [$v, $table]; |
|
571 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
576 | 572 | } |
577 | 573 | } |
578 | - if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) { |
|
579 | - $relations[$k] = [ $v, $table ]; |
|
574 | + if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) { |
|
575 | + $relations[$k] = [$v, $table]; |
|
580 | 576 | } |
581 | 577 | foreach ($h as $kk => $vv) { |
582 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
583 | - $relations[$k] = [ $v, $table ]; |
|
584 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
578 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
579 | + $relations[$k] = [$v, $table]; |
|
580 | + $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
585 | 581 | } |
586 | 582 | } |
587 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
588 | - $relations[$k] = [ $v, $table ]; |
|
589 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
583 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
584 | + $relations[$k] = [$v, $table]; |
|
585 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
590 | 586 | } |
591 | 587 | foreach ($j as $kk => $vv) { |
592 | 588 | foreach ($vv->keymap as $kkk => $vvv) { |
593 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) { |
|
594 | - $relations[$k] = [ $v, $table ]; |
|
589 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) { |
|
590 | + $relations[$k] = [$v, $table]; |
|
595 | 591 | $j[$kk]->keymap[$kkk] = |
596 | - preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv); |
|
592 | + preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv); |
|
597 | 593 | } |
598 | 594 | } |
599 | 595 | } |
@@ -609,13 +605,13 @@ discard block |
||
609 | 605 | foreach ($v->keymap as $kk => $vv) { |
610 | 606 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
611 | 607 | } |
612 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
608 | + $sql .= implode(' AND ', $tmp).' '; |
|
613 | 609 | $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($k).' ON '; |
614 | 610 | $tmp = []; |
615 | 611 | foreach ($v->pivot_keymap as $kk => $vv) { |
616 | 612 | $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
617 | 613 | } |
618 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
614 | + $sql .= implode(' AND ', $tmp).' '; |
|
619 | 615 | } else { |
620 | 616 | $alias = $getAlias($k); |
621 | 617 | $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$alias.' ON '; |
@@ -624,38 +620,38 @@ discard block |
||
624 | 620 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
625 | 621 | } |
626 | 622 | if ($v->sql) { |
627 | - $tmp[] = $v->sql . ' '; |
|
623 | + $tmp[] = $v->sql.' '; |
|
628 | 624 | $par = array_merge($par, $v->par ?? []); |
629 | 625 | } |
630 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
626 | + $sql .= implode(' AND ', $tmp).' '; |
|
631 | 627 | } |
632 | 628 | } |
633 | 629 | foreach ($j as $k => $v) { |
634 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getFullName().' '.$k.' ON '; |
|
630 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getFullName().' '.$k.' ON '; |
|
635 | 631 | $tmp = []; |
636 | 632 | foreach ($v->keymap as $kk => $vv) { |
637 | 633 | $tmp[] = $kk.' = '.$vv; |
638 | 634 | } |
639 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
635 | + $sql .= implode(' AND ', $tmp).' '; |
|
640 | 636 | } |
641 | 637 | if (count($w)) { |
642 | 638 | $sql .= 'WHERE '; |
643 | 639 | $tmp = []; |
644 | 640 | foreach ($w as $v) { |
645 | - $tmp[] = '(' . $v[0] . ')'; |
|
641 | + $tmp[] = '('.$v[0].')'; |
|
646 | 642 | $par = array_merge($par, $v[1]); |
647 | 643 | } |
648 | 644 | $sql .= implode(' AND ', $tmp).' '; |
649 | 645 | } |
650 | 646 | if (count($g)) { |
651 | - $sql .= 'GROUP BY ' . $g[0] . ' '; |
|
647 | + $sql .= 'GROUP BY '.$g[0].' '; |
|
652 | 648 | $par = array_merge($par, $g[1]); |
653 | 649 | } |
654 | 650 | if (count($h)) { |
655 | 651 | $sql .= 'HAVING '; |
656 | 652 | $tmp = []; |
657 | 653 | foreach ($h as $v) { |
658 | - $tmp[] = '(' . $v[0] . ')'; |
|
654 | + $tmp[] = '('.$v[0].')'; |
|
659 | 655 | $par = array_merge($par, $v[1]); |
660 | 656 | } |
661 | 657 | $sql .= implode(' AND ', $tmp).' '; |
@@ -689,7 +685,7 @@ discard block |
||
689 | 685 | $this->with(implode('.', $temp)); |
690 | 686 | $table = array_reduce( |
691 | 687 | $temp, |
692 | - function ($carry, $item) use (&$table) { |
|
688 | + function($carry, $item) use (&$table) { |
|
693 | 689 | return $table->getRelation($item)->table; |
694 | 690 | } |
695 | 691 | ); |
@@ -698,7 +694,7 @@ discard block |
||
698 | 694 | } |
699 | 695 | unset($fields[$k]); |
700 | 696 | foreach ($cols as $col) { |
701 | - $fields[] = $table . '.' . $col; |
|
697 | + $fields[] = $table.'.'.$col; |
|
702 | 698 | } |
703 | 699 | } |
704 | 700 | } |
@@ -732,9 +728,9 @@ discard block |
||
732 | 728 | return $this->qiterator; |
733 | 729 | } |
734 | 730 | $aliases = []; |
735 | - $getAlias = function ($name) use (&$aliases) { |
|
731 | + $getAlias = function($name) use (&$aliases) { |
|
736 | 732 | // to bypass use: return $name; |
737 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
733 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
738 | 734 | }; |
739 | 735 | $table = $this->definition->getName(); |
740 | 736 | if ($fields !== null) { |
@@ -750,7 +746,7 @@ discard block |
||
750 | 746 | $h = $this->having; |
751 | 747 | $o = $this->order; |
752 | 748 | $g = $this->group; |
753 | - $j = array_map(function ($v) { |
|
749 | + $j = array_map(function($v) { |
|
754 | 750 | return clone $v; |
755 | 751 | }, $this->joins); |
756 | 752 | |
@@ -764,32 +760,32 @@ discard block |
||
764 | 760 | continue; |
765 | 761 | } |
766 | 762 | foreach ($f as $kk => $field) { |
767 | - if (strpos($field, $k . '.') === 0) { |
|
768 | - $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field); |
|
763 | + if (strpos($field, $k.'.') === 0) { |
|
764 | + $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field); |
|
769 | 765 | } |
770 | 766 | } |
771 | 767 | foreach ($w as $kk => $v) { |
772 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) { |
|
773 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]); |
|
768 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) { |
|
769 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]); |
|
774 | 770 | } |
775 | 771 | } |
776 | 772 | foreach ($h as $kk => $v) { |
777 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) { |
|
778 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]); |
|
773 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) { |
|
774 | + $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]); |
|
779 | 775 | } |
780 | 776 | } |
781 | - if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) { |
|
782 | - $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]); |
|
777 | + if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) { |
|
778 | + $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]); |
|
783 | 779 | } |
784 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
785 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
780 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
781 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
786 | 782 | } |
787 | 783 | foreach ($j as $kk => $v) { |
788 | 784 | foreach ($v->keymap as $kkk => $vv) { |
789 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) { |
|
785 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) { |
|
790 | 786 | $j[$kk]->keymap[$kkk] = preg_replace( |
791 | - '(\b'.preg_quote($k . '.'). ')i', |
|
792 | - $getAlias($k) . '.', |
|
787 | + '(\b'.preg_quote($k.'.').')i', |
|
788 | + $getAlias($k).'.', |
|
793 | 789 | $vv |
794 | 790 | ); |
795 | 791 | } |
@@ -798,38 +794,38 @@ discard block |
||
798 | 794 | } |
799 | 795 | foreach ($this->definition->getRelations() as $k => $relation) { |
800 | 796 | foreach ($f as $kk => $field) { |
801 | - if (strpos($field, $k . '.') === 0) { |
|
802 | - $relations[$k] = [ $relation, $table ]; |
|
803 | - $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field); |
|
797 | + if (strpos($field, $k.'.') === 0) { |
|
798 | + $relations[$k] = [$relation, $table]; |
|
799 | + $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field); |
|
804 | 800 | } |
805 | 801 | } |
806 | 802 | foreach ($w as $kk => $v) { |
807 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) { |
|
808 | - $relations[$k] = [ $relation, $table ]; |
|
809 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]); |
|
803 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) { |
|
804 | + $relations[$k] = [$relation, $table]; |
|
805 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]); |
|
810 | 806 | } |
811 | 807 | } |
812 | 808 | foreach ($h as $kk => $v) { |
813 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) { |
|
814 | - $relations[$k] = [ $relation, $table ]; |
|
815 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]); |
|
809 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) { |
|
810 | + $relations[$k] = [$relation, $table]; |
|
811 | + $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]); |
|
816 | 812 | } |
817 | 813 | } |
818 | - if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) { |
|
819 | - $relations[$k] = [ $relation, $table ]; |
|
820 | - $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]); |
|
814 | + if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) { |
|
815 | + $relations[$k] = [$relation, $table]; |
|
816 | + $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]); |
|
821 | 817 | } |
822 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
823 | - $relations[$k] = [ $relation, $table ]; |
|
824 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
818 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
819 | + $relations[$k] = [$relation, $table]; |
|
820 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
825 | 821 | } |
826 | 822 | foreach ($j as $kk => $v) { |
827 | 823 | foreach ($v->keymap as $kkk => $vv) { |
828 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) { |
|
829 | - $relations[$k] = [ $relation, $table ]; |
|
824 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) { |
|
825 | + $relations[$k] = [$relation, $table]; |
|
830 | 826 | $j[$kk]->keymap[$kkk] = preg_replace( |
831 | - '(\b'.preg_quote($k . '.'). ')i', |
|
832 | - $getAlias($k) . '.', |
|
827 | + '(\b'.preg_quote($k.'.').')i', |
|
828 | + $getAlias($k).'.', |
|
833 | 829 | $vv |
834 | 830 | ); |
835 | 831 | } |
@@ -838,12 +834,12 @@ discard block |
||
838 | 834 | } |
839 | 835 | $select = []; |
840 | 836 | foreach ($f as $k => $field) { |
841 | - $select[] = $field . (!is_numeric($k) ? ' ' . $k : ''); |
|
837 | + $select[] = $field.(!is_numeric($k) ? ' '.$k : ''); |
|
842 | 838 | } |
843 | 839 | foreach ($this->withr as $name => $relation) { |
844 | 840 | if ($relation[2]) { |
845 | 841 | foreach ($relation[0]->table->getColumns() as $column) { |
846 | - $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column); |
|
842 | + $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column); |
|
847 | 843 | } |
848 | 844 | } |
849 | 845 | } |
@@ -863,13 +859,13 @@ discard block |
||
863 | 859 | foreach ($v->keymap as $kk => $vv) { |
864 | 860 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
865 | 861 | } |
866 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
862 | + $sql .= implode(' AND ', $tmp).' '; |
|
867 | 863 | $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($relation).' ON '; |
868 | 864 | $tmp = []; |
869 | 865 | foreach ($v->pivot_keymap as $kk => $vv) { |
870 | 866 | $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
871 | 867 | } |
872 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
868 | + $sql .= implode(' AND ', $tmp).' '; |
|
873 | 869 | } else { |
874 | 870 | $alias = $getAlias($relation); |
875 | 871 | |
@@ -879,22 +875,22 @@ discard block |
||
879 | 875 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
880 | 876 | } |
881 | 877 | if ($v->sql) { |
882 | - $tmp[] = $v->sql . ' '; |
|
878 | + $tmp[] = $v->sql.' '; |
|
883 | 879 | $par = array_merge($par, $v->par ?? []); |
884 | 880 | } |
885 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
881 | + $sql .= implode(' AND ', $tmp).' '; |
|
886 | 882 | } |
887 | 883 | } |
888 | 884 | foreach ($j as $k => $v) { |
889 | 885 | if ($v->many) { |
890 | 886 | $many = true; |
891 | 887 | } |
892 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
888 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
893 | 889 | $tmp = []; |
894 | 890 | foreach ($v->keymap as $kk => $vv) { |
895 | 891 | $tmp[] = $kk.' = '.$vv; |
896 | 892 | } |
897 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
893 | + $sql .= implode(' AND ', $tmp).' '; |
|
898 | 894 | } |
899 | 895 | if ($many && count($porder) && $this->li_of[2] === 1) { |
900 | 896 | $ids = $this->ids(); |
@@ -902,9 +898,9 @@ discard block |
||
902 | 898 | if (count($porder) > 1) { |
903 | 899 | $pkw = []; |
904 | 900 | foreach ($porder as $name) { |
905 | - $pkw[] = $name . ' = ?'; |
|
901 | + $pkw[] = $name.' = ?'; |
|
906 | 902 | } |
907 | - $pkw = '(' . implode(' AND ', $pkw) . ')'; |
|
903 | + $pkw = '('.implode(' AND ', $pkw).')'; |
|
908 | 904 | $pkp = []; |
909 | 905 | foreach ($ids as $id) { |
910 | 906 | foreach ($id as $p) { |
@@ -916,51 +912,51 @@ discard block |
||
916 | 912 | $pkp |
917 | 913 | ]; |
918 | 914 | } else { |
919 | - $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ]; |
|
915 | + $w[] = [$porder[0].' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids]; |
|
920 | 916 | } |
921 | 917 | } else { |
922 | - $w[] = [ '1=0', [] ]; |
|
918 | + $w[] = ['1=0', []]; |
|
923 | 919 | } |
924 | 920 | } |
925 | 921 | if (count($w)) { |
926 | 922 | $sql .= 'WHERE '; |
927 | 923 | $tmp = []; |
928 | 924 | foreach ($w as $v) { |
929 | - $tmp[] = '(' . $v[0] . ')'; |
|
925 | + $tmp[] = '('.$v[0].')'; |
|
930 | 926 | $par = array_merge($par, $v[1]); |
931 | 927 | } |
932 | 928 | $sql .= implode(' AND ', $tmp).' '; |
933 | 929 | } |
934 | 930 | if (count($g)) { |
935 | - $sql .= 'GROUP BY ' . $g[0] . ' '; |
|
931 | + $sql .= 'GROUP BY '.$g[0].' '; |
|
936 | 932 | $par = array_merge($par, $g[1]); |
937 | 933 | } |
938 | 934 | if (count($h)) { |
939 | 935 | $sql .= 'HAVING '; |
940 | 936 | $tmp = []; |
941 | 937 | foreach ($h as $v) { |
942 | - $tmp[] = '(' . $v[0] . ')'; |
|
938 | + $tmp[] = '('.$v[0].')'; |
|
943 | 939 | $par = array_merge($par, $v[1]); |
944 | 940 | } |
945 | 941 | $sql .= implode(' AND ', $tmp).' '; |
946 | 942 | } |
947 | 943 | if (count($o)) { |
948 | - $sql .= 'ORDER BY ' . $o[0] . ' '; |
|
944 | + $sql .= 'ORDER BY '.$o[0].' '; |
|
949 | 945 | $par = array_merge($par, $o[1]); |
950 | 946 | } |
951 | 947 | if (!count($g) && count($porder)) { |
952 | 948 | $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC'; |
953 | - $porder = array_map(function ($v) use ($pdir) { |
|
954 | - return $v . ' ' . $pdir; |
|
949 | + $porder = array_map(function($v) use ($pdir) { |
|
950 | + return $v.' '.$pdir; |
|
955 | 951 | }, $porder); |
956 | - $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' '; |
|
952 | + $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' '; |
|
957 | 953 | } |
958 | 954 | if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) { |
959 | 955 | if ($this->db->driverName() === 'oracle') { |
960 | - if ((int)$this->db->driverOption('version', 0) >= 12) { |
|
961 | - $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY'; |
|
956 | + if ((int) $this->db->driverOption('version', 0) >= 12) { |
|
957 | + $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY'; |
|
962 | 958 | } else { |
963 | - $f = array_map(function ($v) { |
|
959 | + $f = array_map(function($v) { |
|
964 | 960 | $v = explode(' ', trim($v), 2); |
965 | 961 | if (count($v) === 2) { |
966 | 962 | return $v[1]; |
@@ -968,16 +964,16 @@ discard block |
||
968 | 964 | $v = explode('.', $v[0], 2); |
969 | 965 | return count($v) === 2 ? $v[1] : $v[0]; |
970 | 966 | }, $select); |
971 | - $sql = "SELECT " . implode(', ', $f) . " |
|
967 | + $sql = "SELECT ".implode(', ', $f)." |
|
972 | 968 | FROM ( |
973 | 969 | SELECT tbl__.*, rownum rnum__ FROM ( |
974 | - " . $sql . " |
|
970 | + " . $sql." |
|
975 | 971 | ) tbl__ |
976 | - WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . " |
|
972 | + WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])." |
|
977 | 973 | ) WHERE rnum__ > " . $this->li_of[1]; |
978 | 974 | } |
979 | 975 | } else { |
980 | - $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1]; |
|
976 | + $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1]; |
|
981 | 977 | } |
982 | 978 | } |
983 | 979 | return $this->qiterator = new TableQueryIterator( |
@@ -1027,12 +1023,12 @@ discard block |
||
1027 | 1023 | $ret[$k] = str_repeat(' ', 255); |
1028 | 1024 | $par[] = &$ret[$k]; |
1029 | 1025 | } |
1030 | - $sql .= ' RETURNING ' . implode(',', $primary) . |
|
1031 | - ' INTO ' . implode(',', array_fill(0, count($primary), '?')); |
|
1026 | + $sql .= ' RETURNING '.implode(',', $primary). |
|
1027 | + ' INTO '.implode(',', array_fill(0, count($primary), '?')); |
|
1032 | 1028 | $this->db->query($sql, $par); |
1033 | 1029 | return $ret; |
1034 | 1030 | } elseif ($this->db->driverName() === 'postgre') { |
1035 | - $sql .= ' RETURNING ' . implode(',', $primary); |
|
1031 | + $sql .= ' RETURNING '.implode(',', $primary); |
|
1036 | 1032 | return $this->db->one($sql, $par, false); |
1037 | 1033 | } else { |
1038 | 1034 | $ret = []; |
@@ -1063,9 +1059,9 @@ discard block |
||
1063 | 1059 | } |
1064 | 1060 | $sql = 'UPDATE '.$table.' SET '; |
1065 | 1061 | $par = []; |
1066 | - $sql .= implode(', ', array_map(function ($v) { |
|
1067 | - return $v . ' = ?'; |
|
1068 | - }, array_keys($update))) . ' '; |
|
1062 | + $sql .= implode(', ', array_map(function($v) { |
|
1063 | + return $v.' = ?'; |
|
1064 | + }, array_keys($update))).' '; |
|
1069 | 1065 | $par = array_merge($par, array_values($update)); |
1070 | 1066 | if (count($this->where)) { |
1071 | 1067 | $sql .= 'WHERE '; |
@@ -1074,7 +1070,7 @@ discard block |
||
1074 | 1070 | $tmp[] = $v[0]; |
1075 | 1071 | $par = array_merge($par, $v[1]); |
1076 | 1072 | } |
1077 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1073 | + $sql .= implode(' AND ', $tmp).' '; |
|
1078 | 1074 | } |
1079 | 1075 | if (count($this->order)) { |
1080 | 1076 | $sql .= $this->order[0]; |
@@ -1098,7 +1094,7 @@ discard block |
||
1098 | 1094 | $tmp[] = $v[0]; |
1099 | 1095 | $par = array_merge($par, $v[1]); |
1100 | 1096 | } |
1101 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1097 | + $sql .= implode(' AND ', $tmp).' '; |
|
1102 | 1098 | } |
1103 | 1099 | if (count($this->order)) { |
1104 | 1100 | $sql .= $this->order[0]; |
@@ -1117,17 +1113,17 @@ discard block |
||
1117 | 1113 | $table = $this->definition; |
1118 | 1114 | if ($table->hasRelation($relation)) { |
1119 | 1115 | $temp = $table->getRelation($relation); |
1120 | - $this->withr[$relation] = [ $temp, $table->getName(), $select || ($this->withr[$relation][2] ?? false) ]; |
|
1116 | + $this->withr[$relation] = [$temp, $table->getName(), $select || ($this->withr[$relation][2] ?? false)]; |
|
1121 | 1117 | } else { |
1122 | 1118 | $parts = explode('.', $relation); |
1123 | 1119 | array_reduce( |
1124 | 1120 | $parts, |
1125 | - function ($carry, $item) use (&$table, $select) { |
|
1121 | + function($carry, $item) use (&$table, $select) { |
|
1126 | 1122 | if (!$table->hasRelation($item)) { |
1127 | - throw new DBException('Invalid relation name: '.$table->getName().' -> ' . $item); |
|
1123 | + throw new DBException('Invalid relation name: '.$table->getName().' -> '.$item); |
|
1128 | 1124 | } |
1129 | 1125 | $relation = $table->getRelation($item); |
1130 | - $name = $carry ? $carry . static::SEP . $item : $item; |
|
1126 | + $name = $carry ? $carry.static::SEP.$item : $item; |
|
1131 | 1127 | $this->withr[$name] = [ |
1132 | 1128 | $relation, |
1133 | 1129 | $carry ?? $table->getName(), |
@@ -1178,9 +1174,9 @@ discard block |
||
1178 | 1174 | } |
1179 | 1175 | |
1180 | 1176 | $aliases = []; |
1181 | - $getAlias = function ($name) use (&$aliases) { |
|
1177 | + $getAlias = function($name) use (&$aliases) { |
|
1182 | 1178 | // to bypass use: return $name; |
1183 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
1179 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
1184 | 1180 | }; |
1185 | 1181 | |
1186 | 1182 | $table = $this->definition->getName(); |
@@ -1192,65 +1188,65 @@ discard block |
||
1192 | 1188 | $h = $this->having; |
1193 | 1189 | $o = $this->order; |
1194 | 1190 | $g = $this->group; |
1195 | - $j = array_map(function ($v) { |
|
1191 | + $j = array_map(function($v) { |
|
1196 | 1192 | return clone $v; |
1197 | 1193 | }, $this->joins); |
1198 | 1194 | foreach ($this->definition->getRelations() as $k => $v) { |
1199 | 1195 | foreach ($w as $kk => $vv) { |
1200 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
1201 | - $relations[$k] = [ $v, $table ]; |
|
1202 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
1196 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
1197 | + $relations[$k] = [$v, $table]; |
|
1198 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
1203 | 1199 | } |
1204 | 1200 | } |
1205 | - if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) { |
|
1206 | - $relations[$k] = [ $v, $table ]; |
|
1207 | - $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]); |
|
1208 | - $o[2] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[2]); |
|
1201 | + if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) { |
|
1202 | + $relations[$k] = [$v, $table]; |
|
1203 | + $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]); |
|
1204 | + $o[2] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[2]); |
|
1209 | 1205 | } |
1210 | 1206 | foreach ($h as $kk => $vv) { |
1211 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
1212 | - $relations[$k] = [ $v, $table ]; |
|
1213 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
1207 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
1208 | + $relations[$k] = [$v, $table]; |
|
1209 | + $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
1214 | 1210 | } |
1215 | 1211 | } |
1216 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
1217 | - $relations[$k] = [ $v, $table ]; |
|
1218 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
1212 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
1213 | + $relations[$k] = [$v, $table]; |
|
1214 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
1219 | 1215 | } |
1220 | 1216 | foreach ($j as $kk => $vv) { |
1221 | 1217 | foreach ($vv->keymap as $kkk => $vvv) { |
1222 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) { |
|
1223 | - $relations[$k] = [ $v, $table ]; |
|
1218 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) { |
|
1219 | + $relations[$k] = [$v, $table]; |
|
1224 | 1220 | $j[$kk]->keymap[$kkk] = |
1225 | - preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv); |
|
1221 | + preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv); |
|
1226 | 1222 | } |
1227 | 1223 | } |
1228 | 1224 | } |
1229 | 1225 | } |
1230 | 1226 | |
1231 | - $key = array_map(function ($v) use ($table) { |
|
1232 | - return $table . '.' . $v; |
|
1227 | + $key = array_map(function($v) use ($table) { |
|
1228 | + return $table.'.'.$v; |
|
1233 | 1229 | }, $this->pkey); |
1234 | 1230 | $own = false; |
1235 | 1231 | $dir = 'ASC'; |
1236 | 1232 | if (count($o)) { |
1237 | 1233 | $dir = strpos($o[0], ' DESC') ? 'DESC' : 'ASC'; |
1238 | - $own = strpos($o[2], $table . '.') === 0; |
|
1234 | + $own = strpos($o[2], $table.'.') === 0; |
|
1239 | 1235 | } |
1240 | 1236 | |
1241 | 1237 | $dst = $key; |
1242 | 1238 | if (count($o)) { |
1243 | 1239 | if ($own) { |
1244 | 1240 | // if using own table - do not use max/min in order - that will prevent index usage |
1245 | - $dst[] = $o[2] . ' orderbyfix___'; |
|
1241 | + $dst[] = $o[2].' orderbyfix___'; |
|
1246 | 1242 | } else { |
1247 | - $dst[] = 'MAX(' . $o[2] . ') orderbyfix___'; |
|
1243 | + $dst[] = 'MAX('.$o[2].') orderbyfix___'; |
|
1248 | 1244 | } |
1249 | 1245 | } |
1250 | 1246 | $dst = array_unique($dst); |
1251 | 1247 | |
1252 | 1248 | $par = []; |
1253 | - $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' '; |
|
1249 | + $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' '; |
|
1254 | 1250 | foreach ($relations as $k => $v) { |
1255 | 1251 | $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1]; |
1256 | 1252 | $v = $v[0]; |
@@ -1261,13 +1257,13 @@ discard block |
||
1261 | 1257 | foreach ($v->keymap as $kk => $vv) { |
1262 | 1258 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
1263 | 1259 | } |
1264 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1260 | + $sql .= implode(' AND ', $tmp).' '; |
|
1265 | 1261 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON '; |
1266 | 1262 | $tmp = []; |
1267 | 1263 | foreach ($v->pivot_keymap as $kk => $vv) { |
1268 | 1264 | $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
1269 | 1265 | } |
1270 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1266 | + $sql .= implode(' AND ', $tmp).' '; |
|
1271 | 1267 | } else { |
1272 | 1268 | $alias = $getAlias($k); |
1273 | 1269 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON '; |
@@ -1276,37 +1272,37 @@ discard block |
||
1276 | 1272 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
1277 | 1273 | } |
1278 | 1274 | if ($v->sql) { |
1279 | - $tmp[] = $v->sql . ' '; |
|
1275 | + $tmp[] = $v->sql.' '; |
|
1280 | 1276 | $par = array_merge($par, $v->par ?? []); |
1281 | 1277 | } |
1282 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1278 | + $sql .= implode(' AND ', $tmp).' '; |
|
1283 | 1279 | } |
1284 | 1280 | } |
1285 | 1281 | foreach ($j as $k => $v) { |
1286 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
1282 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
1287 | 1283 | $tmp = []; |
1288 | 1284 | foreach ($v->keymap as $kk => $vv) { |
1289 | 1285 | $tmp[] = $kk.' = '.$vv; |
1290 | 1286 | } |
1291 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1287 | + $sql .= implode(' AND ', $tmp).' '; |
|
1292 | 1288 | } |
1293 | 1289 | if (count($w)) { |
1294 | 1290 | $sql .= 'WHERE '; |
1295 | 1291 | $tmp = []; |
1296 | 1292 | foreach ($w as $v) { |
1297 | - $tmp[] = '(' . $v[0] . ')'; |
|
1293 | + $tmp[] = '('.$v[0].')'; |
|
1298 | 1294 | $par = array_merge($par, $v[1]); |
1299 | 1295 | } |
1300 | 1296 | $sql .= implode(' AND ', $tmp).' '; |
1301 | 1297 | } |
1302 | 1298 | if (!$own) { |
1303 | - $sql .= 'GROUP BY ' . implode(', ', $key) . ' '; |
|
1299 | + $sql .= 'GROUP BY '.implode(', ', $key).' '; |
|
1304 | 1300 | } |
1305 | 1301 | if (count($h)) { |
1306 | 1302 | $sql .= 'HAVING '; |
1307 | 1303 | $tmp = []; |
1308 | 1304 | foreach ($h as $v) { |
1309 | - $tmp[] = '(' . $v[0] . ')'; |
|
1305 | + $tmp[] = '('.$v[0].')'; |
|
1310 | 1306 | $par = array_merge($par, $v[1]); |
1311 | 1307 | } |
1312 | 1308 | $sql .= implode(' AND ', $tmp).' '; |
@@ -1314,38 +1310,38 @@ discard block |
||
1314 | 1310 | if (count($o)) { |
1315 | 1311 | $sql .= 'ORDER BY '; |
1316 | 1312 | if ($own) { |
1317 | - $sql .= $o[2] . ' ' . $dir; |
|
1313 | + $sql .= $o[2].' '.$dir; |
|
1318 | 1314 | } else { |
1319 | - $sql .= 'MAX('.$o[2].') ' . $dir; |
|
1315 | + $sql .= 'MAX('.$o[2].') '.$dir; |
|
1320 | 1316 | } |
1321 | 1317 | } |
1322 | 1318 | $porder = []; |
1323 | 1319 | $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC'; |
1324 | 1320 | foreach ($this->definition->getPrimaryKey() as $field) { |
1325 | - $porder[] = $this->getColumn($field)['name'] . ' ' . $pdir; |
|
1321 | + $porder[] = $this->getColumn($field)['name'].' '.$pdir; |
|
1326 | 1322 | } |
1327 | 1323 | if (count($porder)) { |
1328 | - $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' '; |
|
1324 | + $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' '; |
|
1329 | 1325 | } |
1330 | 1326 | |
1331 | 1327 | if ($this->li_of[0]) { |
1332 | 1328 | if ($this->db->driverName() === 'oracle') { |
1333 | - if ((int)$this->db->driverOption('version', 0) >= 12) { |
|
1334 | - $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY'; |
|
1329 | + if ((int) $this->db->driverOption('version', 0) >= 12) { |
|
1330 | + $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY'; |
|
1335 | 1331 | } else { |
1336 | - $sql = "SELECT " . implode(', ', $dst) . " |
|
1332 | + $sql = "SELECT ".implode(', ', $dst)." |
|
1337 | 1333 | FROM ( |
1338 | 1334 | SELECT tbl__.*, rownum rnum__ FROM ( |
1339 | - " . $sql . " |
|
1335 | + " . $sql." |
|
1340 | 1336 | ) tbl__ |
1341 | - WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . " |
|
1337 | + WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])." |
|
1342 | 1338 | ) WHERE rnum__ > " . $this->li_of[1]; |
1343 | 1339 | } |
1344 | 1340 | } else { |
1345 | - $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1]; |
|
1341 | + $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1]; |
|
1346 | 1342 | } |
1347 | 1343 | } |
1348 | - return array_map(function ($v) { |
|
1344 | + return array_map(function($v) { |
|
1349 | 1345 | if (array_key_exists('orderbyfix___', $v)) { |
1350 | 1346 | unset($v['orderbyfix___']); |
1351 | 1347 | } |