@@ -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; |
@@ -53,18 +53,18 @@ discard block |
||
53 | 53 | ] |
54 | 54 | ) |
55 | 55 | ); |
56 | - $relationsT['main.' . $row['table']][] = $row; |
|
57 | - $relationsR['main.' . $row['referenced_table']][] = $row; |
|
56 | + $relationsT['main.'.$row['table']][] = $row; |
|
57 | + $relationsR['main.'.$row['referenced_table']][] = $row; |
|
58 | 58 | } |
59 | 59 | } |
60 | 60 | } |
61 | 61 | |
62 | 62 | $columns = Collection::from($this |
63 | 63 | ->query("PRAGMA table_info(".$table.")")) |
64 | - ->mapKey(function ($v) { |
|
64 | + ->mapKey(function($v) { |
|
65 | 65 | return $v['name']; |
66 | 66 | }) |
67 | - ->map(function ($v) { |
|
67 | + ->map(function($v) { |
|
68 | 68 | $v['length'] = null; |
69 | 69 | if (!isset($v['type'])) { |
70 | 70 | return $v; |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | default: |
75 | 75 | if (strpos($type, 'char') !== false && strpos($type, '(') !== false) { |
76 | 76 | // extract length from varchar |
77 | - $v['length'] = (int)explode(')', explode('(', $type)[1])[0]; |
|
77 | + $v['length'] = (int) explode(')', explode('(', $type)[1])[0]; |
|
78 | 78 | $v['length'] = $v['length'] > 0 ? $v['length'] : null; |
79 | 79 | } |
80 | 80 | break; |
@@ -83,15 +83,15 @@ discard block |
||
83 | 83 | }) |
84 | 84 | ->toArray(); |
85 | 85 | if (!count($columns)) { |
86 | - throw new DBException('Table not found by name: ' . implode('.', [$schema,$table])); |
|
86 | + throw new DBException('Table not found by name: '.implode('.', [$schema, $table])); |
|
87 | 87 | } |
88 | 88 | $primary = []; |
89 | 89 | foreach ($columns as $column) { |
90 | - if ((int)$column['pk']) { |
|
90 | + if ((int) $column['pk']) { |
|
91 | 91 | $primary[] = $column['name']; |
92 | 92 | } |
93 | 93 | } |
94 | - $tables[$schema . '.' .$table] = $definition = (new Table($table, $schema)) |
|
94 | + $tables[$schema.'.'.$table] = $definition = (new Table($table, $schema)) |
|
95 | 95 | ->addColumns($columns) |
96 | 96 | ->setPrimaryKey($primary) |
97 | 97 | ->setComment(''); |
@@ -101,8 +101,8 @@ discard block |
||
101 | 101 | // assuming current table is on the "one" end having "many" records in the referencing table |
102 | 102 | // resulting in a "hasMany" or "manyToMany" relationship (if a pivot table is detected) |
103 | 103 | $relations = []; |
104 | - foreach ($relationsR[$schema . '.' . $table] ?? [] as $k => $relation) { |
|
105 | - $relations[$relation['constraint_name']]['table'] = 'main.' . $relation['table']; |
|
104 | + foreach ($relationsR[$schema.'.'.$table] ?? [] as $k => $relation) { |
|
105 | + $relations[$relation['constraint_name']]['table'] = 'main.'.$relation['table']; |
|
106 | 106 | $relations[$relation['constraint_name']]['keymap'][$relation['to']] = $relation['from']; |
107 | 107 | } |
108 | 108 | foreach ($relations as $data) { |
@@ -117,11 +117,11 @@ discard block |
||
117 | 117 | $usedcol = []; |
118 | 118 | if (count($columns)) { |
119 | 119 | foreach (Collection::from($relationsT[$data['table']] ?? []) |
120 | - ->filter(function ($v) use ($columns) { |
|
120 | + ->filter(function($v) use ($columns) { |
|
121 | 121 | return in_array($v['from'], $columns); |
122 | 122 | }) as $relation |
123 | 123 | ) { |
124 | - $foreign[$relation['constraint_name']]['table'] = 'main.' . |
|
124 | + $foreign[$relation['constraint_name']]['table'] = 'main.'. |
|
125 | 125 | $relation['referenced_table']; |
126 | 126 | $foreign[$relation['constraint_name']]['keymap'][$relation['from']] = $relation['to']; |
127 | 127 | $usedcol[] = $relation['from']; |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | $definition->getName() == $relname || |
141 | 141 | $definition->getColumn($relname) |
142 | 142 | ) { |
143 | - $relname = $orig . '_' . (++ $cntr); |
|
143 | + $relname = $orig.'_'.(++$cntr); |
|
144 | 144 | } |
145 | 145 | $definition->addRelation( |
146 | 146 | new TableRelation( |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | $definition->getName() == $relname || |
165 | 165 | $definition->getColumn($relname) |
166 | 166 | ) { |
167 | - $relname = $orig . '_' . (++ $cntr); |
|
167 | + $relname = $orig.'_'.(++$cntr); |
|
168 | 168 | } |
169 | 169 | $definition->addRelation( |
170 | 170 | new TableRelation( |
@@ -180,8 +180,8 @@ discard block |
||
180 | 180 | // assuming current table is linked to "one" record in the referenced table |
181 | 181 | // resulting in a "belongsTo" relationship |
182 | 182 | $relations = []; |
183 | - foreach ($relationsT[$schema . '.' . $table] ?? [] as $relation) { |
|
184 | - $relations[$relation['constraint_name']]['table'] = 'main.' . $relation['referenced_table']; |
|
183 | + foreach ($relationsT[$schema.'.'.$table] ?? [] as $relation) { |
|
184 | + $relations[$relation['constraint_name']]['table'] = 'main.'.$relation['referenced_table']; |
|
185 | 185 | $relations[$relation['constraint_name']]['keymap'][$relation['from']] = $relation['to']; |
186 | 186 | } |
187 | 187 | foreach ($relations as $name => $data) { |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | $definition->getName() == $relname || |
197 | 197 | $definition->getColumn($relname) |
198 | 198 | ) { |
199 | - $relname = $orig . '_' . (++ $cntr); |
|
199 | + $relname = $orig.'_'.(++$cntr); |
|
200 | 200 | } |
201 | 201 | $definition->addRelation( |
202 | 202 | new TableRelation( |
@@ -217,9 +217,9 @@ discard block |
||
217 | 217 | "SELECT tbl_name |
218 | 218 | FROM sqlite_schema |
219 | 219 | WHERE (type = ? OR type = ?) AND tbl_name NOT LIKE 'sqlite_%';", |
220 | - [ 'table', 'view' ] |
|
220 | + ['table', 'view'] |
|
221 | 221 | )) |
222 | - ->mapKey(function ($v) { |
|
222 | + ->mapKey(function($v) { |
|
223 | 223 | return strtolower($v['tbl_name']); |
224 | 224 | }) |
225 | 225 | ->pluck('tbl_name') |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | $tables[$k] = $this->table($v, true, array_keys($tables))->toLowerCase(); |
229 | 229 | } |
230 | 230 | foreach (array_keys($tables) as $k) { |
231 | - $tables[($this->connection['opts']['schema'] ?? 'main') . '.' . $k] = &$tables[$k]; |
|
231 | + $tables[($this->connection['opts']['schema'] ?? 'main').'.'.$k] = &$tables[$k]; |
|
232 | 232 | } |
233 | 233 | return $tables; |
234 | 234 | } |
@@ -16,7 +16,7 @@ |
||
16 | 16 | |
17 | 17 | public function __construct( |
18 | 18 | DBInterface $db, |
19 | - Table|string $table, |
|
19 | + Table | string $table, |
|
20 | 20 | bool $findRelations = false, |
21 | 21 | ?MapperInterface $mapper = null |
22 | 22 | ) { |
@@ -48,8 +48,8 @@ |
||
48 | 48 | public function getSchema(): Schema; |
49 | 49 | public function setSchema(Schema $schema): static; |
50 | 50 | public function table(string $table, bool $findRelations = false): TableQuery; |
51 | - public function getMapper(Table|string $table): MapperInterface; |
|
52 | - public function setMapper(Table|string $table, MapperInterface $mapper): static; |
|
51 | + public function getMapper(Table | string $table): MapperInterface; |
|
52 | + public function setMapper(Table | string $table, MapperInterface $mapper): static; |
|
53 | 53 | public function tableMapped( |
54 | 54 | string $table, |
55 | 55 | bool $findRelations = false, |
@@ -69,19 +69,19 @@ discard block |
||
69 | 69 | ]; |
70 | 70 | } |
71 | 71 | } |
72 | - $connection['type'] = isset($temp['scheme']) && strlen((string)$temp['scheme']) ? $temp['scheme'] : null; |
|
73 | - $connection['user'] = isset($temp['user']) && strlen((string)$temp['user']) ? $temp['user'] : null; |
|
74 | - $connection['pass'] = isset($temp['pass']) && strlen((string)$temp['pass']) ? $temp['pass'] : null; |
|
75 | - $connection['host'] = isset($temp['host']) && strlen((string)$temp['host']) ? $temp['host'] : null; |
|
76 | - $connection['name'] = isset($temp['path']) && strlen((string)$temp['path']) ? |
|
77 | - trim((string)$temp['path'], '/') : null; |
|
78 | - $connection['port'] = isset($temp['port']) && (int)$temp['port'] ? (int)$temp['port'] : null; |
|
79 | - if (isset($temp['query']) && strlen((string)$temp['query'])) { |
|
80 | - parse_str((string)$temp['query'], $connection['opts']); |
|
72 | + $connection['type'] = isset($temp['scheme']) && strlen((string) $temp['scheme']) ? $temp['scheme'] : null; |
|
73 | + $connection['user'] = isset($temp['user']) && strlen((string) $temp['user']) ? $temp['user'] : null; |
|
74 | + $connection['pass'] = isset($temp['pass']) && strlen((string) $temp['pass']) ? $temp['pass'] : null; |
|
75 | + $connection['host'] = isset($temp['host']) && strlen((string) $temp['host']) ? $temp['host'] : null; |
|
76 | + $connection['name'] = isset($temp['path']) && strlen((string) $temp['path']) ? |
|
77 | + trim((string) $temp['path'], '/') : null; |
|
78 | + $connection['port'] = isset($temp['port']) && (int) $temp['port'] ? (int) $temp['port'] : null; |
|
79 | + if (isset($temp['query']) && strlen((string) $temp['query'])) { |
|
80 | + parse_str((string) $temp['query'], $connection['opts']); |
|
81 | 81 | } |
82 | 82 | // create the driver |
83 | 83 | $connection['type'] = $aliases[$connection['type']] ?? $connection['type']; |
84 | - $tmp = '\\vakata\\database\\driver\\'.strtolower((string)$connection['type']).'\\Driver'; |
|
84 | + $tmp = '\\vakata\\database\\driver\\'.strtolower((string) $connection['type']).'\\Driver'; |
|
85 | 85 | if (!class_exists($tmp)) { |
86 | 86 | throw new DBException('Unknown DB backend'); |
87 | 87 | } |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | $new = ''; |
122 | 122 | $par = array_values($par); |
123 | 123 | if (substr_count($sql, '?') === 2 && !is_array($par[0])) { |
124 | - $par = [ $par ]; |
|
124 | + $par = [$par]; |
|
125 | 125 | } |
126 | 126 | $parts = explode('??', $sql); |
127 | 127 | $index = 0; |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | $index += count($tmp) - 1; |
132 | 132 | if (isset($par[$index])) { |
133 | 133 | if (!is_array($par[$index])) { |
134 | - $par[$index] = [ $par[$index] ]; |
|
134 | + $par[$index] = [$par[$index]]; |
|
135 | 135 | } |
136 | 136 | $params = $par[$index]; |
137 | 137 | array_splice($par, $index, 1, $params); |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | $new .= implode(',', array_fill(0, count($params), '?')); |
140 | 140 | } |
141 | 141 | } |
142 | - return [ $new, $par ]; |
|
142 | + return [$new, $par]; |
|
143 | 143 | } |
144 | 144 | /** |
145 | 145 | * Run a query (prepare & execute). |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | ): Collection { |
189 | 189 | $coll = Collection::from($this->query($sql, $par, $buff)); |
190 | 190 | if (($keys = $this->driver->option('mode')) && in_array($keys, ['strtoupper', 'strtolower'])) { |
191 | - $coll->map(function ($v) use ($keys) { |
|
191 | + $coll->map(function($v) use ($keys) { |
|
192 | 192 | $new = []; |
193 | 193 | foreach ($v as $k => $vv) { |
194 | 194 | $new[call_user_func($keys, $k)] = $vv; |
@@ -197,18 +197,18 @@ discard block |
||
197 | 197 | }); |
198 | 198 | } |
199 | 199 | if ($key !== null) { |
200 | - $coll->mapKey(function ($v) use ($key) { |
|
200 | + $coll->mapKey(function($v) use ($key) { |
|
201 | 201 | return $v[$key]; |
202 | 202 | }); |
203 | 203 | } |
204 | 204 | if ($skip) { |
205 | - $coll->map(function ($v) use ($key) { |
|
205 | + $coll->map(function($v) use ($key) { |
|
206 | 206 | unset($v[$key]); |
207 | 207 | return $v; |
208 | 208 | }); |
209 | 209 | } |
210 | 210 | if ($opti) { |
211 | - $coll->map(function ($v) { |
|
211 | + $coll->map(function($v) { |
|
212 | 212 | return count($v) === 1 ? current($v) : $v; |
213 | 213 | }); |
214 | 214 | } |
@@ -317,8 +317,7 @@ discard block |
||
317 | 317 | public function definition(string $table, bool $detectRelations = true) : Table |
318 | 318 | { |
319 | 319 | return isset($this->schema) ? |
320 | - $this->schema->getTable($table) : |
|
321 | - $this->driver->table($table, $detectRelations); |
|
320 | + $this->schema->getTable($table) : $this->driver->table($table, $detectRelations); |
|
322 | 321 | } |
323 | 322 | |
324 | 323 | public function hasSchema(): bool |
@@ -363,14 +362,14 @@ discard block |
||
363 | 362 | { |
364 | 363 | return new TableQuery($this, $this->definition($table), $findRelations); |
365 | 364 | } |
366 | - public function getMapper(Table|string $table): MapperInterface |
|
365 | + public function getMapper(Table | string $table): MapperInterface |
|
367 | 366 | { |
368 | 367 | if (is_string($table)) { |
369 | 368 | $table = $this->definition($table); |
370 | 369 | } |
371 | 370 | return $this->mappers[$table->getFullName()] ?? $this->mappers['*']; |
372 | 371 | } |
373 | - public function setMapper(Table|string $table, MapperInterface $mapper): static |
|
372 | + public function setMapper(Table | string $table, MapperInterface $mapper): static |
|
374 | 373 | { |
375 | 374 | if (is_string($table)) { |
376 | 375 | $table = $this->definition($table); |
@@ -386,11 +385,10 @@ discard block |
||
386 | 385 | { |
387 | 386 | return new TableQueryMapped($this, $this->definition($table), $findRelations, $mapper); |
388 | 387 | } |
389 | - public function __call(string $method, array $args): TableQuery|TableQueryMapped |
|
388 | + public function __call(string $method, array $args): TableQuery | TableQueryMapped |
|
390 | 389 | { |
391 | 390 | return ($args[0] ?? false) ? |
392 | - $this->tableMapped($method, $args[1] ?? false, $args[2] ?? null) : |
|
393 | - $this->table($method, $args[1] ?? false); |
|
391 | + $this->tableMapped($method, $args[1] ?? false, $args[2] ?? null) : $this->table($method, $args[1] ?? false); |
|
394 | 392 | } |
395 | 393 | public function findRelation(string $start, string $end): array |
396 | 394 | { |
@@ -426,12 +424,12 @@ discard block |
||
426 | 424 | $relations[$t] = $w; |
427 | 425 | } |
428 | 426 | if (!isset($schema[$name])) { |
429 | - $schema[$name] = [ 'edges' => [] ]; |
|
427 | + $schema[$name] = ['edges' => []]; |
|
430 | 428 | } |
431 | 429 | foreach ($relations as $t => $w) { |
432 | 430 | $schema[$name]['edges'][$t] = $w; |
433 | 431 | if (!isset($schema[$t])) { |
434 | - $schema[$t] = [ 'edges' => [] ]; |
|
432 | + $schema[$t] = ['edges' => []]; |
|
435 | 433 | } |
436 | 434 | $schema[$t]['edges'][$name] = $w; |
437 | 435 | } |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | public function collection(TableQueryIterator $iterator, Table $definition) : Collection |
65 | 65 | { |
66 | 66 | return Collection::from($iterator) |
67 | - ->map(function ($v) use ($definition) { |
|
67 | + ->map(function($v) use ($definition) { |
|
68 | 68 | return $this->entity($definition, $v); |
69 | 69 | }); |
70 | 70 | } |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | { |
73 | 73 | $query = $this->db->tableMapped($relation->table->getFullName()); |
74 | 74 | if ($relation->sql) { |
75 | - $query->where($relation->sql, $relation->par?:[]); |
|
75 | + $query->where($relation->sql, $relation->par ?: []); |
|
76 | 76 | } |
77 | 77 | if ($relation->pivot) { |
78 | 78 | $nm = null; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | ); |
93 | 93 | } |
94 | 94 | foreach ($definition->getPrimaryKey() as $v) { |
95 | - $query->filter($nm . '.' . $v, $entity->{$v} ?? null); |
|
95 | + $query->filter($nm.'.'.$v, $entity->{$v} ?? null); |
|
96 | 96 | } |
97 | 97 | } else { |
98 | 98 | foreach ($relation->keymap as $k => $v) { |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | $definition = $entity->definition(); |
182 | 182 | foreach ($definition->getColumns() as $column) { |
183 | 183 | if (!array_key_exists($column, $data)) { |
184 | - $entity->__lazyProperty($column, function () use ($definition, $primary, $column) { |
|
184 | + $entity->__lazyProperty($column, function() use ($definition, $primary, $column) { |
|
185 | 185 | $query = $this->db->table($definition->getFullName()); |
186 | 186 | foreach ($primary as $k => $v) { |
187 | 187 | $query->filter($k, $v); |
@@ -196,12 +196,12 @@ discard block |
||
196 | 196 | $name, |
197 | 197 | array_key_exists($name, $data) && isset($data[$name]) ? |
198 | 198 | ($relation->many ? |
199 | - array_map(function ($v) use ($relation, $mapper) { |
|
199 | + array_map(function($v) use ($relation, $mapper) { |
|
200 | 200 | return $mapper->entity($relation->table, $v); |
201 | 201 | }, $data[$name]) : |
202 | 202 | $mapper->entity($relation->table, $data[$name]) |
203 | 203 | ) : |
204 | - function ( |
|
204 | + function( |
|
205 | 205 | array $columns = null, |
206 | 206 | string $order = null, |
207 | 207 | bool $desc = false |
@@ -218,8 +218,7 @@ discard block |
||
218 | 218 | $query->sort($order, $desc); |
219 | 219 | } |
220 | 220 | return $relation->many ? |
221 | - $query->iterator() : |
|
222 | - $query[0]; |
|
221 | + $query->iterator() : $query[0]; |
|
223 | 222 | } |
224 | 223 | ); |
225 | 224 | } |