PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/i1598/caiyun_stat
PHP | 143 lines | 71 code | 23 blank | 49 comment | 6 complexity | 79d941e97ad7d8987012567e1ea5e566 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 object $db Database instance
  75. * @return string
  76. */
  77. public function compile(Database $db)
  78. {
  79. if ($this->_type)
  80. {
  81. $sql = strtoupper($this->_type).' JOIN';
  82. }
  83. else
  84. {
  85. $sql = 'JOIN';
  86. }
  87. // Quote the table name that is being joined
  88. $sql .= ' '.$db->quote_table($this->_table);
  89. if ( ! empty($this->_using))
  90. {
  91. // Quote and concat the columns
  92. $sql .= ' USING ('.implode(', ', array_map(array($db, 'quote_column'), $this->_using)).')';
  93. }
  94. else
  95. {
  96. $conditions = array();
  97. foreach ($this->_on as $condition)
  98. {
  99. // Split the condition
  100. list($c1, $op, $c2) = $condition;
  101. if ($op)
  102. {
  103. // Make the operator uppercase and spaced
  104. $op = ' '.strtoupper($op);
  105. }
  106. // Quote each of the columns used for the condition
  107. $conditions[] = $db->quote_column($c1).$op.' '.$db->quote_column($c2);
  108. }
  109. // Concat the conditions "... AND ..."
  110. $sql .= ' ON ('.implode(' AND ', $conditions).')';
  111. }
  112. return $sql;
  113. }
  114. public function reset()
  115. {
  116. $this->_type =
  117. $this->_table = NULL;
  118. $this->_on = array();
  119. }
  120. } // End Database_Query_Builder_Join