@@ -18,156 +18,156 @@ |
||
| 18 | 18 | class DataService implements SingletonInterface |
| 19 | 19 | { |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @param string $tableName |
|
| 23 | - * @param array $demand |
|
| 24 | - * @return array |
|
| 25 | - */ |
|
| 26 | - public function getRecord(string $tableName, array $demand = []): array |
|
| 27 | - { |
|
| 28 | - /** @var QueryBuilder $queryBuilder */ |
|
| 29 | - $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 30 | - $queryBuilder |
|
| 31 | - ->select('*') |
|
| 32 | - ->from($tableName); |
|
| 33 | - |
|
| 34 | - $this->addDemandConstraints($demand, $queryBuilder); |
|
| 35 | - $record = $queryBuilder->execute()->fetch(); |
|
| 36 | - return is_array($record) |
|
| 37 | - ? $record |
|
| 38 | - : []; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param string $tableName |
|
| 43 | - * @param array $demand |
|
| 44 | - * @return array |
|
| 45 | - */ |
|
| 46 | - public function getRecords(string $tableName, array $demand = []): array |
|
| 47 | - { |
|
| 48 | - /** @var QueryBuilder $queryBuilder */ |
|
| 49 | - $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 50 | - $queryBuilder |
|
| 51 | - ->select('*') |
|
| 52 | - ->from($tableName); |
|
| 53 | - |
|
| 54 | - $this->addDemandConstraints($demand, $queryBuilder); |
|
| 55 | - |
|
| 56 | - return $queryBuilder->execute()->fetchAll(); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param string $tableName |
|
| 61 | - * @param array $demand |
|
| 62 | - * @return int |
|
| 63 | - */ |
|
| 64 | - public function count(string $tableName, array $demand = []): int |
|
| 65 | - { |
|
| 66 | - /** @var QueryBuilder $queryBuilder */ |
|
| 67 | - $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 68 | - $queryBuilder |
|
| 69 | - ->count('*') |
|
| 70 | - ->from($tableName); |
|
| 71 | - |
|
| 72 | - $this->addDemandConstraints($demand, $queryBuilder); |
|
| 73 | - |
|
| 74 | - return (int)$queryBuilder->execute()->fetchColumn(0); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param string $tableName |
|
| 79 | - * @param array $values |
|
| 80 | - * @return int |
|
| 81 | - */ |
|
| 82 | - public function insert(string $tableName, array $values): int |
|
| 83 | - { |
|
| 84 | - $connection = $this->getConnection($tableName); |
|
| 85 | - $connection->insert( |
|
| 86 | - $tableName, |
|
| 87 | - $values |
|
| 88 | - ); |
|
| 89 | - return (int)$connection->lastInsertId(); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param string $tableName |
|
| 94 | - * @param array $values |
|
| 95 | - * @param array $identifiers |
|
| 96 | - * @return void |
|
| 97 | - */ |
|
| 98 | - public function update(string $tableName, array $values, array $identifiers): void |
|
| 99 | - { |
|
| 100 | - $connection = $this->getConnection($tableName); |
|
| 101 | - $connection->update( |
|
| 102 | - $tableName, |
|
| 103 | - $values, |
|
| 104 | - $identifiers |
|
| 105 | - ); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param string $tableName |
|
| 110 | - * @param array $identifiers |
|
| 111 | - */ |
|
| 112 | - public function delete(string $tableName, array $identifiers): void |
|
| 113 | - { |
|
| 114 | - $connection = $this->getConnection($tableName); |
|
| 115 | - $connection->delete( |
|
| 116 | - $tableName, |
|
| 117 | - $identifiers |
|
| 118 | - ); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @param array $demand |
|
| 123 | - * @param QueryBuilder $queryBuilder |
|
| 124 | - * @return void |
|
| 125 | - */ |
|
| 126 | - protected function addDemandConstraints(array $demand, $queryBuilder): void |
|
| 127 | - { |
|
| 128 | - $expressions = []; |
|
| 129 | - foreach ($demand as $fieldName => $value) { |
|
| 130 | - if (is_numeric($value)) { |
|
| 131 | - $expressions[] = $queryBuilder->expr()->eq( |
|
| 132 | - $fieldName, |
|
| 133 | - $value |
|
| 134 | - ); |
|
| 135 | - } elseif (is_string($value)) { |
|
| 136 | - $expressions[] = $queryBuilder->expr()->eq( |
|
| 137 | - $fieldName, |
|
| 138 | - $queryBuilder->expr()->literal($value) |
|
| 139 | - ); |
|
| 140 | - } elseif (is_array($value)) { |
|
| 141 | - $expressions[] = $queryBuilder->expr()->in( |
|
| 142 | - $fieldName, |
|
| 143 | - $value |
|
| 144 | - ); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - foreach ($expressions as $expression) { |
|
| 148 | - $queryBuilder->andWhere($expression); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @param string $tableName |
|
| 154 | - * @return object|Connection |
|
| 155 | - */ |
|
| 156 | - protected function getConnection($tableName): Connection |
|
| 157 | - { |
|
| 158 | - /** @var ConnectionPool $connectionPool */ |
|
| 159 | - $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 160 | - return $connectionPool->getConnectionForTable($tableName); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param string $tableName |
|
| 165 | - * @return object|QueryBuilder |
|
| 166 | - */ |
|
| 167 | - protected function getQueryBuilder($tableName): QueryBuilder |
|
| 168 | - { |
|
| 169 | - /** @var ConnectionPool $connectionPool */ |
|
| 170 | - $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 171 | - return $connectionPool->getQueryBuilderForTable($tableName); |
|
| 172 | - } |
|
| 21 | + /** |
|
| 22 | + * @param string $tableName |
|
| 23 | + * @param array $demand |
|
| 24 | + * @return array |
|
| 25 | + */ |
|
| 26 | + public function getRecord(string $tableName, array $demand = []): array |
|
| 27 | + { |
|
| 28 | + /** @var QueryBuilder $queryBuilder */ |
|
| 29 | + $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 30 | + $queryBuilder |
|
| 31 | + ->select('*') |
|
| 32 | + ->from($tableName); |
|
| 33 | + |
|
| 34 | + $this->addDemandConstraints($demand, $queryBuilder); |
|
| 35 | + $record = $queryBuilder->execute()->fetch(); |
|
| 36 | + return is_array($record) |
|
| 37 | + ? $record |
|
| 38 | + : []; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param string $tableName |
|
| 43 | + * @param array $demand |
|
| 44 | + * @return array |
|
| 45 | + */ |
|
| 46 | + public function getRecords(string $tableName, array $demand = []): array |
|
| 47 | + { |
|
| 48 | + /** @var QueryBuilder $queryBuilder */ |
|
| 49 | + $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 50 | + $queryBuilder |
|
| 51 | + ->select('*') |
|
| 52 | + ->from($tableName); |
|
| 53 | + |
|
| 54 | + $this->addDemandConstraints($demand, $queryBuilder); |
|
| 55 | + |
|
| 56 | + return $queryBuilder->execute()->fetchAll(); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param string $tableName |
|
| 61 | + * @param array $demand |
|
| 62 | + * @return int |
|
| 63 | + */ |
|
| 64 | + public function count(string $tableName, array $demand = []): int |
|
| 65 | + { |
|
| 66 | + /** @var QueryBuilder $queryBuilder */ |
|
| 67 | + $queryBuilder = $this->getQueryBuilder($tableName); |
|
| 68 | + $queryBuilder |
|
| 69 | + ->count('*') |
|
| 70 | + ->from($tableName); |
|
| 71 | + |
|
| 72 | + $this->addDemandConstraints($demand, $queryBuilder); |
|
| 73 | + |
|
| 74 | + return (int)$queryBuilder->execute()->fetchColumn(0); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param string $tableName |
|
| 79 | + * @param array $values |
|
| 80 | + * @return int |
|
| 81 | + */ |
|
| 82 | + public function insert(string $tableName, array $values): int |
|
| 83 | + { |
|
| 84 | + $connection = $this->getConnection($tableName); |
|
| 85 | + $connection->insert( |
|
| 86 | + $tableName, |
|
| 87 | + $values |
|
| 88 | + ); |
|
| 89 | + return (int)$connection->lastInsertId(); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param string $tableName |
|
| 94 | + * @param array $values |
|
| 95 | + * @param array $identifiers |
|
| 96 | + * @return void |
|
| 97 | + */ |
|
| 98 | + public function update(string $tableName, array $values, array $identifiers): void |
|
| 99 | + { |
|
| 100 | + $connection = $this->getConnection($tableName); |
|
| 101 | + $connection->update( |
|
| 102 | + $tableName, |
|
| 103 | + $values, |
|
| 104 | + $identifiers |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param string $tableName |
|
| 110 | + * @param array $identifiers |
|
| 111 | + */ |
|
| 112 | + public function delete(string $tableName, array $identifiers): void |
|
| 113 | + { |
|
| 114 | + $connection = $this->getConnection($tableName); |
|
| 115 | + $connection->delete( |
|
| 116 | + $tableName, |
|
| 117 | + $identifiers |
|
| 118 | + ); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @param array $demand |
|
| 123 | + * @param QueryBuilder $queryBuilder |
|
| 124 | + * @return void |
|
| 125 | + */ |
|
| 126 | + protected function addDemandConstraints(array $demand, $queryBuilder): void |
|
| 127 | + { |
|
| 128 | + $expressions = []; |
|
| 129 | + foreach ($demand as $fieldName => $value) { |
|
| 130 | + if (is_numeric($value)) { |
|
| 131 | + $expressions[] = $queryBuilder->expr()->eq( |
|
| 132 | + $fieldName, |
|
| 133 | + $value |
|
| 134 | + ); |
|
| 135 | + } elseif (is_string($value)) { |
|
| 136 | + $expressions[] = $queryBuilder->expr()->eq( |
|
| 137 | + $fieldName, |
|
| 138 | + $queryBuilder->expr()->literal($value) |
|
| 139 | + ); |
|
| 140 | + } elseif (is_array($value)) { |
|
| 141 | + $expressions[] = $queryBuilder->expr()->in( |
|
| 142 | + $fieldName, |
|
| 143 | + $value |
|
| 144 | + ); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + foreach ($expressions as $expression) { |
|
| 148 | + $queryBuilder->andWhere($expression); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @param string $tableName |
|
| 154 | + * @return object|Connection |
|
| 155 | + */ |
|
| 156 | + protected function getConnection($tableName): Connection |
|
| 157 | + { |
|
| 158 | + /** @var ConnectionPool $connectionPool */ |
|
| 159 | + $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 160 | + return $connectionPool->getConnectionForTable($tableName); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param string $tableName |
|
| 165 | + * @return object|QueryBuilder |
|
| 166 | + */ |
|
| 167 | + protected function getQueryBuilder($tableName): QueryBuilder |
|
| 168 | + { |
|
| 169 | + /** @var ConnectionPool $connectionPool */ |
|
| 170 | + $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class); |
|
| 171 | + return $connectionPool->getQueryBuilderForTable($tableName); |
|
| 172 | + } |
|
| 173 | 173 | } |