| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Rougin\Describe\Driver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Rougin\Describe\Column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * SQLite Driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * A database driver extension for SQLite. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package  Describe | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @category Driver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @author   Rougin Royce Gutib <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class SQLiteDriver implements DriverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var \PDO | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $pdo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param PDO $pdo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 18 |  |     public function __construct(\PDO $pdo) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 18 |  |         $this->pdo = $pdo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Returns the result. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 12 |  |     public function getTable($table) | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 12 |  |         $columns = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         // Gets list of columns | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 12 |  |         $query = 'PRAGMA table_info("' . $table . '");'; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 12 |  |         $information = $this->pdo->prepare($query); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 12 |  |         $information->execute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 12 |  |         $information->setFetchMode(\PDO::FETCH_OBJ); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 12 |  |         while ($row = $information->fetch()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 12 |  |             $column = new Column; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 12 |  |             $this->setProperties($row, $column); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 12 |  |             $column->setDefaultValue($row->dflt_value); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 12 |  |             $column->setField($row->name); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 12 |  |             $column->setDataType(strtolower($row->type)); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 12 |  |             array_push($columns, $column); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 12 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 12 |  |         return $this->setForeignColumns($table, $columns); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Shows the list of tables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  |     public function showTables() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 6 |  |         $tables = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         // Gets list of columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  |         $query = 'SELECT name FROM sqlite_master WHERE type = "table";'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 6 |  |         $information = $this->pdo->prepare($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  |         $information->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 6 |  |         $information->setFetchMode(\PDO::FETCH_OBJ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 6 |  |         while ($row = $information->fetch()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 6 |  |             if ($row->name != 'sqlite_sequence') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 6 |  |                 array_push($tables, $row->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 6 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 6 |  |         return $tables; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Sets the properties of the specified column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param  string $tableName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param  array  $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 12 |  |     protected function setForeignColumns($tableName, array $columns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         // Gets list of foreign keys, if any | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 12 |  |         $query = 'PRAGMA foreign_key_list("' . $tableName . '");'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 12 |  |         $table = $this->pdo->prepare($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 12 |  |         $table->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 12 |  |         $table->setFetchMode(\PDO::FETCH_OBJ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 12 |  |         while ($row = $table->fetch()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 12 |  |             foreach ($columns as $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 12 |  |                 if ($column->getField() == $row->from) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 12 |  |                     $column->setForeign(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 12 |  |                     $column->setReferencedField($row->to); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 12 |  |                     $column->setReferencedTable($row->table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 12 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 12 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 12 |  |         return $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Sets the properties of the specified column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param  mixed                   $row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param  \Rougin\Describe\Column &$column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 12 |  |     protected function setProperties($row, Column &$column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 12 |  |         if (! $row->notnull) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 12 |  |             $column->setNull(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 12 |  |         if ($row->pk) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 12 |  |             $column->setPrimary(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 12 |  |             $column->setAutoIncrement(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 12 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 135 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |  |