JoinClause.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Illuminate\Database\Query;
  3. use Closure;
  4. class JoinClause extends Builder
  5. {
  6. /**
  7. * The type of join being performed.
  8. *
  9. * @var string
  10. */
  11. public $type;
  12. /**
  13. * The table the join clause is joining to.
  14. *
  15. * @var string
  16. */
  17. public $table;
  18. /**
  19. * The connection of the parent query builder.
  20. *
  21. * @var \Illuminate\Database\ConnectionInterface
  22. */
  23. protected $parentConnection;
  24. /**
  25. * The grammar of the parent query builder.
  26. *
  27. * @var \Illuminate\Database\Query\Grammars\Grammar
  28. */
  29. protected $parentGrammar;
  30. /**
  31. * The processor of the parent query builder.
  32. *
  33. * @var \Illuminate\Database\Query\Processors\Processor
  34. */
  35. protected $parentProcessor;
  36. /**
  37. * The class name of the parent query builder.
  38. *
  39. * @var string
  40. */
  41. protected $parentClass;
  42. /**
  43. * Create a new join clause instance.
  44. *
  45. * @param \Illuminate\Database\Query\Builder $parentQuery
  46. * @param string $type
  47. * @param string $table
  48. * @return void
  49. */
  50. public function __construct(Builder $parentQuery, $type, $table)
  51. {
  52. $this->type = $type;
  53. $this->table = $table;
  54. $this->parentClass = get_class($parentQuery);
  55. $this->parentGrammar = $parentQuery->getGrammar();
  56. $this->parentProcessor = $parentQuery->getProcessor();
  57. $this->parentConnection = $parentQuery->getConnection();
  58. parent::__construct(
  59. $this->parentConnection, $this->parentGrammar, $this->parentProcessor
  60. );
  61. }
  62. /**
  63. * Add an "on" clause to the join.
  64. *
  65. * On clauses can be chained, e.g.
  66. *
  67. * $join->on('contacts.user_id', '=', 'users.id')
  68. * ->on('contacts.info_id', '=', 'info.id')
  69. *
  70. * will produce the following SQL:
  71. *
  72. * on `contacts`.`user_id` = `users`.`id` and `contacts`.`info_id` = `info`.`id`
  73. *
  74. * @param \Closure|string $first
  75. * @param string|null $operator
  76. * @param string|null $second
  77. * @param string $boolean
  78. * @return $this
  79. *
  80. * @throws \InvalidArgumentException
  81. */
  82. public function on($first, $operator = null, $second = null, $boolean = 'and')
  83. {
  84. if ($first instanceof Closure) {
  85. return $this->whereNested($first, $boolean);
  86. }
  87. return $this->whereColumn($first, $operator, $second, $boolean);
  88. }
  89. /**
  90. * Add an "or on" clause to the join.
  91. *
  92. * @param \Closure|string $first
  93. * @param string|null $operator
  94. * @param string|null $second
  95. * @return \Illuminate\Database\Query\JoinClause
  96. */
  97. public function orOn($first, $operator = null, $second = null)
  98. {
  99. return $this->on($first, $operator, $second, 'or');
  100. }
  101. /**
  102. * Get a new instance of the join clause builder.
  103. *
  104. * @return \Illuminate\Database\Query\JoinClause
  105. */
  106. public function newQuery()
  107. {
  108. return new static($this->newParentQuery(), $this->type, $this->table);
  109. }
  110. /**
  111. * Create a new query instance for sub-query.
  112. *
  113. * @return \Illuminate\Database\Query\Builder
  114. */
  115. protected function forSubQuery()
  116. {
  117. return $this->newParentQuery()->newQuery();
  118. }
  119. /**
  120. * Create a new parent query instance.
  121. *
  122. * @return \Illuminate\Database\Query\Builder
  123. */
  124. protected function newParentQuery()
  125. {
  126. $class = $this->parentClass;
  127. return new $class($this->parentConnection, $this->parentGrammar, $this->parentProcessor);
  128. }
  129. }