@@ -20,8 +20,7 @@ |
||
| 20 | 20 | if (count($this->pk) == 1) { |
| 21 | 21 | $pk = end($this->pk); |
| 22 | 22 | return $this->array[$this->iterator]->$pk; |
| 23 | - } |
|
| 24 | - else { |
|
| 23 | + } else { |
|
| 25 | 24 | $current = $this->current(); |
| 26 | 25 | return array_map(function($pkName) use ($current) { |
| 27 | 26 | return $current->$pkName; |
@@ -19,11 +19,11 @@ discard block |
||
| 19 | 19 | //For dates with times set, search on time, if the time is not set, search on date only. |
| 20 | 20 | //E.g. searching for all records posted on '2015-11-14' should return all records that day, not just the ones posted at 00:00:00 on that day |
| 21 | 21 | if ($value instanceof \DateTime) { |
| 22 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 22 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 23 | 23 | else $value = $value->format('Y-m-d H:i:s'); |
| 24 | 24 | } |
| 25 | 25 | if (is_object($value)) continue; |
| 26 | - if ($prependField){ |
|
| 26 | + if ($prependField) { |
|
| 27 | 27 | $sql[] = $this->quote($field) . ' = :' . $field; |
| 28 | 28 | } else { |
| 29 | 29 | $sql[] = ':' . $field; |
@@ -35,13 +35,13 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | public function insert($table, $data) { |
| 37 | 37 | $query = $this->buildSaveQuery($data); |
| 38 | - return new Query('INSERT INTO ' . $this->quote($table) . ' (' .implode(', ', array_keys($query['args'])).') VALUES ( ' . implode(', ', $query['sql']). ' )', $query['args']); |
|
| 38 | + return new Query('INSERT INTO ' . $this->quote($table) . ' (' . implode(', ', array_keys($query['args'])) . ') VALUES ( ' . implode(', ', $query['sql']) . ' )', $query['args']); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | public function update($table, array $primaryKey, $data) { |
| 42 | 42 | $query = $this->buildSaveQuery($data, true); |
| 43 | 43 | $where = []; |
| 44 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
| 45 | - return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
|
| 44 | + foreach ($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
| 45 | + return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']) . ' WHERE ' . implode(' AND ', $where), $query['args']); |
|
| 46 | 46 | } |
| 47 | 47 | } |
@@ -19,10 +19,15 @@ discard block |
||
| 19 | 19 | //For dates with times set, search on time, if the time is not set, search on date only. |
| 20 | 20 | //E.g. searching for all records posted on '2015-11-14' should return all records that day, not just the ones posted at 00:00:00 on that day |
| 21 | 21 | if ($value instanceof \DateTime) { |
| 22 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 23 | - else $value = $value->format('Y-m-d H:i:s'); |
|
| 22 | + if ($value->format('H:i:s') == '00:00:00') { |
|
| 23 | + $value = $value->format('Y-m-d'); |
|
| 24 | + } else { |
|
| 25 | + $value = $value->format('Y-m-d H:i:s'); |
|
| 26 | + } |
|
| 27 | + } |
|
| 28 | + if (is_object($value)) { |
|
| 29 | + continue; |
|
| 24 | 30 | } |
| 25 | - if (is_object($value)) continue; |
|
| 26 | 31 | if ($prependField){ |
| 27 | 32 | $sql[] = $this->quote($field) . ' = :' . $field; |
| 28 | 33 | } else { |
@@ -41,7 +46,9 @@ discard block |
||
| 41 | 46 | public function update($table, array $primaryKey, $data) { |
| 42 | 47 | $query = $this->buildSaveQuery($data, true); |
| 43 | 48 | $where = []; |
| 44 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
| 49 | + foreach($primaryKey as $field) { |
|
| 50 | + $where[] = $this->quote($field) . ' = :' . $field; |
|
| 51 | + } |
|
| 45 | 52 | return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
| 46 | 53 | } |
| 47 | 54 | } |
@@ -8,7 +8,7 @@ |
||
| 8 | 8 | public function delete($table, $criteria, $args, $limit = null, $offset = null, $order = null) { |
| 9 | 9 | $limit = $limit ? ' LIMIT ' . $limit : ''; |
| 10 | 10 | $offset = $offset ? ' OFFSET ' . $offset : ''; |
| 11 | - $order = $order ? ' ORDER BY ' . $order : ''; |
|
| 11 | + $order = $order ? ' ORDER BY ' . $order : ''; |
|
| 12 | 12 | return new Query('DELETE FROM ' . $table . ' WHERE ' . ($criteria ?: '1 = 1 ') . $order . $limit . $offset, $args); |
| 13 | 13 | } |
| 14 | 14 | |
@@ -24,8 +24,8 @@ discard block |
||
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | public function offsetGet($key) { |
| 27 | - $depth = $this->getDepth()+1; |
|
| 28 | - if (count($this->primaryKey)-1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
| 27 | + $depth = $this->getDepth() + 1; |
|
| 28 | + if (count($this->primaryKey) - 1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
| 29 | 29 | else return new MultiPk($this->mapper, $key, $this->primaryKey, $this); |
| 30 | 30 | } |
| 31 | 31 | |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | public function offsetUnset($key) { |
| 46 | 46 | $keys = $this->primaryKey; |
| 47 | - $this->mapper->filter([ array_pop($keys) => $key])->delete(); |
|
| 47 | + $this->mapper->filter([array_pop($keys) => $key])->delete(); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | public function offsetExists($key) { |
@@ -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) { |
@@ -4,7 +4,7 @@ |
||
| 4 | 4 | private $parent; |
| 5 | 5 | private $primaryKey; |
| 6 | 6 | private $lookup; |
| 7 | - private $mapper; |
|
| 7 | + private $mapper; |
|
| 8 | 8 | |
| 9 | 9 | public function __construct(Maphper $mapper, $lookup, array $primaryKey, MultiPk $parent = null) { |
| 10 | 10 | $this->parent = $parent; |
@@ -6,7 +6,9 @@ discard block |
||
| 6 | 6 | private $localField; |
| 7 | 7 | |
| 8 | 8 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $critiera = []) { |
| 9 | - if ($critiera) $mapper->filter($critiera); |
|
| 9 | + if ($critiera) { |
|
| 10 | + $mapper->filter($critiera); |
|
| 11 | + } |
|
| 10 | 12 | $this->mapper = $mapper; |
| 11 | 13 | $this->parentField = $parentField; |
| 12 | 14 | $this->localField = $localField; |
@@ -14,17 +16,24 @@ discard block |
||
| 14 | 16 | |
| 15 | 17 | |
| 16 | 18 | public function getData($parentObject) { |
| 17 | - if (!isset($parentObject->{$this->parentField})) $mapper = $this->mapper; |
|
| 18 | - else $mapper = $this->mapper->filter([$this->localField => $parentObject->{$this->parentField}]); |
|
| 19 | + if (!isset($parentObject->{$this->parentField})) { |
|
| 20 | + $mapper = $this->mapper; |
|
| 21 | + } else { |
|
| 22 | + $mapper = $this->mapper->filter([$this->localField => $parentObject->{$this->parentField}]); |
|
| 23 | + } |
|
| 19 | 24 | |
| 20 | 25 | return $mapper; |
| 21 | 26 | } |
| 22 | 27 | |
| 23 | 28 | |
| 24 | 29 | public function overwrite($key, &$mapper) { |
| 25 | - if (!isset($key->{$this->parentField})) return false; |
|
| 30 | + if (!isset($key->{$this->parentField})) { |
|
| 31 | + return false; |
|
| 32 | + } |
|
| 26 | 33 | foreach ($mapper as $k => $val) { |
| 27 | - if (!empty($val->{$this->localField}) && $val->{$this->localField} == $key->{$this->parentField}) continue; |
|
| 34 | + if (!empty($val->{$this->localField}) && $val->{$this->localField} == $key->{$this->parentField}) { |
|
| 35 | + continue; |
|
| 36 | + } |
|
| 28 | 37 | $val->{$this->localField} = $key->{$this->parentField}; |
| 29 | 38 | $this->mapper[] = $val; |
| 30 | 39 | } |
@@ -7,17 +7,17 @@ |
||
| 7 | 7 | private $intermediateName; |
| 8 | 8 | |
| 9 | 9 | public function __construct(\Maphper\Iterator $iterator, $intermediateName = null) { |
| 10 | - $this->iterator = $iterator; |
|
| 11 | - $this->intermediateName = $intermediateName; |
|
| 10 | + $this->iterator = $iterator; |
|
| 11 | + $this->intermediateName = $intermediateName; |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | public function current() { |
| 15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
| 15 | + if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
| 16 | 16 | return $this->iterator->current(); |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public function key() { |
| 20 | - return $this->iterator->key(); |
|
| 20 | + return $this->iterator->key(); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | public function next() { |
@@ -12,7 +12,9 @@ |
||
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | public function current() { |
| 15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
| 15 | + if ($this->intermediateName) { |
|
| 16 | + return $this->iterator->current()->{$this->intermediateName}; |
|
| 17 | + } |
|
| 16 | 18 | return $this->iterator->current(); |
| 17 | 19 | } |
| 18 | 20 | |
@@ -1,19 +1,19 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace Maphper; |
| 3 | 3 | class Maphper implements \Countable, \ArrayAccess, \IteratorAggregate { |
| 4 | - const FIND_EXACT = 0x1; |
|
| 5 | - const FIND_LIKE = 0x2; |
|
| 6 | - const FIND_STARTS = 0x4; |
|
| 7 | - const FIND_ENDS = 0x8; |
|
| 8 | - const FIND_BIT = 0x10; |
|
| 9 | - const FIND_GREATER = 0x20; |
|
| 10 | - const FIND_LESS = 0x40; |
|
| 11 | - const FIND_EXPRESSION = 0x80; |
|
| 12 | - const FIND_AND = 0x100; |
|
| 13 | - const FIND_OR = 0x200; |
|
| 14 | - const FIND_NOT = 0x400; |
|
| 15 | - const FIND_BETWEEN = 0x800; |
|
| 16 | - const FIND_NOCASE = 0x1000; |
|
| 4 | + const FIND_EXACT = 0x1; |
|
| 5 | + const FIND_LIKE = 0x2; |
|
| 6 | + const FIND_STARTS = 0x4; |
|
| 7 | + const FIND_ENDS = 0x8; |
|
| 8 | + const FIND_BIT = 0x10; |
|
| 9 | + const FIND_GREATER = 0x20; |
|
| 10 | + const FIND_LESS = 0x40; |
|
| 11 | + const FIND_EXPRESSION = 0x80; |
|
| 12 | + const FIND_AND = 0x100; |
|
| 13 | + const FIND_OR = 0x200; |
|
| 14 | + const FIND_NOT = 0x400; |
|
| 15 | + const FIND_BETWEEN = 0x800; |
|
| 16 | + const FIND_NOCASE = 0x1000; |
|
| 17 | 17 | |
| 18 | 18 | private $dataSource; |
| 19 | 19 | private $relations = []; |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | $results = $this->dataSource->findByField($this->settings['filter'], |
| 60 | - ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset'] ]); |
|
| 60 | + ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset']]); |
|
| 61 | 61 | |
| 62 | 62 | $siblings = new \ArrayObject(); |
| 63 | 63 | |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 109 | 109 | return isset($data[0]); |
| 110 | 110 | } |
| 111 | - return (bool) $this->dataSource->findById($offset); |
|
| 111 | + return (bool)$this->dataSource->findById($offset); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | public function offsetUnset($id) { |
@@ -61,7 +61,9 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | $siblings = new \ArrayObject(); |
| 63 | 63 | |
| 64 | - foreach ($results as &$result) $result = $this->entity->create($result, $this->relations, $siblings); |
|
| 64 | + foreach ($results as &$result) { |
|
| 65 | + $result = $this->entity->create($result, $this->relations, $siblings); |
|
| 66 | + } |
|
| 65 | 67 | |
| 66 | 68 | return $results; |
| 67 | 69 | } |
@@ -78,13 +80,17 @@ discard block |
||
| 78 | 80 | private function processFilters($value) { |
| 79 | 81 | //When saving to a mapper with filters, write the filters back into the object being stored |
| 80 | 82 | foreach ($this->settings['filter'] as $key => $filterValue) { |
| 81 | - if (empty($value->$key) && !is_array($filterValue)) $value->$key = $filterValue; |
|
| 83 | + if (empty($value->$key) && !is_array($filterValue)) { |
|
| 84 | + $value->$key = $filterValue; |
|
| 85 | + } |
|
| 82 | 86 | } |
| 83 | 87 | return $value; |
| 84 | 88 | } |
| 85 | 89 | |
| 86 | 90 | public function offsetSet($offset, $valueObj) { |
| 87 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
| 91 | + if ($valueObj instanceof \Maphper\Relation) { |
|
| 92 | + throw new \Exception(); |
|
| 93 | + } |
|
| 88 | 94 | |
| 89 | 95 | //Extract private properties from the object |
| 90 | 96 | $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
@@ -92,7 +98,9 @@ discard block |
||
| 92 | 98 | |
| 93 | 99 | $value = $this->processFilters($value); |
| 94 | 100 | $pk = $this->dataSource->getPrimaryKey(); |
| 95 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
| 101 | + if ($offset !== null) { |
|
| 102 | + $value->{$pk[0]} = $offset; |
|
| 103 | + } |
|
| 96 | 104 | $valueCopy = clone $value; |
| 97 | 105 | $value = $this->entity->wrap($this->relations, $value); |
| 98 | 106 | $this->dataSource->save($value); |
@@ -104,7 +112,9 @@ discard block |
||
| 104 | 112 | } |
| 105 | 113 | |
| 106 | 114 | public function offsetExists($offset) { |
| 107 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 115 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 116 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 117 | + } |
|
| 108 | 118 | if (!empty($this->settings['filter'])) { |
| 109 | 119 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 110 | 120 | return isset($data[0]); |
@@ -117,7 +127,9 @@ discard block |
||
| 117 | 127 | } |
| 118 | 128 | |
| 119 | 129 | public function offsetGet($offset) { |
| 120 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 130 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 131 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 132 | + } |
|
| 121 | 133 | if (!empty($this->settings['filter'])) { |
| 122 | 134 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 123 | 135 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
@@ -128,11 +140,15 @@ discard block |
||
| 128 | 140 | public function __call($method, $args) { |
| 129 | 141 | if (array_key_exists($method, $this->settings)) { |
| 130 | 142 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
| 131 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 132 | - else $maphper->settings[$method] = $args[0]; |
|
| 143 | + if (is_array($maphper->settings[$method])) { |
|
| 144 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 145 | + } else { |
|
| 146 | + $maphper->settings[$method] = $args[0]; |
|
| 147 | + } |
|
| 133 | 148 | return $maphper; |
| 149 | + } else { |
|
| 150 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 134 | 151 | } |
| 135 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 136 | 152 | } |
| 137 | 153 | |
| 138 | 154 | public function findAggregate($function, $field, $group = null) { |
@@ -96,15 +96,15 @@ discard block |
||
| 96 | 96 | $value = $this->entity->wrap($this->relations, $value); |
| 97 | 97 | $this->dataSource->save($value); |
| 98 | 98 | $visibilityOverride->write($value); |
| 99 | - $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
| 99 | + $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | public function offsetExists($offset) { |
| 103 | 103 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 104 | - if (!empty($this->settings['filter'])) { |
|
| 105 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 106 | - return isset($data[0]); |
|
| 107 | - } |
|
| 104 | + if (!empty($this->settings['filter'])) { |
|
| 105 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 106 | + return isset($data[0]); |
|
| 107 | + } |
|
| 108 | 108 | return (bool) $this->dataSource->findById($offset); |
| 109 | 109 | } |
| 110 | 110 | |
@@ -114,10 +114,10 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | public function offsetGet($offset) { |
| 116 | 116 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 117 | - if (!empty($this->settings['filter'])) { |
|
| 118 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 119 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 120 | - } |
|
| 117 | + if (!empty($this->settings['filter'])) { |
|
| 118 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 119 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 120 | + } |
|
| 121 | 121 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
| 122 | 122 | } |
| 123 | 123 | |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | return; |
| 97 | 97 | |
| 98 | 98 | $runAgain = false; |
| 99 | - $columns = $this->pdo->query('SELECT * FROM '. $this->quote($table) . ' PROCEDURE ANALYSE(1,1)')->fetchAll(\PDO::FETCH_OBJ); |
|
| 99 | + $columns = $this->pdo->query('SELECT * FROM ' . $this->quote($table) . ' PROCEDURE ANALYSE(1,1)')->fetchAll(\PDO::FETCH_OBJ); |
|
| 100 | 100 | foreach ($columns as $column) { |
| 101 | 101 | $parts = explode('.', $column->Field_name); |
| 102 | 102 | $name = $this->quote(end($parts)); |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | } |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' MODIFY '. $name . ' ' . $type); |
|
| 133 | + $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' MODIFY ' . $name . ' ' . $type); |
|
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | //Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed |
@@ -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 | } |
@@ -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 | } |
@@ -1,14 +1,14 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | class StmtCache { |
| 4 | - private $pdo; |
|
| 5 | - private $queryCache = []; |
|
| 4 | + private $pdo; |
|
| 5 | + private $queryCache = []; |
|
| 6 | 6 | |
| 7 | - public function __construct(\PDO $pdo) { |
|
| 8 | - $this->pdo = $pdo; |
|
| 9 | - } |
|
| 7 | + public function __construct(\PDO $pdo) { |
|
| 8 | + $this->pdo = $pdo; |
|
| 9 | + } |
|
| 10 | 10 | |
| 11 | - public function getCachedStmt($sql) { |
|
| 11 | + public function getCachedStmt($sql) { |
|
| 12 | 12 | $queryId = $this->getQueryId($sql); |
| 13 | 13 | if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
| 14 | 14 | else { |
@@ -18,15 +18,15 @@ discard block |
||
| 18 | 18 | return $stmt; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - private function getQueryId($sql) { |
|
| 22 | - return md5($sql); |
|
| 23 | - } |
|
| 21 | + private function getQueryId($sql) { |
|
| 22 | + return md5($sql); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - public function deleteQueryFromCache($sql) { |
|
| 26 | - unset($this->queryCache[$this->getQueryId($sql)]); |
|
| 27 | - } |
|
| 25 | + public function deleteQueryFromCache($sql) { |
|
| 26 | + unset($this->queryCache[$this->getQueryId($sql)]); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - public function clearCache() { |
|
| 30 | - $this->queryCache = []; |
|
| 31 | - } |
|
| 29 | + public function clearCache() { |
|
| 30 | + $this->queryCache = []; |
|
| 31 | + } |
|
| 32 | 32 | } |