grammar.php 4.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php namespace Laravel\Database;

abstract class Grammar {

	/**
	 * The keyword identifier for the database system.
	 *
	 * @var string
	 */
	protected $wrapper = '"%s"';

	/**
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	 * The database connection instance for the grammar.
	 *
	 * @var Connection
	 */
	protected $connection;

	/**
	 * Create a new database grammar instance.
	 *
	 * @param  Connection  $connection
	 * @return void
	 */
	public function __construct(Connection $connection)
	{
		$this->connection = $connection;
	}

	/**
	 * Wrap a table in keyword identifiers.
	 *
	 * @param  string  $table
	 * @return string
	 */
	public function wrap_table($table)
	{
Chris Berthe committed
38
		// Expressions should be injected into the query as raw strings
39 40
		// so we do not want to wrap them in any way. We will just return
		// the string value from the expression to be included.
Kelly Banman committed
41 42 43 44 45
		if ($table instanceof Expression)
		{
			return $this->wrap($table);
		}

46 47 48 49 50 51 52 53 54 55 56 57 58 59
		$prefix = '';

		// Tables may be prefixed with a string. This allows developers to
		// prefix tables by application on the same database which may be
		// required in some brown-field situations.
		if (isset($this->connection->config['prefix']))
		{
			$prefix = $this->connection->config['prefix'];
		}

		return $this->wrap($prefix.$table);
	}

	/**
60 61 62 63 64 65 66
	 * Wrap a value in keyword identifiers.
	 *
	 * @param  string  $value
	 * @return string
	 */
	public function wrap($value)
	{
Chris Berthe committed
67
		// Expressions should be injected into the query as raw strings
68
		// so we do not want to wrap them in any way. We will just return
69
		// the string value from the expression to be included.
Taylor Otwell committed
70 71 72 73
		if ($value instanceof Expression)
		{
			return $value->get();
		}
74

75 76
		// If the value being wrapped contains a column alias, we need to
		// wrap it a little differently as each segment must be wrapped
77
		// and not the entire string.
78 79 80 81
		if (strpos(strtolower($value), ' as ') !== false)
		{
			$segments = explode(' ', $value);

82 83 84 85 86
			return sprintf(
				'%s AS %s',
				$this->wrap($segments[0]),
				$this->wrap($segments[2])
			);
87 88 89 90 91
		}

		// Since columns may be prefixed with their corresponding table
		// name so as to not make them ambiguous, we will need to wrap
		// the table and the column in keyword identifiers.
92 93 94
		$segments = explode('.', $value);

		foreach ($segments as $key => $value)
95
		{
96 97 98 99 100 101 102 103
			if ($key == 0 and count($segments) > 1)
			{
				$wrapped[] = $this->wrap_table($value);
			}
			else
			{
				$wrapped[] = $this->wrap_value($value);
			}
104 105 106 107 108 109
		}

		return implode('.', $wrapped);
	}

	/**
110 111 112 113 114 115 116 117 118 119 120
	 * Wrap a single string value in keyword identifiers.
	 *
	 * @param  string  $value
	 * @return string
	 */
	protected function wrap_value($value)
	{
		return ($value !== '*') ? sprintf($this->wrapper, $value) : $value;
	}

	/**
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
	 * Create query parameters from an array of values.
	 *
	 * <code>
	 *		Returns "?, ?, ?", which may be used as PDO place-holders
	 *		$parameters = $grammar->parameterize(array(1, 2, 3));
	 *
	 *		// Returns "?, "Taylor"" since an expression is used
	 *		$parameters = $grammar->parameterize(array(1, DB::raw('Taylor')));
	 * </code>
	 *
	 * @param  array   $values
	 * @return string
	 */
	final public function parameterize($values)
	{
		return implode(', ', array_map(array($this, 'parameter'), $values));
	}

	/**
	 * Get the appropriate query parameter string for a value.
	 *
	 * <code>
	 *		// Returns a "?" PDO place-holder
	 *		$value = $grammar->parameter('Taylor Otwell');
	 *
	 *		// Returns "Taylor Otwell" as the raw value of the expression
	 *		$value = $grammar->parameter(DB::raw('Taylor Otwell'));
	 * </code>
	 *
	 * @param  mixed   $value
	 * @return string
	 */
	final public function parameter($value)
	{
		return ($value instanceof Expression) ? $value->get() : '?';
	}

	/**
	 * Create a comma-delimited list of wrapped column names.
	 *
	 * <code>
	 *		// Returns ""Taylor", "Otwell"" when the identifier is quotes
	 *		$columns = $grammar->columnize(array('Taylor', 'Otwell'));
	 * </code>
	 *
	 * @param  array   $columns
	 * @return string
	 */
	final public function columnize($columns)
	{
		return implode(', ', array_map(array($this, 'wrap'), $columns));
	}

}