| 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 | 12 |  |         $query = $this->pdo->prepare('PRAGMA table_info("' . $table . '");'); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 12 |  |         $query->execute(); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 12 |  |         $query->setFetchMode(\PDO::FETCH_OBJ); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 12 |  |         while ($row = $query->fetch()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 12 |  |             $column = new Column; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 12 |  |             $this->setProperties($row, $column); | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 12 |  |             $column->setDefaultValue($row->dflt_value); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 12 |  |             $column->setField($row->name); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 12 |  |             $column->setDataType(strtolower($row->type)); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 12 |  |             array_push($columns, $column); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 12 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 12 |  |         return $this->prepareForeignColumns($columns, $table); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * Shows the list of tables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 6 |  |     public function showTables() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 6 |  |         $tables = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 6 |  |         $query = $this->pdo->prepare('SELECT name FROM sqlite_master WHERE type = "table";'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 6 |  |         $query->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  |         $query->setFetchMode(\PDO::FETCH_OBJ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  |         while ($row = $query->fetch()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 6 |  |             if ($row->name != 'sqlite_sequence') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 6 |  |                 array_push($tables, $row->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 6 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 6 |  |         return $tables; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Prepares the columns that have foreign keys. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param array  &$columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param object $row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 12 |  |     protected function setForeignColumn(array &$columns, $row) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 12 |  |         foreach ($columns as $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 12 |  |             if ($column->getField() == $row->from) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 12 |  |                 $column->setForeign(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 12 |  |                 $column->setReferencedField($row->to); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 12 |  |                 $column->setReferencedTable($row->table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 12 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 12 |  |         return $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Prepares the query for getting the foreign columns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param  array  $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param  string $tableName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 12 |  |     protected function prepareForeignColumns(array $columns, $tableName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 12 |  |         $query = $this->pdo->prepare('PRAGMA foreign_key_list("' . $tableName . '");'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 12 |  |         $query->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 12 |  |         $query->setFetchMode(\PDO::FETCH_OBJ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 12 |  |         while ($row = $query->fetch()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 12 |  |             $this->setForeignColumn($columns, $row); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 12 |  |         return $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * Sets the properties of the specified column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param  mixed                   $row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @param  \Rougin\Describe\Column &$column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 12 |  |     protected function setProperties($row, Column &$column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 12 |  |         if (! $row->notnull) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 12 |  |             $column->setNull(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 12 |  |         if ($row->pk) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 12 |  |             $column->setPrimary(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 12 |  |             $column->setAutoIncrement(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 12 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 12 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |  |