@@ -25,8 +25,11 @@ |
||
| 25 | 25 | |
| 26 | 26 | public function offsetGet($key) { |
| 27 | 27 | $depth = $this->getDepth()+1; |
| 28 | - if (count($this->primaryKey)-1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
| 29 | - else return new MultiPk($this->mapper, $key, $this->primaryKey, $this); |
|
| 28 | + if (count($this->primaryKey)-1 == $depth) { |
|
| 29 | + return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
| 30 | + } else { |
|
| 31 | + return new MultiPk($this->mapper, $key, $this->primaryKey, $this); |
|
| 32 | + } |
|
| 30 | 33 | } |
| 31 | 34 | |
| 32 | 35 | public function offsetSet($key, $value) { |
@@ -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 | } |
@@ -53,10 +53,13 @@ discard block |
||
| 53 | 53 | $result = $this->insert($this->table, $this->primaryKey, $data); |
| 54 | 54 | |
| 55 | 55 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 56 | - if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
|
| 57 | - } |
|
| 58 | - catch (\Exception $e) { |
|
| 59 | - if (!$this->databaseModify->getTryInsertAgain($tryagain)) throw $e; |
|
| 56 | + if ($result->errorCode() !== '00000') { |
|
| 57 | + throw new \Exception('Could not insert into ' . $this->table); |
|
| 58 | + } |
|
| 59 | + } catch (\Exception $e) { |
|
| 60 | + if (!$this->databaseModify->getTryInsertAgain($tryagain)) { |
|
| 61 | + throw $e; |
|
| 62 | + } |
|
| 60 | 63 | |
| 61 | 64 | $this->adapter->alterDatabase($this->table, $this->primaryKey, $data); |
| 62 | 65 | $this->save($data, false); |
@@ -69,21 +72,24 @@ discard block |
||
| 69 | 72 | } |
| 70 | 73 | |
| 71 | 74 | private function updatePK($data, $new) { |
| 72 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 75 | + if ($new && count($this->primaryKey) == 1) { |
|
| 76 | + $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 77 | + } |
|
| 73 | 78 | } |
| 74 | 79 | |
| 75 | 80 | private function checkIfUpdateWorked($data) { |
| 76 | 81 | $updateWhere = $this->whereBuilder->createSql($data); |
| 77 | 82 | $matched = $this->databaseSelect->findByField($updateWhere['args']); |
| 78 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 83 | + if (count($matched) == 0) { |
|
| 84 | + throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 85 | + } |
|
| 79 | 86 | } |
| 80 | 87 | |
| 81 | 88 | private function insert($table, array $primaryKey, $data) { |
| 82 | 89 | $error = 0; |
| 83 | 90 | try { |
| 84 | 91 | $result = $this->adapter->query($this->crudBuilder->insert($table, $data)); |
| 85 | - } |
|
| 86 | - catch (\Exception $e) { |
|
| 92 | + } catch (\Exception $e) { |
|
| 87 | 93 | $error = 1; |
| 88 | 94 | } |
| 89 | 95 | |
@@ -96,7 +102,9 @@ discard block |
||
| 96 | 102 | |
| 97 | 103 | private function tryUpdate($table, array $primaryKey, $data) { |
| 98 | 104 | $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
| 99 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 105 | + if ($result->rowCount() === 0) { |
|
| 106 | + $this->checkIfUpdateWorked($data); |
|
| 107 | + } |
|
| 100 | 108 | |
| 101 | 109 | return $result; |
| 102 | 110 | } |
@@ -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) . ')'; |
@@ -10,10 +10,13 @@ |
||
| 10 | 10 | |
| 11 | 11 | public function getCachedStmt($sql) { |
| 12 | 12 | $queryId = $this->getQueryId($sql); |
| 13 | - if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
|
| 14 | - else { |
|
| 13 | + if (isset($this->queryCache[$queryId])) { |
|
| 14 | + $stmt = $this->queryCache[$queryId]; |
|
| 15 | + } else { |
|
| 15 | 16 | $stmt = $this->pdo->prepare($sql, [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]); |
| 16 | - if ($stmt) $this->queryCache[$queryId] = $stmt; |
|
| 17 | + if ($stmt) { |
|
| 18 | + $this->queryCache[$queryId] = $stmt; |
|
| 19 | + } |
|
| 17 | 20 | } |
| 18 | 21 | return $stmt; |
| 19 | 22 | } |
@@ -13,11 +13,15 @@ |
||
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | public function addIndex($args) { |
| 16 | - if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
|
| 16 | + if (Database::EDIT_INDEX & $this->alterDb) { |
|
| 17 | + $this->adapter->addIndex($this->table, $args); |
|
| 18 | + } |
|
| 17 | 19 | } |
| 18 | 20 | |
| 19 | 21 | public function optimizeColumns() { |
| 20 | - if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 22 | + if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) { |
|
| 23 | + $this->adapter->optimiseColumns($this->table); |
|
| 24 | + } |
|
| 21 | 25 | } |
| 22 | 26 | |
| 23 | 27 | public function getTryInsertAgain($tryagain) { |
@@ -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 | } |
@@ -24,13 +24,15 @@ discard block |
||
| 24 | 24 | if (!isset($this->idCache[$id])) { |
| 25 | 25 | try { |
| 26 | 26 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); |
| 27 | - } |
|
| 28 | - catch (\Exception $e) { |
|
| 27 | + } catch (\Exception $e) { |
|
| 29 | 28 | // Don't issue an error if it cannot be found since we return null |
| 30 | 29 | } |
| 31 | 30 | |
| 32 | - if (isset($result[0])) $this->idCache[$id] = $result[0]; |
|
| 33 | - else return null; |
|
| 31 | + if (isset($result[0])) { |
|
| 32 | + $this->idCache[$id] = $result[0]; |
|
| 33 | + } else { |
|
| 34 | + return null; |
|
| 35 | + } |
|
| 34 | 36 | } |
| 35 | 37 | return $this->idCache[$id]; |
| 36 | 38 | } |
@@ -40,14 +42,15 @@ discard block |
||
| 40 | 42 | if (!isset($this->resultCache[$cacheId])) { |
| 41 | 43 | $query = $this->whereBuilder->createSql($fields); |
| 42 | 44 | |
| 43 | - if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
|
| 45 | + if (!isset($options['order'])) { |
|
| 46 | + $options['order'] = $this->defaultSort; |
|
| 47 | + } |
|
| 44 | 48 | |
| 45 | 49 | try { |
| 46 | 50 | $this->resultCache[$cacheId] = $this->selectQuery($this->selectBuilder->select($this->table, $query['sql'], $query['args'], $options)); |
| 47 | 51 | $this->databaseModify->addIndex(array_keys($query['args'])); |
| 48 | 52 | $this->databaseModify->addIndex(explode(',', $options['order'])); |
| 49 | - } |
|
| 50 | - catch (\Exception $e) { |
|
| 53 | + } catch (\Exception $e) { |
|
| 51 | 54 | $this->errors[] = $e; |
| 52 | 55 | $this->resultCache[$cacheId] = []; |
| 53 | 56 | } |
@@ -57,7 +60,9 @@ discard block |
||
| 57 | 60 | |
| 58 | 61 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
| 59 | 62 | //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 |
| 60 | - if (is_array($field)) $field = $field[0]; |
|
| 63 | + if (is_array($field)) { |
|
| 64 | + $field = $field[0]; |
|
| 65 | + } |
|
| 61 | 66 | $query = $this->whereBuilder->createSql($criteria); |
| 62 | 67 | |
| 63 | 68 | try { |
@@ -66,8 +71,7 @@ discard block |
||
| 66 | 71 | $result = $this->selectQuery($this->selectBuilder->aggregate($this->table, $function, $field, $query['sql'], $query['args'], $group)); |
| 67 | 72 | |
| 68 | 73 | return $this->determineAggregateResult($result, $group, $field); |
| 69 | - } |
|
| 70 | - catch (\Exception $e) { |
|
| 74 | + } catch (\Exception $e) { |
|
| 71 | 75 | return $group ? [] : 0; |
| 72 | 76 | } |
| 73 | 77 | } |
@@ -75,11 +79,15 @@ discard block |
||
| 75 | 79 | private function determineAggregateResult($result, $group, $field) { |
| 76 | 80 | if ($group != null) { |
| 77 | 81 | $ret = []; |
| 78 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
| 82 | + foreach ($result as $res) { |
|
| 83 | + $ret[$res->$field] = $res->val; |
|
| 84 | + } |
|
| 79 | 85 | return $ret; |
| 86 | + } else if (isset($result[0])) { |
|
| 87 | + return $result[0]->val; |
|
| 88 | + } else { |
|
| 89 | + return 0; |
|
| 80 | 90 | } |
| 81 | - else if (isset($result[0])) return $result[0]->val; |
|
| 82 | - else return 0; |
|
| 83 | 91 | } |
| 84 | 92 | |
| 85 | 93 | private function selectQuery(\Maphper\Lib\Query $query) { |
@@ -95,8 +103,11 @@ discard block |
||
| 95 | 103 | } |
| 96 | 104 | |
| 97 | 105 | public function updateCache($data, $pkValue) { |
| 98 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 99 | - else $this->cache[$pkValue] = $data; |
|
| 106 | + if (isset($this->cache[$pkValue])) { |
|
| 107 | + $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
| 108 | + } else { |
|
| 109 | + $this->cache[$pkValue] = $data; |
|
| 110 | + } |
|
| 100 | 111 | } |
| 101 | 112 | |
| 102 | 113 | public function deleteIDFromCache($id) { |
@@ -25,8 +25,12 @@ discard block |
||
| 25 | 25 | list($columns, $order) = explode(' ', $options['order']); |
| 26 | 26 | usort($filteredArray, $this->getOrderFunction($order, $columns)); |
| 27 | 27 | } |
| 28 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 29 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 28 | + if (isset($options['offset'])) { |
|
| 29 | + $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 30 | + } |
|
| 31 | + if (isset($options['limit'])) { |
|
| 32 | + $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 33 | + } |
|
| 30 | 34 | return $filteredArray; |
| 31 | 35 | } |
| 32 | 36 | |
@@ -39,14 +43,15 @@ discard block |
||
| 39 | 43 | } |
| 40 | 44 | |
| 41 | 45 | public function deleteByField(array $fields, array $options) { |
| 42 | - foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
| 46 | + foreach ($this->findByField($fields, $options) as $val) { |
|
| 47 | + unset($this->data[$val->{$this->id[0]}]); |
|
| 48 | + } |
|
| 43 | 49 | } |
| 44 | 50 | |
| 45 | 51 | public function save($data) { |
| 46 | 52 | if (isset($data->{$this->id[0]})) { |
| 47 | 53 | $id = $data->{$this->id[0]}; |
| 48 | - } |
|
| 49 | - else { |
|
| 54 | + } else { |
|
| 50 | 55 | $id = count($this->data); |
| 51 | 56 | $data->{$this->id[0]} = $id; |
| 52 | 57 | } |
@@ -66,12 +71,16 @@ discard block |
||
| 66 | 71 | if ($aColumn === $bColumn) { |
| 67 | 72 | $sortVal = 0; |
| 68 | 73 | continue; |
| 74 | + } else { |
|
| 75 | + $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 69 | 76 | } |
| 70 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 71 | 77 | break; |
| 72 | 78 | } |
| 73 | - if ($order === 'desc') return -$sortVal; |
|
| 74 | - else return $sortVal; |
|
| 79 | + if ($order === 'desc') { |
|
| 80 | + return -$sortVal; |
|
| 81 | + } else { |
|
| 82 | + return $sortVal; |
|
| 83 | + } |
|
| 75 | 84 | }; |
| 76 | 85 | } |
| 77 | 86 | } |