| 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 | 40 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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->connection->prepareBindings($query->getBindings()); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 36 |  |         foreach ($bindings as &$binding) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 36 |  |             if (is_object($binding)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 4 |  |                 $binding = (string) $binding; | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 36 |  |             if (is_string($binding)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 8 |  |                 $binding = $this->connection->getPdo()->quote($binding); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 36 |  |         return Str::replaceArray('?', $bindings, $query->toSql()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Rename a view on the schema. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string $from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |     public function renameView($from, $to) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |         $this->rename($from, $to); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * Drop a view from the schema. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param bool $ifExists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 33 |  |     public function dropView($name, $ifExists = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 33 |  |         $this->connection->statement( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 33 |  |             $this->grammar->compileDropView($name, $ifExists) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 33 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * Drop a view from the schema if it exists. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 44 |  |     public function dropViewIfExists($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 44 |  |         $this->dropView($name, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 44 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Determine if the given view exists on the schema. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 12 |  |     public function hasView($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 12 |  |         $view = $this->connection->getTablePrefix().$name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 12 |  |         $view = $this->connection->selectOne( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 12 |  |             $this->grammar->compileViewExists(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 12 |  |             $this->getBindingsForHasView($view) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 12 |  |         return !is_null($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * Get the bindings for a "Has View" statement. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param string $view | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 3 |  |     protected function getBindingsForHasView($view) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 3 |  |         return [$this->connection->getDatabaseName(), $view]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * Get the column listing for a given view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 3 |  |     public function getViewColumnListing($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 3 |  |         return $this->getColumnListing($name); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  |