| Conditions | 31 |
| Paths | 6 |
| Total Lines | 240 |
| Code Lines | 161 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 127 | public function table( |
||
| 128 | string $table, |
||
| 129 | bool $detectRelations = true |
||
| 130 | ) : Table |
||
| 131 | { |
||
| 132 | static $tables = []; |
||
| 133 | if (isset($tables[$table])) { |
||
| 134 | return $tables[$table]; |
||
| 135 | } |
||
| 136 | |||
| 137 | $columns = Collection::from($this |
||
| 138 | ->query( |
||
| 139 | "SELECT * FROM all_tab_cols WHERE table_name = ? AND owner = ?", |
||
| 140 | [ strtoupper($table), $this->name() ] |
||
| 141 | )) |
||
| 142 | ->map(function ($v) { |
||
| 143 | $new = []; |
||
| 144 | foreach ($v as $kk => $vv) { |
||
| 145 | $new[strtoupper($kk)] = $vv; |
||
| 146 | } |
||
| 147 | return $new; |
||
| 148 | }) |
||
| 149 | ->mapKey(function ($v) { return $v['COLUMN_NAME']; }) |
||
| 150 | ->toArray(); |
||
| 151 | if (!count($columns)) { |
||
| 152 | throw new DBException('Table not found by name'); |
||
| 153 | } |
||
| 154 | $owner = $this->name(); // used to be the current column's OWNER |
||
| 155 | $pkname = Collection::from($this |
||
| 156 | ->query( |
||
| 157 | "SELECT constraint_name FROM all_constraints |
||
| 158 | WHERE table_name = ? AND constraint_type = ? AND owner = ?", |
||
| 159 | [ strtoupper($table), 'P', $owner ] |
||
| 160 | )) |
||
| 161 | ->map(function ($v) { |
||
| 162 | $new = []; |
||
| 163 | foreach ($v as $kk => $vv) { |
||
| 164 | $new[strtoupper($kk)] = $vv; |
||
| 165 | } |
||
| 166 | return $new; |
||
| 167 | }) |
||
| 168 | ->pluck('CONSTRAINT_NAME') |
||
| 169 | ->value(); |
||
| 170 | $primary = []; |
||
| 171 | if ($pkname) { |
||
| 172 | $primary = Collection::from($this |
||
| 173 | ->query( |
||
| 174 | "SELECT column_name FROM all_cons_columns |
||
| 175 | WHERE table_name = ? AND constraint_name = ? AND owner = ?", |
||
| 176 | [ strtoupper($table), $pkname, $owner ] |
||
| 177 | )) |
||
| 178 | ->map(function ($v) { |
||
| 179 | $new = []; |
||
| 180 | foreach ($v as $kk => $vv) { |
||
| 181 | $new[strtoupper($kk)] = $vv; |
||
| 182 | } |
||
| 183 | return $new; |
||
| 184 | }) |
||
| 185 | ->pluck('COLUMN_NAME') |
||
| 186 | ->toArray(); |
||
| 187 | } |
||
| 188 | $tables[$table] = $definition = (new Table($table)) |
||
| 189 | ->addColumns($columns) |
||
| 190 | ->setPrimaryKey($primary) |
||
| 191 | ->setComment(''); |
||
| 192 | |||
| 193 | if ($detectRelations) { |
||
| 194 | // relations where the current table is referenced |
||
| 195 | // assuming current table is on the "one" end having "many" records in the referencing table |
||
| 196 | // resulting in a "hasMany" or "manyToMany" relationship (if a pivot table is detected) |
||
| 197 | $relations = []; |
||
| 198 | foreach (Collection::from($this |
||
| 199 | ->query( |
||
| 200 | "SELECT ac.TABLE_NAME, ac.CONSTRAINT_NAME, cc.COLUMN_NAME, cc.POSITION |
||
| 201 | FROM all_constraints ac |
||
| 202 | LEFT JOIN all_cons_columns cc ON cc.OWNER = ac.OWNER AND cc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME |
||
| 203 | WHERE ac.OWNER = ? AND ac.R_OWNER = ? AND ac.R_CONSTRAINT_NAME = ? AND ac.CONSTRAINT_TYPE = ? |
||
| 204 | ORDER BY cc.POSITION", |
||
| 205 | [ $owner, $owner, $pkname, 'R' ] |
||
| 206 | )) |
||
| 207 | ->map(function ($v) { |
||
| 208 | $new = []; |
||
| 209 | foreach ($v as $kk => $vv) { |
||
| 210 | $new[strtoupper($kk)] = $vv; |
||
| 211 | } |
||
| 212 | return $new; |
||
| 213 | }) |
||
| 214 | as $relation |
||
| 215 | ) { |
||
| 216 | $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['TABLE_NAME']; |
||
| 217 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$primary[(int)$relation['POSITION']-1]] = $relation['COLUMN_NAME']; |
||
| 218 | } |
||
| 219 | foreach ($relations as $data) { |
||
| 220 | $rtable = $this->table($data['table'], true); |
||
| 221 | $columns = []; |
||
| 222 | foreach ($rtable->getColumns() as $column) { |
||
| 223 | if (!in_array($column, $data['keymap'])) { |
||
| 224 | $columns[] = $column; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | $foreign = []; |
||
| 228 | $usedcol = []; |
||
| 229 | if (count($columns)) { |
||
| 230 | foreach (Collection::from($this |
||
| 231 | ->query( |
||
| 232 | "SELECT |
||
| 233 | cc.COLUMN_NAME, ac.CONSTRAINT_NAME, rc.TABLE_NAME AS REFERENCED_TABLE_NAME, ac.R_CONSTRAINT_NAME |
||
| 234 | FROM all_constraints ac |
||
| 235 | JOIN all_constraints rc ON rc.CONSTRAINT_NAME = ac.R_CONSTRAINT_NAME AND rc.OWNER = ac.OWNER |
||
| 236 | LEFT JOIN all_cons_columns cc ON cc.OWNER = ac.OWNER AND cc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME |
||
| 237 | WHERE |
||
| 238 | ac.OWNER = ? AND ac.R_OWNER = ? AND ac.TABLE_NAME = ? AND ac.CONSTRAINT_TYPE = ? AND |
||
| 239 | cc.COLUMN_NAME IN (??) |
||
| 240 | ORDER BY POSITION", |
||
| 241 | [ $owner, $owner, $data['table'], 'R', $columns ] |
||
| 242 | )) |
||
| 243 | ->map(function ($v) { |
||
| 244 | $new = []; |
||
| 245 | foreach ($v as $kk => $vv) { |
||
| 246 | $new[strtoupper($kk)] = $vv; |
||
| 247 | } |
||
| 248 | return $new; |
||
| 249 | }) as $relation |
||
| 250 | ) { |
||
| 251 | $foreign[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_NAME']; |
||
| 252 | $foreign[$relation['CONSTRAINT_NAME']]['keymap'][$relation['COLUMN_NAME']] = $relation['R_CONSTRAINT_NAME']; |
||
| 253 | $usedcol[] = $relation['COLUMN_NAME']; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | if (count($foreign) === 1 && !count(array_diff($columns, $usedcol))) { |
||
| 257 | $foreign = current($foreign); |
||
| 258 | $rcolumns = Collection::from($this |
||
| 259 | ->query( |
||
| 260 | "SELECT COLUMN_NAME FROM all_cons_columns WHERE OWNER = ? AND CONSTRAINT_NAME = ? ORDER BY POSITION", |
||
| 261 | [ $owner, current($foreign['keymap']) ] |
||
| 262 | )) |
||
| 263 | ->map(function ($v) { |
||
| 264 | $new = []; |
||
| 265 | foreach ($v as $kk => $vv) { |
||
| 266 | $new[strtoupper($kk)] = $vv; |
||
| 267 | } |
||
| 268 | return $new; |
||
| 269 | }) |
||
| 270 | ->pluck('COLUMN_NAME') |
||
| 271 | ->toArray(); |
||
| 272 | foreach ($foreign['keymap'] as $column => $related) { |
||
| 273 | $foreign['keymap'][$column] = array_shift($rcolumns); |
||
| 274 | } |
||
| 275 | $relname = $foreign['table']; |
||
| 276 | $cntr = 1; |
||
| 277 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 278 | $relname = $foreign['table'] . '_' . (++ $cntr); |
||
| 279 | } |
||
| 280 | $definition->addRelation( |
||
| 281 | new TableRelation( |
||
| 282 | $relname, |
||
| 283 | $this->table($foreign['table'], true), |
||
| 284 | $data['keymap'], |
||
| 285 | true, |
||
| 286 | $rtable, |
||
| 287 | $foreign['keymap'] |
||
| 288 | ) |
||
| 289 | ); |
||
| 290 | } else { |
||
| 291 | $relname = $data['table']; |
||
| 292 | $cntr = 1; |
||
| 293 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 294 | $relname = $data['table'] . '_' . (++ $cntr); |
||
| 295 | } |
||
| 296 | $definition->addRelation( |
||
| 297 | new TableRelation( |
||
| 298 | $relname, |
||
| 299 | $this->table($data['table'], true), |
||
| 300 | $data['keymap'], |
||
| 301 | true |
||
| 302 | ) |
||
| 303 | ); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | // relations where the current table references another table |
||
| 307 | // assuming current table is linked to "one" record in the referenced table |
||
| 308 | // resulting in a "belongsTo" relationship |
||
| 309 | $relations = []; |
||
| 310 | foreach (Collection::from($this |
||
| 311 | ->query( |
||
| 312 | "SELECT ac.CONSTRAINT_NAME, cc.COLUMN_NAME, rc.TABLE_NAME AS REFERENCED_TABLE_NAME, ac.R_CONSTRAINT_NAME |
||
| 313 | FROM all_constraints ac |
||
| 314 | JOIN all_constraints rc ON rc.CONSTRAINT_NAME = ac.R_CONSTRAINT_NAME AND rc.OWNER = ac.OWNER |
||
| 315 | LEFT JOIN all_cons_columns cc ON cc.OWNER = ac.OWNER AND cc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME |
||
| 316 | WHERE ac.OWNER = ? AND ac.R_OWNER = ? AND ac.TABLE_NAME = ? AND ac.CONSTRAINT_TYPE = ? |
||
| 317 | ORDER BY cc.POSITION", |
||
| 318 | [ $owner, $owner, strtoupper($table), 'R' ] |
||
| 319 | )) |
||
| 320 | ->map(function ($v) { |
||
| 321 | $new = []; |
||
| 322 | foreach ($v as $kk => $vv) { |
||
| 323 | $new[strtoupper($kk)] = $vv; |
||
| 324 | } |
||
| 325 | return $new; |
||
| 326 | }) |
||
| 327 | as $relation |
||
| 328 | ) { |
||
| 329 | $relations[$relation['CONSTRAINT_NAME']]['table'] = $relation['REFERENCED_TABLE_NAME']; |
||
| 330 | $relations[$relation['CONSTRAINT_NAME']]['keymap'][$relation['COLUMN_NAME']] = $relation['R_CONSTRAINT_NAME']; |
||
| 331 | } |
||
| 332 | foreach ($relations as $name => $data) { |
||
| 333 | $rcolumns = Collection::from($this |
||
| 334 | ->query( |
||
| 335 | "SELECT COLUMN_NAME FROM all_cons_columns WHERE OWNER = ? AND CONSTRAINT_NAME = ? ORDER BY POSITION", |
||
| 336 | [ $owner, current($data['keymap']) ] |
||
| 337 | )) |
||
| 338 | ->map(function ($v) { |
||
| 339 | $new = []; |
||
| 340 | foreach ($v as $kk => $vv) { |
||
| 341 | $new[strtoupper($kk)] = $vv; |
||
| 342 | } |
||
| 343 | return $new; |
||
| 344 | }) |
||
| 345 | ->pluck('COLUMN_NAME') |
||
| 346 | ->toArray(); |
||
| 347 | foreach ($data['keymap'] as $column => $related) { |
||
| 348 | $data['keymap'][$column] = array_shift($rcolumns); |
||
| 349 | } |
||
| 350 | $relname = $data['table']; |
||
| 351 | $cntr = 1; |
||
| 352 | while ($definition->hasRelation($relname) || $definition->getName() == $relname) { |
||
| 353 | $relname = $data['table'] . '_' . (++ $cntr); |
||
| 354 | } |
||
| 355 | $definition->addRelation( |
||
| 356 | new TableRelation( |
||
| 357 | $relname, |
||
| 358 | $this->table($data['table'], true), |
||
| 359 | $data['keymap'], |
||
| 360 | false |
||
| 361 | ) |
||
| 362 | ); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | return $definition->toLowerCase(); |
||
| 366 | } |
||
| 367 | public function tables() : array |
||
| 388 |