PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sriedel/iccrm-wip
PHP | 118 lines | 55 code | 21 blank | 42 comment | 4 complexity | 71134f595c97a7b9a00a62aed363c1af MD5 | raw file
Possible License(s): MIT
  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 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. $this->_on[] = array($c1, $op, $c2);
  49. return $this;
  50. }
  51. /**
  52. * Compile the SQL partial for a JOIN statement and return it.
  53. *
  54. * @param mixed Database instance or instance name
  55. * @return string
  56. */
  57. public function compile($db = null)
  58. {
  59. if ( ! $db instanceof \Database_Connection)
  60. {
  61. // Get the database instance
  62. $db = \Database_Connection::instance($db);
  63. }
  64. if ($this->_type)
  65. {
  66. $sql = strtoupper($this->_type).' JOIN';
  67. }
  68. else
  69. {
  70. $sql = 'JOIN';
  71. }
  72. // Quote the table name that is being joined
  73. $sql .= ' '.$db->quote_table($this->_table);
  74. $conditions = array();
  75. foreach ($this->_on as $condition)
  76. {
  77. // Split the condition
  78. list($c1, $op, $c2) = $condition;
  79. if ($op)
  80. {
  81. // Make the operator uppercase and spaced
  82. $op = ' '.strtoupper($op);
  83. }
  84. // Quote each of the identifiers used for the condition
  85. $conditions[] = $db->quote_identifier($c1).$op.' '.$db->quote_identifier($c2);
  86. }
  87. // if there are conditions, concat the conditions "... AND ..." and glue them on...
  88. empty($conditions) or $sql .= ' ON ('.implode(' AND ', $conditions).')';
  89. return $sql;
  90. }
  91. public function reset()
  92. {
  93. $this->_type =
  94. $this->_table = NULL;
  95. $this->_on = array();
  96. }
  97. } // End Database_Query_Builder_Join