InConditionBuilder.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db\oci\conditions;
  8. use yii\db\conditions\InCondition;
  9. use yii\db\ExpressionInterface;
  10. /**
  11. * {@inheritdoc}
  12. */
  13. class InConditionBuilder extends \yii\db\conditions\InConditionBuilder
  14. {
  15. /**
  16. * Method builds the raw SQL from the $expression that will not be additionally
  17. * escaped or quoted.
  18. *
  19. * @param ExpressionInterface|InCondition $expression the expression to be built.
  20. * @param array $params the binding parameters.
  21. * @return string the raw SQL that will not be additionally escaped or quoted.
  22. */
  23. public function build(ExpressionInterface $expression, array &$params = [])
  24. {
  25. $splitCondition = $this->splitCondition($expression, $params);
  26. if ($splitCondition !== null) {
  27. return $splitCondition;
  28. }
  29. return parent::build($expression, $params);
  30. }
  31. /**
  32. * Oracle DBMS does not support more than 1000 parameters in `IN` condition.
  33. * This method splits long `IN` condition into series of smaller ones.
  34. *
  35. * @param ExpressionInterface|InCondition $condition the expression to be built.
  36. * @param array $params the binding parameters.
  37. * @return null|string null when split is not required. Otherwise - built SQL condition.
  38. */
  39. protected function splitCondition(InCondition $condition, &$params)
  40. {
  41. $operator = $condition->getOperator();
  42. $values = $condition->getValues();
  43. $column = $condition->getColumn();
  44. if ($values instanceof \Traversable) {
  45. $values = iterator_to_array($values);
  46. }
  47. if (!is_array($values)) {
  48. return null;
  49. }
  50. $maxParameters = 1000;
  51. $count = count($values);
  52. if ($count <= $maxParameters) {
  53. return null;
  54. }
  55. $slices = [];
  56. for ($i = 0; $i < $count; $i += $maxParameters) {
  57. $slices[] = $this->queryBuilder->createConditionFromArray([$operator, $column, array_slice($values, $i, $maxParameters)]);
  58. }
  59. array_unshift($slices, ($operator === 'IN') ? 'OR' : 'AND');
  60. return $this->queryBuilder->buildCondition($slices, $params);
  61. }
  62. }