1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelMigrationViews\Schema\Builders; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
trait ManagesViews |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Create a new view on the schema. |
11
|
|
|
* |
12
|
|
|
* @param string $name |
13
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query |
14
|
|
|
* @param array|null $columns |
15
|
|
|
* @param bool $orReplace |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
40 |
|
public function createView($name, $query, array $columns = null, $orReplace = false) |
19
|
|
|
{ |
20
|
40 |
|
$query = $this->getQueryString($query); |
21
|
|
|
|
22
|
40 |
|
$this->connection->statement( |
23
|
40 |
|
$this->grammar->compileCreateView($name, $query, $columns, $orReplace) |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new view on the schema or replace an existing one. |
29
|
|
|
* |
30
|
|
|
* @param string $name |
31
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query |
32
|
|
|
* @param array|null $columns |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
4 |
|
public function createOrReplaceView($name, $query, array $columns = null) |
36
|
|
|
{ |
37
|
4 |
|
$this->createView($name, $query, $columns, true); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Convert the query and its bindings to an SQL string. |
42
|
|
|
* |
43
|
|
|
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
40 |
|
protected function getQueryString($query) |
47
|
|
|
{ |
48
|
40 |
|
if (is_string($query)) { |
49
|
4 |
|
return $query; |
50
|
|
|
} |
51
|
|
|
|
52
|
36 |
|
$bindings = $this->stringifyBindings( |
53
|
36 |
|
$query->getBindings() |
|
|
|
|
54
|
|
|
); |
55
|
|
|
|
56
|
36 |
|
return Str::replaceArray('?', $bindings, $query->toSql()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Stringify the query bindings. |
61
|
|
|
* |
62
|
|
|
* @param array $bindings |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
36 |
|
protected function stringifyBindings(array $bindings) |
66
|
|
|
{ |
67
|
36 |
|
$bindings = $this->connection->prepareBindings($bindings); |
68
|
|
|
|
69
|
36 |
|
foreach ($bindings as &$binding) { |
70
|
36 |
|
if (is_object($binding)) { |
71
|
4 |
|
$binding = (string) $binding; |
72
|
|
|
} |
73
|
|
|
|
74
|
36 |
|
if (is_string($binding)) { |
75
|
15 |
|
$binding = $this->connection->getPdo()->quote($binding); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
36 |
|
return $bindings; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Rename a view on the schema. |
84
|
|
|
* |
85
|
|
|
* @param string $from |
86
|
|
|
* @param string $to |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
3 |
|
public function renameView($from, $to) |
90
|
|
|
{ |
91
|
3 |
|
$this->rename($from, $to); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Drop a view from the schema. |
96
|
|
|
* |
97
|
|
|
* @param string $name |
98
|
|
|
* @param bool $ifExists |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
33 |
|
public function dropView($name, $ifExists = false) |
102
|
|
|
{ |
103
|
33 |
|
$this->connection->statement( |
104
|
33 |
|
$this->grammar->compileDropView($name, $ifExists) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Drop a view from the schema if it exists. |
110
|
|
|
* |
111
|
|
|
* @param string $name |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
44 |
|
public function dropViewIfExists($name) |
115
|
|
|
{ |
116
|
44 |
|
$this->dropView($name, true); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Determine if the given view exists on the schema. |
121
|
|
|
* |
122
|
|
|
* @param string $name |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
12 |
|
public function hasView($name) |
126
|
|
|
{ |
127
|
12 |
|
$view = $this->connection->getTablePrefix().$name; |
128
|
|
|
|
129
|
12 |
|
$view = $this->connection->selectOne( |
130
|
12 |
|
$this->grammar->compileViewExists(), |
131
|
12 |
|
$this->getBindingsForHasView($view) |
132
|
|
|
); |
133
|
|
|
|
134
|
12 |
|
return !is_null($view); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the bindings for a "Has View" statement. |
139
|
|
|
* |
140
|
|
|
* @param string $view |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
3 |
|
protected function getBindingsForHasView($view) |
144
|
|
|
{ |
145
|
3 |
|
return [$this->connection->getDatabaseName(), $view]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the column listing for a given view. |
150
|
|
|
* |
151
|
|
|
* @param string $name |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
3 |
|
public function getViewColumnListing($name) |
155
|
|
|
{ |
156
|
3 |
|
return $this->getColumnListing($name); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|