grammar.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php namespace Laravel\Database;
  2. abstract class Grammar {
  3. /**
  4. * The keyword identifier for the database system.
  5. *
  6. * @var string
  7. */
  8. protected $wrapper = '"%s"';
  9. /**
  10. * Wrap a value in keyword identifiers.
  11. *
  12. * @param string $value
  13. * @return string
  14. */
  15. public function wrap($value)
  16. {
  17. // Expressions should be injected into the query as raw strings so
  18. // so we do not want to wrap them in any way. We will just return
  19. // the string value from the expression to be included.
  20. if ($value instanceof Expression) return $value->get();
  21. // If the value being wrapped contains a column alias, we need to
  22. // wrap it a little differently as each segment must be wrapped
  23. // and not the entire string. We'll split the value on the "as"
  24. // joiner to extract the column and the alias.
  25. if (strpos(strtolower($value), ' as ') !== false)
  26. {
  27. $segments = explode(' ', $value);
  28. return sprintf(
  29. '%s AS %s',
  30. $this->wrap($segments[0]),
  31. $this->wrap($segments[2])
  32. );
  33. }
  34. // Since columns may be prefixed with their corresponding table
  35. // name so as to not make them ambiguous, we will need to wrap
  36. // the table and the column in keyword identifiers.
  37. foreach (explode('.', $value) as $segment)
  38. {
  39. if ($segment == '*')
  40. {
  41. $wrapped[] = $segment;
  42. }
  43. else
  44. {
  45. $wrapped[] = sprintf($this->wrapper, $segment);
  46. }
  47. }
  48. return implode('.', $wrapped);
  49. }
  50. /**
  51. * Create query parameters from an array of values.
  52. *
  53. * <code>
  54. * Returns "?, ?, ?", which may be used as PDO place-holders
  55. * $parameters = $grammar->parameterize(array(1, 2, 3));
  56. *
  57. * // Returns "?, "Taylor"" since an expression is used
  58. * $parameters = $grammar->parameterize(array(1, DB::raw('Taylor')));
  59. * </code>
  60. *
  61. * @param array $values
  62. * @return string
  63. */
  64. final public function parameterize($values)
  65. {
  66. return implode(', ', array_map(array($this, 'parameter'), $values));
  67. }
  68. /**
  69. * Get the appropriate query parameter string for a value.
  70. *
  71. * <code>
  72. * // Returns a "?" PDO place-holder
  73. * $value = $grammar->parameter('Taylor Otwell');
  74. *
  75. * // Returns "Taylor Otwell" as the raw value of the expression
  76. * $value = $grammar->parameter(DB::raw('Taylor Otwell'));
  77. * </code>
  78. *
  79. * @param mixed $value
  80. * @return string
  81. */
  82. final public function parameter($value)
  83. {
  84. return ($value instanceof Expression) ? $value->get() : '?';
  85. }
  86. /**
  87. * Create a comma-delimited list of wrapped column names.
  88. *
  89. * <code>
  90. * // Returns ""Taylor", "Otwell"" when the identifier is quotes
  91. * $columns = $grammar->columnize(array('Taylor', 'Otwell'));
  92. * </code>
  93. *
  94. * @param array $columns
  95. * @return string
  96. */
  97. final public function columnize($columns)
  98. {
  99. return implode(', ', array_map(array($this, 'wrap'), $columns));
  100. }
  101. }