Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database;
4
 
5
use Illuminate\Database\Query\Expression;
6
use Illuminate\Support\Traits\Macroable;
7
use RuntimeException;
8
 
9
abstract class Grammar
10
{
11
    use Macroable;
12
 
13
    /**
14
     * The grammar table prefix.
15
     *
16
     * @var string
17
     */
18
    protected $tablePrefix = '';
19
 
20
    /**
21
     * Wrap an array of values.
22
     *
23
     * @param  array  $values
24
     * @return array
25
     */
26
    public function wrapArray(array $values)
27
    {
28
        return array_map([$this, 'wrap'], $values);
29
    }
30
 
31
    /**
32
     * Wrap a table in keyword identifiers.
33
     *
34
     * @param  \Illuminate\Database\Query\Expression|string  $table
35
     * @return string
36
     */
37
    public function wrapTable($table)
38
    {
39
        if (! $this->isExpression($table)) {
40
            return $this->wrap($this->tablePrefix.$table, true);
41
        }
42
 
43
        return $this->getValue($table);
44
    }
45
 
46
    /**
47
     * Wrap a value in keyword identifiers.
48
     *
49
     * @param  \Illuminate\Database\Query\Expression|string  $value
50
     * @param  bool  $prefixAlias
51
     * @return string
52
     */
53
    public function wrap($value, $prefixAlias = false)
54
    {
55
        if ($this->isExpression($value)) {
56
            return $this->getValue($value);
57
        }
58
 
59
        // If the value being wrapped has a column alias we will need to separate out
60
        // the pieces so we can wrap each of the segments of the expression on its
61
        // own, and then join these both back together using the "as" connector.
62
        if (stripos($value, ' as ') !== false) {
63
            return $this->wrapAliasedValue($value, $prefixAlias);
64
        }
65
 
66
        // If the given value is a JSON selector we will wrap it differently than a
67
        // traditional value. We will need to split this path and wrap each part
68
        // wrapped, etc. Otherwise, we will simply wrap the value as a string.
69
        if ($this->isJsonSelector($value)) {
70
            return $this->wrapJsonSelector($value);
71
        }
72
 
73
        return $this->wrapSegments(explode('.', $value));
74
    }
75
 
76
    /**
77
     * Wrap a value that has an alias.
78
     *
79
     * @param  string  $value
80
     * @param  bool  $prefixAlias
81
     * @return string
82
     */
83
    protected function wrapAliasedValue($value, $prefixAlias = false)
84
    {
85
        $segments = preg_split('/\s+as\s+/i', $value);
86
 
87
        // If we are wrapping a table we need to prefix the alias with the table prefix
88
        // as well in order to generate proper syntax. If this is a column of course
89
        // no prefix is necessary. The condition will be true when from wrapTable.
90
        if ($prefixAlias) {
91
            $segments[1] = $this->tablePrefix.$segments[1];
92
        }
93
 
94
        return $this->wrap($segments[0]).' as '.$this->wrapValue($segments[1]);
95
    }
96
 
97
    /**
98
     * Wrap the given value segments.
99
     *
100
     * @param  array  $segments
101
     * @return string
102
     */
103
    protected function wrapSegments($segments)
104
    {
105
        return collect($segments)->map(function ($segment, $key) use ($segments) {
106
            return $key == 0 && count($segments) > 1
107
                            ? $this->wrapTable($segment)
108
                            : $this->wrapValue($segment);
109
        })->implode('.');
110
    }
111
 
112
    /**
113
     * Wrap a single string in keyword identifiers.
114
     *
115
     * @param  string  $value
116
     * @return string
117
     */
118
    protected function wrapValue($value)
119
    {
120
        if ($value !== '*') {
121
            return '"'.str_replace('"', '""', $value).'"';
122
        }
123
 
124
        return $value;
125
    }
126
 
127
    /**
128
     * Wrap the given JSON selector.
129
     *
130
     * @param  string  $value
131
     * @return string
132
     *
133
     * @throws \RuntimeException
134
     */
135
    protected function wrapJsonSelector($value)
136
    {
137
        throw new RuntimeException('This database engine does not support JSON operations.');
138
    }
139
 
140
    /**
141
     * Determine if the given string is a JSON selector.
142
     *
143
     * @param  string  $value
144
     * @return bool
145
     */
146
    protected function isJsonSelector($value)
147
    {
148
        return str_contains($value, '->');
149
    }
150
 
151
    /**
152
     * Convert an array of column names into a delimited string.
153
     *
154
     * @param  array  $columns
155
     * @return string
156
     */
157
    public function columnize(array $columns)
158
    {
159
        return implode(', ', array_map([$this, 'wrap'], $columns));
160
    }
161
 
162
    /**
163
     * Create query parameter place-holders for an array.
164
     *
165
     * @param  array  $values
166
     * @return string
167
     */
168
    public function parameterize(array $values)
169
    {
170
        return implode(', ', array_map([$this, 'parameter'], $values));
171
    }
172
 
173
    /**
174
     * Get the appropriate query parameter place-holder for a value.
175
     *
176
     * @param  mixed  $value
177
     * @return string
178
     */
179
    public function parameter($value)
180
    {
181
        return $this->isExpression($value) ? $this->getValue($value) : '?';
182
    }
183
 
184
    /**
185
     * Quote the given string literal.
186
     *
187
     * @param  string|array  $value
188
     * @return string
189
     */
190
    public function quoteString($value)
191
    {
192
        if (is_array($value)) {
193
            return implode(', ', array_map([$this, __FUNCTION__], $value));
194
        }
195
 
196
        return "'$value'";
197
    }
198
 
199
    /**
200
     * Determine if the given value is a raw expression.
201
     *
202
     * @param  mixed  $value
203
     * @return bool
204
     */
205
    public function isExpression($value)
206
    {
207
        return $value instanceof Expression;
208
    }
209
 
210
    /**
211
     * Get the value of a raw expression.
212
     *
213
     * @param  \Illuminate\Database\Query\Expression  $expression
214
     * @return mixed
215
     */
216
    public function getValue($expression)
217
    {
218
        return $expression->getValue();
219
    }
220
 
221
    /**
222
     * Get the format for database stored dates.
223
     *
224
     * @return string
225
     */
226
    public function getDateFormat()
227
    {
228
        return 'Y-m-d H:i:s';
229
    }
230
 
231
    /**
232
     * Get the grammar's table prefix.
233
     *
234
     * @return string
235
     */
236
    public function getTablePrefix()
237
    {
238
        return $this->tablePrefix;
239
    }
240
 
241
    /**
242
     * Set the grammar's table prefix.
243
     *
244
     * @param  string  $prefix
245
     * @return $this
246
     */
247
    public function setTablePrefix($prefix)
248
    {
249
        $this->tablePrefix = $prefix;
250
 
251
        return $this;
252
    }
253
}