@@ -2,68 +2,68 @@ |
||
2 | 2 | namespace Maphper\Lib\Sql; |
3 | 3 | |
4 | 4 | class WhereBuilder { |
5 | - private $conditionals = []; |
|
5 | + private $conditionals = []; |
|
6 | 6 | |
7 | - public function __construct() { |
|
8 | - $defaultConditionals = [ |
|
9 | - 'Maphper\Lib\Sql\Between', |
|
10 | - 'Maphper\Lib\Sql\In', |
|
11 | - 'Maphper\Lib\Sql\NullConditional', |
|
12 | - 'Maphper\Lib\Sql\Like', |
|
13 | - 'Maphper\Lib\Sql\GeneralOperator' |
|
14 | - ]; |
|
7 | + public function __construct() { |
|
8 | + $defaultConditionals = [ |
|
9 | + 'Maphper\Lib\Sql\Between', |
|
10 | + 'Maphper\Lib\Sql\In', |
|
11 | + 'Maphper\Lib\Sql\NullConditional', |
|
12 | + 'Maphper\Lib\Sql\Like', |
|
13 | + 'Maphper\Lib\Sql\GeneralOperator' |
|
14 | + ]; |
|
15 | 15 | |
16 | - foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
17 | - } |
|
16 | + foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
17 | + } |
|
18 | 18 | |
19 | - public function addConditional(WhereConditional $conditional) { |
|
20 | - $this->conditionals[] = $conditional; |
|
21 | - } |
|
19 | + public function addConditional(WhereConditional $conditional) { |
|
20 | + $this->conditionals[] = $conditional; |
|
21 | + } |
|
22 | 22 | |
23 | 23 | public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND) { |
24 | 24 | $args = []; |
25 | 25 | $sql = []; |
26 | 26 | |
27 | - foreach ($fields as $key => $value) { |
|
28 | - $value = $this->convertDates($value); |
|
27 | + foreach ($fields as $key => $value) { |
|
28 | + $value = $this->convertDates($value); |
|
29 | 29 | |
30 | - if (is_object($value)) continue; |
|
30 | + if (is_object($value)) continue; |
|
31 | 31 | $result = $this->getResult($key, $value, $mode); |
32 | - $sql = array_merge($sql, (array)$result['sql']); |
|
33 | - $args = array_merge($args, $result['args']); |
|
34 | - } |
|
32 | + $sql = array_merge($sql, (array)$result['sql']); |
|
33 | + $args = array_merge($args, $result['args']); |
|
34 | + } |
|
35 | 35 | |
36 | 36 | return ['args' => $args, 'sql' => $this->sqlArrayToString($sql, $mode)]; |
37 | 37 | } |
38 | 38 | |
39 | - /* |
|
39 | + /* |
|
40 | 40 | * Either get sql from a conditional or call createSql again because the mode needs to be changed |
41 | 41 | */ |
42 | - private function getResult($key, $value, $mode) { |
|
43 | - if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
44 | - return $this->getConditional($key, $value, $mode); |
|
45 | - } |
|
42 | + private function getResult($key, $value, $mode) { |
|
43 | + if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
44 | + return $this->getConditional($key, $value, $mode); |
|
45 | + } |
|
46 | 46 | |
47 | - private function sqlArrayToString($sql, $mode) { |
|
48 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
47 | + private function sqlArrayToString($sql, $mode) { |
|
48 | + if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
49 | 49 | else $query = implode(' AND ', $sql); |
50 | 50 | if (!empty($query)) $query = '(' . $query . ')'; |
51 | - return $query; |
|
52 | - } |
|
51 | + return $query; |
|
52 | + } |
|
53 | 53 | |
54 | - private function getConditional($key, $value, $mode) { |
|
55 | - foreach ($this->conditionals as $conditional) { |
|
56 | - if ($conditional->matches($key, $value, $mode)) |
|
57 | - return $conditional->getSql($key, $value, $mode); |
|
58 | - } |
|
59 | - throw new \Exception("Invalid WHERE query"); |
|
60 | - } |
|
54 | + private function getConditional($key, $value, $mode) { |
|
55 | + foreach ($this->conditionals as $conditional) { |
|
56 | + if ($conditional->matches($key, $value, $mode)) |
|
57 | + return $conditional->getSql($key, $value, $mode); |
|
58 | + } |
|
59 | + throw new \Exception("Invalid WHERE query"); |
|
60 | + } |
|
61 | 61 | |
62 | - private function convertDates($value) { |
|
63 | - if ($value instanceof \DateTime) { |
|
64 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
65 | - else $value = $value->format('Y-m-d H:i:s'); |
|
66 | - } |
|
67 | - return $value; |
|
68 | - } |
|
62 | + private function convertDates($value) { |
|
63 | + if ($value instanceof \DateTime) { |
|
64 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
65 | + else $value = $value->format('Y-m-d H:i:s'); |
|
66 | + } |
|
67 | + return $value; |
|
68 | + } |
|
69 | 69 | } |
@@ -3,58 +3,58 @@ |
||
3 | 3 | use Maphper\Maphper; |
4 | 4 | |
5 | 5 | class ArrayFilter { |
6 | - private $array; |
|
7 | - |
|
8 | - public function __construct(array $array) { |
|
9 | - $this->array = $array; |
|
10 | - } |
|
11 | - |
|
12 | - public function filter($fields) { |
|
13 | - $filteredArray = array_filter($this->array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
14 | - // Need to reset indexes |
|
15 | - $filteredArray = array_values($filteredArray); |
|
16 | - return $filteredArray; |
|
17 | - } |
|
18 | - |
|
19 | - private function getSearchFieldFunction($fields, $mode) { |
|
20 | - return function ($data) use ($fields, $mode) { |
|
21 | - foreach ($fields as $key => $val) { |
|
22 | - $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
23 | - |
|
24 | - if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
25 | - else if (Maphper::FIND_OR ^ $mode && $currentFieldResult === false) return false; |
|
26 | - } |
|
27 | - return (bool)(Maphper::FIND_OR ^ $mode); |
|
28 | - }; |
|
29 | - } |
|
30 | - |
|
31 | - private function getIfFieldMatches($key, $val, $data, $mode) { |
|
32 | - if ($this->shouldRunDeepSearch($key, $val)) { |
|
33 | - return $this->getSearchFieldFunction($val, $key)($data); |
|
34 | - } |
|
35 | - else if (!isset($data->$key)) return false; |
|
36 | - else if ($this->isInArraySearch($mode, $key, $val)) |
|
37 | - return in_array($data->$key, $val); |
|
38 | - else |
|
39 | - return $this->processFilter($mode, $val, $data->$key); |
|
40 | - } |
|
41 | - |
|
42 | - private function shouldRunDeepSearch($key, $val) { |
|
43 | - return is_numeric($key) && is_array($val); |
|
44 | - } |
|
45 | - |
|
46 | - private function isInArraySearch($mode, $key, $val) { |
|
47 | - return !(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val); |
|
48 | - } |
|
49 | - |
|
50 | - private function processFilter($mode, $expected, $actual) { |
|
51 | - if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
52 | - else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) return $expected <= $actual; |
|
53 | - else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) return $expected >= $actual; |
|
54 | - else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
55 | - else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
56 | - else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
57 | - else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
58 | - return $expected == $actual; |
|
59 | - } |
|
6 | + private $array; |
|
7 | + |
|
8 | + public function __construct(array $array) { |
|
9 | + $this->array = $array; |
|
10 | + } |
|
11 | + |
|
12 | + public function filter($fields) { |
|
13 | + $filteredArray = array_filter($this->array, $this->getSearchFieldFunction($fields, \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND)); |
|
14 | + // Need to reset indexes |
|
15 | + $filteredArray = array_values($filteredArray); |
|
16 | + return $filteredArray; |
|
17 | + } |
|
18 | + |
|
19 | + private function getSearchFieldFunction($fields, $mode) { |
|
20 | + return function ($data) use ($fields, $mode) { |
|
21 | + foreach ($fields as $key => $val) { |
|
22 | + $currentFieldResult = $this->getIfFieldMatches($key, $val, $data, $mode); |
|
23 | + |
|
24 | + if (Maphper::FIND_OR & $mode && $currentFieldResult === true) return true; |
|
25 | + else if (Maphper::FIND_OR ^ $mode && $currentFieldResult === false) return false; |
|
26 | + } |
|
27 | + return (bool)(Maphper::FIND_OR ^ $mode); |
|
28 | + }; |
|
29 | + } |
|
30 | + |
|
31 | + private function getIfFieldMatches($key, $val, $data, $mode) { |
|
32 | + if ($this->shouldRunDeepSearch($key, $val)) { |
|
33 | + return $this->getSearchFieldFunction($val, $key)($data); |
|
34 | + } |
|
35 | + else if (!isset($data->$key)) return false; |
|
36 | + else if ($this->isInArraySearch($mode, $key, $val)) |
|
37 | + return in_array($data->$key, $val); |
|
38 | + else |
|
39 | + return $this->processFilter($mode, $val, $data->$key); |
|
40 | + } |
|
41 | + |
|
42 | + private function shouldRunDeepSearch($key, $val) { |
|
43 | + return is_numeric($key) && is_array($val); |
|
44 | + } |
|
45 | + |
|
46 | + private function isInArraySearch($mode, $key, $val) { |
|
47 | + return !(Maphper::FIND_BETWEEN & $mode) && !is_numeric($key) && is_array($val); |
|
48 | + } |
|
49 | + |
|
50 | + private function processFilter($mode, $expected, $actual) { |
|
51 | + if (Maphper::FIND_NOT & $mode) return $expected != $actual; |
|
52 | + else if ((Maphper::FIND_GREATER | Maphper::FIND_EXACT) === $mode) return $expected <= $actual; |
|
53 | + else if ((Maphper::FIND_LESS | Maphper::FIND_EXACT) === $mode) return $expected >= $actual; |
|
54 | + else if (Maphper::FIND_GREATER & $mode) return $expected < $actual; |
|
55 | + else if (Maphper::FIND_LESS & $mode) return $expected > $actual; |
|
56 | + else if (Maphper::FIND_BETWEEN & $mode) return $expected[0] <= $actual && $actual <= $expected[1]; |
|
57 | + else if (Maphper::FIND_NOCASE & $mode) return strtolower($expected) == strtolower($actual); |
|
58 | + return $expected == $actual; |
|
59 | + } |
|
60 | 60 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | $this->pdo = $pdo; |
9 | 9 | //Set to strict mode to detect 'out of range' errors, action at a distance but it needs to be set for all INSERT queries |
10 | 10 | $this->pdo->query('SET sql_mode = STRICT_ALL_TABLES'); |
11 | - $this->stmtCache = new StmtCache($pdo); |
|
11 | + $this->stmtCache = new StmtCache($pdo); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | public function quote($str) { |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | public function query(\Maphper\Lib\Query $query) { |
19 | 19 | $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
20 | 20 | $args = $query->getArgs(); |
21 | - $stmt->execute($args); |
|
21 | + $stmt->execute($args); |
|
22 | 22 | |
23 | 23 | return $stmt; |
24 | 24 | } |
@@ -44,8 +44,8 @@ discard block |
||
44 | 44 | $this->pdo->query('CREATE TABLE IF NOT EXISTS ' . $table . ' (' . $pkField . ')'); |
45 | 45 | } |
46 | 46 | |
47 | - private function alterColumns($table, array $primaryKey, $data) { |
|
48 | - foreach ($data as $key => $value) { |
|
47 | + private function alterColumns($table, array $primaryKey, $data) { |
|
48 | + foreach ($data as $key => $value) { |
|
49 | 49 | if ($this->isNotSavableType($value, $key, $primaryKey)) continue; |
50 | 50 | |
51 | 51 | $type = $this->getType($value); |
@@ -57,16 +57,16 @@ discard block |
||
57 | 57 | $this->pdo->query('ALTER TABLE ' . $table . ' MODIFY ' . $this->quote($key) . ' ' . $type); |
58 | 58 | } |
59 | 59 | } |
60 | - } |
|
60 | + } |
|
61 | 61 | |
62 | - private function isNotSavableType($value, $key, $primaryKey) { |
|
63 | - return is_array($value) || (is_object($value) && !($value instanceof \DateTime)) || |
|
64 | - in_array($key, $primaryKey); |
|
65 | - } |
|
62 | + private function isNotSavableType($value, $key, $primaryKey) { |
|
63 | + return is_array($value) || (is_object($value) && !($value instanceof \DateTime)) || |
|
64 | + in_array($key, $primaryKey); |
|
65 | + } |
|
66 | 66 | |
67 | 67 | public function alterDatabase($table, array $primaryKey, $data) { |
68 | 68 | $this->createTable($table, $primaryKey, $data); |
69 | - $this->alterColumns($table, $primaryKey, $data); |
|
69 | + $this->alterColumns($table, $primaryKey, $data); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | public function lastInsertId() { |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | |
8 | 8 | public function __construct(\PDO $pdo) { |
9 | 9 | $this->pdo = $pdo; |
10 | - $this->stmtCache = new StmtCache($pdo); |
|
10 | + $this->stmtCache = new StmtCache($pdo); |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | public function quote($str) { |
@@ -15,19 +15,19 @@ discard block |
||
15 | 15 | } |
16 | 16 | |
17 | 17 | public function query(\Maphper\Lib\Query $query) { |
18 | - $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
18 | + $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
19 | 19 | $args = $query->getArgs(); |
20 | 20 | |
21 | - //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
22 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
21 | + //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
22 | + if ($stmt === false) throw new \Exception('Invalid query'); |
|
23 | 23 | |
24 | - $stmt->execute($args); |
|
25 | - if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
24 | + $stmt->execute($args); |
|
25 | + if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
26 | 26 | $this->stmtCache->deleteQueryFromCache($query->getSql()); |
27 | 27 | return $this->query($query); |
28 | - } |
|
28 | + } |
|
29 | 29 | |
30 | - return $stmt; |
|
30 | + return $stmt; |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | public function lastInsertId() { |
@@ -63,11 +63,11 @@ discard block |
||
63 | 63 | // SQLSTATE[HY000]: General error: 17 database schema has changed |
64 | 64 | $this->stmtCache->clearCache(); |
65 | 65 | |
66 | - // Create temp table to create a new structure |
|
66 | + // Create temp table to create a new structure |
|
67 | 67 | $affix = '_'.substr(md5($table), 0, 6); |
68 | - $tempTable = $table . $affix; |
|
68 | + $tempTable = $table . $affix; |
|
69 | 69 | $this->createTable($tempTable, $primaryKey, $data); |
70 | - $this->alterColumns($tempTable, $primaryKey, $data); |
|
70 | + $this->alterColumns($tempTable, $primaryKey, $data); |
|
71 | 71 | $this->copyTableData($table, $tempTable); |
72 | 72 | |
73 | 73 | $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
@@ -75,8 +75,8 @@ discard block |
||
75 | 75 | |
76 | 76 | } |
77 | 77 | |
78 | - private function copyTableData($tableFrom, $tableTo) { |
|
79 | - try { |
|
78 | + private function copyTableData($tableFrom, $tableTo) { |
|
79 | + try { |
|
80 | 80 | if ($this->tableExists($tableFrom)) { |
81 | 81 | $columns = implode(', ', $this->getColumns($tableFrom)); |
82 | 82 | |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | // No data to copy |
88 | 88 | echo $e->getMessage(); |
89 | 89 | } |
90 | - } |
|
90 | + } |
|
91 | 91 | |
92 | 92 | private function createTable($table, array $primaryKey, $data) { |
93 | 93 | $parts = []; |
@@ -102,20 +102,20 @@ discard block |
||
102 | 102 | $this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')'); |
103 | 103 | } |
104 | 104 | |
105 | - private function alterColumns($table, array $primaryKey, $data) { |
|
106 | - foreach ($data as $key => $value) { |
|
105 | + private function alterColumns($table, array $primaryKey, $data) { |
|
106 | + foreach ($data as $key => $value) { |
|
107 | 107 | if ($this->isNotSavableType($value, $key, $primaryKey)) continue; |
108 | 108 | |
109 | 109 | $type = $this->getType($value); |
110 | 110 | |
111 | 111 | $this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type); |
112 | 112 | } |
113 | - } |
|
113 | + } |
|
114 | 114 | |
115 | - private function isNotSavableType($value, $key, $primaryKey) { |
|
116 | - return is_array($value) || (is_object($value) && !($value instanceof \DateTime)) || |
|
117 | - in_array($key, $primaryKey); |
|
118 | - } |
|
115 | + private function isNotSavableType($value, $key, $primaryKey) { |
|
116 | + return is_array($value) || (is_object($value) && !($value instanceof \DateTime)) || |
|
117 | + in_array($key, $primaryKey); |
|
118 | + } |
|
119 | 119 | |
120 | 120 | public function addIndex($table, array $fields) { |
121 | 121 | if (empty($fields)) return false; |