@@ -2,55 +2,55 @@ |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | |
4 | 4 | class GeneralEditDatabase { |
5 | - private $pdo; |
|
6 | - private $dataTypes = [ |
|
7 | - 'datetime' => 'DATETIME', |
|
8 | - 'int' => 'INT(11)', |
|
9 | - 'decimal' => 'DECIMAL', |
|
10 | - 'short_string' => 'VARCHAR', |
|
11 | - 'long_string' => 'LONGBLOG', |
|
12 | - 'short_string_max_len' => 255, |
|
13 | - 'other' => 'VARCHAR(255)', |
|
5 | + private $pdo; |
|
6 | + private $dataTypes = [ |
|
7 | + 'datetime' => 'DATETIME', |
|
8 | + 'int' => 'INT(11)', |
|
9 | + 'decimal' => 'DECIMAL', |
|
10 | + 'short_string' => 'VARCHAR', |
|
11 | + 'long_string' => 'LONGBLOG', |
|
12 | + 'short_string_max_len' => 255, |
|
13 | + 'other' => 'VARCHAR(255)', |
|
14 | 14 | |
15 | - 'pk_default' => 'INT(11) NOT NULL AUTO_INCREMENT' |
|
16 | - ]; |
|
15 | + 'pk_default' => 'INT(11) NOT NULL AUTO_INCREMENT' |
|
16 | + ]; |
|
17 | 17 | |
18 | - public function __construct(\PDO $pdo, array $dataTypes) { |
|
19 | - $this->pdo = $pdo; |
|
20 | - $this->dataTypes = array_merge($this->dataTypes, $dataTypes); |
|
21 | - } |
|
18 | + public function __construct(\PDO $pdo, array $dataTypes) { |
|
19 | + $this->pdo = $pdo; |
|
20 | + $this->dataTypes = array_merge($this->dataTypes, $dataTypes); |
|
21 | + } |
|
22 | 22 | |
23 | - public function quote($str) { |
|
23 | + public function quote($str) { |
|
24 | 24 | return '`' . str_replace('.', '`.`', trim($str, '`')) . '`'; |
25 | 25 | } |
26 | 26 | |
27 | - public function getType($val) { |
|
27 | + public function getType($val) { |
|
28 | 28 | if ($val instanceof \DateTimeInterface) return $this->dataTypes['datetime']; |
29 | 29 | else if ($result = $this->doNumberTypes($val)) return $result; |
30 | 30 | else if ($result = $this->doStringTypes($val)) return $result; |
31 | 31 | else return $this->dataTypes['other']; |
32 | 32 | } |
33 | 33 | |
34 | - private function doNumberTypes($val) { |
|
35 | - if (is_int($val)) return $this->dataTypes['int']; |
|
34 | + private function doNumberTypes($val) { |
|
35 | + if (is_int($val)) return $this->dataTypes['int']; |
|
36 | 36 | else if (is_double($val)) return $this->dataTypes['decimal'] . '(9,' . (strlen($val) - strrpos($val, '.') - 1) . ')'; |
37 | - else return false; |
|
38 | - } |
|
37 | + else return false; |
|
38 | + } |
|
39 | 39 | |
40 | - 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'] . ')'; |
|
40 | + 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 | 44 | else return $this->dataTypes['long_string']; |
45 | - } |
|
45 | + } |
|
46 | 46 | |
47 | - public function isNotSavableType($value, $key, $primaryKey) { |
|
48 | - return is_array($value) || (is_object($value) && !($value instanceof \DateTimeInterface)) || |
|
49 | - in_array($key, $primaryKey); |
|
50 | - } |
|
47 | + public function isNotSavableType($value, $key, $primaryKey) { |
|
48 | + return is_array($value) || (is_object($value) && !($value instanceof \DateTimeInterface)) || |
|
49 | + in_array($key, $primaryKey); |
|
50 | + } |
|
51 | 51 | |
52 | - //Alter the database so that it can store $data |
|
53 | - public function createTable($table, array $primaryKey, $data) { |
|
52 | + //Alter the database so that it can store $data |
|
53 | + public function createTable($table, array $primaryKey, $data) { |
|
54 | 54 | $parts = []; |
55 | 55 | foreach ($primaryKey as $key) { |
56 | 56 | $pk = $data->$key; |
@@ -18,15 +18,15 @@ discard block |
||
18 | 18 | return $obj; |
19 | 19 | } |
20 | 20 | |
21 | - private function tryToGetDateObjFromString($obj) { |
|
22 | - try { |
|
23 | - $date = new \DateTimeImmutable($obj); |
|
21 | + private function tryToGetDateObjFromString($obj) { |
|
22 | + try { |
|
23 | + $date = new \DateTimeImmutable($obj); |
|
24 | 24 | if ($this->dateMatchesFormats($date, $obj)) $obj = $date; |
25 | - } |
|
26 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
27 | - } |
|
28 | - return $obj; |
|
29 | - } |
|
25 | + } |
|
26 | + catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
27 | + } |
|
28 | + return $obj; |
|
29 | + } |
|
30 | 30 | |
31 | 31 | private function dateMatchesFormats($date, $str) { |
32 | 32 | foreach ($this->dateFormats as list($format, $len)) { |
@@ -35,17 +35,17 @@ discard block |
||
35 | 35 | return false; |
36 | 36 | } |
37 | 37 | |
38 | - private function isIterable($obj) { |
|
39 | - return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
40 | - } |
|
38 | + private function isIterable($obj) { |
|
39 | + return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
40 | + } |
|
41 | 41 | |
42 | - private function isPossiblyDateString($obj) { |
|
43 | - return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
44 | - } |
|
42 | + private function isPossiblyDateString($obj) { |
|
43 | + return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
44 | + } |
|
45 | 45 | |
46 | 46 | private function checkCache($obj, $reset) { |
47 | 47 | if ($reset) $this->processCache = new \SplObjectStorage(); |
48 | - if (!is_object($obj)) return false; |
|
48 | + if (!is_object($obj)) return false; |
|
49 | 49 | |
50 | 50 | if ($this->processCache->contains($obj)) return $obj; |
51 | 51 | else $this->processCache->attach($obj, true); |
@@ -11,9 +11,9 @@ discard block |
||
11 | 11 | $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
12 | 12 | } |
13 | 13 | else { |
14 | - $visOverride = $this; |
|
14 | + $visOverride = $this; |
|
15 | 15 | $this->readClosure = function() use ($visOverride) { |
16 | - return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
16 | + return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
17 | 17 | }; |
18 | 18 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
19 | 19 | |
@@ -23,9 +23,9 @@ discard block |
||
23 | 23 | |
24 | 24 | } |
25 | 25 | |
26 | - public function isReturnableDataType($v) { |
|
27 | - return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTimeInterface); |
|
28 | - } |
|
26 | + public function isReturnableDataType($v) { |
|
27 | + return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTimeInterface); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | public function getProperties() { |
31 | 31 | return ($this->readClosure)(); |
@@ -7,8 +7,8 @@ discard block |
||
7 | 7 | |
8 | 8 | private $primaryKey; |
9 | 9 | private $fields = '*'; |
10 | - private $databaseSelect; |
|
11 | - private $databaseCrud; |
|
10 | + private $databaseSelect; |
|
11 | + private $databaseCrud; |
|
12 | 12 | |
13 | 13 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
14 | 14 | $options = new DatabaseOptions($db, $options); |
@@ -20,10 +20,10 @@ discard block |
||
20 | 20 | |
21 | 21 | $defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort') : implode(', ', $this->primaryKey); |
22 | 22 | |
23 | - $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table); |
|
23 | + $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table); |
|
24 | 24 | |
25 | - $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode()); |
|
26 | - $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey); |
|
25 | + $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode()); |
|
26 | + $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey); |
|
27 | 27 | |
28 | 28 | $databaseModify->optimizeColumns(); |
29 | 29 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | public function findByField(array $fields, $options = []) { |
48 | - return $this->databaseSelect->findByField($fields, $options); |
|
48 | + return $this->databaseSelect->findByField($fields, $options); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | public function deleteByField(array $fields, array $options = []) { |
@@ -53,6 +53,6 @@ discard block |
||
53 | 53 | } |
54 | 54 | |
55 | 55 | public function save($data) { |
56 | - $this->databaseCrud->save($data, true); |
|
56 | + $this->databaseCrud->save($data, true); |
|
57 | 57 | } |
58 | 58 | } |
@@ -2,48 +2,48 @@ discard block |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | |
4 | 4 | class DatabaseSelect { |
5 | - private $resultCache = []; |
|
6 | - private $idCache = []; |
|
7 | - private $selectBuilder; |
|
8 | - private $whereBuilder; |
|
9 | - private $adapter; |
|
10 | - private $databaseModify; |
|
11 | - private $defaultSort; |
|
12 | - private $table; |
|
13 | - |
|
14 | - public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort, $cacheMode) { |
|
15 | - $this->adapter = $adapter; |
|
16 | - $this->databaseModify = $databaseModify; |
|
17 | - $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
18 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
19 | - $this->defaultSort = $defaultSort; |
|
20 | - $this->cacheMode = $cacheMode; |
|
21 | - $this->table = $table; |
|
22 | - } |
|
23 | - |
|
24 | - public function findById($id, $pk) { |
|
5 | + private $resultCache = []; |
|
6 | + private $idCache = []; |
|
7 | + private $selectBuilder; |
|
8 | + private $whereBuilder; |
|
9 | + private $adapter; |
|
10 | + private $databaseModify; |
|
11 | + private $defaultSort; |
|
12 | + private $table; |
|
13 | + |
|
14 | + public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort, $cacheMode) { |
|
15 | + $this->adapter = $adapter; |
|
16 | + $this->databaseModify = $databaseModify; |
|
17 | + $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
18 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
19 | + $this->defaultSort = $defaultSort; |
|
20 | + $this->cacheMode = $cacheMode; |
|
21 | + $this->table = $table; |
|
22 | + } |
|
23 | + |
|
24 | + public function findById($id, $pk) { |
|
25 | 25 | if (($this->cacheMode && !isset($this->idCache[$id])) || !$this->cacheMode) { |
26 | 26 | try { |
27 | 27 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); |
28 | 28 | } |
29 | 29 | catch (\Exception $e) { |
30 | - // Don't issue an error if it cannot be found since we return null |
|
30 | + // Don't issue an error if it cannot be found since we return null |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | if (isset($result[0])) $result = $result[0]; |
34 | 34 | else return null; |
35 | 35 | |
36 | - if (!$this->cacheMode) return $result; |
|
37 | - else return $this->idCache[$id] = $result; |
|
36 | + if (!$this->cacheMode) return $result; |
|
37 | + else return $this->idCache[$id] = $result; |
|
38 | 38 | } |
39 | 39 | // cacheMode is true and cache is set |
40 | - return $this->idCache[$id]; |
|
40 | + return $this->idCache[$id]; |
|
41 | 41 | } |
42 | 42 | |
43 | - public function findByField(array $fields, $options = []) { |
|
43 | + public function findByField(array $fields, $options = []) { |
|
44 | 44 | $cacheId = md5(serialize(func_get_args())); |
45 | 45 | |
46 | - if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) { |
|
46 | + if (($this->cacheMode && !isset($this->resultCache[$cacheId])) || !$this->cacheMode) { |
|
47 | 47 | $query = $this->whereBuilder->createSql($fields); |
48 | 48 | |
49 | 49 | if (!isset($options['order'])) $options['order'] = $this->defaultSort; |
@@ -59,15 +59,15 @@ discard block |
||
59 | 59 | } |
60 | 60 | } |
61 | 61 | |
62 | - if ($this->cacheMode) { |
|
63 | - if (isset($result)) $this->resultCache[$cacheId] = $result; |
|
64 | - if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId]; |
|
65 | - } |
|
62 | + if ($this->cacheMode) { |
|
63 | + if (isset($result)) $this->resultCache[$cacheId] = $result; |
|
64 | + if (isset($this->resultCache[$cacheId])) return $this->resultCache[$cacheId]; |
|
65 | + } |
|
66 | 66 | |
67 | - return $result; |
|
67 | + return $result; |
|
68 | 68 | } |
69 | 69 | |
70 | - public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
70 | + public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
71 | 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 |
72 | 72 | if (is_array($field)) $field = $field[0]; |
73 | 73 | $query = $this->whereBuilder->createSql($criteria); |
@@ -84,36 +84,36 @@ discard block |
||
84 | 84 | } |
85 | 85 | } |
86 | 86 | |
87 | - private function determineAggregateResult($result, $group, $field) { |
|
88 | - if ($group != null) { |
|
89 | - $ret = []; |
|
90 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
91 | - return $ret; |
|
92 | - } |
|
93 | - else if (isset($result[0])) return $result[0]->val; |
|
94 | - else return 0; |
|
95 | - } |
|
96 | - |
|
97 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
98 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
99 | - } |
|
100 | - |
|
101 | - public function clearResultCache() { |
|
102 | - if ($this->cacheMode) $this->resultCache = []; |
|
103 | - } |
|
104 | - |
|
105 | - public function clearIDCache() { |
|
106 | - if ($this->cacheMode) $this->idCache = []; |
|
107 | - } |
|
108 | - |
|
109 | - public function updateCache($data, $pkValue) { |
|
110 | - if ($this->cacheMode) { |
|
111 | - if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
112 | - else $this->cache[$pkValue] = $data; |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - public function deleteIDFromCache($id) { |
|
117 | - if ($this->cacheMode) unset($this->idCache[$id]); |
|
118 | - } |
|
87 | + private function determineAggregateResult($result, $group, $field) { |
|
88 | + if ($group != null) { |
|
89 | + $ret = []; |
|
90 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
91 | + return $ret; |
|
92 | + } |
|
93 | + else if (isset($result[0])) return $result[0]->val; |
|
94 | + else return 0; |
|
95 | + } |
|
96 | + |
|
97 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
98 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
99 | + } |
|
100 | + |
|
101 | + public function clearResultCache() { |
|
102 | + if ($this->cacheMode) $this->resultCache = []; |
|
103 | + } |
|
104 | + |
|
105 | + public function clearIDCache() { |
|
106 | + if ($this->cacheMode) $this->idCache = []; |
|
107 | + } |
|
108 | + |
|
109 | + public function updateCache($data, $pkValue) { |
|
110 | + if ($this->cacheMode) { |
|
111 | + if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
|
112 | + else $this->cache[$pkValue] = $data; |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + public function deleteIDFromCache($id) { |
|
117 | + if ($this->cacheMode) unset($this->idCache[$id]); |
|
118 | + } |
|
119 | 119 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | private $siblings = []; |
10 | 10 | |
11 | 11 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $criteria = []) { |
12 | - if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
12 | + if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
13 | 13 | $this->mapper = $mapper; |
14 | 14 | $this->parentField = $parentField; |
15 | 15 | $this->localField = $localField; |
@@ -49,18 +49,18 @@ discard block |
||
49 | 49 | //Fetch the results so they're in the cache for the corresponding maphper object |
50 | 50 | $results = $this->mapper->filter([$this->localField => $recordsToLoad]); |
51 | 51 | |
52 | - $this->loadDataIntoSiblings($results); |
|
52 | + $this->loadDataIntoSiblings($results); |
|
53 | 53 | } |
54 | 54 | |
55 | - private function loadDataIntoSiblings($results) { |
|
56 | - $cache = []; |
|
55 | + private function loadDataIntoSiblings($results) { |
|
56 | + $cache = []; |
|
57 | 57 | foreach ($results as $result) { |
58 | 58 | $cache[$result->{$this->localField}] = $result; |
59 | 59 | } |
60 | 60 | |
61 | 61 | foreach ($this->siblings as $sibling) { |
62 | - if ($sibling->parentField === $this->parentField && |
|
63 | - isset($cache[$sibling->parentObject->{$this->parentField}]))$sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
62 | + if ($sibling->parentField === $this->parentField && |
|
63 | + isset($cache[$sibling->parentObject->{$this->parentField}]))$sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
64 | 64 | } |
65 | 65 | /* |
66 | 66 | foreach ($this->siblings as $sibling) { |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | else $sibling->data = $sibling->mapper->filter([$sibling->localField => $sibling->parentObject->{$this->parentField}])->item(0); |
69 | 69 | } |
70 | 70 | */ |
71 | - } |
|
71 | + } |
|
72 | 72 | |
73 | 73 | public function __call($func, array $args = []) { |
74 | 74 | if ($this->lazyLoad() == null) return ''; |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | |
78 | 78 | public function __get($name) { |
79 | 79 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
80 | - else return null; |
|
80 | + else return null; |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | public function __isset($name) { |
@@ -85,9 +85,9 @@ discard block |
||
85 | 85 | } |
86 | 86 | |
87 | 87 | public function overwrite($parentObject, &$data) { |
88 | - $this->mapper[] = $data; |
|
88 | + $this->mapper[] = $data; |
|
89 | 89 | |
90 | - if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
90 | + if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
91 | 91 | $parentObject->{$this->parentField} = $data->{$this->localField}; |
92 | 92 | //Trigger an update of the parent object |
93 | 93 | return true; |