@@ -4,15 +4,15 @@ discard block |
||
| 4 | 4 | class SqliteAdapter implements DatabaseAdapter { |
| 5 | 5 | private $pdo; |
| 6 | 6 | private $stmtCache; |
| 7 | - private $generalEditor; |
|
| 7 | + private $generalEditor; |
|
| 8 | 8 | |
| 9 | 9 | public function __construct(\PDO $pdo) { |
| 10 | 10 | $this->pdo = $pdo; |
| 11 | - $this->stmtCache = new StmtCache($pdo); |
|
| 12 | - $this->generalEditor = new GeneralEditDatabase($this->pdo, [ |
|
| 13 | - 'int' => 'INTEGER', |
|
| 14 | - 'pk_default' => 'INTEGER NOT NULL', |
|
| 15 | - ]); |
|
| 11 | + $this->stmtCache = new StmtCache($pdo); |
|
| 12 | + $this->generalEditor = new GeneralEditDatabase($this->pdo, [ |
|
| 13 | + 'int' => 'INTEGER', |
|
| 14 | + 'pk_default' => 'INTEGER NOT NULL', |
|
| 15 | + ]); |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | public function quote($str) { |
@@ -20,19 +20,19 @@ discard block |
||
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public function query(\Maphper\Lib\Query $query) { |
| 23 | - $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
| 23 | + $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
| 24 | 24 | $args = $query->getArgs(); |
| 25 | 25 | |
| 26 | - //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
| 27 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
| 26 | + //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
| 27 | + if ($stmt === false) throw new \Exception('Invalid query'); |
|
| 28 | 28 | |
| 29 | - $stmt->execute($args); |
|
| 30 | - if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
| 29 | + $stmt->execute($args); |
|
| 30 | + if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
| 31 | 31 | $this->stmtCache->deleteQueryFromCache($query->getSql()); |
| 32 | 32 | return $this->query($query); |
| 33 | - } |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - return $stmt; |
|
| 35 | + return $stmt; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function lastInsertId() { |
@@ -59,11 +59,11 @@ discard block |
||
| 59 | 59 | // SQLSTATE[HY000]: General error: 17 database schema has changed |
| 60 | 60 | $this->stmtCache->clearCache(); |
| 61 | 61 | |
| 62 | - // Create temp table to create a new structure |
|
| 62 | + // Create temp table to create a new structure |
|
| 63 | 63 | $affix = '_'.substr(md5($table), 0, 6); |
| 64 | - $tempTable = $table . $affix; |
|
| 64 | + $tempTable = $table . $affix; |
|
| 65 | 65 | $this->generalEditor->createTable($tempTable, $primaryKey, $data); |
| 66 | - $this->alterColumns($tempTable, $primaryKey, $data); |
|
| 66 | + $this->alterColumns($tempTable, $primaryKey, $data); |
|
| 67 | 67 | $this->copyTableData($table, $tempTable); |
| 68 | 68 | |
| 69 | 69 | $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
@@ -71,8 +71,8 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - private function copyTableData($tableFrom, $tableTo) { |
|
| 75 | - try { |
|
| 74 | + private function copyTableData($tableFrom, $tableTo) { |
|
| 75 | + try { |
|
| 76 | 76 | if ($this->tableExists($tableFrom)) { |
| 77 | 77 | $columns = implode(', ', $this->getColumns($tableFrom)); |
| 78 | 78 | |
@@ -83,19 +83,19 @@ discard block |
||
| 83 | 83 | // No data to copy |
| 84 | 84 | echo $e->getMessage(); |
| 85 | 85 | } |
| 86 | - } |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - private function alterColumns($table, array $primaryKey, $data) { |
|
| 89 | - foreach ($data as $key => $value) { |
|
| 88 | + private function alterColumns($table, array $primaryKey, $data) { |
|
| 89 | + foreach ($data as $key => $value) { |
|
| 90 | 90 | if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
| 91 | 91 | |
| 92 | 92 | $type = $this->generalEditor->getType($value); |
| 93 | 93 | |
| 94 | 94 | $this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type); |
| 95 | 95 | } |
| 96 | - } |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - public function addIndex($table, array $fields) { |
|
| 98 | + public function addIndex($table, array $fields) { |
|
| 99 | 99 | if (empty($fields)) return false; |
| 100 | 100 | |
| 101 | 101 | //SQLite doesn't support ASC/DESC indexes, remove the keywords |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | private function tableExists($name) { |
| 43 | - $result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="'. $name.'"'); |
|
| 43 | + $result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="' . $name . '"'); |
|
| 44 | 44 | return count($result->fetchAll()) == 1; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -60,14 +60,14 @@ discard block |
||
| 60 | 60 | $this->stmtCache->clearCache(); |
| 61 | 61 | |
| 62 | 62 | // Create temp table to create a new structure |
| 63 | - $affix = '_'.substr(md5($table), 0, 6); |
|
| 63 | + $affix = '_' . substr(md5($table), 0, 6); |
|
| 64 | 64 | $tempTable = $table . $affix; |
| 65 | 65 | $this->generalEditor->createTable($tempTable, $primaryKey, $data); |
| 66 | 66 | $this->alterColumns($tempTable, $primaryKey, $data); |
| 67 | 67 | $this->copyTableData($table, $tempTable); |
| 68 | 68 | |
| 69 | - $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
|
| 70 | - $this->pdo->query('ALTER TABLE ' . $tempTable . ' RENAME TO '. $table ); |
|
| 69 | + $this->pdo->query('DROP TABLE IF EXISTS ' . $table); |
|
| 70 | + $this->pdo->query('ALTER TABLE ' . $tempTable . ' RENAME TO ' . $table); |
|
| 71 | 71 | |
| 72 | 72 | } |
| 73 | 73 | |
@@ -24,7 +24,9 @@ discard block |
||
| 24 | 24 | $args = $query->getArgs(); |
| 25 | 25 | |
| 26 | 26 | //Handle SQLite when PDO_ERRMODE is set to SILENT |
| 27 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
| 27 | + if ($stmt === false) { |
|
| 28 | + throw new \Exception('Invalid query'); |
|
| 29 | + } |
|
| 28 | 30 | |
| 29 | 31 | $stmt->execute($args); |
| 30 | 32 | if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
@@ -78,8 +80,7 @@ discard block |
||
| 78 | 80 | |
| 79 | 81 | $this->pdo->query('INSERT INTO ' . $this->quote($tableTo) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($tableFrom)); |
| 80 | 82 | } |
| 81 | - } |
|
| 82 | - catch (\PDOException $e) { |
|
| 83 | + } catch (\PDOException $e) { |
|
| 83 | 84 | // No data to copy |
| 84 | 85 | echo $e->getMessage(); |
| 85 | 86 | } |
@@ -87,7 +88,9 @@ discard block |
||
| 87 | 88 | |
| 88 | 89 | private function alterColumns($table, array $primaryKey, $data) { |
| 89 | 90 | foreach ($data as $key => $value) { |
| 90 | - if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
|
| 91 | + if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) { |
|
| 92 | + continue; |
|
| 93 | + } |
|
| 91 | 94 | |
| 92 | 95 | $type = $this->generalEditor->getType($value); |
| 93 | 96 | |
@@ -96,10 +99,14 @@ discard block |
||
| 96 | 99 | } |
| 97 | 100 | |
| 98 | 101 | public function addIndex($table, array $fields) { |
| 99 | - if (empty($fields)) return false; |
|
| 102 | + if (empty($fields)) { |
|
| 103 | + return false; |
|
| 104 | + } |
|
| 100 | 105 | |
| 101 | 106 | //SQLite doesn't support ASC/DESC indexes, remove the keywords |
| 102 | - foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field); |
|
| 107 | + foreach ($fields as &$field) { |
|
| 108 | + $field = str_ireplace([' desc', ' asc'], '', $field); |
|
| 109 | + } |
|
| 103 | 110 | sort($fields); |
| 104 | 111 | $fields = array_map('strtolower', $fields); |
| 105 | 112 | $fields = array_map('trim', $fields); |
@@ -108,8 +115,7 @@ discard block |
||
| 108 | 115 | |
| 109 | 116 | try { |
| 110 | 117 | $this->pdo->query('CREATE INDEX IF NOT EXISTS ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')'); |
| 111 | - } |
|
| 112 | - catch (\Exception $e) { |
|
| 118 | + } catch (\Exception $e) { |
|
| 113 | 119 | |
| 114 | 120 | } |
| 115 | 121 | } |