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\Schema;
4
 
5
use Illuminate\Database\Concerns\ParsesSearchPath;
6
 
7
class PostgresBuilder extends Builder
8
{
9
    use ParsesSearchPath {
10
        parseSearchPath as baseParseSearchPath;
11
    }
12
 
13
    /**
14
     * Create a database in the schema.
15
     *
16
     * @param  string  $name
17
     * @return bool
18
     */
19
    public function createDatabase($name)
20
    {
21
        return $this->connection->statement(
22
            $this->grammar->compileCreateDatabase($name, $this->connection)
23
        );
24
    }
25
 
26
    /**
27
     * Drop a database from the schema if the database exists.
28
     *
29
     * @param  string  $name
30
     * @return bool
31
     */
32
    public function dropDatabaseIfExists($name)
33
    {
34
        return $this->connection->statement(
35
            $this->grammar->compileDropDatabaseIfExists($name)
36
        );
37
    }
38
 
39
    /**
40
     * Determine if the given table exists.
41
     *
42
     * @param  string  $table
43
     * @return bool
44
     */
45
    public function hasTable($table)
46
    {
47
        [$database, $schema, $table] = $this->parseSchemaAndTable($table);
48
 
49
        $table = $this->connection->getTablePrefix().$table;
50
 
51
        return count($this->connection->selectFromWriteConnection(
52
            $this->grammar->compileTableExists(), [$database, $schema, $table]
53
        )) > 0;
54
    }
55
 
56
    /**
57
     * Drop all tables from the database.
58
     *
59
     * @return void
60
     */
61
    public function dropAllTables()
62
    {
63
        $tables = [];
64
 
65
        $excludedTables = $this->grammar->escapeNames(
66
            $this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys']
67
        );
68
 
69
        foreach ($this->getAllTables() as $row) {
70
            $row = (array) $row;
71
 
72
            if (empty(array_intersect($this->grammar->escapeNames($row), $excludedTables))) {
73
                $tables[] = $row['qualifiedname'] ?? reset($row);
74
            }
75
        }
76
 
77
        if (empty($tables)) {
78
            return;
79
        }
80
 
81
        $this->connection->statement(
82
            $this->grammar->compileDropAllTables($tables)
83
        );
84
    }
85
 
86
    /**
87
     * Drop all views from the database.
88
     *
89
     * @return void
90
     */
91
    public function dropAllViews()
92
    {
93
        $views = [];
94
 
95
        foreach ($this->getAllViews() as $row) {
96
            $row = (array) $row;
97
 
98
            $views[] = $row['qualifiedname'] ?? reset($row);
99
        }
100
 
101
        if (empty($views)) {
102
            return;
103
        }
104
 
105
        $this->connection->statement(
106
            $this->grammar->compileDropAllViews($views)
107
        );
108
    }
109
 
110
    /**
111
     * Drop all types from the database.
112
     *
113
     * @return void
114
     */
115
    public function dropAllTypes()
116
    {
117
        $types = [];
118
 
119
        foreach ($this->getAllTypes() as $row) {
120
            $row = (array) $row;
121
 
122
            $types[] = reset($row);
123
        }
124
 
125
        if (empty($types)) {
126
            return;
127
        }
128
 
129
        $this->connection->statement(
130
            $this->grammar->compileDropAllTypes($types)
131
        );
132
    }
133
 
134
    /**
135
     * Get all of the table names for the database.
136
     *
137
     * @return array
138
     */
139
    public function getAllTables()
140
    {
141
        return $this->connection->select(
142
            $this->grammar->compileGetAllTables(
143
                $this->parseSearchPath(
144
                    $this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema')
145
                )
146
            )
147
        );
148
    }
149
 
150
    /**
151
     * Get all of the view names for the database.
152
     *
153
     * @return array
154
     */
155
    public function getAllViews()
156
    {
157
        return $this->connection->select(
158
            $this->grammar->compileGetAllViews(
159
                $this->parseSearchPath(
160
                    $this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema')
161
                )
162
            )
163
        );
164
    }
165
 
166
    /**
167
     * Get all of the type names for the database.
168
     *
169
     * @return array
170
     */
171
    public function getAllTypes()
172
    {
173
        return $this->connection->select(
174
            $this->grammar->compileGetAllTypes()
175
        );
176
    }
177
 
178
    /**
179
     * Get the column listing for a given table.
180
     *
181
     * @param  string  $table
182
     * @return array
183
     */
184
    public function getColumnListing($table)
185
    {
186
        [$database, $schema, $table] = $this->parseSchemaAndTable($table);
187
 
188
        $table = $this->connection->getTablePrefix().$table;
189
 
190
        $results = $this->connection->selectFromWriteConnection(
191
            $this->grammar->compileColumnListing(), [$database, $schema, $table]
192
        );
193
 
194
        return $this->connection->getPostProcessor()->processColumnListing($results);
195
    }
196
 
197
    /**
198
     * Parse the database object reference and extract the database, schema, and table.
199
     *
200
     * @param  string  $reference
201
     * @return array
202
     */
203
    protected function parseSchemaAndTable($reference)
204
    {
205
        $searchPath = $this->parseSearchPath(
206
            $this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema') ?: 'public'
207
        );
208
 
209
        $parts = explode('.', $reference);
210
 
211
        $database = $this->connection->getConfig('database');
212
 
213
        // If the reference contains a database name, we will use that instead of the
214
        // default database name for the connection. This allows the database name
215
        // to be specified in the query instead of at the full connection level.
216
        if (count($parts) === 3) {
217
            $database = $parts[0];
218
            array_shift($parts);
219
        }
220
 
221
        // We will use the default schema unless the schema has been specified in the
222
        // query. If the schema has been specified in the query then we can use it
223
        // instead of a default schema configured in the connection search path.
224
        $schema = $searchPath[0];
225
 
226
        if (count($parts) === 2) {
227
            $schema = $parts[0];
228
            array_shift($parts);
229
        }
230
 
231
        return [$database, $schema, $parts[0]];
232
    }
233
 
234
    /**
235
     * Parse the "search_path" configuration value into an array.
236
     *
237
     * @param  string|array|null  $searchPath
238
     * @return array
239
     */
240
    protected function parseSearchPath($searchPath)
241
    {
242
        return array_map(function ($schema) {
243
            return $schema === '$user'
244
                ? $this->connection->getConfig('username')
245
                : $schema;
246
        }, $this->baseParseSearchPath($searchPath));
247
    }
248
}