grammar.php 3.5 KB

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