Expression.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Database expressions can be used to add unescaped SQL fragments to a
  4. * [Database_Query_Builder] object.
  5. *
  6. * For example, you can use an expression to generate a column alias:
  7. *
  8. * // SELECT CONCAT(first_name, last_name) AS full_name
  9. * $query = DB::select(array(DB::expr('CONCAT(first_name, last_name)'), 'full_name')));
  10. *
  11. * More examples are available on the [Query Builder](database/query/builder#database-expressions) page
  12. *
  13. * @package Kohana/Database
  14. * @category Base
  15. * @author Kohana Team
  16. * @copyright (c) Kohana Team
  17. * @license https://koseven.ga/LICENSE.md
  18. */
  19. class Kohana_Database_Expression {
  20. // Unquoted parameters
  21. protected $_parameters;
  22. // Raw expression string
  23. protected $_value;
  24. /**
  25. * Sets the expression string.
  26. *
  27. * $expression = new Database_Expression('COUNT(users.id)');
  28. *
  29. * @param string $value raw SQL expression string
  30. * @param array $parameters unquoted parameter values
  31. * @return void
  32. */
  33. public function __construct($value, $parameters = [])
  34. {
  35. // Set the expression string
  36. $this->_value = $value;
  37. $this->_parameters = $parameters;
  38. }
  39. /**
  40. * Bind a variable to a parameter.
  41. *
  42. * @param string $param parameter key to replace
  43. * @param mixed $var variable to use
  44. * @return $this
  45. */
  46. public function bind($param, & $var)
  47. {
  48. $this->_parameters[$param] =& $var;
  49. return $this;
  50. }
  51. /**
  52. * Set the value of a parameter.
  53. *
  54. * @param string $param parameter key to replace
  55. * @param mixed $value value to use
  56. * @return $this
  57. */
  58. public function param($param, $value)
  59. {
  60. $this->_parameters[$param] = $value;
  61. return $this;
  62. }
  63. /**
  64. * Add multiple parameter values.
  65. *
  66. * @param array $params list of parameter values
  67. * @return $this
  68. */
  69. public function parameters(array $params)
  70. {
  71. $this->_parameters = $params + $this->_parameters;
  72. return $this;
  73. }
  74. /**
  75. * Get the expression value as a string.
  76. *
  77. * $sql = $expression->value();
  78. *
  79. * @return string
  80. */
  81. public function value()
  82. {
  83. return (string) $this->_value;
  84. }
  85. /**
  86. * Return the value of the expression as a string.
  87. *
  88. * echo $expression;
  89. *
  90. * @return string
  91. * @uses Database_Expression::value
  92. */
  93. public function __toString()
  94. {
  95. return $this->value();
  96. }
  97. /**
  98. * Compile the SQL expression and return it. Replaces any parameters with
  99. * their given values.
  100. *
  101. * @param mixed Database instance or name of instance
  102. * @return string
  103. */
  104. public function compile($db = NULL)
  105. {
  106. if ( ! is_object($db))
  107. {
  108. // Get the database instance
  109. $db = Database::instance($db);
  110. }
  111. $value = $this->value();
  112. if ( ! empty($this->_parameters))
  113. {
  114. // Quote all of the parameter values
  115. $params = array_map([$db, 'quote'], $this->_parameters);
  116. // Replace the values in the expression
  117. $value = strtr($value, $params);
  118. }
  119. return $value;
  120. }
  121. } // End Database_Expression