| Conditions | 2 |
| Paths | 2 |
| Total Lines | 339 |
| Code Lines | 185 |
| 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 |
||
| 242 | public function getBuildConditions(ConnectionInterface $db): array |
||
| 243 | { |
||
| 244 | $conditions = [ |
||
| 245 | /* empty values */ |
||
| 246 | [['like', 'name', []], '0=1', []], |
||
| 247 | [['not like', 'name', []], '', []], |
||
| 248 | [['or like', 'name', []], '0=1', []], |
||
| 249 | [['or not like', 'name', []], '', []], |
||
| 250 | |||
| 251 | /* not */ |
||
| 252 | [['not', 'name'], 'NOT (name)', []], |
||
| 253 | [ |
||
| 254 | [ |
||
| 255 | 'not', |
||
| 256 | (new Query($db))->select('exists')->from('some_table'), |
||
| 257 | ], |
||
| 258 | 'NOT ((SELECT [[exists]] FROM [[some_table]]))', [], |
||
| 259 | ], |
||
| 260 | |||
| 261 | /* and */ |
||
| 262 | [['and', 'id=1', 'id=2'], '(id=1) AND (id=2)', []], |
||
| 263 | [['and', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) AND ((id=1) OR (id=2))', []], |
||
| 264 | [['and', 'id=1', new Expression('id=:qp0', [':qp0' => 2])], '(id=1) AND (id=:qp0)', [':qp0' => 2]], |
||
| 265 | [ |
||
| 266 | [ |
||
| 267 | 'and', |
||
| 268 | ['expired' => false], |
||
| 269 | (new Query($db))->select('count(*) > 1')->from('queue'), |
||
| 270 | ], |
||
| 271 | '([[expired]]=:qp0) AND ((SELECT count(*) > 1 FROM [[queue]]))', |
||
| 272 | [':qp0' => false], |
||
| 273 | ], |
||
| 274 | |||
| 275 | /* or */ |
||
| 276 | [['or', 'id=1', 'id=2'], '(id=1) OR (id=2)', []], |
||
| 277 | [['or', 'type=1', ['or', 'id=1', 'id=2']], '(type=1) OR ((id=1) OR (id=2))', []], |
||
| 278 | [['or', 'type=1', new Expression('id=:qp0', [':qp0' => 1])], '(type=1) OR (id=:qp0)', [':qp0' => 1]], |
||
| 279 | |||
| 280 | /* between */ |
||
| 281 | [['between', 'id', 1, 10], '[[id]] BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
| 282 | [['not between', 'id', 1, 10], '[[id]] NOT BETWEEN :qp0 AND :qp1', [':qp0' => 1, ':qp1' => 10]], |
||
| 283 | [ |
||
| 284 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
| 285 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
| 286 | [], |
||
| 287 | ], |
||
| 288 | [ |
||
| 289 | ['between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
| 290 | '[[date]] BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
| 291 | [':qp0' => 123], |
||
| 292 | ], |
||
| 293 | [ |
||
| 294 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), new Expression('NOW()')], |
||
| 295 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()', |
||
| 296 | [], |
||
| 297 | ], |
||
| 298 | [ |
||
| 299 | ['not between', 'date', new Expression('(NOW() - INTERVAL 1 MONTH)'), 123], |
||
| 300 | '[[date]] NOT BETWEEN (NOW() - INTERVAL 1 MONTH) AND :qp0', |
||
| 301 | [':qp0' => 123], |
||
| 302 | ], |
||
| 303 | [ |
||
| 304 | new BetweenColumnsCondition('2018-02-11', 'BETWEEN', 'create_time', 'update_time'), |
||
| 305 | ':qp0 BETWEEN [[create_time]] AND [[update_time]]', |
||
| 306 | [':qp0' => '2018-02-11'], |
||
| 307 | ], |
||
| 308 | [ |
||
| 309 | new BetweenColumnsCondition('2018-02-11', 'NOT BETWEEN', 'NOW()', 'update_time'), |
||
| 310 | ':qp0 NOT BETWEEN NOW() AND [[update_time]]', |
||
| 311 | [':qp0' => '2018-02-11'], |
||
| 312 | ], |
||
| 313 | [ |
||
| 314 | new BetweenColumnsCondition(new Expression('NOW()'), 'BETWEEN', 'create_time', 'update_time'), |
||
| 315 | 'NOW() BETWEEN [[create_time]] AND [[update_time]]', |
||
| 316 | [], |
||
| 317 | ], |
||
| 318 | [ |
||
| 319 | new BetweenColumnsCondition(new Expression('NOW()'), 'NOT BETWEEN', 'create_time', 'update_time'), |
||
| 320 | 'NOW() NOT BETWEEN [[create_time]] AND [[update_time]]', |
||
| 321 | [], |
||
| 322 | ], |
||
| 323 | [ |
||
| 324 | new BetweenColumnsCondition( |
||
| 325 | new Expression('NOW()'), |
||
| 326 | 'NOT BETWEEN', |
||
| 327 | (new Query($db))->select('min_date')->from('some_table'), |
||
| 328 | 'max_date' |
||
| 329 | ), |
||
| 330 | 'NOW() NOT BETWEEN (SELECT [[min_date]] FROM [[some_table]]) AND [[max_date]]', |
||
| 331 | [], |
||
| 332 | ], |
||
| 333 | |||
| 334 | /* in */ |
||
| 335 | [ |
||
| 336 | [ |
||
| 337 | 'in', |
||
| 338 | 'id', |
||
| 339 | [ |
||
| 340 | 1, |
||
| 341 | 2, |
||
| 342 | (new Query($db))->select('three')->from('digits'), |
||
| 343 | ], |
||
| 344 | ], |
||
| 345 | '[[id]] IN (:qp0, :qp1, (SELECT [[three]] FROM [[digits]]))', |
||
| 346 | [':qp0' => 1, ':qp1' => 2], |
||
| 347 | ], |
||
| 348 | [ |
||
| 349 | ['not in', 'id', [1, 2, 3]], |
||
| 350 | '[[id]] NOT IN (:qp0, :qp1, :qp2)', |
||
| 351 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
| 352 | ], |
||
| 353 | [ |
||
| 354 | [ |
||
| 355 | 'in', |
||
| 356 | 'id', |
||
| 357 | (new Query($db))->select('id')->from('users')->where(['active' => 1]), |
||
| 358 | ], |
||
| 359 | '[[id]] IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 360 | [':qp0' => 1], |
||
| 361 | ], |
||
| 362 | [ |
||
| 363 | [ |
||
| 364 | 'not in', |
||
| 365 | 'id', |
||
| 366 | (new Query($db))->select('id')->from('users')->where(['active' => 1]), |
||
| 367 | ], |
||
| 368 | '[[id]] NOT IN (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 369 | [':qp0' => 1], |
||
| 370 | ], |
||
| 371 | [['in', 'id', 1], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 372 | [['in', 'id', [1]], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 373 | [['in', 'id', new TraversableObject([1])], '[[id]]=:qp0', [':qp0' => 1]], |
||
| 374 | 'composite in' => [ |
||
| 375 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'oy']]], |
||
| 376 | '([[id]], [[name]]) IN ((:qp0, :qp1))', |
||
| 377 | [':qp0' => 1, ':qp1' => 'oy'], |
||
| 378 | ], |
||
| 379 | 'composite in (just one column)' => [ |
||
| 380 | ['in', ['id'], [['id' => 1, 'name' => 'Name1'], ['id' => 2, 'name' => 'Name2']]], |
||
| 381 | '[[id]] IN (:qp0, :qp1)', |
||
| 382 | [':qp0' => 1, ':qp1' => 2], |
||
| 383 | ], |
||
| 384 | 'composite in using array objects (just one column)' => [ |
||
| 385 | ['in', new TraversableObject(['id']), new TraversableObject([ |
||
| 386 | ['id' => 1, 'name' => 'Name1'], |
||
| 387 | ['id' => 2, 'name' => 'Name2'], |
||
| 388 | ])], |
||
| 389 | '[[id]] IN (:qp0, :qp1)', |
||
| 390 | [':qp0' => 1, ':qp1' => 2], |
||
| 391 | ], |
||
| 392 | |||
| 393 | /* in using array objects. */ |
||
| 394 | [['id' => new TraversableObject([1, 2])], '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 395 | [ |
||
| 396 | ['in', 'id', new TraversableObject([1, 2, 3])], |
||
| 397 | '[[id]] IN (:qp0, :qp1, :qp2)', |
||
| 398 | [':qp0' => 1, ':qp1' => 2, ':qp2' => 3], |
||
| 399 | ], |
||
| 400 | |||
| 401 | /* in using array objects containing null value */ |
||
| 402 | [['in', 'id', new TraversableObject([1, null])], '[[id]]=:qp0 OR [[id]] IS NULL', [':qp0' => 1]], |
||
| 403 | [ |
||
| 404 | ['in', 'id', new TraversableObject([1, 2, null])], |
||
| 405 | '[[id]] IN (:qp0, :qp1) OR [[id]] IS NULL', [':qp0' => 1, ':qp1' => 2], |
||
| 406 | ], |
||
| 407 | |||
| 408 | /* not in using array object containing null value */ |
||
| 409 | [ |
||
| 410 | ['not in', 'id', new TraversableObject([1, null])], |
||
| 411 | '[[id]]<>:qp0 AND [[id]] IS NOT NULL', [':qp0' => 1], |
||
| 412 | ], |
||
| 413 | [ |
||
| 414 | ['not in', 'id', new TraversableObject([1, 2, null])], |
||
| 415 | '[[id]] NOT IN (:qp0, :qp1) AND [[id]] IS NOT NULL', |
||
| 416 | [':qp0' => 1, ':qp1' => 2], |
||
| 417 | ], |
||
| 418 | |||
| 419 | /* in using array object containing only null value */ |
||
| 420 | [['in', 'id', new TraversableObject([null])], '[[id]] IS NULL', []], |
||
| 421 | [['not in', 'id', new TraversableObject([null])], '[[id]] IS NOT NULL', []], |
||
| 422 | 'composite in using array objects' => [ |
||
| 423 | ['in', new TraversableObject(['id', 'name']), new TraversableObject([ |
||
| 424 | ['id' => 1, 'name' => 'oy'], |
||
| 425 | ['id' => 2, 'name' => 'yo'], |
||
| 426 | ])], |
||
| 427 | '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 428 | [':qp0' => 1, ':qp1' => 'oy', ':qp2' => 2, ':qp3' => 'yo'], |
||
| 429 | ], |
||
| 430 | |||
| 431 | /* in object conditions */ |
||
| 432 | [new InCondition('id', 'in', 1), '[[id]]=:qp0', [':qp0' => 1]], |
||
| 433 | [new InCondition('id', 'in', [1]), '[[id]]=:qp0', [':qp0' => 1]], |
||
| 434 | [new InCondition('id', 'not in', 1), '[[id]]<>:qp0', [':qp0' => 1]], |
||
| 435 | [new InCondition('id', 'not in', [1]), '[[id]]<>:qp0', [':qp0' => 1]], |
||
| 436 | [new InCondition('id', 'in', [1, 2]), '[[id]] IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 437 | [new InCondition('id', 'not in', [1, 2]), '[[id]] NOT IN (:qp0, :qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 438 | |||
| 439 | /* exists */ |
||
| 440 | [ |
||
| 441 | [ |
||
| 442 | 'exists', |
||
| 443 | (new Query($db))->select('id')->from('users')->where(['active' => 1]), |
||
| 444 | ], |
||
| 445 | 'EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 446 | [':qp0' => 1], |
||
| 447 | ], |
||
| 448 | [ |
||
| 449 | [ |
||
| 450 | 'not exists', |
||
| 451 | (new Query($db))->select('id')->from('users')->where(['active' => 1]), |
||
| 452 | ], |
||
| 453 | 'NOT EXISTS (SELECT [[id]] FROM [[users]] WHERE [[active]]=:qp0)', [':qp0' => 1], |
||
| 454 | ], |
||
| 455 | |||
| 456 | /* simple conditions */ |
||
| 457 | [['=', 'a', 'b'], '[[a]] = :qp0', [':qp0' => 'b']], |
||
| 458 | [['>', 'a', 1], '[[a]] > :qp0', [':qp0' => 1]], |
||
| 459 | [['>=', 'a', 'b'], '[[a]] >= :qp0', [':qp0' => 'b']], |
||
| 460 | [['<', 'a', 2], '[[a]] < :qp0', [':qp0' => 2]], |
||
| 461 | [['<=', 'a', 'b'], '[[a]] <= :qp0', [':qp0' => 'b']], |
||
| 462 | [['<>', 'a', 3], '[[a]] <> :qp0', [':qp0' => 3]], |
||
| 463 | [['!=', 'a', 'b'], '[[a]] != :qp0', [':qp0' => 'b']], |
||
| 464 | [ |
||
| 465 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL 1 MONTH)')], |
||
| 466 | '[[date]] >= DATE_SUB(NOW(), INTERVAL 1 MONTH)', |
||
| 467 | [], |
||
| 468 | ], |
||
| 469 | [ |
||
| 470 | ['>=', 'date', new Expression('DATE_SUB(NOW(), INTERVAL :month MONTH)', [':month' => 2])], |
||
| 471 | '[[date]] >= DATE_SUB(NOW(), INTERVAL :month MONTH)', |
||
| 472 | [':month' => 2], |
||
| 473 | ], |
||
| 474 | [ |
||
| 475 | [ |
||
| 476 | '=', |
||
| 477 | 'date', |
||
| 478 | (new Query($db))->select('max(date)')->from('test')->where(['id' => 5]), |
||
| 479 | ], |
||
| 480 | '[[date]] = (SELECT max(date) FROM [[test]] WHERE [[id]]=:qp0)', |
||
| 481 | [':qp0' => 5], |
||
| 482 | ], |
||
| 483 | |||
| 484 | /* operand1 is Expression */ |
||
| 485 | [ |
||
| 486 | ['=', new Expression('date'), '2019-08-01'], |
||
| 487 | 'date = :qp0', |
||
| 488 | [':qp0' => '2019-08-01'], |
||
| 489 | ], |
||
| 490 | [ |
||
| 491 | [ |
||
| 492 | '=', |
||
| 493 | (new Query($db))->select('COUNT(*)')->from('test')->where(['id' => 6]), |
||
| 494 | 0, |
||
| 495 | ], |
||
| 496 | '(SELECT COUNT(*) FROM [[test]] WHERE [[id]]=:qp0) = :qp1', |
||
| 497 | [':qp0' => 6, ':qp1' => 0], |
||
| 498 | ], |
||
| 499 | |||
| 500 | /* hash condition */ |
||
| 501 | [['a' => 1, 'b' => 2], '([[a]]=:qp0) AND ([[b]]=:qp1)', [':qp0' => 1, ':qp1' => 2]], |
||
| 502 | [ |
||
| 503 | ['a' => new Expression('CONCAT(col1, col2)'), 'b' => 2], |
||
| 504 | '([[a]]=CONCAT(col1, col2)) AND ([[b]]=:qp0)', |
||
| 505 | [':qp0' => 2], |
||
| 506 | ], |
||
| 507 | |||
| 508 | /* direct conditions */ |
||
| 509 | ['a = CONCAT(col1, col2)', 'a = CONCAT(col1, col2)', []], |
||
| 510 | [ |
||
| 511 | new Expression('a = CONCAT(col1, :param1)', ['param1' => 'value1']), |
||
| 512 | 'a = CONCAT(col1, :param1)', |
||
| 513 | ['param1' => 'value1'], |
||
| 514 | ], |
||
| 515 | |||
| 516 | /* Expression with params as operand of 'not' */ |
||
| 517 | [ |
||
| 518 | ['not', new Expression('any_expression(:a)', [':a' => 1])], |
||
| 519 | 'NOT (any_expression(:a))', [':a' => 1], |
||
| 520 | ], |
||
| 521 | [new Expression('NOT (any_expression(:a))', [':a' => 1]), 'NOT (any_expression(:a))', [':a' => 1]], |
||
| 522 | ]; |
||
| 523 | |||
| 524 | $conditions = match ($db->getName()) { |
||
| 525 | 'sqlsrv', 'sqlite' => array_merge($conditions, [ |
||
| 526 | [ |
||
| 527 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 528 | '(([[id]] = :qp0 AND [[name]] = :qp1) OR ([[id]] = :qp2 AND [[name]] = :qp3))', |
||
| 529 | [':qp0' => 1, |
||
| 530 | ':qp1' => 'foo', |
||
| 531 | ':qp2' => 2, |
||
| 532 | ':qp3' => 'bar',], |
||
| 533 | ], |
||
| 534 | [ |
||
| 535 | ['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 536 | '(([[id]] != :qp0 OR [[name]] != :qp1) AND ([[id]] != :qp2 OR [[name]] != :qp3))', |
||
| 537 | [':qp0' => 1, |
||
| 538 | ':qp1' => 'foo', |
||
| 539 | ':qp2' => 2, |
||
| 540 | ':qp3' => 'bar',], |
||
| 541 | ], |
||
| 542 | ]), |
||
| 543 | default => array_merge($conditions, [ |
||
| 544 | [ |
||
| 545 | ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 546 | '([[id]], [[name]]) IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 547 | [':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar'], |
||
| 548 | ], |
||
| 549 | [ |
||
| 550 | ['not in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]], |
||
| 551 | '([[id]], [[name]]) NOT IN ((:qp0, :qp1), (:qp2, :qp3))', |
||
| 552 | [':qp0' => 1, ':qp1' => 'foo', ':qp2' => 2, ':qp3' => 'bar'], |
||
| 553 | ], |
||
| 554 | [ |
||
| 555 | [ |
||
| 556 | 'in', |
||
| 557 | ['id', 'name'], |
||
| 558 | (new Query($db))->select(['id', 'name'])->from('users')->where(['active' => 1]), |
||
| 559 | ], |
||
| 560 | '([[id]], [[name]]) IN (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 561 | [':qp0' => 1], |
||
| 562 | ], |
||
| 563 | [ |
||
| 564 | [ |
||
| 565 | 'not in', |
||
| 566 | ['id', 'name'], |
||
| 567 | (new Query($db))->select(['id', 'name'])->from('users')->where(['active' => 1]), |
||
| 568 | ], |
||
| 569 | '([[id]], [[name]]) NOT IN (SELECT [[id]], [[name]] FROM [[users]] WHERE [[active]]=:qp0)', |
||
| 570 | [':qp0' => 1], |
||
| 571 | ], |
||
| 572 | ]), |
||
| 573 | }; |
||
| 574 | |||
| 575 | /* adjust dbms specific escaping */ |
||
| 576 | foreach ($conditions as $i => $condition) { |
||
| 577 | $conditions[$i][1] = DbHelper::replaceQuotes($condition[1], $db->getName()); |
||
| 578 | } |
||
| 579 | |||
| 580 | return $conditions; |
||
| 581 | } |
||
| 1039 |