| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Lagdo\DbAdmin\Driver\Sqlite\Db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Lagdo\DbAdmin\Driver\Entity\TableEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Lagdo\DbAdmin\Driver\Db\Database as AbstractDatabase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use function is_object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use function intval; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use function implode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use function array_reverse; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | class Database extends AbstractDatabase | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     use DatabaseTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function tables() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         return $this->driver->keyValues('SELECT name, type FROM sqlite_master ' . | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |             "WHERE type IN ('table', 'view') ORDER BY (name = 'sqlite_sequence'), name"); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function countTables(array $databases) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $counts = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $query = "SELECT count(*) FROM sqlite_master WHERE type IN ('table', 'view')"; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         foreach ($databases as $database) { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             $counts[$database] = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             $connection = $this->driver->connect($database); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             $statement = $connection->query($query); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             if (is_object($statement) && ($row = $statement->fetchRow())) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                 $counts[$database] = intval($row[0]); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         return $counts; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function dropViews(array $views) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         return $this->driver->applyQueries('DROP VIEW', $views); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     public function dropTables(array $tables) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         return $this->driver->applyQueries('DROP TABLE', $tables); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     public function moveTables(array $tables, array $views, string $target) | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     public function truncateTables(array $tables) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         return $this->driver->applyQueries('DELETE FROM', $tables); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function createTable(TableEntity $tableAttrs) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         foreach ($tableAttrs->fields as $key => $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $tableAttrs->fields[$key] = '  ' . implode($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $tableAttrs->fields = array_merge($tableAttrs->fields, array_filter($tableAttrs->foreign)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         if (!$this->driver->execute('CREATE TABLE ' . $this->driver->escapeTableName($tableAttrs->name) . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             " (\n" . implode(",\n", $tableAttrs->fields) . "\n)")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             // implicit ROLLBACK to not overwrite $this->driver->error() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->setAutoIncrement($tableAttrs->name, $tableAttrs->autoIncrement); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |         return true; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 95 |  |  |      * @inheritDoc | 
            
                                                                        
                                                                
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     public function alterTable(string $table, TableEntity $tableAttrs) | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $clauses = $this->getAlterTableClauses($tableAttrs); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $queries = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         foreach ($clauses as $clause) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             $queries[] = 'ALTER TABLE ' . $this->driver->escapeTableName($table) . ' ' . $clause; | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |         if ($table != $tableAttrs->name) { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             $queries[] = 'ALTER TABLE ' . $this->driver->escapeTableName($table) . ' RENAME TO ' . | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 $this->driver->escapeTableName($tableAttrs->name); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         if (!$this->executeQueries($queries)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         $this->setAutoIncrement($tableAttrs->name, $tableAttrs->autoIncrement); | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @inheritDoc | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function alterIndexes(string $table, array $alter, array $drop) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $queries = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         foreach (array_reverse($drop) as $index) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $queries[] = 'DROP INDEX ' . $this->driver->escapeId($index->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         foreach (array_reverse($alter) as $index) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             // Can't alter primary keys | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             if ($index->type !== 'PRIMARY') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 $queries[] =  $this->driver->getCreateIndexQuery($table, $index->type, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                     $index->name, '(' . implode(', ', $index->columns) . ')'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  |         return $this->executeQueries($queries); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  |