Projekt

Obecné

Profil

Stáhnout (4.39 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace Illuminate\Database;
4

    
5
use Illuminate\Database\Query\Expression;
6

    
7
abstract class Grammar
8
{
9
    /**
10
     * The grammar table prefix.
11
     *
12
     * @var string
13
     */
14
    protected $tablePrefix = '';
15

    
16
    /**
17
     * Wrap an array of values.
18
     *
19
     * @param  array  $values
20
     * @return array
21
     */
22
    public function wrapArray(array $values)
23
    {
24
        return array_map([$this, 'wrap'], $values);
25
    }
26

    
27
    /**
28
     * Wrap a table in keyword identifiers.
29
     *
30
     * @param  \Illuminate\Database\Query\Expression|string  $table
31
     * @return string
32
     */
33
    public function wrapTable($table)
34
    {
35
        if ($this->isExpression($table)) {
36
            return $this->getValue($table);
37
        }
38

    
39
        return $this->wrap($this->tablePrefix.$table, true);
40
    }
41

    
42
    /**
43
     * Wrap a value in keyword identifiers.
44
     *
45
     * @param  \Illuminate\Database\Query\Expression|string  $value
46
     * @param  bool    $prefixAlias
47
     * @return string
48
     */
49
    public function wrap($value, $prefixAlias = false)
50
    {
51
        if ($this->isExpression($value)) {
52
            return $this->getValue($value);
53
        }
54

    
55
        // If the value being wrapped has a column alias we will need to separate out
56
        // the pieces so we can wrap each of the segments of the expression on it
57
        // own, and then joins them both back together with the "as" connector.
58
        if (strpos(strtolower($value), ' as ') !== false) {
59
            $segments = explode(' ', $value);
60

    
61
            if ($prefixAlias) {
62
                $segments[2] = $this->tablePrefix.$segments[2];
63
            }
64

    
65
            return $this->wrap($segments[0]).' as '.$this->wrapValue($segments[2]);
66
        }
67

    
68
        $wrapped = [];
69

    
70
        $segments = explode('.', $value);
71

    
72
        // If the value is not an aliased table expression, we'll just wrap it like
73
        // normal, so if there is more than one segment, we will wrap the first
74
        // segments as if it was a table and the rest as just regular values.
75
        foreach ($segments as $key => $segment) {
76
            if ($key == 0 && count($segments) > 1) {
77
                $wrapped[] = $this->wrapTable($segment);
78
            } else {
79
                $wrapped[] = $this->wrapValue($segment);
80
            }
81
        }
82

    
83
        return implode('.', $wrapped);
84
    }
85

    
86
    /**
87
     * Wrap a single string in keyword identifiers.
88
     *
89
     * @param  string  $value
90
     * @return string
91
     */
92
    protected function wrapValue($value)
93
    {
94
        if ($value === '*') {
95
            return $value;
96
        }
97

    
98
        return '"'.str_replace('"', '""', $value).'"';
99
    }
100

    
101
    /**
102
     * Convert an array of column names into a delimited string.
103
     *
104
     * @param  array   $columns
105
     * @return string
106
     */
107
    public function columnize(array $columns)
108
    {
109
        return implode(', ', array_map([$this, 'wrap'], $columns));
110
    }
111

    
112
    /**
113
     * Create query parameter place-holders for an array.
114
     *
115
     * @param  array   $values
116
     * @return string
117
     */
118
    public function parameterize(array $values)
119
    {
120
        return implode(', ', array_map([$this, 'parameter'], $values));
121
    }
122

    
123
    /**
124
     * Get the appropriate query parameter place-holder for a value.
125
     *
126
     * @param  mixed   $value
127
     * @return string
128
     */
129
    public function parameter($value)
130
    {
131
        return $this->isExpression($value) ? $this->getValue($value) : '?';
132
    }
133

    
134
    /**
135
     * Get the value of a raw expression.
136
     *
137
     * @param  \Illuminate\Database\Query\Expression  $expression
138
     * @return string
139
     */
140
    public function getValue($expression)
141
    {
142
        return $expression->getValue();
143
    }
144

    
145
    /**
146
     * Determine if the given value is a raw expression.
147
     *
148
     * @param  mixed  $value
149
     * @return bool
150
     */
151
    public function isExpression($value)
152
    {
153
        return $value instanceof Expression;
154
    }
155

    
156
    /**
157
     * Get the format for database stored dates.
158
     *
159
     * @return string
160
     */
161
    public function getDateFormat()
162
    {
163
        return 'Y-m-d H:i:s';
164
    }
165

    
166
    /**
167
     * Get the grammar's table prefix.
168
     *
169
     * @return string
170
     */
171
    public function getTablePrefix()
172
    {
173
        return $this->tablePrefix;
174
    }
175

    
176
    /**
177
     * Set the grammar's table prefix.
178
     *
179
     * @param  string  $prefix
180
     * @return $this
181
     */
182
    public function setTablePrefix($prefix)
183
    {
184
        $this->tablePrefix = $prefix;
185

    
186
        return $this;
187
    }
188
}
(8-8/18)