@@ -3,21 +3,21 @@ |
||
3 | 3 | use Maphper\Maphper; |
4 | 4 | |
5 | 5 | class Like implements WhereConditional { |
6 | - public function matches($key, $value, $mode) { |
|
7 | - return (Maphper::FIND_LIKE | Maphper::FIND_STARTS | |
|
8 | - Maphper::FIND_ENDS | Maphper::FIND_NOCASE) & $mode; |
|
9 | - } |
|
6 | + public function matches($key, $value, $mode) { |
|
7 | + return (Maphper::FIND_LIKE | Maphper::FIND_STARTS | |
|
8 | + Maphper::FIND_ENDS | Maphper::FIND_NOCASE) & $mode; |
|
9 | + } |
|
10 | 10 | |
11 | - public function getSql($key, $value, $mode) { |
|
12 | - return [ |
|
13 | - 'sql' => [$key . ' LIKE :' . $key], |
|
14 | - 'args' => [$key => $this->getValue($value, $mode)] |
|
15 | - ]; |
|
16 | - } |
|
11 | + public function getSql($key, $value, $mode) { |
|
12 | + return [ |
|
13 | + 'sql' => [$key . ' LIKE :' . $key], |
|
14 | + 'args' => [$key => $this->getValue($value, $mode)] |
|
15 | + ]; |
|
16 | + } |
|
17 | 17 | |
18 | - private function getValue($value, $mode) { |
|
19 | - if ((Maphper::FIND_LIKE | Maphper::FIND_ENDS) & $mode) $value = '%' . $value; |
|
20 | - if ((Maphper::FIND_LIKE | Maphper::FIND_STARTS) & $mode) $value .= '%'; |
|
21 | - return $value; |
|
22 | - } |
|
18 | + private function getValue($value, $mode) { |
|
19 | + if ((Maphper::FIND_LIKE | Maphper::FIND_ENDS) & $mode) $value = '%' . $value; |
|
20 | + if ((Maphper::FIND_LIKE | Maphper::FIND_STARTS) & $mode) $value .= '%'; |
|
21 | + return $value; |
|
22 | + } |
|
23 | 23 | } |
@@ -16,8 +16,12 @@ |
||
16 | 16 | } |
17 | 17 | |
18 | 18 | private function getValue($value, $mode) { |
19 | - if ((Maphper::FIND_LIKE | Maphper::FIND_ENDS) & $mode) $value = '%' . $value; |
|
20 | - if ((Maphper::FIND_LIKE | Maphper::FIND_STARTS) & $mode) $value .= '%'; |
|
19 | + if ((Maphper::FIND_LIKE | Maphper::FIND_ENDS) & $mode) { |
|
20 | + $value = '%' . $value; |
|
21 | + } |
|
22 | + if ((Maphper::FIND_LIKE | Maphper::FIND_STARTS) & $mode) { |
|
23 | + $value .= '%'; |
|
24 | + } |
|
21 | 25 | return $value; |
22 | 26 | } |
23 | 27 | } |
@@ -3,33 +3,33 @@ |
||
3 | 3 | use Maphper\Maphper; |
4 | 4 | |
5 | 5 | class GeneralOperator implements WhereConditional { |
6 | - public function matches($key, $value, $mode) { |
|
7 | - return (Maphper::FIND_BIT ^ Maphper::FIND_GREATER ^ Maphper::FIND_LESS ^ Maphper::FIND_NOT & $mode) |
|
8 | - || Maphper::FIND_EXACT & $mode; |
|
9 | - } |
|
6 | + public function matches($key, $value, $mode) { |
|
7 | + return (Maphper::FIND_BIT ^ Maphper::FIND_GREATER ^ Maphper::FIND_LESS ^ Maphper::FIND_NOT & $mode) |
|
8 | + || Maphper::FIND_EXACT & $mode; |
|
9 | + } |
|
10 | 10 | |
11 | - public function getSql($key, $value, $mode) { |
|
12 | - return [ |
|
13 | - 'sql' => [$key . ' ' . $this->getOperator($mode) . ' :' . $key], |
|
14 | - 'args' => [$key => $value] |
|
15 | - ]; |
|
16 | - } |
|
11 | + public function getSql($key, $value, $mode) { |
|
12 | + return [ |
|
13 | + 'sql' => [$key . ' ' . $this->getOperator($mode) . ' :' . $key], |
|
14 | + 'args' => [$key => $value] |
|
15 | + ]; |
|
16 | + } |
|
17 | 17 | |
18 | - private function getOperator($mode) { |
|
19 | - if (\Maphper\Maphper::FIND_BIT & $mode) return '&'; |
|
20 | - else if (\Maphper\Maphper::FIND_NOT & $mode) return '!='; |
|
18 | + private function getOperator($mode) { |
|
19 | + if (\Maphper\Maphper::FIND_BIT & $mode) return '&'; |
|
20 | + else if (\Maphper\Maphper::FIND_NOT & $mode) return '!='; |
|
21 | 21 | |
22 | - return $this->getEqualsOperators($mode); |
|
23 | - } |
|
22 | + return $this->getEqualsOperators($mode); |
|
23 | + } |
|
24 | 24 | |
25 | - private function getEqualsOperators($mode) { |
|
26 | - $operator = ""; |
|
25 | + private function getEqualsOperators($mode) { |
|
26 | + $operator = ""; |
|
27 | 27 | |
28 | - if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
29 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
28 | + if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
29 | + else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
30 | 30 | |
31 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
31 | + if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
32 | 32 | |
33 | - return $operator; |
|
34 | - } |
|
33 | + return $operator; |
|
34 | + } |
|
35 | 35 | } |
@@ -16,8 +16,11 @@ discard block |
||
16 | 16 | } |
17 | 17 | |
18 | 18 | private function getOperator($mode) { |
19 | - if (\Maphper\Maphper::FIND_BIT & $mode) return '&'; |
|
20 | - else if (\Maphper\Maphper::FIND_NOT & $mode) return '!='; |
|
19 | + if (\Maphper\Maphper::FIND_BIT & $mode) { |
|
20 | + return '&'; |
|
21 | + } else if (\Maphper\Maphper::FIND_NOT & $mode) { |
|
22 | + return '!='; |
|
23 | + } |
|
21 | 24 | |
22 | 25 | return $this->getEqualsOperators($mode); |
23 | 26 | } |
@@ -25,10 +28,15 @@ discard block |
||
25 | 28 | private function getEqualsOperators($mode) { |
26 | 29 | $operator = ""; |
27 | 30 | |
28 | - if (\Maphper\Maphper::FIND_GREATER & $mode) $operator = '>'; |
|
29 | - else if (\Maphper\Maphper::FIND_LESS & $mode) $operator = '<'; |
|
31 | + if (\Maphper\Maphper::FIND_GREATER & $mode) { |
|
32 | + $operator = '>'; |
|
33 | + } else if (\Maphper\Maphper::FIND_LESS & $mode) { |
|
34 | + $operator = '<'; |
|
35 | + } |
|
30 | 36 | |
31 | - if (\Maphper\Maphper::FIND_EXACT & $mode) $operator .= '='; |
|
37 | + if (\Maphper\Maphper::FIND_EXACT & $mode) { |
|
38 | + $operator .= '='; |
|
39 | + } |
|
32 | 40 | |
33 | 41 | return $operator; |
34 | 42 | } |
@@ -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 \DateTime); |
|
28 | - } |
|
26 | + public function isReturnableDataType($v) { |
|
27 | + return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTime); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | public function getProperties() { |
31 | 31 | return ($this->readClosure)(); |
@@ -7,17 +7,17 @@ discard block |
||
7 | 7 | |
8 | 8 | public function __construct($object) { |
9 | 9 | if ($object instanceof \stdclass) { |
10 | - $this->readClosure = function() use ($object) { return $object; }; |
|
11 | - $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
|
10 | + $this->readClosure = function() use ($object) { return $object; }; |
|
11 | + $this->writeClosure = function($field, $value) use ($object) { $object->$field = $value; }; |
|
12 | 12 | } |
13 | 13 | else { |
14 | 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 | |
20 | - $this->writeClosure = function ($field, $value) { $this->$field = $value; }; |
|
20 | + $this->writeClosure = function($field, $value) { $this->$field = $value; }; |
|
21 | 21 | $this->writeClosure = $this->writeClosure->bindTo($object, $object); |
22 | 22 | } |
23 | 23 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | public function write($data) { |
35 | 35 | if ($data != null) { |
36 | 36 | foreach ($data as $key => $value) { |
37 | - ($this->writeClosure)($key, $this->processDates($value)); |
|
37 | + ($this->writeClosure)($key, $this->processDates($value)); |
|
38 | 38 | } |
39 | 39 | } |
40 | 40 | } |
@@ -9,8 +9,7 @@ |
||
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 | $visOverride = $this; |
15 | 14 | $this->readClosure = function() use ($visOverride) { |
16 | 15 | return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | private $parent; |
9 | 9 | |
10 | 10 | public function __construct(\Maphper\Maphper $parent, $className = null) { |
11 | - $this->parent = $parent; |
|
11 | + $this->parent = $parent; |
|
12 | 12 | $this->className = $className; |
13 | 13 | } |
14 | 14 | |
@@ -25,12 +25,12 @@ discard block |
||
25 | 25 | return $object; |
26 | 26 | } |
27 | 27 | |
28 | - private function addRelationData($object, $name, $relation, $siblings) { |
|
29 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
30 | - //After overwriting the relation, does the parent object ($object) need overwriting as well? |
|
31 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
32 | - } |
|
28 | + private function addRelationData($object, $name, $relation, $siblings) { |
|
29 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
30 | + //After overwriting the relation, does the parent object ($object) need overwriting as well? |
|
31 | + if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
32 | + } |
|
33 | 33 | |
34 | - $object->$name = $relation->getData($object, $siblings); |
|
35 | - } |
|
34 | + $object->$name = $relation->getData($object, $siblings); |
|
35 | + } |
|
36 | 36 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | } |
27 | 27 | |
28 | 28 | private function addRelationData($object, $name, $relation, $siblings) { |
29 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
29 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) { |
|
30 | 30 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
31 | 31 | if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
32 | 32 | } |
@@ -21,14 +21,18 @@ |
||
21 | 21 | |
22 | 22 | public function wrap($relations, $object, $siblings = []) { |
23 | 23 | //see if any relations need overwriting |
24 | - foreach ($relations as $name => $relation) $this->addRelationData($object, $name, $relation, $siblings); |
|
24 | + foreach ($relations as $name => $relation) { |
|
25 | + $this->addRelationData($object, $name, $relation, $siblings); |
|
26 | + } |
|
25 | 27 | return $object; |
26 | 28 | } |
27 | 29 | |
28 | 30 | private function addRelationData($object, $name, $relation, $siblings) { |
29 | 31 | if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
30 | 32 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
31 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
33 | + if ($relation->overwrite($object, $object->$name)) { |
|
34 | + $this->parent[] = $object; |
|
35 | + } |
|
32 | 36 | } |
33 | 37 | |
34 | 38 | $object->$name = $relation->getData($object, $siblings); |
@@ -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 \DateTime) 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 \DateTime)) || |
|
49 | - in_array($key, $primaryKey); |
|
50 | - } |
|
47 | + public function isNotSavableType($value, $key, $primaryKey) { |
|
48 | + return is_array($value) || (is_object($value) && !($value instanceof \DateTime)) || |
|
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; |
@@ -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) . ')'; |
@@ -4,15 +4,15 @@ discard block |
||
4 | 4 | class SqliteAdapter implements DatabaseAdapter { |
5 | 5 | private $pdo; |
6 | 6 | private $stmtCache; |
7 | - private $generalEditor; |
|
7 | + private $generalEditor; |
|
8 | 8 | |
9 | 9 | public function __construct(\PDO $pdo) { |
10 | 10 | $this->pdo = $pdo; |
11 | - $this->stmtCache = new StmtCache($pdo); |
|
12 | - $this->generalEditor = new GeneralEditDatabase($this->pdo, [ |
|
13 | - 'int' => 'INTEGER', |
|
14 | - 'pk_default' => 'INTEGER NOT NULL', |
|
15 | - ]); |
|
11 | + $this->stmtCache = new StmtCache($pdo); |
|
12 | + $this->generalEditor = new GeneralEditDatabase($this->pdo, [ |
|
13 | + 'int' => 'INTEGER', |
|
14 | + 'pk_default' => 'INTEGER NOT NULL', |
|
15 | + ]); |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | public function quote($str) { |
@@ -20,19 +20,19 @@ discard block |
||
20 | 20 | } |
21 | 21 | |
22 | 22 | public function query(\Maphper\Lib\Query $query) { |
23 | - $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
23 | + $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
24 | 24 | $args = $query->getArgs(); |
25 | 25 | |
26 | - //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
27 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
26 | + //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
27 | + if ($stmt === false) throw new \Exception('Invalid query'); |
|
28 | 28 | |
29 | - $stmt->execute($args); |
|
30 | - if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
29 | + $stmt->execute($args); |
|
30 | + if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
31 | 31 | $this->stmtCache->deleteQueryFromCache($query->getSql()); |
32 | 32 | return $this->query($query); |
33 | - } |
|
33 | + } |
|
34 | 34 | |
35 | - return $stmt; |
|
35 | + return $stmt; |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | public function lastInsertId() { |
@@ -59,11 +59,11 @@ discard block |
||
59 | 59 | // SQLSTATE[HY000]: General error: 17 database schema has changed |
60 | 60 | $this->stmtCache->clearCache(); |
61 | 61 | |
62 | - // Create temp table to create a new structure |
|
62 | + // Create temp table to create a new structure |
|
63 | 63 | $affix = '_'.substr(md5($table), 0, 6); |
64 | - $tempTable = $table . $affix; |
|
64 | + $tempTable = $table . $affix; |
|
65 | 65 | $this->generalEditor->createTable($tempTable, $primaryKey, $data); |
66 | - $this->alterColumns($tempTable, $primaryKey, $data); |
|
66 | + $this->alterColumns($tempTable, $primaryKey, $data); |
|
67 | 67 | $this->copyTableData($table, $tempTable); |
68 | 68 | |
69 | 69 | $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
@@ -71,8 +71,8 @@ discard block |
||
71 | 71 | |
72 | 72 | } |
73 | 73 | |
74 | - private function copyTableData($tableFrom, $tableTo) { |
|
75 | - try { |
|
74 | + private function copyTableData($tableFrom, $tableTo) { |
|
75 | + try { |
|
76 | 76 | if ($this->tableExists($tableFrom)) { |
77 | 77 | $columns = implode(', ', $this->getColumns($tableFrom)); |
78 | 78 | |
@@ -83,19 +83,19 @@ discard block |
||
83 | 83 | // No data to copy |
84 | 84 | echo $e->getMessage(); |
85 | 85 | } |
86 | - } |
|
86 | + } |
|
87 | 87 | |
88 | - private function alterColumns($table, array $primaryKey, $data) { |
|
89 | - foreach ($data as $key => $value) { |
|
88 | + private function alterColumns($table, array $primaryKey, $data) { |
|
89 | + foreach ($data as $key => $value) { |
|
90 | 90 | if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
91 | 91 | |
92 | 92 | $type = $this->generalEditor->getType($value); |
93 | 93 | |
94 | 94 | $this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type); |
95 | 95 | } |
96 | - } |
|
96 | + } |
|
97 | 97 | |
98 | - public function addIndex($table, array $fields) { |
|
98 | + public function addIndex($table, array $fields) { |
|
99 | 99 | if (empty($fields)) return false; |
100 | 100 | |
101 | 101 | //SQLite doesn't support ASC/DESC indexes, remove the keywords |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | } |
41 | 41 | |
42 | 42 | private function tableExists($name) { |
43 | - $result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="'. $name.'"'); |
|
43 | + $result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="' . $name . '"'); |
|
44 | 44 | return count($result->fetchAll()) == 1; |
45 | 45 | } |
46 | 46 | |
@@ -60,14 +60,14 @@ discard block |
||
60 | 60 | $this->stmtCache->clearCache(); |
61 | 61 | |
62 | 62 | // Create temp table to create a new structure |
63 | - $affix = '_'.substr(md5($table), 0, 6); |
|
63 | + $affix = '_' . substr(md5($table), 0, 6); |
|
64 | 64 | $tempTable = $table . $affix; |
65 | 65 | $this->generalEditor->createTable($tempTable, $primaryKey, $data); |
66 | 66 | $this->alterColumns($tempTable, $primaryKey, $data); |
67 | 67 | $this->copyTableData($table, $tempTable); |
68 | 68 | |
69 | - $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
|
70 | - $this->pdo->query('ALTER TABLE ' . $tempTable . ' RENAME TO '. $table ); |
|
69 | + $this->pdo->query('DROP TABLE IF EXISTS ' . $table); |
|
70 | + $this->pdo->query('ALTER TABLE ' . $tempTable . ' RENAME TO ' . $table); |
|
71 | 71 | |
72 | 72 | } |
73 | 73 |
@@ -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 | } |
@@ -88,25 +88,25 @@ |
||
88 | 88 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
89 | 89 | if ($this->autoTraverse) $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
90 | 90 | else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
91 | - $record = $value; |
|
91 | + $record = $value; |
|
92 | 92 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
93 | 93 | $record->$valueField = $this->object->{$relatedField}; |
94 | 94 | $this->intermediateMapper[] = $record; |
95 | 95 | } |
96 | 96 | } |
97 | 97 | |
98 | - private function doUpdateInterMapper($record, $relatedField, $valueField) { |
|
99 | - return !(isset($record->{$this->parentField}) && isset($record->{$this->intermediateName}) && |
|
100 | - $record->{$this->parentField} == $record->{$this->intermediateName}->{$this->localField} && |
|
101 | - $record->$valueField == $this->object->{$relatedField}); |
|
102 | - } |
|
103 | - |
|
104 | - private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
|
105 | - $record = new \stdClass; |
|
106 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
107 | - $record->$valueField = $this->object->{$relatedField}; |
|
108 | - $this->intermediateMapper[] = $record; |
|
109 | - } |
|
98 | + private function doUpdateInterMapper($record, $relatedField, $valueField) { |
|
99 | + return !(isset($record->{$this->parentField}) && isset($record->{$this->intermediateName}) && |
|
100 | + $record->{$this->parentField} == $record->{$this->intermediateName}->{$this->localField} && |
|
101 | + $record->$valueField == $this->object->{$relatedField}); |
|
102 | + } |
|
103 | + |
|
104 | + private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
|
105 | + $record = new \stdClass; |
|
106 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
107 | + $record->$valueField = $this->object->{$relatedField}; |
|
108 | + $this->intermediateMapper[] = $record; |
|
109 | + } |
|
110 | 110 | |
111 | 111 | public function offsetUnset($id) { |
112 | 112 | //$this->relation->mapper->filter([$relatedField => $this->object->$valueField, $this->relation->parentField => $id])->delete(); |
@@ -37,7 +37,7 @@ discard block |
||
37 | 37 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
38 | 38 | private function getOtherFieldNameInfo() { |
39 | 39 | if ($this->otherInfo == null) { |
40 | - $propertyReader = function($name) {return $this->$name; }; |
|
40 | + $propertyReader = function($name) {return $this->$name; }; |
|
41 | 41 | |
42 | 42 | $reader = $propertyReader->bindTo($this->intermediateMapper, $this->intermediateMapper); |
43 | 43 | |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | $propertyReader = $propertyReader->bindTo($relation, $relation); |
46 | 46 | if ($propertyReader('parentField') != $this->parentField) { |
47 | 47 | $relation = $relation->getData($this->object); |
48 | - $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
48 | + $this->otherInfo = [$propertyReader('localField'), $propertyReader('parentField'), $propertyReader('mapper')]; |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | } |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | |
104 | 104 | private function offsetSetAutotraverse($value, $relatedField, $valueField) { |
105 | 105 | $record = new \stdClass; |
106 | - $record->{$this->parentField} = $value->{$this->localField}; |
|
106 | + $record->{$this->parentField} = $value->{$this->localField}; |
|
107 | 107 | $record->$valueField = $this->object->{$relatedField}; |
108 | 108 | $this->intermediateMapper[] = $record; |
109 | 109 | } |
@@ -31,7 +31,9 @@ discard block |
||
31 | 31 | public function overwrite($parentObject, &$data) { |
32 | 32 | $this->results = $data; |
33 | 33 | $this->object = $parentObject; |
34 | - foreach ($data as $dt) $this[] = $dt; |
|
34 | + foreach ($data as $dt) { |
|
35 | + $this[] = $dt; |
|
36 | + } |
|
35 | 37 | } |
36 | 38 | |
37 | 39 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
@@ -86,8 +88,9 @@ discard block |
||
86 | 88 | |
87 | 89 | public function offsetSet($name, $value) { |
88 | 90 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
89 | - if ($this->autoTraverse) $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
90 | - else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
91 | + if ($this->autoTraverse) { |
|
92 | + $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
93 | + } else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
91 | 94 | $record = $value; |
92 | 95 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
93 | 96 | $record->$valueField = $this->object->{$relatedField}; |
@@ -9,7 +9,9 @@ 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) { |
|
13 | + $mapper = $mapper->filter($this->criteira); |
|
14 | + } |
|
13 | 15 | $this->mapper = $mapper; |
14 | 16 | $this->parentField = $parentField; |
15 | 17 | $this->localField = $localField; |
@@ -29,7 +31,9 @@ discard block |
||
29 | 31 | private function lazyLoad() { |
30 | 32 | if (!isset($this->data)) { |
31 | 33 | |
32 | - if ($this->parentObject == null) throw new \Exception('Error, no object set'); |
|
34 | + if ($this->parentObject == null) { |
|
35 | + throw new \Exception('Error, no object set'); |
|
36 | + } |
|
33 | 37 | |
34 | 38 | $this->eagerLoad(); |
35 | 39 | |
@@ -69,13 +73,18 @@ discard block |
||
69 | 73 | } |
70 | 74 | |
71 | 75 | public function __call($func, array $args = []) { |
72 | - if ($this->lazyLoad() == null) return ''; |
|
76 | + if ($this->lazyLoad() == null) { |
|
77 | + return ''; |
|
78 | + } |
|
73 | 79 | return call_user_func_array([$this->lazyLoad(), $func], $args); |
74 | 80 | } |
75 | 81 | |
76 | 82 | public function __get($name) { |
77 | - if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
|
78 | - else return null; |
|
83 | + if ($this->lazyLoad()) { |
|
84 | + return $this->lazyLoad()->$name; |
|
85 | + } else { |
|
86 | + return null; |
|
87 | + } |
|
79 | 88 | } |
80 | 89 | |
81 | 90 | public function __isset($name) { |
@@ -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; |
@@ -48,11 +48,11 @@ discard block |
||
48 | 48 | //Fetch the results so they're in the cache for the corresponding maphper object |
49 | 49 | $results = $this->mapper->filter([$this->localField => $recordsToLoad]); |
50 | 50 | |
51 | - $this->loadDataIntoSiblings($results); |
|
51 | + $this->loadDataIntoSiblings($results); |
|
52 | 52 | } |
53 | 53 | |
54 | - private function loadDataIntoSiblings($results) { |
|
55 | - $cache = []; |
|
54 | + private function loadDataIntoSiblings($results) { |
|
55 | + $cache = []; |
|
56 | 56 | foreach ($results as $result) { |
57 | 57 | $cache[$result->{$this->localField}] = $result; |
58 | 58 | } |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | else $sibling->data = $sibling->mapper->filter([$sibling->localField => $sibling->parentObject->{$this->parentField}])->item(0); |
67 | 67 | } |
68 | 68 | */ |
69 | - } |
|
69 | + } |
|
70 | 70 | |
71 | 71 | public function __call($func, array $args = []) { |
72 | 72 | if ($this->lazyLoad() == null) return ''; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | |
76 | 76 | public function __get($name) { |
77 | 77 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
78 | - else return null; |
|
78 | + else return null; |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | public function __isset($name) { |
@@ -83,9 +83,9 @@ discard block |
||
83 | 83 | } |
84 | 84 | |
85 | 85 | public function overwrite($parentObject, &$data) { |
86 | - $this->mapper[] = $data; |
|
86 | + $this->mapper[] = $data; |
|
87 | 87 | |
88 | - if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
88 | + if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
89 | 89 | $parentObject->{$this->parentField} = $data->{$this->localField}; |
90 | 90 | //Trigger an update of the parent object |
91 | 91 | return true; |
@@ -3,14 +3,14 @@ discard block |
||
3 | 3 | class MysqlAdapter implements DatabaseAdapter { |
4 | 4 | private $pdo; |
5 | 5 | private $stmtCache; |
6 | - private $generalEditor; |
|
6 | + private $generalEditor; |
|
7 | 7 | |
8 | 8 | public function __construct(\PDO $pdo) { |
9 | 9 | $this->pdo = $pdo; |
10 | 10 | //Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries |
11 | 11 | $this->pdo->query('SET sql_mode = STRICT_ALL_TABLES'); |
12 | - $this->stmtCache = new StmtCache($pdo); |
|
13 | - $this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]); |
|
12 | + $this->stmtCache = new StmtCache($pdo); |
|
13 | + $this->generalEditor = new GeneralEditDatabase($this->pdo, ['short_string_max_len' => 191]); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public function quote($str) { |
@@ -20,32 +20,32 @@ discard block |
||
20 | 20 | public function query(\Maphper\Lib\Query $query) { |
21 | 21 | $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
22 | 22 | $args = $query->getArgs(); |
23 | - $stmt->execute($args); |
|
23 | + $stmt->execute($args); |
|
24 | 24 | |
25 | 25 | return $stmt; |
26 | 26 | } |
27 | 27 | |
28 | - private function alterColumns($table, array $primaryKey, $data) { |
|
29 | - foreach ($data as $key => $value) { |
|
28 | + private function alterColumns($table, array $primaryKey, $data) { |
|
29 | + foreach ($data as $key => $value) { |
|
30 | 30 | if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
31 | 31 | |
32 | 32 | $type = $this->generalEditor->getType($value); |
33 | 33 | $this->tryAlteringColumn($table, $key, $type); |
34 | 34 | } |
35 | - } |
|
35 | + } |
|
36 | 36 | |
37 | - private function tryAlteringColumn($table, $key, $type) { |
|
38 | - try { |
|
39 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
40 | - } |
|
41 | - catch (\Exception $e) { |
|
42 | - $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
|
43 | - } |
|
44 | - } |
|
37 | + private function tryAlteringColumn($table, $key, $type) { |
|
38 | + try { |
|
39 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
40 | + } |
|
41 | + catch (\Exception $e) { |
|
42 | + $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
|
43 | + } |
|
44 | + } |
|
45 | 45 | |
46 | 46 | public function alterDatabase($table, array $primaryKey, $data) { |
47 | 47 | $this->generalEditor->createTable($table, $primaryKey, $data); |
48 | - $this->alterColumns($table, $primaryKey, $data); |
|
48 | + $this->alterColumns($table, $primaryKey, $data); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | public function lastInsertId() { |
@@ -27,7 +27,9 @@ discard block |
||
27 | 27 | |
28 | 28 | private function alterColumns($table, array $primaryKey, $data) { |
29 | 29 | foreach ($data as $key => $value) { |
30 | - if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
|
30 | + if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) { |
|
31 | + continue; |
|
32 | + } |
|
31 | 33 | |
32 | 34 | $type = $this->generalEditor->getType($value); |
33 | 35 | $this->tryAlteringColumn($table, $key, $type); |
@@ -36,9 +38,10 @@ discard block |
||
36 | 38 | |
37 | 39 | private function tryAlteringColumn($table, $key, $type) { |
38 | 40 | try { |
39 | - if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) throw new \Exception('Could not alter table'); |
|
40 | - } |
|
41 | - catch (\Exception $e) { |
|
41 | + if (!$this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type)) { |
|
42 | + throw new \Exception('Could not alter table'); |
|
43 | + } |
|
44 | + } catch (\Exception $e) { |
|
42 | 45 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
43 | 46 | } |
44 | 47 | } |
@@ -60,7 +63,9 @@ discard block |
||
60 | 63 | $keyName = $this->quote(implode('_', $fields)); |
61 | 64 | |
62 | 65 | $results = $this->pdo->query('SHOW INDEX FROM ' . $this->quote($table) . ' WHERE Key_Name = "' . $keyName . '"'); |
63 | - if ($results && count($results->fetchAll()) == 0) $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
66 | + if ($results && count($results->fetchAll()) == 0) { |
|
67 | + $this->pdo->query('CREATE INDEX ' . $keyName . ' ON ' . $this->quote($table) . ' (' . implode(', ', $fields) . ')'); |
|
68 | + } |
|
64 | 69 | } |
65 | 70 | |
66 | 71 | public function optimiseColumns($table) { |