@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | } |
24 | 24 | public function affected() : int |
25 | 25 | { |
26 | - return (int)\oci_num_rows($this->statement); |
|
26 | + return (int) \oci_num_rows($this->statement); |
|
27 | 27 | } |
28 | 28 | public function insertID(string $sequence = null) |
29 | 29 | { |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | } |
65 | 65 | public function next() |
66 | 66 | { |
67 | - $this->fetched ++; |
|
67 | + $this->fetched++; |
|
68 | 68 | $this->last = \oci_fetch_array($this->statement, \OCI_ASSOC + \OCI_RETURN_NULLS + \OCI_RETURN_LOBS); |
69 | 69 | } |
70 | 70 | public function valid() |
@@ -36,8 +36,7 @@ discard block |
||
36 | 36 | $temp = @\pg_query( |
37 | 37 | $this->driver, |
38 | 38 | $sequence ? |
39 | - 'SELECT currval('.@\pg_escape_string($this->driver, $sequence).')' : |
|
40 | - 'SELECT lastval()' |
|
39 | + 'SELECT currval('.@\pg_escape_string($this->driver, $sequence).')' : 'SELECT lastval()' |
|
41 | 40 | ); |
42 | 41 | if ($temp) { |
43 | 42 | $res = \pg_fetch_row($temp); |
@@ -75,7 +74,7 @@ discard block |
||
75 | 74 | } |
76 | 75 | public function next() |
77 | 76 | { |
78 | - $this->fetched ++; |
|
77 | + $this->fetched++; |
|
79 | 78 | $this->last = \pg_fetch_array($this->statement, null, \PGSQL_ASSOC); |
80 | 79 | } |
81 | 80 | public function valid() |
@@ -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(); |
@@ -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)); |
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)); |
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 | |
@@ -761,38 +757,38 @@ discard block |
||
761 | 757 | |
762 | 758 | foreach ($this->definition->getRelations() as $k => $relation) { |
763 | 759 | foreach ($f as $kk => $field) { |
764 | - if (strpos($field, $k . '.') === 0) { |
|
765 | - $relations[$k] = [ $relation, $table ]; |
|
766 | - $f[$kk] = str_replace($k . '.', $getAlias($k) . '.', $field); |
|
760 | + if (strpos($field, $k.'.') === 0) { |
|
761 | + $relations[$k] = [$relation, $table]; |
|
762 | + $f[$kk] = str_replace($k.'.', $getAlias($k).'.', $field); |
|
767 | 763 | } |
768 | 764 | } |
769 | 765 | foreach ($w as $kk => $v) { |
770 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) { |
|
771 | - $relations[$k] = [ $relation, $table ]; |
|
772 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]); |
|
766 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) { |
|
767 | + $relations[$k] = [$relation, $table]; |
|
768 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $v[0]); |
|
773 | 769 | } |
774 | 770 | } |
775 | 771 | foreach ($h as $kk => $v) { |
776 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $v[0])) { |
|
777 | - $relations[$k] = [ $relation, $table ]; |
|
778 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $v[0]); |
|
772 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $v[0])) { |
|
773 | + $relations[$k] = [$relation, $table]; |
|
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 | - $relations[$k] = [ $relation, $table ]; |
|
783 | - $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 | + $relations[$k] = [$relation, $table]; |
|
779 | + $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]); |
|
784 | 780 | } |
785 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
786 | - $relations[$k] = [ $relation, $table ]; |
|
787 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
781 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
782 | + $relations[$k] = [$relation, $table]; |
|
783 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
788 | 784 | } |
789 | 785 | foreach ($j as $kk => $v) { |
790 | 786 | foreach ($v->keymap as $kkk => $vv) { |
791 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv)) { |
|
792 | - $relations[$k] = [ $relation, $table ]; |
|
787 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv)) { |
|
788 | + $relations[$k] = [$relation, $table]; |
|
793 | 789 | $j[$kk]->keymap[$kkk] = preg_replace( |
794 | - '(\b'.preg_quote($k . '.'). ')i', |
|
795 | - $getAlias($k) . '.', |
|
790 | + '(\b'.preg_quote($k.'.').')i', |
|
791 | + $getAlias($k).'.', |
|
796 | 792 | $vv |
797 | 793 | ); |
798 | 794 | } |
@@ -801,11 +797,11 @@ discard block |
||
801 | 797 | } |
802 | 798 | $select = []; |
803 | 799 | foreach ($f as $k => $field) { |
804 | - $select[] = $field . (!is_numeric($k) ? ' ' . $k : ''); |
|
800 | + $select[] = $field.(!is_numeric($k) ? ' '.$k : ''); |
|
805 | 801 | } |
806 | 802 | foreach ($this->withr as $name => $relation) { |
807 | 803 | foreach ($relation[0]->table->getColumns() as $column) { |
808 | - $select[] = $getAlias($name) . '.' . $column . ' ' . $getAlias($name . static::SEP . $column); |
|
804 | + $select[] = $getAlias($name).'.'.$column.' '.$getAlias($name.static::SEP.$column); |
|
809 | 805 | } |
810 | 806 | } |
811 | 807 | $sql = 'SELECT '.implode(', ', $select).' FROM '.$this->definition->getFullName().' '; |
@@ -824,13 +820,13 @@ discard block |
||
824 | 820 | foreach ($v->keymap as $kk => $vv) { |
825 | 821 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
826 | 822 | } |
827 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
823 | + $sql .= implode(' AND ', $tmp).' '; |
|
828 | 824 | $sql .= 'LEFT JOIN '.$v->table->getFullName().' '.$getAlias($relation).' ON '; |
829 | 825 | $tmp = []; |
830 | 826 | foreach ($v->pivot_keymap as $kk => $vv) { |
831 | 827 | $tmp[] = $getAlias($relation).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
832 | 828 | } |
833 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
829 | + $sql .= implode(' AND ', $tmp).' '; |
|
834 | 830 | } else { |
835 | 831 | $alias = $getAlias($relation); |
836 | 832 | |
@@ -840,22 +836,22 @@ discard block |
||
840 | 836 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
841 | 837 | } |
842 | 838 | if ($v->sql) { |
843 | - $tmp[] = $v->sql . ' '; |
|
839 | + $tmp[] = $v->sql.' '; |
|
844 | 840 | $par = array_merge($par, $v->par ?? []); |
845 | 841 | } |
846 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
842 | + $sql .= implode(' AND ', $tmp).' '; |
|
847 | 843 | } |
848 | 844 | } |
849 | 845 | foreach ($j as $k => $v) { |
850 | 846 | if ($v->many) { |
851 | 847 | $many = true; |
852 | 848 | } |
853 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
849 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
854 | 850 | $tmp = []; |
855 | 851 | foreach ($v->keymap as $kk => $vv) { |
856 | 852 | $tmp[] = $kk.' = '.$vv; |
857 | 853 | } |
858 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
854 | + $sql .= implode(' AND ', $tmp).' '; |
|
859 | 855 | } |
860 | 856 | if ($many && count($porder) && $this->li_of[2] === 1) { |
861 | 857 | $ids = $this->ids(); |
@@ -863,9 +859,9 @@ discard block |
||
863 | 859 | if (count($porder) > 1) { |
864 | 860 | $pkw = []; |
865 | 861 | foreach ($porder as $name) { |
866 | - $pkw[] = $name . ' = ?'; |
|
862 | + $pkw[] = $name.' = ?'; |
|
867 | 863 | } |
868 | - $pkw = '(' . implode(' AND ', $pkw) . ')'; |
|
864 | + $pkw = '('.implode(' AND ', $pkw).')'; |
|
869 | 865 | $pkp = []; |
870 | 866 | foreach ($ids as $id) { |
871 | 867 | foreach ($id as $p) { |
@@ -877,51 +873,51 @@ discard block |
||
877 | 873 | $pkp |
878 | 874 | ]; |
879 | 875 | } else { |
880 | - $w[] = [ $porder[0] . ' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids ]; |
|
876 | + $w[] = [$porder[0].' IN ('.implode(',', array_fill(0, count($ids), '?')).')', $ids]; |
|
881 | 877 | } |
882 | 878 | } else { |
883 | - $w[] = [ '1=0', [] ]; |
|
879 | + $w[] = ['1=0', []]; |
|
884 | 880 | } |
885 | 881 | } |
886 | 882 | if (count($w)) { |
887 | 883 | $sql .= 'WHERE '; |
888 | 884 | $tmp = []; |
889 | 885 | foreach ($w as $v) { |
890 | - $tmp[] = '(' . $v[0] . ')'; |
|
886 | + $tmp[] = '('.$v[0].')'; |
|
891 | 887 | $par = array_merge($par, $v[1]); |
892 | 888 | } |
893 | 889 | $sql .= implode(' AND ', $tmp).' '; |
894 | 890 | } |
895 | 891 | if (count($g)) { |
896 | - $sql .= 'GROUP BY ' . $g[0] . ' '; |
|
892 | + $sql .= 'GROUP BY '.$g[0].' '; |
|
897 | 893 | $par = array_merge($par, $g[1]); |
898 | 894 | } |
899 | 895 | if (count($h)) { |
900 | 896 | $sql .= 'HAVING '; |
901 | 897 | $tmp = []; |
902 | 898 | foreach ($h as $v) { |
903 | - $tmp[] = '(' . $v[0] . ')'; |
|
899 | + $tmp[] = '('.$v[0].')'; |
|
904 | 900 | $par = array_merge($par, $v[1]); |
905 | 901 | } |
906 | 902 | $sql .= implode(' AND ', $tmp).' '; |
907 | 903 | } |
908 | 904 | if (count($o)) { |
909 | - $sql .= 'ORDER BY ' . $o[0] . ' '; |
|
905 | + $sql .= 'ORDER BY '.$o[0].' '; |
|
910 | 906 | $par = array_merge($par, $o[1]); |
911 | 907 | } |
912 | 908 | if (!count($g) && count($porder)) { |
913 | 909 | $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC'; |
914 | - $porder = array_map(function ($v) use ($pdir) { |
|
915 | - return $v . ' ' . $pdir; |
|
910 | + $porder = array_map(function($v) use ($pdir) { |
|
911 | + return $v.' '.$pdir; |
|
916 | 912 | }, $porder); |
917 | - $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' '; |
|
913 | + $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' '; |
|
918 | 914 | } |
919 | 915 | if ((!$many || $this->li_of[2] === 0 || !count($porder)) && $this->li_of[0]) { |
920 | 916 | if ($this->db->driverName() === 'oracle') { |
921 | - if ((int)$this->db->driverOption('version', 0) >= 12) { |
|
922 | - $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY'; |
|
917 | + if ((int) $this->db->driverOption('version', 0) >= 12) { |
|
918 | + $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY'; |
|
923 | 919 | } else { |
924 | - $f = array_map(function ($v) { |
|
920 | + $f = array_map(function($v) { |
|
925 | 921 | $v = explode(' ', trim($v), 2); |
926 | 922 | if (count($v) === 2) { |
927 | 923 | return $v[1]; |
@@ -929,16 +925,16 @@ discard block |
||
929 | 925 | $v = explode('.', $v[0], 2); |
930 | 926 | return count($v) === 2 ? $v[1] : $v[0]; |
931 | 927 | }, $select); |
932 | - $sql = "SELECT " . implode(', ', $f) . " |
|
928 | + $sql = "SELECT ".implode(', ', $f)." |
|
933 | 929 | FROM ( |
934 | 930 | SELECT tbl__.*, rownum rnum__ FROM ( |
935 | - " . $sql . " |
|
931 | + " . $sql." |
|
936 | 932 | ) tbl__ |
937 | - WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . " |
|
933 | + WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])." |
|
938 | 934 | ) WHERE rnum__ > " . $this->li_of[1]; |
939 | 935 | } |
940 | 936 | } else { |
941 | - $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1]; |
|
937 | + $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1]; |
|
942 | 938 | } |
943 | 939 | } |
944 | 940 | return $this->qiterator = new TableQueryIterator( |
@@ -988,12 +984,12 @@ discard block |
||
988 | 984 | $ret[$k] = str_repeat(' ', 255); |
989 | 985 | $par[] = &$ret[$k]; |
990 | 986 | } |
991 | - $sql .= ' RETURNING ' . implode(',', $primary) . |
|
992 | - ' INTO ' . implode(',', array_fill(0, count($primary), '?')); |
|
987 | + $sql .= ' RETURNING '.implode(',', $primary). |
|
988 | + ' INTO '.implode(',', array_fill(0, count($primary), '?')); |
|
993 | 989 | $this->db->query($sql, $par); |
994 | 990 | return $ret; |
995 | 991 | } elseif ($this->db->driverName() === 'postgre') { |
996 | - $sql .= ' RETURNING ' . implode(',', $primary); |
|
992 | + $sql .= ' RETURNING '.implode(',', $primary); |
|
997 | 993 | return $this->db->one($sql, $par, false); |
998 | 994 | } else { |
999 | 995 | $ret = []; |
@@ -1024,9 +1020,9 @@ discard block |
||
1024 | 1020 | } |
1025 | 1021 | $sql = 'UPDATE '.$table.' SET '; |
1026 | 1022 | $par = []; |
1027 | - $sql .= implode(', ', array_map(function ($v) { |
|
1028 | - return $v . ' = ?'; |
|
1029 | - }, array_keys($update))) . ' '; |
|
1023 | + $sql .= implode(', ', array_map(function($v) { |
|
1024 | + return $v.' = ?'; |
|
1025 | + }, array_keys($update))).' '; |
|
1030 | 1026 | $par = array_merge($par, array_values($update)); |
1031 | 1027 | if (count($this->where)) { |
1032 | 1028 | $sql .= 'WHERE '; |
@@ -1035,7 +1031,7 @@ discard block |
||
1035 | 1031 | $tmp[] = $v[0]; |
1036 | 1032 | $par = array_merge($par, $v[1]); |
1037 | 1033 | } |
1038 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1034 | + $sql .= implode(' AND ', $tmp).' '; |
|
1039 | 1035 | } |
1040 | 1036 | if (count($this->order)) { |
1041 | 1037 | $sql .= $this->order[0]; |
@@ -1059,7 +1055,7 @@ discard block |
||
1059 | 1055 | $tmp[] = $v[0]; |
1060 | 1056 | $par = array_merge($par, $v[1]); |
1061 | 1057 | } |
1062 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1058 | + $sql .= implode(' AND ', $tmp).' '; |
|
1063 | 1059 | } |
1064 | 1060 | if (count($this->order)) { |
1065 | 1061 | $sql .= $this->order[0]; |
@@ -1078,18 +1074,18 @@ discard block |
||
1078 | 1074 | $table = $this->definition; |
1079 | 1075 | if ($table->hasRelation($relation)) { |
1080 | 1076 | $temp = $table->getRelation($relation); |
1081 | - $this->withr[$relation] = [ $temp, $table->getName() ]; |
|
1077 | + $this->withr[$relation] = [$temp, $table->getName()]; |
|
1082 | 1078 | } else { |
1083 | 1079 | $parts = explode('.', $relation); |
1084 | 1080 | array_reduce( |
1085 | 1081 | $parts, |
1086 | - function ($carry, $item) use (&$table) { |
|
1082 | + function($carry, $item) use (&$table) { |
|
1087 | 1083 | if (!$table->hasRelation($item)) { |
1088 | - throw new DBException('Invalid relation name: '.$table->getName().' -> ' . $item); |
|
1084 | + throw new DBException('Invalid relation name: '.$table->getName().' -> '.$item); |
|
1089 | 1085 | } |
1090 | 1086 | $relation = $table->getRelation($item); |
1091 | - $name = $carry ? $carry . static::SEP . $item : $item; |
|
1092 | - $this->withr[$name] = [ $relation, $carry ?? $table->getName() ]; |
|
1087 | + $name = $carry ? $carry.static::SEP.$item : $item; |
|
1088 | + $this->withr[$name] = [$relation, $carry ?? $table->getName()]; |
|
1093 | 1089 | $table = $relation->table; |
1094 | 1090 | return $name; |
1095 | 1091 | } |
@@ -1135,9 +1131,9 @@ discard block |
||
1135 | 1131 | } |
1136 | 1132 | |
1137 | 1133 | $aliases = []; |
1138 | - $getAlias = function ($name) use (&$aliases) { |
|
1134 | + $getAlias = function($name) use (&$aliases) { |
|
1139 | 1135 | // to bypass use: return $name; |
1140 | - return $aliases[$name] = $aliases[$name] ?? 'alias' . static::SEP . count($aliases); |
|
1136 | + return $aliases[$name] = $aliases[$name] ?? 'alias'.static::SEP.count($aliases); |
|
1141 | 1137 | }; |
1142 | 1138 | |
1143 | 1139 | $table = $this->definition->getName(); |
@@ -1149,65 +1145,65 @@ discard block |
||
1149 | 1145 | $h = $this->having; |
1150 | 1146 | $o = $this->order; |
1151 | 1147 | $g = $this->group; |
1152 | - $j = array_map(function ($v) { |
|
1148 | + $j = array_map(function($v) { |
|
1153 | 1149 | return clone $v; |
1154 | 1150 | }, $this->joins); |
1155 | 1151 | foreach ($this->definition->getRelations() as $k => $v) { |
1156 | 1152 | foreach ($w as $kk => $vv) { |
1157 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
1158 | - $relations[$k] = [ $v, $table ]; |
|
1159 | - $w[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
1153 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
1154 | + $relations[$k] = [$v, $table]; |
|
1155 | + $w[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
1160 | 1156 | } |
1161 | 1157 | } |
1162 | - if (isset($o[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $o[0])) { |
|
1163 | - $relations[$k] = [ $v, $table ]; |
|
1164 | - $o[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[0]); |
|
1165 | - $o[2] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $o[2]); |
|
1158 | + if (isset($o[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $o[0])) { |
|
1159 | + $relations[$k] = [$v, $table]; |
|
1160 | + $o[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[0]); |
|
1161 | + $o[2] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $o[2]); |
|
1166 | 1162 | } |
1167 | 1163 | foreach ($h as $kk => $vv) { |
1168 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vv[0])) { |
|
1169 | - $relations[$k] = [ $v, $table ]; |
|
1170 | - $h[$kk][0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vv[0]); |
|
1164 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vv[0])) { |
|
1165 | + $relations[$k] = [$v, $table]; |
|
1166 | + $h[$kk][0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vv[0]); |
|
1171 | 1167 | } |
1172 | 1168 | } |
1173 | - if (isset($g[0]) && preg_match('(\b'.preg_quote($k . '.'). ')i', $g[0])) { |
|
1174 | - $relations[$k] = [ $v, $table ]; |
|
1175 | - $g[0] = preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $g[0]); |
|
1169 | + if (isset($g[0]) && preg_match('(\b'.preg_quote($k.'.').')i', $g[0])) { |
|
1170 | + $relations[$k] = [$v, $table]; |
|
1171 | + $g[0] = preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $g[0]); |
|
1176 | 1172 | } |
1177 | 1173 | foreach ($j as $kk => $vv) { |
1178 | 1174 | foreach ($vv->keymap as $kkk => $vvv) { |
1179 | - if (preg_match('(\b'.preg_quote($k . '.'). ')i', $vvv)) { |
|
1180 | - $relations[$k] = [ $v, $table ]; |
|
1175 | + if (preg_match('(\b'.preg_quote($k.'.').')i', $vvv)) { |
|
1176 | + $relations[$k] = [$v, $table]; |
|
1181 | 1177 | $j[$kk]->keymap[$kkk] = |
1182 | - preg_replace('(\b'.preg_quote($k . '.'). ')i', $getAlias($k) . '.', $vvv); |
|
1178 | + preg_replace('(\b'.preg_quote($k.'.').')i', $getAlias($k).'.', $vvv); |
|
1183 | 1179 | } |
1184 | 1180 | } |
1185 | 1181 | } |
1186 | 1182 | } |
1187 | 1183 | |
1188 | - $key = array_map(function ($v) use ($table) { |
|
1189 | - return $table . '.' . $v; |
|
1184 | + $key = array_map(function($v) use ($table) { |
|
1185 | + return $table.'.'.$v; |
|
1190 | 1186 | }, $this->pkey); |
1191 | 1187 | $own = false; |
1192 | 1188 | $dir = 'ASC'; |
1193 | 1189 | if (count($o)) { |
1194 | 1190 | $dir = strpos($o[0], ' DESC') ? 'DESC' : 'ASC'; |
1195 | - $own = strpos($o[2], $table . '.') === 0; |
|
1191 | + $own = strpos($o[2], $table.'.') === 0; |
|
1196 | 1192 | } |
1197 | 1193 | |
1198 | 1194 | $dst = $key; |
1199 | 1195 | if (count($o)) { |
1200 | 1196 | if ($own) { |
1201 | 1197 | // if using own table - do not use max/min in order - that will prevent index usage |
1202 | - $dst[] = $o[2] . ' orderbyfix___'; |
|
1198 | + $dst[] = $o[2].' orderbyfix___'; |
|
1203 | 1199 | } else { |
1204 | - $dst[] = 'MAX(' . $o[2] . ') orderbyfix___'; |
|
1200 | + $dst[] = 'MAX('.$o[2].') orderbyfix___'; |
|
1205 | 1201 | } |
1206 | 1202 | } |
1207 | 1203 | $dst = array_unique($dst); |
1208 | 1204 | |
1209 | 1205 | $par = []; |
1210 | - $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' '; |
|
1206 | + $sql = 'SELECT DISTINCT '.implode(', ', $dst).' FROM '.$this->definition->getFullName().' '; |
|
1211 | 1207 | foreach ($relations as $k => $v) { |
1212 | 1208 | $table = $v[1] !== $this->definition->getName() ? $getAlias($v[1]) : $v[1]; |
1213 | 1209 | $v = $v[0]; |
@@ -1218,13 +1214,13 @@ discard block |
||
1218 | 1214 | foreach ($v->keymap as $kk => $vv) { |
1219 | 1215 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
1220 | 1216 | } |
1221 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1217 | + $sql .= implode(' AND ', $tmp).' '; |
|
1222 | 1218 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$getAlias($k).' ON '; |
1223 | 1219 | $tmp = []; |
1224 | 1220 | foreach ($v->pivot_keymap as $kk => $vv) { |
1225 | 1221 | $tmp[] = $getAlias($k).'.'.$vv.' = '.$alias.'.'.$kk.' '; |
1226 | 1222 | } |
1227 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1223 | + $sql .= implode(' AND ', $tmp).' '; |
|
1228 | 1224 | } else { |
1229 | 1225 | $alias = $getAlias($k); |
1230 | 1226 | $sql .= 'LEFT JOIN '.$v->table->getName().' '.$alias.' ON '; |
@@ -1233,37 +1229,37 @@ discard block |
||
1233 | 1229 | $tmp[] = $table.'.'.$kk.' = '.$alias.'.'.$vv.' '; |
1234 | 1230 | } |
1235 | 1231 | if ($v->sql) { |
1236 | - $tmp[] = $v->sql . ' '; |
|
1232 | + $tmp[] = $v->sql.' '; |
|
1237 | 1233 | $par = array_merge($par, $v->par ?? []); |
1238 | 1234 | } |
1239 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1235 | + $sql .= implode(' AND ', $tmp).' '; |
|
1240 | 1236 | } |
1241 | 1237 | } |
1242 | 1238 | foreach ($j as $k => $v) { |
1243 | - $sql .= ($v->many ? 'LEFT ' : '' ) . 'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
1239 | + $sql .= ($v->many ? 'LEFT ' : '').'JOIN '.$v->table->getName().' '.$k.' ON '; |
|
1244 | 1240 | $tmp = []; |
1245 | 1241 | foreach ($v->keymap as $kk => $vv) { |
1246 | 1242 | $tmp[] = $kk.' = '.$vv; |
1247 | 1243 | } |
1248 | - $sql .= implode(' AND ', $tmp) . ' '; |
|
1244 | + $sql .= implode(' AND ', $tmp).' '; |
|
1249 | 1245 | } |
1250 | 1246 | if (count($w)) { |
1251 | 1247 | $sql .= 'WHERE '; |
1252 | 1248 | $tmp = []; |
1253 | 1249 | foreach ($w as $v) { |
1254 | - $tmp[] = '(' . $v[0] . ')'; |
|
1250 | + $tmp[] = '('.$v[0].')'; |
|
1255 | 1251 | $par = array_merge($par, $v[1]); |
1256 | 1252 | } |
1257 | 1253 | $sql .= implode(' AND ', $tmp).' '; |
1258 | 1254 | } |
1259 | 1255 | if (!$own) { |
1260 | - $sql .= 'GROUP BY ' . implode(', ', $key) . ' '; |
|
1256 | + $sql .= 'GROUP BY '.implode(', ', $key).' '; |
|
1261 | 1257 | } |
1262 | 1258 | if (count($h)) { |
1263 | 1259 | $sql .= 'HAVING '; |
1264 | 1260 | $tmp = []; |
1265 | 1261 | foreach ($h as $v) { |
1266 | - $tmp[] = '(' . $v[0] . ')'; |
|
1262 | + $tmp[] = '('.$v[0].')'; |
|
1267 | 1263 | $par = array_merge($par, $v[1]); |
1268 | 1264 | } |
1269 | 1265 | $sql .= implode(' AND ', $tmp).' '; |
@@ -1271,38 +1267,38 @@ discard block |
||
1271 | 1267 | if (count($o)) { |
1272 | 1268 | $sql .= 'ORDER BY '; |
1273 | 1269 | if ($own) { |
1274 | - $sql .= $o[2] . ' ' . $dir; |
|
1270 | + $sql .= $o[2].' '.$dir; |
|
1275 | 1271 | } else { |
1276 | - $sql .= 'MAX('.$o[2].') ' . $dir; |
|
1272 | + $sql .= 'MAX('.$o[2].') '.$dir; |
|
1277 | 1273 | } |
1278 | 1274 | } |
1279 | 1275 | $porder = []; |
1280 | 1276 | $pdir = (count($o) && strpos($o[0], 'DESC') !== false) ? 'DESC' : 'ASC'; |
1281 | 1277 | foreach ($this->definition->getPrimaryKey() as $field) { |
1282 | - $porder[] = $this->getColumn($field)['name'] . ' ' . $pdir; |
|
1278 | + $porder[] = $this->getColumn($field)['name'].' '.$pdir; |
|
1283 | 1279 | } |
1284 | 1280 | if (count($porder)) { |
1285 | - $sql .= (count($o) ? ', ' : 'ORDER BY ') . implode(', ', $porder) . ' '; |
|
1281 | + $sql .= (count($o) ? ', ' : 'ORDER BY ').implode(', ', $porder).' '; |
|
1286 | 1282 | } |
1287 | 1283 | |
1288 | 1284 | if ($this->li_of[0]) { |
1289 | 1285 | if ($this->db->driverName() === 'oracle') { |
1290 | - if ((int)$this->db->driverOption('version', 0) >= 12) { |
|
1291 | - $sql .= 'OFFSET ' . $this->li_of[1] . ' ROWS FETCH NEXT ' . $this->li_of[0] . ' ROWS ONLY'; |
|
1286 | + if ((int) $this->db->driverOption('version', 0) >= 12) { |
|
1287 | + $sql .= 'OFFSET '.$this->li_of[1].' ROWS FETCH NEXT '.$this->li_of[0].' ROWS ONLY'; |
|
1292 | 1288 | } else { |
1293 | - $sql = "SELECT " . implode(', ', $dst) . " |
|
1289 | + $sql = "SELECT ".implode(', ', $dst)." |
|
1294 | 1290 | FROM ( |
1295 | 1291 | SELECT tbl__.*, rownum rnum__ FROM ( |
1296 | - " . $sql . " |
|
1292 | + " . $sql." |
|
1297 | 1293 | ) tbl__ |
1298 | - WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1]) . " |
|
1294 | + WHERE rownum <= " . ($this->li_of[0] + $this->li_of[1])." |
|
1299 | 1295 | ) WHERE rnum__ > " . $this->li_of[1]; |
1300 | 1296 | } |
1301 | 1297 | } else { |
1302 | - $sql .= 'LIMIT ' . $this->li_of[0] . ' OFFSET ' . $this->li_of[1]; |
|
1298 | + $sql .= 'LIMIT '.$this->li_of[0].' OFFSET '.$this->li_of[1]; |
|
1303 | 1299 | } |
1304 | 1300 | } |
1305 | - return array_map(function ($v) { |
|
1301 | + return array_map(function($v) { |
|
1306 | 1302 | if (array_key_exists('orderbyfix___', $v)) { |
1307 | 1303 | unset($v['orderbyfix___']); |
1308 | 1304 | } |