grammar.php 4.0 KB

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