grammar.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);
  29. }
  30. // Since columns may be prefixed with their corresponding table
  31. // name so as to not make them ambiguous, we will need to wrap
  32. // the table and the column in keyword identifiers.
  33. foreach (explode('.', $value) as $segment)
  34. {
  35. if ($segment == '*')
  36. {
  37. $wrapped[] = $segment;
  38. }
  39. else
  40. {
  41. $wrapped[] = sprintf($this->wrapper, $segment);
  42. }
  43. }
  44. return implode('.', $wrapped);
  45. }
  46. /**
  47. * Create query parameters from an array of values.
  48. *
  49. * <code>
  50. * Returns "?, ?, ?", which may be used as PDO place-holders
  51. * $parameters = $grammar->parameterize(array(1, 2, 3));
  52. *
  53. * // Returns "?, "Taylor"" since an expression is used
  54. * $parameters = $grammar->parameterize(array(1, DB::raw('Taylor')));
  55. * </code>
  56. *
  57. * @param array $values
  58. * @return string
  59. */
  60. final public function parameterize($values)
  61. {
  62. return implode(', ', array_map(array($this, 'parameter'), $values));
  63. }
  64. /**
  65. * Get the appropriate query parameter string for a value.
  66. *
  67. * <code>
  68. * // Returns a "?" PDO place-holder
  69. * $value = $grammar->parameter('Taylor Otwell');
  70. *
  71. * // Returns "Taylor Otwell" as the raw value of the expression
  72. * $value = $grammar->parameter(DB::raw('Taylor Otwell'));
  73. * </code>
  74. *
  75. * @param mixed $value
  76. * @return string
  77. */
  78. final public function parameter($value)
  79. {
  80. return ($value instanceof Expression) ? $value->get() : '?';
  81. }
  82. /**
  83. * Create a comma-delimited list of wrapped column names.
  84. *
  85. * <code>
  86. * // Returns ""Taylor", "Otwell"" when the identifier is quotes
  87. * $columns = $grammar->columnize(array('Taylor', 'Otwell'));
  88. * </code>
  89. *
  90. * @param array $columns
  91. * @return string
  92. */
  93. final public function columnize($columns)
  94. {
  95. return implode(', ', array_map(array($this, 'wrap'), $columns));
  96. }
  97. }