PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/database/classes/Kohana/Database/Query/Builder/Join.php

https://bitbucket.org/alexpozdnyakov/kohana
PHP | 149 lines | 75 code | 24 blank | 50 comment | 7 complexity | f2a22ddfe5ed6441de891f09b37c6def MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Database query builder for JOIN statements. See [Query Builder](/database/query/builder) for usage and examples.
  4. *
  5. * @package Kohana/Database
  6. * @category Query
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. class Kohana_Database_Query_Builder_Join extends Database_Query_Builder {
  12. // Type of JOIN
  13. protected $_type;
  14. // JOIN ...
  15. protected $_table;
  16. // ON ...
  17. protected $_on = array();
  18. // USING ...
  19. protected $_using = 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 $table column name or array($column, $alias) or object
  25. * @param string $type 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 condition for joining.
  40. *
  41. * @param mixed $c1 column name or array($column, $alias) or object
  42. * @param string $op logic operator
  43. * @param mixed $c2 column name or array($column, $alias) or object
  44. * @return $this
  45. */
  46. public function on($c1, $op, $c2)
  47. {
  48. if ( ! empty($this->_using))
  49. {
  50. throw new Kohana_Exception('JOIN ... ON ... cannot be combined with JOIN ... USING ...');
  51. }
  52. $this->_on[] = array($c1, $op, $c2);
  53. return $this;
  54. }
  55. /**
  56. * Adds a new condition for joining.
  57. *
  58. * @param string $columns column name
  59. * @return $this
  60. */
  61. public function using($columns)
  62. {
  63. if ( ! empty($this->_on))
  64. {
  65. throw new Kohana_Exception('JOIN ... ON ... cannot be combined with JOIN ... USING ...');
  66. }
  67. $columns = func_get_args();
  68. $this->_using = array_merge($this->_using, $columns);
  69. return $this;
  70. }
  71. /**
  72. * Compile the SQL partial for a JOIN statement and return it.
  73. *
  74. * @param mixed $db Database instance or name of instance
  75. * @return string
  76. */
  77. public function compile($db = NULL)
  78. {
  79. if ( ! is_object($db))
  80. {
  81. // Get the database instance
  82. $db = Database::instance($db);
  83. }
  84. if ($this->_type)
  85. {
  86. $sql = strtoupper($this->_type).' JOIN';
  87. }
  88. else
  89. {
  90. $sql = 'JOIN';
  91. }
  92. // Quote the table name that is being joined
  93. $sql .= ' '.$db->quote_table($this->_table);
  94. if ( ! empty($this->_using))
  95. {
  96. // Quote and concat the columns
  97. $sql .= ' USING ('.implode(', ', array_map(array($db, 'quote_column'), $this->_using)).')';
  98. }
  99. else
  100. {
  101. $conditions = array();
  102. foreach ($this->_on as $condition)
  103. {
  104. // Split the condition
  105. list($c1, $op, $c2) = $condition;
  106. if ($op)
  107. {
  108. // Make the operator uppercase and spaced
  109. $op = ' '.strtoupper($op);
  110. }
  111. // Quote each of the columns used for the condition
  112. $conditions[] = $db->quote_column($c1).$op.' '.$db->quote_column($c2);
  113. }
  114. // Concat the conditions "... AND ..."
  115. $sql .= ' ON ('.implode(' AND ', $conditions).')';
  116. }
  117. return $sql;
  118. }
  119. public function reset()
  120. {
  121. $this->_type =
  122. $this->_table = NULL;
  123. $this->_on = array();
  124. }
  125. } // End Database_Query_Builder_Join