@@ -9,12 +9,13 @@ |
||
| 9 | 9 | if ($object instanceof \stdclass) { |
| 10 | 10 | $this->readClosure = function() use ($object) { return $object; }; |
| 11 | 11 | $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
| 12 | - } |
|
| 13 | - else { |
|
| 12 | + } else { |
|
| 14 | 13 | $this->readClosure = function() { |
| 15 | 14 | $data = new \stdClass; |
| 16 | 15 | foreach ($this as $k => $v) { |
| 17 | - if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) $data->$k = $v; |
|
| 16 | + if (is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime)) { |
|
| 17 | + $data->$k = $v; |
|
| 18 | + } |
|
| 18 | 19 | } |
| 19 | 20 | return $data; |
| 20 | 21 | }; |
@@ -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 |
@@ -10,7 +10,9 @@ discard block |
||
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | public function getAdapter() { |
| 13 | - if (!($this->db instanceof \PDO)) return $this->db; |
|
| 13 | + if (!($this->db instanceof \PDO)) { |
|
| 14 | + return $this->db; |
|
| 15 | + } |
|
| 14 | 16 | |
| 15 | 17 | $adapter = '\\Maphper\\DataSource\\' . ucfirst($this->db->getAttribute(\PDO::ATTR_DRIVER_NAME)) . 'Adapter'; |
| 16 | 18 | |
@@ -18,7 +20,9 @@ discard block |
||
| 18 | 20 | } |
| 19 | 21 | |
| 20 | 22 | public function getEditMode() { |
| 21 | - if (!isset($this->options['editmode'])) return false; |
|
| 23 | + if (!isset($this->options['editmode'])) { |
|
| 24 | + return false; |
|
| 25 | + } |
|
| 22 | 26 | |
| 23 | 27 | return $this->options['editmode'] === true ? Database::EDIT_STRUCTURE | Database::EDIT_INDEX | Database::EDIT_OPTIMISE : $this->options['editmode']; |
| 24 | 28 | } |
@@ -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 | } |
@@ -29,15 +32,23 @@ discard block |
||
| 29 | 32 | $args = $query->getArgs(); |
| 30 | 33 | $stmt->execute($args); |
| 31 | 34 | |
| 32 | - if (strpos(trim($query->getSql()), 'SELECT') === 0) return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
| 33 | - else return $stmt; |
|
| 35 | + if (strpos(trim($query->getSql()), 'SELECT') === 0) { |
|
| 36 | + return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
| 37 | + } else { |
|
| 38 | + return $stmt; |
|
| 39 | + } |
|
| 34 | 40 | } |
| 35 | 41 | |
| 36 | 42 | private function getType($val) { |
| 37 | - if ($val instanceof \DateTime) return 'DATETIME'; |
|
| 38 | - else if (is_int($val)) return 'INT(11)'; |
|
| 39 | - else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
| 40 | - else if (is_string($val)) return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB'; |
|
| 43 | + if ($val instanceof \DateTime) { |
|
| 44 | + return 'DATETIME'; |
|
| 45 | + } else if (is_int($val)) { |
|
| 46 | + return 'INT(11)'; |
|
| 47 | + } else if (is_double($val)) { |
|
| 48 | + return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
| 49 | + } else if (is_string($val)) { |
|
| 50 | + return strlen($val) < 192 ? 'VARCHAR(191)' : 'LONGBLOB'; |
|
| 51 | + } |
|
| 41 | 52 | return 'VARCHAR(191)'; |
| 42 | 53 | } |
| 43 | 54 | |
@@ -46,8 +57,11 @@ discard block |
||
| 46 | 57 | $parts = []; |
| 47 | 58 | foreach ($primaryKey as $key) { |
| 48 | 59 | $pk = $data->$key; |
| 49 | - if ($pk == null) $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT'; |
|
| 50 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
| 60 | + if ($pk == null) { |
|
| 61 | + $parts[] = $key . ' INT(11) NOT NULL AUTO_INCREMENT'; |
|
| 62 | + } else { |
|
| 63 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
| 64 | + } |
|
| 51 | 65 | } |
| 52 | 66 | |
| 53 | 67 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -58,15 +72,20 @@ discard block |
||
| 58 | 72 | $this->createTable($table, $primaryKey, $data); |
| 59 | 73 | |
| 60 | 74 | foreach ($data as $key => $value) { |
| 61 | - if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue; |
|
| 62 | - if (in_array($key, $primaryKey)) continue; |
|
| 75 | + if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) { |
|
| 76 | + continue; |
|
| 77 | + } |
|
| 78 | + if (in_array($key, $primaryKey)) { |
|
| 79 | + continue; |
|
| 80 | + } |
|
| 63 | 81 | |
| 64 | 82 | $type = $this->getType($value); |
| 65 | 83 | |
| 66 | 84 | try { |
| 67 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
| 68 | - } |
|
| 69 | - catch (\Exception $e) { |
|
| 85 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) { |
|
| 86 | + throw new \Exception('Could not alter table'); |
|
| 87 | + } |
|
| 88 | + } catch (\Exception $e) { |
|
| 70 | 89 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
| 71 | 90 | } |
| 72 | 91 | } |
@@ -84,7 +103,9 @@ discard block |
||
| 84 | 103 | $keyName = $this->quote(implode('_', $fields)); |
| 85 | 104 | |
| 86 | 105 | $results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"'); |
| 87 | - if ($results && count($results->fetchAll()) == 0) $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
| 106 | + if ($results && count($results->fetchAll()) == 0) { |
|
| 107 | + $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
| 108 | + } |
|
| 88 | 109 | } |
| 89 | 110 | |
| 90 | 111 | public function optimiseColumns($table) { |
@@ -15,10 +15,13 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | private function getCachedStmt($sql) { |
| 17 | 17 | $queryId = md5($sql); |
| 18 | - if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
|
| 19 | - else { |
|
| 18 | + if (isset($this->queryCache[$queryId])) { |
|
| 19 | + $stmt = $this->queryCache[$queryId]; |
|
| 20 | + } else { |
|
| 20 | 21 | $stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]); |
| 21 | - if ($stmt) $this->queryCache[$queryId] = $stmt; |
|
| 22 | + if ($stmt) { |
|
| 23 | + $this->queryCache[$queryId] = $stmt; |
|
| 24 | + } |
|
| 22 | 25 | } |
| 23 | 26 | return $stmt; |
| 24 | 27 | } |
@@ -29,7 +32,9 @@ discard block |
||
| 29 | 32 | $args = $query->getArgs(); |
| 30 | 33 | |
| 31 | 34 | //Handle SQLite when PDO_ERRMODE is set to SILENT |
| 32 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
| 35 | + if ($stmt === false) { |
|
| 36 | + throw new \Exception('Invalid query'); |
|
| 37 | + } |
|
| 33 | 38 | |
| 34 | 39 | $stmt->execute($args); |
| 35 | 40 | if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
@@ -37,8 +42,11 @@ discard block |
||
| 37 | 42 | return $this->query($query); |
| 38 | 43 | } |
| 39 | 44 | |
| 40 | - if (substr($query->getSql(), 0, 6) === 'SELECT') return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
| 41 | - else return $stmt; |
|
| 45 | + if (substr($query->getSql(), 0, 6) === 'SELECT') { |
|
| 46 | + return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
| 47 | + } else { |
|
| 48 | + return $stmt; |
|
| 49 | + } |
|
| 42 | 50 | } |
| 43 | 51 | |
| 44 | 52 | public function lastInsertId() { |
@@ -46,12 +54,19 @@ discard block |
||
| 46 | 54 | } |
| 47 | 55 | |
| 48 | 56 | private function getType($val) { |
| 49 | - if ($val instanceof \DateTime) return 'DATETIME'; |
|
| 50 | - else if (is_int($val)) return 'INTEGER'; |
|
| 51 | - else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
| 52 | - else if (is_string($val) && strlen($val) < 256) return 'VARCHAR(255)'; |
|
| 53 | - else if (is_string($val) && strlen($val) > 256) return 'LONGBLOG'; |
|
| 54 | - else return 'VARCHAR(255)'; |
|
| 57 | + if ($val instanceof \DateTime) { |
|
| 58 | + return 'DATETIME'; |
|
| 59 | + } else if (is_int($val)) { |
|
| 60 | + return 'INTEGER'; |
|
| 61 | + } else if (is_double($val)) { |
|
| 62 | + return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; |
|
| 63 | + } else if (is_string($val) && strlen($val) < 256) { |
|
| 64 | + return 'VARCHAR(255)'; |
|
| 65 | + } else if (is_string($val) && strlen($val) > 256) { |
|
| 66 | + return 'LONGBLOG'; |
|
| 67 | + } else { |
|
| 68 | + return 'VARCHAR(255)'; |
|
| 69 | + } |
|
| 55 | 70 | } |
| 56 | 71 | |
| 57 | 72 | private function tableExists($name) { |
@@ -85,8 +100,7 @@ discard block |
||
| 85 | 100 | $this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table)); |
| 86 | 101 | $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
| 87 | 102 | } |
| 88 | - } |
|
| 89 | - catch (\PDOException $e) { |
|
| 103 | + } catch (\PDOException $e) { |
|
| 90 | 104 | // No data to copy |
| 91 | 105 | echo $e->getMessage(); |
| 92 | 106 | } |
@@ -100,8 +114,11 @@ discard block |
||
| 100 | 114 | $parts = []; |
| 101 | 115 | foreach ($primaryKey as $key) { |
| 102 | 116 | $pk = $data->$key; |
| 103 | - if ($pk == null) $parts[] = $key . ' INTEGER'; |
|
| 104 | - else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
| 117 | + if ($pk == null) { |
|
| 118 | + $parts[] = $key . ' INTEGER'; |
|
| 119 | + } else { |
|
| 120 | + $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; |
|
| 121 | + } |
|
| 105 | 122 | } |
| 106 | 123 | |
| 107 | 124 | $pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; |
@@ -110,8 +127,12 @@ discard block |
||
| 110 | 127 | $this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')'); |
| 111 | 128 | |
| 112 | 129 | foreach ($data as $key => $value) { |
| 113 | - if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue; |
|
| 114 | - if (in_array($key, $primaryKey)) continue; |
|
| 130 | + if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) { |
|
| 131 | + continue; |
|
| 132 | + } |
|
| 133 | + if (in_array($key, $primaryKey)) { |
|
| 134 | + continue; |
|
| 135 | + } |
|
| 115 | 136 | |
| 116 | 137 | $type = $this->getType($value); |
| 117 | 138 | |
@@ -121,10 +142,14 @@ discard block |
||
| 121 | 142 | |
| 122 | 143 | |
| 123 | 144 | public function addIndex($table, array $fields) { |
| 124 | - if (empty($fields)) return false; |
|
| 145 | + if (empty($fields)) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 125 | 148 | |
| 126 | 149 | //SQLite doesn't support ASC/DESC indexes, remove the keywords |
| 127 | - foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field); |
|
| 150 | + foreach ($fields as &$field) { |
|
| 151 | + $field = str_ireplace([' desc', ' asc'], '', $field); |
|
| 152 | + } |
|
| 128 | 153 | sort($fields); |
| 129 | 154 | $fields = array_map('strtolower', $fields); |
| 130 | 155 | $fields = array_map('trim', $fields); |
@@ -133,8 +158,7 @@ discard block |
||
| 133 | 158 | |
| 134 | 159 | try { |
| 135 | 160 | $this->pdo->query('CREATE INDEX IF NOT EXISTS ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')'); |
| 136 | - } |
|
| 137 | - catch (\Exception $e) { |
|
| 161 | + } catch (\Exception $e) { |
|
| 138 | 162 | |
| 139 | 163 | } |
| 140 | 164 | } |
@@ -27,8 +27,12 @@ discard block |
||
| 27 | 27 | list($columns, $order) = explode(' ', $options['order']); |
| 28 | 28 | usort($filteredArray, $this->getOrderFunction($order, $columns)); |
| 29 | 29 | } |
| 30 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 31 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 30 | + if (isset($options['offset'])) { |
|
| 31 | + $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 32 | + } |
|
| 33 | + if (isset($options['limit'])) { |
|
| 34 | + $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 35 | + } |
|
| 32 | 36 | return $filteredArray; |
| 33 | 37 | } |
| 34 | 38 | |
@@ -37,8 +41,11 @@ discard block |
||
| 37 | 41 | foreach ($fields as $key => $val) { |
| 38 | 42 | $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
| 39 | 43 | |
| 40 | - if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
| 41 | - else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) return false; |
|
| 44 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) { |
|
| 45 | + return true; |
|
| 46 | + } else if (!(Maphper::FIND_OR & $mode) && $currentFieldResult === false) { |
|
| 47 | + return false; |
|
| 48 | + } |
|
| 42 | 49 | } |
| 43 | 50 | return !(Maphper::FIND_OR & $mode); |
| 44 | 51 | }; |
@@ -47,12 +54,13 @@ discard block |
||
| 47 | 54 | private function getIfFieldMatches($key, $val, $data, $mode) { |
| 48 | 55 | if (is_numeric($key) && is_array($val)) { |
| 49 | 56 | return $this->getSearchFieldFunction($val, $key)($data); |
| 57 | + } else if (!isset($data->$key)) { |
|
| 58 | + return false; |
|
| 59 | + } else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) { |
|
| 60 | + return in_array($data->$key, $val); |
|
| 61 | + } else { |
|
| 62 | + return $this->processFilter($mode, $val, $data->$key); |
|
| 50 | 63 | } |
| 51 | - else if (!isset($data->$key)) return false; |
|
| 52 | - else if (!(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val)) |
|
| 53 | - return in_array($data->$key, $val); |
|
| 54 | - else |
|
| 55 | - return $this->processFilter($mode, $val, $data->$key); |
|
| 56 | 64 | } |
| 57 | 65 | |
| 58 | 66 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
@@ -64,14 +72,15 @@ discard block |
||
| 64 | 72 | } |
| 65 | 73 | |
| 66 | 74 | public function deleteByField(array $fields, array $options) { |
| 67 | - foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
| 75 | + foreach ($this->findByField($fields, $options) as $val) { |
|
| 76 | + unset($this->data[$val->{$this->id[0]}]); |
|
| 77 | + } |
|
| 68 | 78 | } |
| 69 | 79 | |
| 70 | 80 | public function save($data) { |
| 71 | 81 | if (isset($data->{$this->id[0]})) { |
| 72 | 82 | $id = $data->{$this->id[0]}; |
| 73 | - } |
|
| 74 | - else { |
|
| 83 | + } else { |
|
| 75 | 84 | $id = count($this->data); |
| 76 | 85 | $data->{$this->id[0]} = $id; |
| 77 | 86 | } |
@@ -91,23 +100,35 @@ discard block |
||
| 91 | 100 | if ($aColumn === $bColumn) { |
| 92 | 101 | $sortVal = 0; |
| 93 | 102 | continue; |
| 103 | + } else { |
|
| 104 | + $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 94 | 105 | } |
| 95 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 96 | 106 | break; |
| 97 | 107 | } |
| 98 | - if ($order === 'desc') return -$sortVal; |
|
| 99 | - else return $sortVal; |
|
| 108 | + if ($order === 'desc') { |
|
| 109 | + return -$sortVal; |
|
| 110 | + } else { |
|
| 111 | + return $sortVal; |
|
| 112 | + } |
|
| 100 | 113 | }; |
| 101 | 114 | } |
| 102 | 115 | |
| 103 | 116 | private function processFilter($mode, $expected, $actual) { |
| 104 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
| 105 | - else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) return $expected <= $actual; |
|
| 106 | - else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) return $expected >= $actual; |
|
| 107 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
| 108 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
| 109 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
| 110 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
| 117 | + if (Maphper::FIND_NOT & $mode) { |
|
| 118 | + return $expected != $actual; |
|
| 119 | + } else if (Maphper::FIND_GREATER & $mode && Maphper::FIND_EXACT & $mode) { |
|
| 120 | + return $expected <= $actual; |
|
| 121 | + } else if (Maphper::FIND_LESS & $mode && Maphper::FIND_EXACT & $mode) { |
|
| 122 | + return $expected >= $actual; |
|
| 123 | + } else if (Maphper::FIND_GREATER & $mode) { |
|
| 124 | + return $expected < $actual; |
|
| 125 | + } else if (Maphper::FIND_LESS & $mode) { |
|
| 126 | + return $expected > $actual; |
|
| 127 | + } else if (Maphper::FIND_BETWEEN & $mode) { |
|
| 128 | + return $expected[0] <= $actual && $actual <= $expected[1]; |
|
| 129 | + } else if (Maphper::FIND_NOCASE & $mode) { |
|
| 130 | + return strtolower($expected) == strtolower($actual); |
|
| 131 | + } |
|
| 111 | 132 | return $expected == $actual; |
| 112 | 133 | } |
| 113 | 134 | } |
@@ -37,7 +37,9 @@ discard block |
||
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | private function optimizeColumns() { |
| 40 | - if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 40 | + if (self::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
| 41 | + $this->adapter->optimiseColumns($this->table); |
|
| 42 | + } |
|
| 41 | 43 | } |
| 42 | 44 | |
| 43 | 45 | public function getPrimaryKey() { |
@@ -53,19 +55,23 @@ discard block |
||
| 53 | 55 | if (!isset($this->cache[$id])) { |
| 54 | 56 | try { |
| 55 | 57 | $result = $this->adapter->query($this->selectBuilder->select($this->table, $this->getPrimaryKey()[0] . ' = :id', [':id' => $id], ['limit' => 1])); |
| 56 | - } |
|
| 57 | - catch (\Exception $e) { |
|
| 58 | + } catch (\Exception $e) { |
|
| 58 | 59 | } |
| 59 | 60 | |
| 60 | - if (isset($result[0])) $this->cache[$id] = $result[0]; |
|
| 61 | - else return null; |
|
| 61 | + if (isset($result[0])) { |
|
| 62 | + $this->cache[$id] = $result[0]; |
|
| 63 | + } else { |
|
| 64 | + return null; |
|
| 65 | + } |
|
| 62 | 66 | } |
| 63 | 67 | return $this->cache[$id]; |
| 64 | 68 | } |
| 65 | 69 | |
| 66 | 70 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 67 | 71 | //Cannot count/sum/max multiple fields, pick the first one. This should only come into play when trying to count() a mapper with multiple primary keys |
| 68 | - if (is_array($field)) $field = $field[0]; |
|
| 72 | + if (is_array($field)) { |
|
| 73 | + $field = $field[0]; |
|
| 74 | + } |
|
| 69 | 75 | $query = $this->selectBuilder->createSql($criteria, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
| 70 | 76 | |
| 71 | 77 | try { |
@@ -74,8 +80,7 @@ discard block |
||
| 74 | 80 | $result = $this->adapter->query($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
| 75 | 81 | |
| 76 | 82 | return $this->determineAggregateResult($result, $group, $field); |
| 77 | - } |
|
| 78 | - catch (\Exception $e) { |
|
| 83 | + } catch (\Exception $e) { |
|
| 79 | 84 | return $group ? [] : 0; |
| 80 | 85 | } |
| 81 | 86 | } |
@@ -83,15 +88,21 @@ discard block |
||
| 83 | 88 | private function determineAggregateResult($result, $group, $field) { |
| 84 | 89 | if ($group != null) { |
| 85 | 90 | $ret = []; |
| 86 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 91 | + foreach ($result as $res) { |
|
| 92 | + $ret[$res->$field] = $res->val; |
|
| 93 | + } |
|
| 87 | 94 | return $ret; |
| 95 | + } else if (isset($result[0])) { |
|
| 96 | + return $result[0]->val; |
|
| 97 | + } else { |
|
| 98 | + return 0; |
|
| 88 | 99 | } |
| 89 | - else if (isset($result[0])) return $result[0]->val; |
|
| 90 | - else return 0; |
|
| 91 | 100 | } |
| 92 | 101 | |
| 93 | 102 | private function addIndex($args) { |
| 94 | - if (self::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
| 103 | + if (self::EDIT_INDEX & $this->alterDb) { |
|
| 104 | + $this->adapter->addIndex($this->table, $args); |
|
| 105 | + } |
|
| 95 | 106 | } |
| 96 | 107 | |
| 97 | 108 | public function findByField(array $fields, $options = []) { |
@@ -99,14 +110,15 @@ discard block |
||
| 99 | 110 | if (!isset($this->resultCache[$cacheId])) { |
| 100 | 111 | $query = $this->selectBuilder->createSql($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND); |
| 101 | 112 | |
| 102 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
| 113 | + if (!isset($options['order'])) { |
|
| 114 | + $options['order'] = $this->defaultSort; |
|
| 115 | + } |
|
| 103 | 116 | |
| 104 | 117 | try { |
| 105 | 118 | $this->resultCache[$cacheId] = $this->adapter->query($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
| 106 | 119 | $this->addIndex(array_keys($query['args'])); |
| 107 | 120 | $this->addIndex(explode(',', $options['order'])); |
| 108 | - } |
|
| 109 | - catch (\Exception $e) { |
|
| 121 | + } catch (\Exception $e) { |
|
| 110 | 122 | $this->errors[] = $e; |
| 111 | 123 | $this->resultCache[$cacheId] = []; |
| 112 | 124 | } |
@@ -142,10 +154,13 @@ discard block |
||
| 142 | 154 | $result = $this->insert($this->table, $this->primaryKey, $data); |
| 143 | 155 | |
| 144 | 156 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 145 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
| 146 | - } |
|
| 147 | - catch (\Exception $e) { |
|
| 148 | - if (!$this->getTryAgain($tryagain)) throw $e; |
|
| 157 | + if ($result->errorCode() !== '00000') { |
|
| 158 | + throw new \Exception('Could not insert into ' . $this->table); |
|
| 159 | + } |
|
| 160 | + } catch (\Exception $e) { |
|
| 161 | + if (!$this->getTryAgain($tryagain)) { |
|
| 162 | + throw $e; |
|
| 163 | + } |
|
| 149 | 164 | |
| 150 | 165 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
| 151 | 166 | $this->save($data, false); |
@@ -162,28 +177,34 @@ discard block |
||
| 162 | 177 | } |
| 163 | 178 | |
| 164 | 179 | private function updatePK($data, $new) { |
| 165 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 180 | + if ($new && count($this->primaryKey) == 1) { |
|
| 181 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 182 | + } |
|
| 166 | 183 | } |
| 167 | 184 | |
| 168 | 185 | private function checkIfUpdateWorked($data) { |
| 169 | 186 | $updateWhere = $this->crudBuilder->update($this->table, $this->primaryKey, $data); |
| 170 | 187 | $matched = $this->findByField($updateWhere->getArgs()); |
| 171 | 188 | |
| 172 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 189 | + if (count($matched) == 0) { |
|
| 190 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 191 | + } |
|
| 173 | 192 | } |
| 174 | 193 | |
| 175 | 194 | private function updateCache($data) { |
| 176 | 195 | $pkValue = $data->{$this->primaryKey[0]}; |
| 177 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 178 | - else $this->cache[$pkValue] = $data; |
|
| 196 | + if (isset($this->cache[$pkValue])) { |
|
| 197 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 198 | + } else { |
|
| 199 | + $this->cache[$pkValue] = $data; |
|
| 200 | + } |
|
| 179 | 201 | } |
| 180 | 202 | |
| 181 | 203 | private function insert($table, array $primaryKey, $data) { |
| 182 | 204 | $error = 0; |
| 183 | 205 | try { |
| 184 | 206 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
| 185 | - } |
|
| 186 | - catch (\Exception $e) { |
|
| 207 | + } catch (\Exception $e) { |
|
| 187 | 208 | $error = 1; |
| 188 | 209 | } |
| 189 | 210 | |
@@ -196,7 +217,9 @@ discard block |
||
| 196 | 217 | |
| 197 | 218 | private function tryUpdate($table, array $primaryKey, $data) { |
| 198 | 219 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
| 199 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 220 | + if ($result->rowCount() === 0) { |
|
| 221 | + $this->checkIfUpdateWorked($data); |
|
| 222 | + } |
|
| 200 | 223 | |
| 201 | 224 | return $result; |
| 202 | 225 | } |
@@ -8,17 +8,23 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | if (isset($options['offset'])) { |
| 10 | 10 | $offset = ' OFFSET ' . $options['offset']; |
| 11 | - if (!$limit) $limit = ' LIMIT 1000'; |
|
| 11 | + if (!$limit) { |
|
| 12 | + $limit = ' LIMIT 1000'; |
|
| 13 | + } |
|
| 14 | + } else { |
|
| 15 | + $offset = ''; |
|
| 12 | 16 | } |
| 13 | - else $offset = ''; |
|
| 14 | 17 | |
| 15 | 18 | $order = isset($options['order']) ? ' ORDER BY ' . $options['order'] : ''; |
| 16 | 19 | return new Query('SELECT * FROM ' . $table . ' ' . $where . $order . $limit . $offset, $args); |
| 17 | 20 | } |
| 18 | 21 | |
| 19 | 22 | public function aggregate($table, $function, $field, $where, $args, $group) { |
| 20 | - if ($group == true) $groupBy = ' GROUP BY ' . $field; |
|
| 21 | - else $groupBy = ''; |
|
| 23 | + if ($group == true) { |
|
| 24 | + $groupBy = ' GROUP BY ' . $field; |
|
| 25 | + } else { |
|
| 26 | + $groupBy = ''; |
|
| 27 | + } |
|
| 22 | 28 | return new Query('SELECT ' . $function . '(' . $field . ') as val, ' . $field . ' FROM ' . $table . ($where != null ? ' WHERE ' . $where : '') . ' ' . $groupBy, $args); |
| 23 | 29 | } |
| 24 | 30 | |
@@ -30,70 +36,91 @@ discard block |
||
| 30 | 36 | |
| 31 | 37 | foreach ($fields as $key => $value) { |
| 32 | 38 | if ($value instanceof \DateTime) { |
| 33 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 34 | - else $value = $value->format('Y-m-d H:i:s'); |
|
| 39 | + if ($value->format('H:i:s') == '00:00:00') { |
|
| 40 | + $value = $value->format('Y-m-d'); |
|
| 41 | + } else { |
|
| 42 | + $value = $value->format('Y-m-d H:i:s'); |
|
| 43 | + } |
|
| 35 | 44 | } |
| 36 | 45 | |
| 37 | 46 | if (is_numeric($key) && is_array($value)) { |
| 38 | 47 | $result = $this->createSql($value, $key); |
| 39 | - foreach ($result['args'] as $arg_key => $arg) $args[$arg_key] = $arg; |
|
| 40 | - foreach ($result['sql'] as $arg) $sql[] = $arg; |
|
| 41 | - } |
|
| 42 | - else if (\Maphper\Maphper::FIND_BETWEEN & $mode) { |
|
| 48 | + foreach ($result['args'] as $arg_key => $arg) { |
|
| 49 | + $args[$arg_key] = $arg; |
|
| 50 | + } |
|
| 51 | + foreach ($result['sql'] as $arg) { |
|
| 52 | + $sql[] = $arg; |
|
| 53 | + } |
|
| 54 | + } else if (\Maphper\Maphper::FIND_BETWEEN & $mode) { |
|
| 43 | 55 | $sql[] = $key . '>= :' . $key . 'from'; |
| 44 | 56 | $sql[] = $key . ' <= :' . $key . 'to'; |
| 45 | 57 | |
| 46 | 58 | $args[$key . 'from'] = $value[0]; |
| 47 | 59 | $args[$key . 'to'] = $value[1]; |
| 48 | - } |
|
| 49 | - else if (!is_numeric($key) && is_array($value)) { |
|
| 60 | + } else if (!is_numeric($key) && is_array($value)) { |
|
| 50 | 61 | $inSql = []; |
| 51 | 62 | $count = count($value); |
| 52 | 63 | for ($i = 0; $i < $count; $i++) { |
| 53 | 64 | $args[$key . $i] = $value[$i]; |
| 54 | 65 | $inSql[] = ':' . $key . $i; |
| 55 | 66 | } |
| 56 | - if (count($inSql) == 0) return []; |
|
| 57 | - else $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
| 58 | - } |
|
| 59 | - else if ($value === NULL) { |
|
| 67 | + if (count($inSql) == 0) { |
|
| 68 | + return []; |
|
| 69 | + } else { |
|
| 70 | + $sql[] = $key . ' IN ( ' . implode(', ', $inSql) . ')'; |
|
| 71 | + } |
|
| 72 | + } else if ($value === NULL) { |
|
| 60 | 73 | $nullSql = $key . ' IS '; |
| 61 | - if (\Maphper\Maphper::FIND_NOT & $mode) $nullSql .= 'NOT '; |
|
| 74 | + if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
| 75 | + $nullSql .= 'NOT '; |
|
| 76 | + } |
|
| 62 | 77 | $sql[] = $nullSql . 'NULL'; |
| 63 | - } |
|
| 64 | - else { |
|
| 78 | + } else { |
|
| 65 | 79 | |
| 66 | 80 | if (\Maphper\Maphper::FIND_LIKE & $mode) { |
| 67 | 81 | $operator = 'LIKE'; |
| 68 | 82 | $value = '%' . $value . '%'; |
| 69 | - } |
|
| 70 | - else if (\Maphper\Maphper::FIND_STARTS & $mode) { |
|
| 83 | + } else if (\Maphper\Maphper::FIND_STARTS & $mode) { |
|
| 71 | 84 | $operator = 'LIKE'; |
| 72 | 85 | $value = $value . '%'; |
| 86 | + } else { |
|
| 87 | + $operator = $this->getOperator($mode); |
|
| 73 | 88 | } |
| 74 | - else $operator = $this->getOperator($mode); |
|
| 75 | 89 | |
| 76 | 90 | $args[$key] = $value; |
| 77 | 91 | $sql[] = $key . ' ' . $operator . ' :' . $key; |
| 78 | 92 | } |
| 79 | 93 | } |
| 80 | 94 | |
| 81 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
| 82 | - else $query = implode(' AND ', $sql); |
|
| 83 | - if (!empty($query)) $query = '(' . $query . ')'; |
|
| 95 | + if (\Maphper\Maphper::FIND_OR & $mode) { |
|
| 96 | + $query = implode(' OR ', $sql); |
|
| 97 | + } else { |
|
| 98 | + $query = implode(' AND ', $sql); |
|
| 99 | + } |
|
| 100 | + if (!empty($query)) { |
|
| 101 | + $query = '(' . $query . ')'; |
|
| 102 | + } |
|
| 84 | 103 | return ['args' => $args, 'sql' => $query]; |
| 85 | 104 | } |
| 86 | 105 | |
| 87 | 106 | private function getOperator($mode) { |
| 88 | 107 | $operator = ""; |
| 89 | 108 | |
| 90 | - if (\Maphper\Maphper::FIND_NOCASE & $mode) $operator = 'LIKE'; |
|
| 91 | - else if (\Maphper\Maphper::FIND_BIT & $mode) $operator = '&'; |
|
| 92 | - else if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
| 93 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
| 94 | - else if (\Maphper\Maphper::FIND_NOT & $mode) $operator = '!='; |
|
| 95 | - |
|
| 96 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
| 109 | + if (\Maphper\Maphper::FIND_NOCASE & $mode) { |
|
| 110 | + $operator = 'LIKE'; |
|
| 111 | + } else if (\Maphper\Maphper::FIND_BIT & $mode) { |
|
| 112 | + $operator = '&'; |
|
| 113 | + } else if (\Maphper\Maphper::FIND_GREATER & $mode) { |
|
| 114 | + $operator = '>'; |
|
| 115 | + } else if (\Maphper\Maphper::FIND_LESS & $mode) { |
|
| 116 | + $operator = '<'; |
|
| 117 | + } else if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
| 118 | + $operator = '!='; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + if (\Maphper\Maphper::FIND_EXACT & $mode) { |
|
| 122 | + $operator .= '='; |
|
| 123 | + } |
|
| 97 | 124 | |
| 98 | 125 | return $operator; |
| 99 | 126 | } |