grammar.php 3.46 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	 * 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)
	{
		$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);
	}

	/**
52 53 54 55 56 57 58
	 * Wrap a value in keyword identifiers.
	 *
	 * @param  string  $value
	 * @return string
	 */
	public function wrap($value)
	{
59 60
		// Expressions should be injected into the query as raw strings so
		// so we do not want to wrap them in any way. We will just return
61 62 63
		// the string value from the expression to be included.
		if ($value instanceof Expression) return $value->get();

64 65
		// If the value being wrapped contains a column alias, we need to
		// wrap it a little differently as each segment must be wrapped
66
		// and not the entire string.
67 68 69 70
		if (strpos(strtolower($value), ' as ') !== false)
		{
			$segments = explode(' ', $value);

71 72 73 74 75
			return sprintf(
				'%s AS %s',
				$this->wrap($segments[0]),
				$this->wrap($segments[2])
			);
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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
		}

		// 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.
		foreach (explode('.', $value) as $segment)
		{
			if ($segment == '*')
			{
				$wrapped[] = $segment;
			}
			else
			{
				$wrapped[] = sprintf($this->wrapper, $segment);
			}
		}

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

	/**
	 * 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));
	}

}