staudenmeir /
laravel-migration-views
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Staudenmeir\LaravelMigrationViews\Schema\Builders; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
||||||
| 6 | use Illuminate\Database\Query\Builder as QueryBuilder; |
||||||
| 7 | use Illuminate\Support\Str; |
||||||
| 8 | |||||||
| 9 | trait ManagesViews |
||||||
| 10 | { |
||||||
| 11 | /** |
||||||
| 12 | * Create a new view on the schema. |
||||||
| 13 | * |
||||||
| 14 | * @param string $name |
||||||
| 15 | * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query |
||||||
| 16 | * @param array|null $columns |
||||||
| 17 | * @param bool $orReplace |
||||||
| 18 | * @param bool $materialized |
||||||
| 19 | * @return void |
||||||
| 20 | */ |
||||||
| 21 | public function createView($name, $query, ?array $columns = null, $orReplace = false, bool $materialized = false) |
||||||
| 22 | { |
||||||
| 23 | $query = $this->getQueryString($query); |
||||||
| 24 | |||||||
| 25 | $this->connection->statement( |
||||||
| 26 | $this->grammar->compileCreateView($name, $query, $columns, $orReplace, $materialized) |
||||||
| 27 | ); |
||||||
| 28 | } |
||||||
| 29 | |||||||
| 30 | /** |
||||||
| 31 | * Create a new view on the schema or replace an existing one. |
||||||
| 32 | * |
||||||
| 33 | * @param string $name |
||||||
| 34 | * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query |
||||||
| 35 | * @param array|null $columns |
||||||
| 36 | * @return void |
||||||
| 37 | */ |
||||||
| 38 | public function createOrReplaceView($name, $query, ?array $columns = null) |
||||||
| 39 | { |
||||||
| 40 | $this->createView($name, $query, $columns, true); |
||||||
| 41 | } |
||||||
| 42 | |||||||
| 43 | /** |
||||||
| 44 | * Create a new materialized view on the schema. |
||||||
| 45 | * |
||||||
| 46 | * @param string $name |
||||||
| 47 | * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query |
||||||
| 48 | * @param array|null $columns |
||||||
| 49 | * @return void |
||||||
| 50 | */ |
||||||
| 51 | public function createMaterializedView(string $name, EloquentBuilder|QueryBuilder $query, ?array $columns = null): void |
||||||
| 52 | { |
||||||
| 53 | $this->createView($name, $query, $columns, materialized: true); |
||||||
| 54 | } |
||||||
| 55 | |||||||
| 56 | /** |
||||||
| 57 | * Convert the query and its bindings to an SQL string. |
||||||
| 58 | * |
||||||
| 59 | * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query |
||||||
| 60 | * @return string |
||||||
| 61 | */ |
||||||
| 62 | protected function getQueryString($query) |
||||||
| 63 | { |
||||||
| 64 | if (is_string($query)) { |
||||||
| 65 | return $query; |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 | $bindings = $this->stringifyBindings( |
||||||
| 69 | $query->getBindings() |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 70 | ); |
||||||
| 71 | |||||||
| 72 | return Str::replaceArray('?', $bindings, $query->toSql()); |
||||||
|
0 ignored issues
–
show
It seems like
$query->toSql() can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Builder; however, parameter $subject of Illuminate\Support\Str::replaceArray() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | /** |
||||||
| 76 | * Stringify the query bindings. |
||||||
| 77 | * |
||||||
| 78 | * @param array $bindings |
||||||
| 79 | * @return array |
||||||
| 80 | */ |
||||||
| 81 | protected function stringifyBindings(array $bindings) |
||||||
| 82 | { |
||||||
| 83 | $bindings = $this->connection->prepareBindings($bindings); |
||||||
| 84 | |||||||
| 85 | foreach ($bindings as &$binding) { |
||||||
| 86 | if (is_object($binding)) { |
||||||
| 87 | $binding = (string) $binding; |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | if (is_string($binding)) { |
||||||
| 91 | $binding = $this->connection->getPdo()->quote($binding); |
||||||
| 92 | } |
||||||
| 93 | } |
||||||
| 94 | |||||||
| 95 | return $bindings; |
||||||
| 96 | } |
||||||
| 97 | |||||||
| 98 | /** |
||||||
| 99 | * Rename a view on the schema. |
||||||
| 100 | * |
||||||
| 101 | * @param string $from |
||||||
| 102 | * @param string $to |
||||||
| 103 | * @return void |
||||||
| 104 | */ |
||||||
| 105 | public function renameView($from, $to) |
||||||
| 106 | { |
||||||
| 107 | $this->rename($from, $to); |
||||||
|
0 ignored issues
–
show
The method
rename() does not exist on Staudenmeir\LaravelMigra...a\Builders\ManagesViews. Did you maybe mean renameView()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 108 | } |
||||||
| 109 | |||||||
| 110 | /** |
||||||
| 111 | * Drop a view from the schema. |
||||||
| 112 | * |
||||||
| 113 | * @param string $name |
||||||
| 114 | * @param bool $ifExists |
||||||
| 115 | * @return void |
||||||
| 116 | */ |
||||||
| 117 | public function dropView($name, $ifExists = false) |
||||||
| 118 | { |
||||||
| 119 | $this->connection->statement( |
||||||
| 120 | $this->grammar->compileDropView($name, $ifExists) |
||||||
| 121 | ); |
||||||
| 122 | } |
||||||
| 123 | |||||||
| 124 | /** |
||||||
| 125 | * Drop a view from the schema if it exists. |
||||||
| 126 | * |
||||||
| 127 | * @param string $name |
||||||
| 128 | * @return void |
||||||
| 129 | */ |
||||||
| 130 | public function dropViewIfExists($name) |
||||||
| 131 | { |
||||||
| 132 | $this->dropView($name, true); |
||||||
| 133 | } |
||||||
| 134 | |||||||
| 135 | /** |
||||||
| 136 | * Get the column listing for a given view. |
||||||
| 137 | * |
||||||
| 138 | * @param string $name |
||||||
| 139 | * @return array |
||||||
| 140 | */ |
||||||
| 141 | public function getViewColumnListing($name) |
||||||
| 142 | { |
||||||
| 143 | return $this->getColumnListing($name); |
||||||
|
0 ignored issues
–
show
It seems like
getColumnListing() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 144 | } |
||||||
| 145 | |||||||
| 146 | /** |
||||||
| 147 | * Refresh a materialized view on the schema. |
||||||
| 148 | * |
||||||
| 149 | * @param string $name |
||||||
| 150 | * @return void |
||||||
| 151 | */ |
||||||
| 152 | public function refreshMaterializedView(string $name): void |
||||||
| 153 | { |
||||||
| 154 | $this->connection->statement( |
||||||
| 155 | $this->grammar->compileRefreshMaterializedView($name) |
||||||
| 156 | ); |
||||||
| 157 | } |
||||||
| 158 | } |
||||||
| 159 |