PageRenderTime 88ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/classes/database/query/builder/join.php

https://bitbucket.org/codeyash/bootstrap
PHP | 154 lines | 66 code | 23 blank | 65 comment | 4 complexity | f82364ed31fc7e5b247209b9f6dedc16 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. <?php
  2. /**
  3. * Database query builder for JOIN statements.
  4. *
  5. * @package Fuel/Database
  6. * @category Query
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. namespace Fuel\Core;
  12. class Database_Query_Builder_Join extends \Database_Query_Builder
  13. {
  14. // Type of JOIN
  15. protected $_type;
  16. // JOIN ...
  17. protected $_table;
  18. // ON ...
  19. protected $_on = array();
  20. /**
  21. * Creates a new JOIN statement for a table. Optionally, the type of JOIN
  22. * can be specified as the second parameter.
  23. *
  24. * @param mixed column name or array($column, $alias) or object
  25. * @param string type of JOIN: INNER, RIGHT, LEFT, etc
  26. * @return void
  27. */
  28. public function __construct($table, $type = null)
  29. {
  30. // Set the table to JOIN on
  31. $this->_table = $table;
  32. if ($type !== null)
  33. {
  34. // Set the JOIN type
  35. $this->_type = (string) $type;
  36. }
  37. }
  38. /**
  39. * Adds a new OR condition for joining.
  40. *
  41. * @param mixed column name or array($column, $alias) or object
  42. * @param string logic operator
  43. * @param mixed column name or array($column, $alias) or object
  44. * @return $this
  45. */
  46. public function or_on($c1, $op, $c2)
  47. {
  48. $this->_on[] = array($c1, $op, $c2, 'OR');
  49. return $this;
  50. }
  51. /**
  52. * Adds a new AND condition for joining.
  53. *
  54. * @param mixed column name or array($column, $alias) or object
  55. * @param string logic operator
  56. * @param mixed column name or array($column, $alias) or object
  57. * @return $this
  58. */
  59. public function on($c1, $op, $c2)
  60. {
  61. $this->_on[] = array($c1, $op, $c2, 'AND');
  62. return $this;
  63. }
  64. /**
  65. * Adds a new AND condition for joining.
  66. *
  67. * @param mixed column name or array($column, $alias) or object
  68. * @param string logic operator
  69. * @param mixed column name or array($column, $alias) or object
  70. * @return $this
  71. */
  72. public function and_on($c1, $op, $c2)
  73. {
  74. return $this->on($c1, $op, $c2);
  75. }
  76. /**
  77. * Compile the SQL partial for a JOIN statement and return it.
  78. *
  79. * @param mixed Database instance or instance name
  80. * @return string
  81. */
  82. public function compile($db = null)
  83. {
  84. if ( ! $db instanceof \Database_Connection)
  85. {
  86. // Get the database instance
  87. $db = \Database_Connection::instance($db);
  88. }
  89. if ($this->_type)
  90. {
  91. $sql = strtoupper($this->_type).' JOIN';
  92. }
  93. else
  94. {
  95. $sql = 'JOIN';
  96. }
  97. // Quote the table name that is being joined
  98. $sql .= ' '.$db->quote_table($this->_table);
  99. $conditions = array();
  100. foreach ($this->_on as $condition)
  101. {
  102. // Split the condition
  103. list($c1, $op, $c2, $chaining) = $condition;
  104. // Add chain type
  105. $conditions[] = ' '.$chaining.' ';
  106. if ($op)
  107. {
  108. // Make the operator uppercase and spaced
  109. $op = ' '.strtoupper($op);
  110. }
  111. // Quote each of the identifiers used for the condition
  112. $conditions[] = $db->quote_identifier($c1).$op.' '.$db->quote_identifier($c2);
  113. }
  114. // remove the first chain type
  115. array_shift($conditions);
  116. // if there are conditions, concat the conditions "... AND ..." and glue them on...
  117. empty($conditions) or $sql .= ' ON ('.implode('', $conditions).')';
  118. return $sql;
  119. }
  120. /**
  121. * Resets the join values.
  122. *
  123. * @return object $this
  124. */
  125. public function reset()
  126. {
  127. $this->_type =
  128. $this->_table = NULL;
  129. $this->_on = array();
  130. }
  131. }