@@ -74,7 +74,7 @@ |
||
| 74 | 74 | |
| 75 | 75 | public function __get($name) { |
| 76 | 76 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
| 77 | - else return null; |
|
| 77 | + else return null; |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | public function __isset($name) { |
@@ -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() { |
@@ -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 | |
@@ -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; |
@@ -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 | |
@@ -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 | } |
@@ -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() { |
@@ -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 | } |
@@ -2,30 +2,30 @@ discard block |
||
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | |
| 4 | 4 | class DatabaseCrud { |
| 5 | - private $crudBuilder; |
|
| 6 | - private $whereBuilder; |
|
| 7 | - private $adapter; |
|
| 8 | - private $databaseModify; |
|
| 9 | - private $databaseSelect; |
|
| 10 | - private $table; |
|
| 11 | - private $primaryKey; |
|
| 12 | - |
|
| 13 | - public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, DatabaseSelect $databaseSelect, $table, $primaryKey) { |
|
| 14 | - $this->adapter = $adapter; |
|
| 15 | - $this->databaseModify = $databaseModify; |
|
| 16 | - $this->databaseSelect = $databaseSelect; |
|
| 17 | - $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
|
| 18 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 19 | - $this->table = $table; |
|
| 20 | - $this->primaryKey = $primaryKey; |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public function deleteById($id) { |
|
| 24 | - $this->adapter->query($this->crudBuilder->delete($this->table, [$this->primaryKey[0] . ' = :id'], [':id' => $id], 1)); |
|
| 25 | - $this->databaseSelect->deleteIDFromCache($id); |
|
| 5 | + private $crudBuilder; |
|
| 6 | + private $whereBuilder; |
|
| 7 | + private $adapter; |
|
| 8 | + private $databaseModify; |
|
| 9 | + private $databaseSelect; |
|
| 10 | + private $table; |
|
| 11 | + private $primaryKey; |
|
| 12 | + |
|
| 13 | + public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, DatabaseSelect $databaseSelect, $table, $primaryKey) { |
|
| 14 | + $this->adapter = $adapter; |
|
| 15 | + $this->databaseModify = $databaseModify; |
|
| 16 | + $this->databaseSelect = $databaseSelect; |
|
| 17 | + $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
|
| 18 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 19 | + $this->table = $table; |
|
| 20 | + $this->primaryKey = $primaryKey; |
|
| 26 | 21 | } |
| 27 | 22 | |
| 28 | - public function deleteByField(array $fields, array $options = []) { |
|
| 23 | + public function deleteById($id) { |
|
| 24 | + $this->adapter->query($this->crudBuilder->delete($this->table, [$this->primaryKey[0] . ' = :id'], [':id' => $id], 1)); |
|
| 25 | + $this->databaseSelect->deleteIDFromCache($id); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public function deleteByField(array $fields, array $options = []) { |
|
| 29 | 29 | $query = $this->whereBuilder->createSql($fields); |
| 30 | 30 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $options['limit'], null, $options['order'])); |
| 31 | 31 | $this->databaseModify->addIndex(array_keys($query['args'])); |
@@ -35,22 +35,22 @@ discard block |
||
| 35 | 35 | $this->databaseSelect->clearResultCache(); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - private function getIfNew($data) { |
|
| 39 | - $new = false; |
|
| 40 | - foreach ($this->primaryKey as $k) { |
|
| 41 | - if (empty($data->$k)) { |
|
| 42 | - $data->$k = null; |
|
| 43 | - $new = true; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - return $new; |
|
| 47 | - } |
|
| 38 | + private function getIfNew($data) { |
|
| 39 | + $new = false; |
|
| 40 | + foreach ($this->primaryKey as $k) { |
|
| 41 | + if (empty($data->$k)) { |
|
| 42 | + $data->$k = null; |
|
| 43 | + $new = true; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + return $new; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - public function save($data, $tryagain = true) { |
|
| 50 | - $new = $this->getIfNew($data); |
|
| 49 | + public function save($data, $tryagain = true) { |
|
| 50 | + $new = $this->getIfNew($data); |
|
| 51 | 51 | |
| 52 | 52 | try { |
| 53 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 53 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 54 | 54 | |
| 55 | 55 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 56 | 56 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -68,15 +68,15 @@ discard block |
||
| 68 | 68 | $this->databaseSelect->updateCache($data, $data->{$this->primaryKey[0]}); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - private function updatePK($data, $new) { |
|
| 72 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 73 | - } |
|
| 71 | + private function updatePK($data, $new) { |
|
| 72 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - private function checkIfUpdateWorked($data) { |
|
| 76 | - $updateWhere = $this->whereBuilder->createSql($data); |
|
| 77 | - $matched = $this->databaseSelect->findByField($updateWhere['args']); |
|
| 78 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 79 | - } |
|
| 75 | + private function checkIfUpdateWorked($data) { |
|
| 76 | + $updateWhere = $this->whereBuilder->createSql($data); |
|
| 77 | + $matched = $this->databaseSelect->findByField($updateWhere['args']); |
|
| 78 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | private function insert($table, array $primaryKey, $data) { |
| 82 | 82 | $error = 0; |
@@ -88,16 +88,16 @@ discard block |
||
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | if ($error || $result->errorCode() !== '00000') { |
| 91 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 92 | - } |
|
| 91 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | 94 | return $result; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
| 98 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 99 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 97 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
| 98 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 99 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 100 | 100 | |
| 101 | - return $result; |
|
| 102 | - } |
|
| 101 | + return $result; |
|
| 102 | + } |
|
| 103 | 103 | } |