PageRenderTime 68ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/database/classes/kohana/database/query/builder/join.php

https://bitbucket.org/Ahineya/trn_dev
PHP | 144 lines | 71 code | 23 blank | 50 comment | 6 complexity | 8b1d7326462c6ceaafa0fbda060f6a17 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 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 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 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 column name
  59. * @param ...
  60. * @return $this
  61. */
  62. public function using($columns)
  63. {
  64. if ( ! empty($this->_on))
  65. {
  66. throw new Kohana_Exception('JOIN ... ON ... cannot be combined with JOIN ... USING ...');
  67. }
  68. $columns = func_get_args();
  69. $this->_using = array_merge($this->_using, $columns);
  70. return $this;
  71. }
  72. /**
  73. * Compile the SQL partial for a JOIN statement and return it.
  74. *
  75. * @param object Database instance
  76. * @return string
  77. */
  78. public function compile(Database $db)
  79. {
  80. if ($this->_type)
  81. {
  82. $sql = strtoupper($this->_type).' JOIN';
  83. }
  84. else
  85. {
  86. $sql = 'JOIN';
  87. }
  88. // Quote the table name that is being joined
  89. $sql .= ' '.$db->quote_table($this->_table);
  90. if ( ! empty($this->_using))
  91. {
  92. // Quote and concat the columns
  93. $sql .= ' USING ('.implode(', ', array_map(array($db, 'quote_column'), $this->_using)).')';
  94. }
  95. else
  96. {
  97. $conditions = array();
  98. foreach ($this->_on as $condition)
  99. {
  100. // Split the condition
  101. list($c1, $op, $c2) = $condition;
  102. if ($op)
  103. {
  104. // Make the operator uppercase and spaced
  105. $op = ' '.strtoupper($op);
  106. }
  107. // Quote each of the columns used for the condition
  108. $conditions[] = $db->quote_column($c1).$op.' '.$db->quote_column($c2);
  109. }
  110. // Concat the conditions "... AND ..."
  111. $sql .= ' ON ('.implode(' AND ', $conditions).')';
  112. }
  113. return $sql;
  114. }
  115. public function reset()
  116. {
  117. $this->_type =
  118. $this->_table = NULL;
  119. $this->_on = array();
  120. }
  121. } // End Database_Query_Builder_Join