grammar.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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)
  55. {
  56. return $value->get();
  57. }
  58. // If the value being wrapped contains a column alias, we need to
  59. // wrap it a little differently as each segment must be wrapped
  60. // and not the entire string.
  61. if (strpos(strtolower($value), ' as ') !== false)
  62. {
  63. $segments = explode(' ', $value);
  64. return sprintf(
  65. '%s AS %s',
  66. $this->wrap($segments[0]),
  67. $this->wrap($segments[2])
  68. );
  69. }
  70. // Since columns may be prefixed with their corresponding table
  71. // name so as to not make them ambiguous, we will need to wrap
  72. // the table and the column in keyword identifiers.
  73. foreach (explode('.', $value) as $segment)
  74. {
  75. if ($segment == '*')
  76. {
  77. $wrapped[] = $segment;
  78. }
  79. else
  80. {
  81. $wrapped[] = sprintf($this->wrapper, $segment);
  82. }
  83. }
  84. return implode('.', $wrapped);
  85. }
  86. /**
  87. * Create query parameters from an array of values.
  88. *
  89. * <code>
  90. * Returns "?, ?, ?", which may be used as PDO place-holders
  91. * $parameters = $grammar->parameterize(array(1, 2, 3));
  92. *
  93. * // Returns "?, "Taylor"" since an expression is used
  94. * $parameters = $grammar->parameterize(array(1, DB::raw('Taylor')));
  95. * </code>
  96. *
  97. * @param array $values
  98. * @return string
  99. */
  100. final public function parameterize($values)
  101. {
  102. return implode(', ', array_map(array($this, 'parameter'), $values));
  103. }
  104. /**
  105. * Get the appropriate query parameter string for a value.
  106. *
  107. * <code>
  108. * // Returns a "?" PDO place-holder
  109. * $value = $grammar->parameter('Taylor Otwell');
  110. *
  111. * // Returns "Taylor Otwell" as the raw value of the expression
  112. * $value = $grammar->parameter(DB::raw('Taylor Otwell'));
  113. * </code>
  114. *
  115. * @param mixed $value
  116. * @return string
  117. */
  118. final public function parameter($value)
  119. {
  120. return ($value instanceof Expression) ? $value->get() : '?';
  121. }
  122. /**
  123. * Create a comma-delimited list of wrapped column names.
  124. *
  125. * <code>
  126. * // Returns ""Taylor", "Otwell"" when the identifier is quotes
  127. * $columns = $grammar->columnize(array('Taylor', 'Otwell'));
  128. * </code>
  129. *
  130. * @param array $columns
  131. * @return string
  132. */
  133. final public function columnize($columns)
  134. {
  135. return implode(', ', array_map(array($this, 'wrap'), $columns));
  136. }
  137. }