@@ -27,14 +27,17 @@ discard block |
||
27 | 27 | |
28 | 28 | private function alterColumns($table, array $primaryKey, $data) { |
29 | 29 | foreach ($data as $key => $value) { |
30 | - if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
|
30 | + if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) { |
|
31 | + continue; |
|
32 | + } |
|
31 | 33 | |
32 | 34 | $type = $this->generalEditor->getType($value); |
33 | 35 | |
34 | 36 | try { |
35 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
36 | - } |
|
37 | - catch (\Exception $e) { |
|
37 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) { |
|
38 | + throw new \Exception('Could not alter table'); |
|
39 | + } |
|
40 | + } catch (\Exception $e) { |
|
38 | 41 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
39 | 42 | } |
40 | 43 | } |
@@ -57,7 +60,9 @@ discard block |
||
57 | 60 | $keyName = $this->quote(implode('_', $fields)); |
58 | 61 | |
59 | 62 | $results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"'); |
60 | - if ($results && count($results->fetchAll()) == 0) $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
63 | + if ($results && count($results->fetchAll()) == 0) { |
|
64 | + $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
65 | + } |
|
61 | 66 | } |
62 | 67 | |
63 | 68 | public function optimiseColumns($table) { |
@@ -25,23 +25,36 @@ discard block |
||
25 | 25 | } |
26 | 26 | |
27 | 27 | public function getType($val) { |
28 | - if ($val instanceof \DateTime) return $this->dataTypes['datetime']; |
|
29 | - else if ($result = $this->doNumberTypes($val)) return $result; |
|
30 | - else if ($result = $this->doStringTypes($val)) return $result; |
|
31 | - else return $this->dataTypes['other']; |
|
28 | + if ($val instanceof \DateTime) { |
|
29 | + return $this->dataTypes['datetime']; |
|
30 | + } else if ($result = $this->doNumberTypes($val)) { |
|
31 | + return $result; |
|
32 | + } else if ($result = $this->doStringTypes($val)) { |
|
33 | + return $result; |
|
34 | + } else { |
|
35 | + return $this->dataTypes['other']; |
|
36 | + } |
|
32 | 37 | } |
33 | 38 | |
34 | 39 | private function doNumberTypes($val) { |
35 | - if (is_int($val)) return $this->dataTypes['int']; |
|
36 | - else if (is_double($val)) return $this->dataTypes['decimal'] . '(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
37 | - else return false; |
|
40 | + if (is_int($val)) { |
|
41 | + return $this->dataTypes['int']; |
|
42 | + } else if (is_double($val)) { |
|
43 | + return $this->dataTypes['decimal'] . '(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
44 | + } else { |
|
45 | + return false; |
|
46 | + } |
|
38 | 47 | } |
39 | 48 | |
40 | 49 | private function doStringTypes($val) { |
41 | - if (!is_string($val)) return false; |
|
42 | - if (strlen($val) <= $this->dataTypes['short_string_max_len']) |
|
43 | - return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; |
|
44 | - else return $this->dataTypes['long_string']; |
|
50 | + if (!is_string($val)) { |
|
51 | + return false; |
|
52 | + } |
|
53 | + if (strlen($val) <= $this->dataTypes['short_string_max_len']) { |
|
54 | + return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; |
|
55 | + } else { |
|
56 | + return $this->dataTypes['long_string']; |
|
57 | + } |
|
45 | 58 | } |
46 | 59 | |
47 | 60 | public function isNotSavableType($value, $key, $primaryKey) { |
@@ -54,8 +67,11 @@ discard block |
||
54 | 67 | $parts = []; |
55 | 68 | foreach ($primaryKey as $key) { |
56 | 69 | $pk = $data->$key; |
57 | - if ($pk == null) $parts[] = $key . ' ' . $this->dataTypes['pk_default']; |
|
58 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
70 | + if ($pk == null) { |
|
71 | + $parts[] = $key . ' ' . $this->dataTypes['pk_default']; |
|
72 | + } else { |
|
73 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
74 | + } |
|
59 | 75 | } |
60 | 76 | |
61 | 77 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -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 | } |