grammar.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. * The database connection instance for the grammar.
  11. *
  12. * @var Connection
  13. */
  14. protected $connection;
  15. /**
  16. * Create a new database grammar instance.
  17. *
  18. * @param Connection $connection
  19. * @return void
  20. */
  21. public function __construct(Connection $connection)
  22. {
  23. $this->connection = $connection;
  24. }
  25. /**
  26. * Wrap a table in keyword identifiers.
  27. *
  28. * @param string $table
  29. * @return string
  30. */
  31. public function wrap_table($table)
  32. {
  33. $prefix = '';
  34. // Tables may be prefixed with a string. This allows developers to
  35. // prefix tables by application on the same database which may be
  36. // required in some brown-field situations.
  37. if (isset($this->connection->config['prefix']))
  38. {
  39. $prefix = $this->connection->config['prefix'];
  40. }
  41. return $this->wrap($prefix.$table);
  42. }
  43. /**
  44. * Wrap a value in keyword identifiers.
  45. *
  46. * @param string $value
  47. * @return string
  48. */
  49. public function wrap($value)
  50. {
  51. // Expressions should be injected into the query as raw strings so
  52. // so we do not want to wrap them in any way. We will just return
  53. // the string value from the expression to be included.
  54. if ($value instanceof Expression) return $value->get();
  55. // If the value being wrapped contains a column alias, we need to
  56. // wrap it a little differently as each segment must be wrapped
  57. // and not the entire string. We'll split the value on the "as"
  58. // joiner to extract the column and the alias.
  59. if (strpos(strtolower($value), ' as ') !== false)
  60. {
  61. $segments = explode(' ', $value);
  62. return sprintf(
  63. '%s AS %s',
  64. $this->wrap($segments[0]),
  65. $this->wrap($segments[2])
  66. );
  67. }
  68. // Since columns may be prefixed with their corresponding table
  69. // name so as to not make them ambiguous, we will need to wrap
  70. // the table and the column in keyword identifiers.
  71. foreach (explode('.', $value) as $segment)
  72. {
  73. if ($segment == '*')
  74. {
  75. $wrapped[] = $segment;
  76. }
  77. else
  78. {
  79. $wrapped[] = sprintf($this->wrapper, $segment);
  80. }
  81. }
  82. return implode('.', $wrapped);
  83. }
  84. /**
  85. * Create query parameters from an array of values.
  86. *
  87. * <code>
  88. * Returns "?, ?, ?", which may be used as PDO place-holders
  89. * $parameters = $grammar->parameterize(array(1, 2, 3));
  90. *
  91. * // Returns "?, "Taylor"" since an expression is used
  92. * $parameters = $grammar->parameterize(array(1, DB::raw('Taylor')));
  93. * </code>
  94. *
  95. * @param array $values
  96. * @return string
  97. */
  98. final public function parameterize($values)
  99. {
  100. return implode(', ', array_map(array($this, 'parameter'), $values));
  101. }
  102. /**
  103. * Get the appropriate query parameter string for a value.
  104. *
  105. * <code>
  106. * // Returns a "?" PDO place-holder
  107. * $value = $grammar->parameter('Taylor Otwell');
  108. *
  109. * // Returns "Taylor Otwell" as the raw value of the expression
  110. * $value = $grammar->parameter(DB::raw('Taylor Otwell'));
  111. * </code>
  112. *
  113. * @param mixed $value
  114. * @return string
  115. */
  116. final public function parameter($value)
  117. {
  118. return ($value instanceof Expression) ? $value->get() : '?';
  119. }
  120. /**
  121. * Create a comma-delimited list of wrapped column names.
  122. *
  123. * <code>
  124. * // Returns ""Taylor", "Otwell"" when the identifier is quotes
  125. * $columns = $grammar->columnize(array('Taylor', 'Otwell'));
  126. * </code>
  127. *
  128. * @param array $columns
  129. * @return string
  130. */
  131. final public function columnize($columns)
  132. {
  133. return implode(', ', array_map(array($this, 'wrap'), $columns));
  134. }
  135. }