@@ -11,16 +11,21 @@ discard block |
||
| 11 | 11 | foreach ($columns as $column) { |
| 12 | 12 | $parts = explode('.', $column->Field_name); |
| 13 | 13 | $name = $this->quote(end($parts)); |
| 14 | - if ($column->Min_value === null && $column->Max_value === null) $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
| 15 | - else { |
|
| 14 | + if ($column->Min_value === null && $column->Max_value === null) { |
|
| 15 | + $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
| 16 | + } else { |
|
| 16 | 17 | $type = $column->Optimal_fieldtype; |
| 17 | 18 | if ($column->Max_length < 11) { |
| 18 | 19 | //Check for dates |
| 19 | 20 | $count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count; |
| 20 | - if ($count == 0) $type = 'DATETIME'; |
|
| 21 | + if ($count == 0) { |
|
| 22 | + $type = 'DATETIME'; |
|
| 23 | + } |
|
| 21 | 24 | |
| 22 | 25 | $count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count; |
| 23 | - if ($count == 0) $type = 'DATE'; |
|
| 26 | + if ($count == 0) { |
|
| 27 | + $type = 'DATE'; |
|
| 28 | + } |
|
| 24 | 29 | } |
| 25 | 30 | |
| 26 | 31 | //If it's text, work out if it would be better to be something else |
@@ -30,8 +35,7 @@ discard block |
||
| 30 | 35 | if ($count == 0) { |
| 31 | 36 | $type = 'INT(11)'; |
| 32 | 37 | $runAgain = true; |
| 33 | - } |
|
| 34 | - else { |
|
| 38 | + } else { |
|
| 35 | 39 | //See if it's decimal |
| 36 | 40 | $count = $this->pdo->query('SELECT count(*) FROM ' . $table . ' WHERE concat(\'\', ' . $name . ' * 1) != ' . $name . ')')->fetch(\PDO::FETCH_OBJ)->count; |
| 37 | 41 | if ($count == 0) { |
@@ -45,6 +49,8 @@ discard block |
||
| 45 | 49 | } |
| 46 | 50 | } |
| 47 | 51 | //Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed |
| 48 | - if ($runAgain) $this->optimiseColumns($table); |
|
| 52 | + if ($runAgain) { |
|
| 53 | + $this->optimiseColumns($table); |
|
| 54 | + } |
|
| 49 | 55 | } |
| 50 | 56 | } |
| 51 | 57 | \ No newline at end of file |
@@ -16,10 +16,13 @@ discard block |
||
| 16 | 16 | |
| 17 | 17 | private function getCachedStmt($sql) { |
| 18 | 18 | $queryId = md5($sql); |
| 19 | - if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
|
| 20 | - else { |
|
| 19 | + if (isset($this->queryCache[$queryId])) { |
|
| 20 | + $stmt = $this->queryCache[$queryId]; |
|
| 21 | + } else { |
|
| 21 | 22 | $stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]); |
| 22 | - if ($stmt) $this->queryCache[$queryId] = $stmt; |
|
| 23 | + if ($stmt) { |
|
| 24 | + $this->queryCache[$queryId] = $stmt; |
|
| 25 | + } |
|
| 23 | 26 | } |
| 24 | 27 | return $stmt; |
| 25 | 28 | } |
@@ -28,20 +31,30 @@ discard block |
||
| 28 | 31 | $stmt = $this->getCachedStmt($query->getSql()); |
| 29 | 32 | $args = $query->getArgs(); |
| 30 | 33 | foreach ($args as $name => &$arg) { |
| 31 | - if ($arg instanceof \DateTime) $arg = $arg->format('Y-m-d H:i:s'); |
|
| 34 | + if ($arg instanceof \DateTime) { |
|
| 35 | + $arg = $arg->format('Y-m-d H:i:s'); |
|
| 36 | + } |
|
| 32 | 37 | } |
| 33 | 38 | |
| 34 | 39 | $res = $stmt->execute($args); |
| 35 | 40 | |
| 36 | - if (strpos(trim($query->getSql()), 'SELECT') === 0) return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
| 37 | - else return $stmt; |
|
| 41 | + if (strpos(trim($query->getSql()), 'SELECT') === 0) { |
|
| 42 | + return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
| 43 | + } else { |
|
| 44 | + return $stmt; |
|
| 45 | + } |
|
| 38 | 46 | } |
| 39 | 47 | |
| 40 | 48 | private function getType($val) { |
| 41 | - if ($val instanceof \DateTime) return 'DATETIME'; |
|
| 42 | - else if (is_int($val)) return 'INT(11)'; |
|
| 43 | - else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
| 44 | - else if (is_string($val)) return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB'; |
|
| 49 | + if ($val instanceof \DateTime) { |
|
| 50 | + return 'DATETIME'; |
|
| 51 | + } else if (is_int($val)) { |
|
| 52 | + return 'INT(11)'; |
|
| 53 | + } else if (is_double($val)) { |
|
| 54 | + return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
| 55 | + } else if (is_string($val)) { |
|
| 56 | + return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB'; |
|
| 57 | + } |
|
| 45 | 58 | return 'VARCHAR(191)'; |
| 46 | 59 | } |
| 47 | 60 | |
@@ -50,8 +63,11 @@ discard block |
||
| 50 | 63 | $parts = []; |
| 51 | 64 | foreach ($primaryKey as $key) { |
| 52 | 65 | $pk = $data->$key; |
| 53 | - if ($pk == null) $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT'; |
|
| 54 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
| 66 | + if ($pk == null) { |
|
| 67 | + $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT'; |
|
| 68 | + } else { |
|
| 69 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
| 70 | + } |
|
| 55 | 71 | } |
| 56 | 72 | |
| 57 | 73 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -62,15 +78,20 @@ discard block |
||
| 62 | 78 | $this->createTable($table, $primaryKey, $data); |
| 63 | 79 | |
| 64 | 80 | foreach ($data as $key => $value) { |
| 65 | - if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue; |
|
| 66 | - if (in_array($key, $primaryKey)) continue; |
|
| 81 | + if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) { |
|
| 82 | + continue; |
|
| 83 | + } |
|
| 84 | + if (in_array($key, $primaryKey)) { |
|
| 85 | + continue; |
|
| 86 | + } |
|
| 67 | 87 | |
| 68 | 88 | $type = $this->getType($value); |
| 69 | 89 | |
| 70 | 90 | try { |
| 71 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
| 72 | - } |
|
| 73 | - catch (\Exception $e) { |
|
| 91 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) { |
|
| 92 | + throw new \Exception('Could not alter table'); |
|
| 93 | + } |
|
| 94 | + } catch (\Exception $e) { |
|
| 74 | 95 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
| 75 | 96 | } |
| 76 | 97 | } |
@@ -88,7 +109,9 @@ discard block |
||
| 88 | 109 | $keyName = $this->quote(implode('_', $fields)); |
| 89 | 110 | |
| 90 | 111 | $results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"'); |
| 91 | - if ($results && count($results->fetchAll()) == 0) $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
| 112 | + if ($results && count($results->fetchAll()) == 0) { |
|
| 113 | + $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
| 114 | + } |
|
| 92 | 115 | } |
| 93 | 116 | |
| 94 | 117 | public function optimiseColumns($table) { |