expression.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. * @package Fuel/Database
  12. * @category Base
  13. * @author Kohana Team
  14. * @copyright (c) 2009 Kohana Team
  15. * @license http://kohanaphp.com/license
  16. */
  17. namespace Fuel\Core;
  18. class Database_Expression
  19. {
  20. // Raw expression string
  21. protected $_value;
  22. /**
  23. * Sets the expression string.
  24. *
  25. * $expression = new Database_Expression('COUNT(users.id)');
  26. *
  27. * @return void
  28. */
  29. public function __construct($value)
  30. {
  31. // Set the expression string
  32. $this->_value = $value;
  33. }
  34. /**
  35. * Get the expression value as a string.
  36. *
  37. * $sql = $expression->value();
  38. *
  39. * @return string
  40. */
  41. public function value()
  42. {
  43. return (string) $this->_value;
  44. }
  45. /**
  46. * Return the value of the expression as a string.
  47. *
  48. * echo $expression;
  49. *
  50. * @return string
  51. * @uses Database_Expression::value
  52. */
  53. public function __toString()
  54. {
  55. return $this->value();
  56. }
  57. }