@@ -29,55 +29,55 @@ |
||
29 | 29 | * @since 12.0.0 |
30 | 30 | */ |
31 | 31 | interface IFunctionBuilder { |
32 | - /** |
|
33 | - * Calculates the MD5 hash of a given input |
|
34 | - * |
|
35 | - * @param mixed $input The input to be hashed |
|
36 | - * |
|
37 | - * @return IQueryFunction |
|
38 | - * @since 12.0.0 |
|
39 | - */ |
|
40 | - public function md5($input); |
|
32 | + /** |
|
33 | + * Calculates the MD5 hash of a given input |
|
34 | + * |
|
35 | + * @param mixed $input The input to be hashed |
|
36 | + * |
|
37 | + * @return IQueryFunction |
|
38 | + * @since 12.0.0 |
|
39 | + */ |
|
40 | + public function md5($input); |
|
41 | 41 | |
42 | - /** |
|
43 | - * Combines two input strings |
|
44 | - * |
|
45 | - * @param mixed $x The first input string |
|
46 | - * @param mixed $y The seccond input string |
|
47 | - * |
|
48 | - * @return IQueryFunction |
|
49 | - * @since 12.0.0 |
|
50 | - */ |
|
51 | - public function concat($x, $y); |
|
42 | + /** |
|
43 | + * Combines two input strings |
|
44 | + * |
|
45 | + * @param mixed $x The first input string |
|
46 | + * @param mixed $y The seccond input string |
|
47 | + * |
|
48 | + * @return IQueryFunction |
|
49 | + * @since 12.0.0 |
|
50 | + */ |
|
51 | + public function concat($x, $y); |
|
52 | 52 | |
53 | - /** |
|
54 | - * Takes a substring from the input string |
|
55 | - * |
|
56 | - * @param mixed $input The input string |
|
57 | - * @param mixed $start The start of the substring, note that counting starts at 1 |
|
58 | - * @param mixed $length The length of the substring |
|
59 | - * |
|
60 | - * @return IQueryFunction |
|
61 | - * @since 12.0.0 |
|
62 | - */ |
|
63 | - public function substring($input, $start, $length = null); |
|
53 | + /** |
|
54 | + * Takes a substring from the input string |
|
55 | + * |
|
56 | + * @param mixed $input The input string |
|
57 | + * @param mixed $start The start of the substring, note that counting starts at 1 |
|
58 | + * @param mixed $length The length of the substring |
|
59 | + * |
|
60 | + * @return IQueryFunction |
|
61 | + * @since 12.0.0 |
|
62 | + */ |
|
63 | + public function substring($input, $start, $length = null); |
|
64 | 64 | |
65 | - /** |
|
66 | - * Takes the sum of all rows in a column |
|
67 | - * |
|
68 | - * @param mixed $field the column to sum |
|
69 | - * |
|
70 | - * @return IQueryFunction |
|
71 | - * @since 12.0.0 |
|
72 | - */ |
|
73 | - public function sum($field); |
|
65 | + /** |
|
66 | + * Takes the sum of all rows in a column |
|
67 | + * |
|
68 | + * @param mixed $field the column to sum |
|
69 | + * |
|
70 | + * @return IQueryFunction |
|
71 | + * @since 12.0.0 |
|
72 | + */ |
|
73 | + public function sum($field); |
|
74 | 74 | |
75 | - /** |
|
76 | - * Transforms a string field or value to lower case |
|
77 | - * |
|
78 | - * @param mixed $field |
|
79 | - * @return IQueryFunction |
|
80 | - * @since 13.0.0 |
|
81 | - */ |
|
82 | - public function lower($field); |
|
75 | + /** |
|
76 | + * Transforms a string field or value to lower case |
|
77 | + * |
|
78 | + * @param mixed $field |
|
79 | + * @return IQueryFunction |
|
80 | + * @since 13.0.0 |
|
81 | + */ |
|
82 | + public function lower($field); |
|
83 | 83 | } |
@@ -44,405 +44,405 @@ |
||
44 | 44 | use OCP\PreConditionNotMetException; |
45 | 45 | |
46 | 46 | class Connection extends \Doctrine\DBAL\Connection implements IDBConnection { |
47 | - /** |
|
48 | - * @var string $tablePrefix |
|
49 | - */ |
|
50 | - protected $tablePrefix; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var \OC\DB\Adapter $adapter |
|
54 | - */ |
|
55 | - protected $adapter; |
|
56 | - |
|
57 | - protected $lockedTable = null; |
|
58 | - |
|
59 | - public function connect() { |
|
60 | - try { |
|
61 | - return parent::connect(); |
|
62 | - } catch (DBALException $e) { |
|
63 | - // throw a new exception to prevent leaking info from the stacktrace |
|
64 | - throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Returns a QueryBuilder for the connection. |
|
70 | - * |
|
71 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
72 | - */ |
|
73 | - public function getQueryBuilder() { |
|
74 | - return new QueryBuilder( |
|
75 | - $this, |
|
76 | - \OC::$server->getSystemConfig(), |
|
77 | - \OC::$server->getLogger() |
|
78 | - ); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Gets the QueryBuilder for the connection. |
|
83 | - * |
|
84 | - * @return \Doctrine\DBAL\Query\QueryBuilder |
|
85 | - * @deprecated please use $this->getQueryBuilder() instead |
|
86 | - */ |
|
87 | - public function createQueryBuilder() { |
|
88 | - $backtrace = $this->getCallerBacktrace(); |
|
89 | - \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
90 | - return parent::createQueryBuilder(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Gets the ExpressionBuilder for the connection. |
|
95 | - * |
|
96 | - * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
97 | - * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
98 | - */ |
|
99 | - public function getExpressionBuilder() { |
|
100 | - $backtrace = $this->getCallerBacktrace(); |
|
101 | - \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
102 | - return parent::getExpressionBuilder(); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
107 | - * |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - protected function getCallerBacktrace() { |
|
111 | - $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
112 | - |
|
113 | - // 0 is the method where we use `getCallerBacktrace` |
|
114 | - // 1 is the target method which uses the method we want to log |
|
115 | - if (isset($traces[1])) { |
|
116 | - return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
117 | - } |
|
118 | - |
|
119 | - return ''; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - public function getPrefix() { |
|
126 | - return $this->tablePrefix; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Initializes a new instance of the Connection class. |
|
131 | - * |
|
132 | - * @param array $params The connection parameters. |
|
133 | - * @param \Doctrine\DBAL\Driver $driver |
|
134 | - * @param \Doctrine\DBAL\Configuration $config |
|
135 | - * @param \Doctrine\Common\EventManager $eventManager |
|
136 | - * @throws \Exception |
|
137 | - */ |
|
138 | - public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
139 | - EventManager $eventManager = null) |
|
140 | - { |
|
141 | - if (!isset($params['adapter'])) { |
|
142 | - throw new \Exception('adapter not set'); |
|
143 | - } |
|
144 | - if (!isset($params['tablePrefix'])) { |
|
145 | - throw new \Exception('tablePrefix not set'); |
|
146 | - } |
|
147 | - parent::__construct($params, $driver, $config, $eventManager); |
|
148 | - $this->adapter = new $params['adapter']($this); |
|
149 | - $this->tablePrefix = $params['tablePrefix']; |
|
150 | - |
|
151 | - parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Prepares an SQL statement. |
|
156 | - * |
|
157 | - * @param string $statement The SQL statement to prepare. |
|
158 | - * @param int $limit |
|
159 | - * @param int $offset |
|
160 | - * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
161 | - */ |
|
162 | - public function prepare( $statement, $limit=null, $offset=null ) { |
|
163 | - if ($limit === -1) { |
|
164 | - $limit = null; |
|
165 | - } |
|
166 | - if (!is_null($limit)) { |
|
167 | - $platform = $this->getDatabasePlatform(); |
|
168 | - $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
169 | - } |
|
170 | - $statement = $this->replaceTablePrefix($statement); |
|
171 | - $statement = $this->adapter->fixupStatement($statement); |
|
172 | - |
|
173 | - return parent::prepare($statement); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Executes an, optionally parametrized, SQL query. |
|
178 | - * |
|
179 | - * If the query is parametrized, a prepared statement is used. |
|
180 | - * If an SQLLogger is configured, the execution is logged. |
|
181 | - * |
|
182 | - * @param string $query The SQL query to execute. |
|
183 | - * @param array $params The parameters to bind to the query, if any. |
|
184 | - * @param array $types The types the previous parameters are in. |
|
185 | - * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
186 | - * |
|
187 | - * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
188 | - * |
|
189 | - * @throws \Doctrine\DBAL\DBALException |
|
190 | - */ |
|
191 | - public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
192 | - { |
|
193 | - $query = $this->replaceTablePrefix($query); |
|
194 | - $query = $this->adapter->fixupStatement($query); |
|
195 | - return parent::executeQuery($query, $params, $types, $qcp); |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
200 | - * and returns the number of affected rows. |
|
201 | - * |
|
202 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
203 | - * |
|
204 | - * @param string $query The SQL query. |
|
205 | - * @param array $params The query parameters. |
|
206 | - * @param array $types The parameter types. |
|
207 | - * |
|
208 | - * @return integer The number of affected rows. |
|
209 | - * |
|
210 | - * @throws \Doctrine\DBAL\DBALException |
|
211 | - */ |
|
212 | - public function executeUpdate($query, array $params = array(), array $types = array()) |
|
213 | - { |
|
214 | - $query = $this->replaceTablePrefix($query); |
|
215 | - $query = $this->adapter->fixupStatement($query); |
|
216 | - return parent::executeUpdate($query, $params, $types); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
221 | - * depending on the underlying driver. |
|
222 | - * |
|
223 | - * Note: This method may not return a meaningful or consistent result across different drivers, |
|
224 | - * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
225 | - * columns or sequences. |
|
226 | - * |
|
227 | - * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
228 | - * @return string A string representation of the last inserted ID. |
|
229 | - */ |
|
230 | - public function lastInsertId($seqName = null) { |
|
231 | - if ($seqName) { |
|
232 | - $seqName = $this->replaceTablePrefix($seqName); |
|
233 | - } |
|
234 | - return $this->adapter->lastInsertId($seqName); |
|
235 | - } |
|
236 | - |
|
237 | - // internal use |
|
238 | - public function realLastInsertId($seqName = null) { |
|
239 | - return parent::lastInsertId($seqName); |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Insert a row if the matching row does not exists. |
|
244 | - * |
|
245 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
246 | - * @param array $input data that should be inserted into the table (column name => value) |
|
247 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
248 | - * If this is null or an empty array, all keys of $input will be compared |
|
249 | - * Please note: text fields (clob) must not be used in the compare array |
|
250 | - * @return int number of inserted rows |
|
251 | - * @throws \Doctrine\DBAL\DBALException |
|
252 | - */ |
|
253 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
254 | - return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
255 | - } |
|
256 | - |
|
257 | - private function getType($value) { |
|
258 | - if (is_bool($value)) { |
|
259 | - return IQueryBuilder::PARAM_BOOL; |
|
260 | - } else if (is_int($value)) { |
|
261 | - return IQueryBuilder::PARAM_INT; |
|
262 | - } else { |
|
263 | - return IQueryBuilder::PARAM_STR; |
|
264 | - } |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Insert or update a row value |
|
269 | - * |
|
270 | - * @param string $table |
|
271 | - * @param array $keys (column name => value) |
|
272 | - * @param array $values (column name => value) |
|
273 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
274 | - * @return int number of new rows |
|
275 | - * @throws \Doctrine\DBAL\DBALException |
|
276 | - * @throws PreConditionNotMetException |
|
277 | - * @suppress SqlInjectionChecker |
|
278 | - */ |
|
279 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
280 | - try { |
|
281 | - $insertQb = $this->getQueryBuilder(); |
|
282 | - $insertQb->insert($table) |
|
283 | - ->values( |
|
284 | - array_map(function($value) use ($insertQb) { |
|
285 | - return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
286 | - }, array_merge($keys, $values)) |
|
287 | - ); |
|
288 | - return $insertQb->execute(); |
|
289 | - } catch (ConstraintViolationException $e) { |
|
290 | - // value already exists, try update |
|
291 | - $updateQb = $this->getQueryBuilder(); |
|
292 | - $updateQb->update($table); |
|
293 | - foreach ($values as $name => $value) { |
|
294 | - $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
295 | - } |
|
296 | - $where = $updateQb->expr()->andX(); |
|
297 | - $whereValues = array_merge($keys, $updatePreconditionValues); |
|
298 | - foreach ($whereValues as $name => $value) { |
|
299 | - $where->add($updateQb->expr()->eq( |
|
300 | - $name, |
|
301 | - $updateQb->createNamedParameter($value, $this->getType($value)), |
|
302 | - $this->getType($value) |
|
303 | - )); |
|
304 | - } |
|
305 | - $updateQb->where($where); |
|
306 | - $affected = $updateQb->execute(); |
|
307 | - |
|
308 | - if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
309 | - throw new PreConditionNotMetException(); |
|
310 | - } |
|
311 | - |
|
312 | - return 0; |
|
313 | - } |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Create an exclusive read+write lock on a table |
|
318 | - * |
|
319 | - * @param string $tableName |
|
320 | - * @throws \BadMethodCallException When trying to acquire a second lock |
|
321 | - * @since 9.1.0 |
|
322 | - */ |
|
323 | - public function lockTable($tableName) { |
|
324 | - if ($this->lockedTable !== null) { |
|
325 | - throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
326 | - } |
|
327 | - |
|
328 | - $tableName = $this->tablePrefix . $tableName; |
|
329 | - $this->lockedTable = $tableName; |
|
330 | - $this->adapter->lockTable($tableName); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Release a previous acquired lock again |
|
335 | - * |
|
336 | - * @since 9.1.0 |
|
337 | - */ |
|
338 | - public function unlockTable() { |
|
339 | - $this->adapter->unlockTable(); |
|
340 | - $this->lockedTable = null; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * returns the error code and message as a string for logging |
|
345 | - * works with DoctrineException |
|
346 | - * @return string |
|
347 | - */ |
|
348 | - public function getError() { |
|
349 | - $msg = $this->errorCode() . ': '; |
|
350 | - $errorInfo = $this->errorInfo(); |
|
351 | - if (is_array($errorInfo)) { |
|
352 | - $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
353 | - $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
354 | - $msg .= 'Driver Message = '.$errorInfo[2]; |
|
355 | - } |
|
356 | - return $msg; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Drop a table from the database if it exists |
|
361 | - * |
|
362 | - * @param string $table table name without the prefix |
|
363 | - */ |
|
364 | - public function dropTable($table) { |
|
365 | - $table = $this->tablePrefix . trim($table); |
|
366 | - $schema = $this->getSchemaManager(); |
|
367 | - if($schema->tablesExist(array($table))) { |
|
368 | - $schema->dropTable($table); |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Check if a table exists |
|
374 | - * |
|
375 | - * @param string $table table name without the prefix |
|
376 | - * @return bool |
|
377 | - */ |
|
378 | - public function tableExists($table){ |
|
379 | - $table = $this->tablePrefix . trim($table); |
|
380 | - $schema = $this->getSchemaManager(); |
|
381 | - return $schema->tablesExist(array($table)); |
|
382 | - } |
|
383 | - |
|
384 | - // internal use |
|
385 | - /** |
|
386 | - * @param string $statement |
|
387 | - * @return string |
|
388 | - */ |
|
389 | - protected function replaceTablePrefix($statement) { |
|
390 | - return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Check if a transaction is active |
|
395 | - * |
|
396 | - * @return bool |
|
397 | - * @since 8.2.0 |
|
398 | - */ |
|
399 | - public function inTransaction() { |
|
400 | - return $this->getTransactionNestingLevel() > 0; |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Escape a parameter to be used in a LIKE query |
|
405 | - * |
|
406 | - * @param string $param |
|
407 | - * @return string |
|
408 | - */ |
|
409 | - public function escapeLikeParameter($param) { |
|
410 | - return addcslashes($param, '\\_%'); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Check whether or not the current database support 4byte wide unicode |
|
415 | - * |
|
416 | - * @return bool |
|
417 | - * @since 11.0.0 |
|
418 | - */ |
|
419 | - public function supports4ByteText() { |
|
420 | - if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
421 | - return true; |
|
422 | - } |
|
423 | - return $this->getParams()['charset'] === 'utf8mb4'; |
|
424 | - } |
|
425 | - |
|
426 | - |
|
427 | - /** |
|
428 | - * Create the schema of the connected database |
|
429 | - * |
|
430 | - * @return Schema |
|
431 | - */ |
|
432 | - public function createSchema() { |
|
433 | - $schemaManager = new MDB2SchemaManager($this); |
|
434 | - $migrator = $schemaManager->getMigrator(); |
|
435 | - return $migrator->createSchema(); |
|
436 | - } |
|
437 | - |
|
438 | - /** |
|
439 | - * Migrate the database to the given schema |
|
440 | - * |
|
441 | - * @param Schema $toSchema |
|
442 | - */ |
|
443 | - public function migrateToSchema(Schema $toSchema) { |
|
444 | - $schemaManager = new MDB2SchemaManager($this); |
|
445 | - $migrator = $schemaManager->getMigrator(); |
|
446 | - $migrator->migrate($toSchema); |
|
447 | - } |
|
47 | + /** |
|
48 | + * @var string $tablePrefix |
|
49 | + */ |
|
50 | + protected $tablePrefix; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var \OC\DB\Adapter $adapter |
|
54 | + */ |
|
55 | + protected $adapter; |
|
56 | + |
|
57 | + protected $lockedTable = null; |
|
58 | + |
|
59 | + public function connect() { |
|
60 | + try { |
|
61 | + return parent::connect(); |
|
62 | + } catch (DBALException $e) { |
|
63 | + // throw a new exception to prevent leaking info from the stacktrace |
|
64 | + throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Returns a QueryBuilder for the connection. |
|
70 | + * |
|
71 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
72 | + */ |
|
73 | + public function getQueryBuilder() { |
|
74 | + return new QueryBuilder( |
|
75 | + $this, |
|
76 | + \OC::$server->getSystemConfig(), |
|
77 | + \OC::$server->getLogger() |
|
78 | + ); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Gets the QueryBuilder for the connection. |
|
83 | + * |
|
84 | + * @return \Doctrine\DBAL\Query\QueryBuilder |
|
85 | + * @deprecated please use $this->getQueryBuilder() instead |
|
86 | + */ |
|
87 | + public function createQueryBuilder() { |
|
88 | + $backtrace = $this->getCallerBacktrace(); |
|
89 | + \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
90 | + return parent::createQueryBuilder(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Gets the ExpressionBuilder for the connection. |
|
95 | + * |
|
96 | + * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
97 | + * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
98 | + */ |
|
99 | + public function getExpressionBuilder() { |
|
100 | + $backtrace = $this->getCallerBacktrace(); |
|
101 | + \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
102 | + return parent::getExpressionBuilder(); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
107 | + * |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + protected function getCallerBacktrace() { |
|
111 | + $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
112 | + |
|
113 | + // 0 is the method where we use `getCallerBacktrace` |
|
114 | + // 1 is the target method which uses the method we want to log |
|
115 | + if (isset($traces[1])) { |
|
116 | + return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
117 | + } |
|
118 | + |
|
119 | + return ''; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + public function getPrefix() { |
|
126 | + return $this->tablePrefix; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Initializes a new instance of the Connection class. |
|
131 | + * |
|
132 | + * @param array $params The connection parameters. |
|
133 | + * @param \Doctrine\DBAL\Driver $driver |
|
134 | + * @param \Doctrine\DBAL\Configuration $config |
|
135 | + * @param \Doctrine\Common\EventManager $eventManager |
|
136 | + * @throws \Exception |
|
137 | + */ |
|
138 | + public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
139 | + EventManager $eventManager = null) |
|
140 | + { |
|
141 | + if (!isset($params['adapter'])) { |
|
142 | + throw new \Exception('adapter not set'); |
|
143 | + } |
|
144 | + if (!isset($params['tablePrefix'])) { |
|
145 | + throw new \Exception('tablePrefix not set'); |
|
146 | + } |
|
147 | + parent::__construct($params, $driver, $config, $eventManager); |
|
148 | + $this->adapter = new $params['adapter']($this); |
|
149 | + $this->tablePrefix = $params['tablePrefix']; |
|
150 | + |
|
151 | + parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Prepares an SQL statement. |
|
156 | + * |
|
157 | + * @param string $statement The SQL statement to prepare. |
|
158 | + * @param int $limit |
|
159 | + * @param int $offset |
|
160 | + * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
161 | + */ |
|
162 | + public function prepare( $statement, $limit=null, $offset=null ) { |
|
163 | + if ($limit === -1) { |
|
164 | + $limit = null; |
|
165 | + } |
|
166 | + if (!is_null($limit)) { |
|
167 | + $platform = $this->getDatabasePlatform(); |
|
168 | + $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
169 | + } |
|
170 | + $statement = $this->replaceTablePrefix($statement); |
|
171 | + $statement = $this->adapter->fixupStatement($statement); |
|
172 | + |
|
173 | + return parent::prepare($statement); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Executes an, optionally parametrized, SQL query. |
|
178 | + * |
|
179 | + * If the query is parametrized, a prepared statement is used. |
|
180 | + * If an SQLLogger is configured, the execution is logged. |
|
181 | + * |
|
182 | + * @param string $query The SQL query to execute. |
|
183 | + * @param array $params The parameters to bind to the query, if any. |
|
184 | + * @param array $types The types the previous parameters are in. |
|
185 | + * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
186 | + * |
|
187 | + * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
188 | + * |
|
189 | + * @throws \Doctrine\DBAL\DBALException |
|
190 | + */ |
|
191 | + public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) |
|
192 | + { |
|
193 | + $query = $this->replaceTablePrefix($query); |
|
194 | + $query = $this->adapter->fixupStatement($query); |
|
195 | + return parent::executeQuery($query, $params, $types, $qcp); |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
200 | + * and returns the number of affected rows. |
|
201 | + * |
|
202 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
203 | + * |
|
204 | + * @param string $query The SQL query. |
|
205 | + * @param array $params The query parameters. |
|
206 | + * @param array $types The parameter types. |
|
207 | + * |
|
208 | + * @return integer The number of affected rows. |
|
209 | + * |
|
210 | + * @throws \Doctrine\DBAL\DBALException |
|
211 | + */ |
|
212 | + public function executeUpdate($query, array $params = array(), array $types = array()) |
|
213 | + { |
|
214 | + $query = $this->replaceTablePrefix($query); |
|
215 | + $query = $this->adapter->fixupStatement($query); |
|
216 | + return parent::executeUpdate($query, $params, $types); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
221 | + * depending on the underlying driver. |
|
222 | + * |
|
223 | + * Note: This method may not return a meaningful or consistent result across different drivers, |
|
224 | + * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
225 | + * columns or sequences. |
|
226 | + * |
|
227 | + * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
228 | + * @return string A string representation of the last inserted ID. |
|
229 | + */ |
|
230 | + public function lastInsertId($seqName = null) { |
|
231 | + if ($seqName) { |
|
232 | + $seqName = $this->replaceTablePrefix($seqName); |
|
233 | + } |
|
234 | + return $this->adapter->lastInsertId($seqName); |
|
235 | + } |
|
236 | + |
|
237 | + // internal use |
|
238 | + public function realLastInsertId($seqName = null) { |
|
239 | + return parent::lastInsertId($seqName); |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Insert a row if the matching row does not exists. |
|
244 | + * |
|
245 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
246 | + * @param array $input data that should be inserted into the table (column name => value) |
|
247 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
248 | + * If this is null or an empty array, all keys of $input will be compared |
|
249 | + * Please note: text fields (clob) must not be used in the compare array |
|
250 | + * @return int number of inserted rows |
|
251 | + * @throws \Doctrine\DBAL\DBALException |
|
252 | + */ |
|
253 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
254 | + return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
255 | + } |
|
256 | + |
|
257 | + private function getType($value) { |
|
258 | + if (is_bool($value)) { |
|
259 | + return IQueryBuilder::PARAM_BOOL; |
|
260 | + } else if (is_int($value)) { |
|
261 | + return IQueryBuilder::PARAM_INT; |
|
262 | + } else { |
|
263 | + return IQueryBuilder::PARAM_STR; |
|
264 | + } |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Insert or update a row value |
|
269 | + * |
|
270 | + * @param string $table |
|
271 | + * @param array $keys (column name => value) |
|
272 | + * @param array $values (column name => value) |
|
273 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
274 | + * @return int number of new rows |
|
275 | + * @throws \Doctrine\DBAL\DBALException |
|
276 | + * @throws PreConditionNotMetException |
|
277 | + * @suppress SqlInjectionChecker |
|
278 | + */ |
|
279 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
280 | + try { |
|
281 | + $insertQb = $this->getQueryBuilder(); |
|
282 | + $insertQb->insert($table) |
|
283 | + ->values( |
|
284 | + array_map(function($value) use ($insertQb) { |
|
285 | + return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
286 | + }, array_merge($keys, $values)) |
|
287 | + ); |
|
288 | + return $insertQb->execute(); |
|
289 | + } catch (ConstraintViolationException $e) { |
|
290 | + // value already exists, try update |
|
291 | + $updateQb = $this->getQueryBuilder(); |
|
292 | + $updateQb->update($table); |
|
293 | + foreach ($values as $name => $value) { |
|
294 | + $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
295 | + } |
|
296 | + $where = $updateQb->expr()->andX(); |
|
297 | + $whereValues = array_merge($keys, $updatePreconditionValues); |
|
298 | + foreach ($whereValues as $name => $value) { |
|
299 | + $where->add($updateQb->expr()->eq( |
|
300 | + $name, |
|
301 | + $updateQb->createNamedParameter($value, $this->getType($value)), |
|
302 | + $this->getType($value) |
|
303 | + )); |
|
304 | + } |
|
305 | + $updateQb->where($where); |
|
306 | + $affected = $updateQb->execute(); |
|
307 | + |
|
308 | + if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
309 | + throw new PreConditionNotMetException(); |
|
310 | + } |
|
311 | + |
|
312 | + return 0; |
|
313 | + } |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Create an exclusive read+write lock on a table |
|
318 | + * |
|
319 | + * @param string $tableName |
|
320 | + * @throws \BadMethodCallException When trying to acquire a second lock |
|
321 | + * @since 9.1.0 |
|
322 | + */ |
|
323 | + public function lockTable($tableName) { |
|
324 | + if ($this->lockedTable !== null) { |
|
325 | + throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
326 | + } |
|
327 | + |
|
328 | + $tableName = $this->tablePrefix . $tableName; |
|
329 | + $this->lockedTable = $tableName; |
|
330 | + $this->adapter->lockTable($tableName); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Release a previous acquired lock again |
|
335 | + * |
|
336 | + * @since 9.1.0 |
|
337 | + */ |
|
338 | + public function unlockTable() { |
|
339 | + $this->adapter->unlockTable(); |
|
340 | + $this->lockedTable = null; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * returns the error code and message as a string for logging |
|
345 | + * works with DoctrineException |
|
346 | + * @return string |
|
347 | + */ |
|
348 | + public function getError() { |
|
349 | + $msg = $this->errorCode() . ': '; |
|
350 | + $errorInfo = $this->errorInfo(); |
|
351 | + if (is_array($errorInfo)) { |
|
352 | + $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
353 | + $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
354 | + $msg .= 'Driver Message = '.$errorInfo[2]; |
|
355 | + } |
|
356 | + return $msg; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Drop a table from the database if it exists |
|
361 | + * |
|
362 | + * @param string $table table name without the prefix |
|
363 | + */ |
|
364 | + public function dropTable($table) { |
|
365 | + $table = $this->tablePrefix . trim($table); |
|
366 | + $schema = $this->getSchemaManager(); |
|
367 | + if($schema->tablesExist(array($table))) { |
|
368 | + $schema->dropTable($table); |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Check if a table exists |
|
374 | + * |
|
375 | + * @param string $table table name without the prefix |
|
376 | + * @return bool |
|
377 | + */ |
|
378 | + public function tableExists($table){ |
|
379 | + $table = $this->tablePrefix . trim($table); |
|
380 | + $schema = $this->getSchemaManager(); |
|
381 | + return $schema->tablesExist(array($table)); |
|
382 | + } |
|
383 | + |
|
384 | + // internal use |
|
385 | + /** |
|
386 | + * @param string $statement |
|
387 | + * @return string |
|
388 | + */ |
|
389 | + protected function replaceTablePrefix($statement) { |
|
390 | + return str_replace( '*PREFIX*', $this->tablePrefix, $statement ); |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Check if a transaction is active |
|
395 | + * |
|
396 | + * @return bool |
|
397 | + * @since 8.2.0 |
|
398 | + */ |
|
399 | + public function inTransaction() { |
|
400 | + return $this->getTransactionNestingLevel() > 0; |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Escape a parameter to be used in a LIKE query |
|
405 | + * |
|
406 | + * @param string $param |
|
407 | + * @return string |
|
408 | + */ |
|
409 | + public function escapeLikeParameter($param) { |
|
410 | + return addcslashes($param, '\\_%'); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Check whether or not the current database support 4byte wide unicode |
|
415 | + * |
|
416 | + * @return bool |
|
417 | + * @since 11.0.0 |
|
418 | + */ |
|
419 | + public function supports4ByteText() { |
|
420 | + if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
421 | + return true; |
|
422 | + } |
|
423 | + return $this->getParams()['charset'] === 'utf8mb4'; |
|
424 | + } |
|
425 | + |
|
426 | + |
|
427 | + /** |
|
428 | + * Create the schema of the connected database |
|
429 | + * |
|
430 | + * @return Schema |
|
431 | + */ |
|
432 | + public function createSchema() { |
|
433 | + $schemaManager = new MDB2SchemaManager($this); |
|
434 | + $migrator = $schemaManager->getMigrator(); |
|
435 | + return $migrator->createSchema(); |
|
436 | + } |
|
437 | + |
|
438 | + /** |
|
439 | + * Migrate the database to the given schema |
|
440 | + * |
|
441 | + * @param Schema $toSchema |
|
442 | + */ |
|
443 | + public function migrateToSchema(Schema $toSchema) { |
|
444 | + $schemaManager = new MDB2SchemaManager($this); |
|
445 | + $migrator = $schemaManager->getMigrator(); |
|
446 | + $migrator->migrate($toSchema); |
|
447 | + } |
|
448 | 448 | } |
@@ -37,397 +37,397 @@ |
||
37 | 37 | use OCP\IDBConnection; |
38 | 38 | |
39 | 39 | class ExpressionBuilder implements IExpressionBuilder { |
40 | - /** @var \Doctrine\DBAL\Query\Expression\ExpressionBuilder */ |
|
41 | - protected $expressionBuilder; |
|
40 | + /** @var \Doctrine\DBAL\Query\Expression\ExpressionBuilder */ |
|
41 | + protected $expressionBuilder; |
|
42 | 42 | |
43 | - /** @var QuoteHelper */ |
|
44 | - protected $helper; |
|
43 | + /** @var QuoteHelper */ |
|
44 | + protected $helper; |
|
45 | 45 | |
46 | - /** @var IDBConnection */ |
|
47 | - protected $connection; |
|
46 | + /** @var IDBConnection */ |
|
47 | + protected $connection; |
|
48 | 48 | |
49 | - /** @var FunctionBuilder */ |
|
50 | - protected $functionBuilder; |
|
49 | + /** @var FunctionBuilder */ |
|
50 | + protected $functionBuilder; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Initializes a new <tt>ExpressionBuilder</tt>. |
|
54 | - * |
|
55 | - * @param \OCP\IDBConnection $connection |
|
56 | - */ |
|
57 | - public function __construct(IDBConnection $connection) { |
|
58 | - $this->connection = $connection; |
|
59 | - $this->helper = new QuoteHelper(); |
|
60 | - $this->expressionBuilder = new DoctrineExpressionBuilder($connection); |
|
61 | - $this->functionBuilder = $connection->getQueryBuilder()->func(); |
|
62 | - } |
|
52 | + /** |
|
53 | + * Initializes a new <tt>ExpressionBuilder</tt>. |
|
54 | + * |
|
55 | + * @param \OCP\IDBConnection $connection |
|
56 | + */ |
|
57 | + public function __construct(IDBConnection $connection) { |
|
58 | + $this->connection = $connection; |
|
59 | + $this->helper = new QuoteHelper(); |
|
60 | + $this->expressionBuilder = new DoctrineExpressionBuilder($connection); |
|
61 | + $this->functionBuilder = $connection->getQueryBuilder()->func(); |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Creates a conjunction of the given boolean expressions. |
|
66 | - * |
|
67 | - * Example: |
|
68 | - * |
|
69 | - * [php] |
|
70 | - * // (u.type = ?) AND (u.role = ?) |
|
71 | - * $expr->andX('u.type = ?', 'u.role = ?')); |
|
72 | - * |
|
73 | - * @param mixed $x Optional clause. Defaults = null, but requires |
|
74 | - * at least one defined when converting to string. |
|
75 | - * |
|
76 | - * @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
77 | - */ |
|
78 | - public function andX($x = null) { |
|
79 | - $arguments = func_get_args(); |
|
80 | - $compositeExpression = call_user_func_array([$this->expressionBuilder, 'andX'], $arguments); |
|
81 | - return new CompositeExpression($compositeExpression); |
|
82 | - } |
|
64 | + /** |
|
65 | + * Creates a conjunction of the given boolean expressions. |
|
66 | + * |
|
67 | + * Example: |
|
68 | + * |
|
69 | + * [php] |
|
70 | + * // (u.type = ?) AND (u.role = ?) |
|
71 | + * $expr->andX('u.type = ?', 'u.role = ?')); |
|
72 | + * |
|
73 | + * @param mixed $x Optional clause. Defaults = null, but requires |
|
74 | + * at least one defined when converting to string. |
|
75 | + * |
|
76 | + * @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
77 | + */ |
|
78 | + public function andX($x = null) { |
|
79 | + $arguments = func_get_args(); |
|
80 | + $compositeExpression = call_user_func_array([$this->expressionBuilder, 'andX'], $arguments); |
|
81 | + return new CompositeExpression($compositeExpression); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Creates a disjunction of the given boolean expressions. |
|
86 | - * |
|
87 | - * Example: |
|
88 | - * |
|
89 | - * [php] |
|
90 | - * // (u.type = ?) OR (u.role = ?) |
|
91 | - * $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?')); |
|
92 | - * |
|
93 | - * @param mixed $x Optional clause. Defaults = null, but requires |
|
94 | - * at least one defined when converting to string. |
|
95 | - * |
|
96 | - * @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
97 | - */ |
|
98 | - public function orX($x = null) { |
|
99 | - $arguments = func_get_args(); |
|
100 | - $compositeExpression = call_user_func_array([$this->expressionBuilder, 'orX'], $arguments); |
|
101 | - return new CompositeExpression($compositeExpression); |
|
102 | - } |
|
84 | + /** |
|
85 | + * Creates a disjunction of the given boolean expressions. |
|
86 | + * |
|
87 | + * Example: |
|
88 | + * |
|
89 | + * [php] |
|
90 | + * // (u.type = ?) OR (u.role = ?) |
|
91 | + * $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?')); |
|
92 | + * |
|
93 | + * @param mixed $x Optional clause. Defaults = null, but requires |
|
94 | + * at least one defined when converting to string. |
|
95 | + * |
|
96 | + * @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
97 | + */ |
|
98 | + public function orX($x = null) { |
|
99 | + $arguments = func_get_args(); |
|
100 | + $compositeExpression = call_user_func_array([$this->expressionBuilder, 'orX'], $arguments); |
|
101 | + return new CompositeExpression($compositeExpression); |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * Creates a comparison expression. |
|
106 | - * |
|
107 | - * @param mixed $x The left expression. |
|
108 | - * @param string $operator One of the IExpressionBuilder::* constants. |
|
109 | - * @param mixed $y The right expression. |
|
110 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
111 | - * required when comparing text fields for oci compatibility |
|
112 | - * |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - public function comparison($x, $operator, $y, $type = null) { |
|
116 | - $x = $this->helper->quoteColumnName($x); |
|
117 | - $y = $this->helper->quoteColumnName($y); |
|
118 | - return $this->expressionBuilder->comparison($x, $operator, $y); |
|
119 | - } |
|
104 | + /** |
|
105 | + * Creates a comparison expression. |
|
106 | + * |
|
107 | + * @param mixed $x The left expression. |
|
108 | + * @param string $operator One of the IExpressionBuilder::* constants. |
|
109 | + * @param mixed $y The right expression. |
|
110 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
111 | + * required when comparing text fields for oci compatibility |
|
112 | + * |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + public function comparison($x, $operator, $y, $type = null) { |
|
116 | + $x = $this->helper->quoteColumnName($x); |
|
117 | + $y = $this->helper->quoteColumnName($y); |
|
118 | + return $this->expressionBuilder->comparison($x, $operator, $y); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Creates an equality comparison expression with the given arguments. |
|
123 | - * |
|
124 | - * First argument is considered the left expression and the second is the right expression. |
|
125 | - * When converted to string, it will generated a <left expr> = <right expr>. Example: |
|
126 | - * |
|
127 | - * [php] |
|
128 | - * // u.id = ? |
|
129 | - * $expr->eq('u.id', '?'); |
|
130 | - * |
|
131 | - * @param mixed $x The left expression. |
|
132 | - * @param mixed $y The right expression. |
|
133 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
134 | - * required when comparing text fields for oci compatibility |
|
135 | - * |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - public function eq($x, $y, $type = null) { |
|
139 | - $x = $this->helper->quoteColumnName($x); |
|
140 | - $y = $this->helper->quoteColumnName($y); |
|
141 | - return $this->expressionBuilder->eq($x, $y); |
|
142 | - } |
|
121 | + /** |
|
122 | + * Creates an equality comparison expression with the given arguments. |
|
123 | + * |
|
124 | + * First argument is considered the left expression and the second is the right expression. |
|
125 | + * When converted to string, it will generated a <left expr> = <right expr>. Example: |
|
126 | + * |
|
127 | + * [php] |
|
128 | + * // u.id = ? |
|
129 | + * $expr->eq('u.id', '?'); |
|
130 | + * |
|
131 | + * @param mixed $x The left expression. |
|
132 | + * @param mixed $y The right expression. |
|
133 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
134 | + * required when comparing text fields for oci compatibility |
|
135 | + * |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + public function eq($x, $y, $type = null) { |
|
139 | + $x = $this->helper->quoteColumnName($x); |
|
140 | + $y = $this->helper->quoteColumnName($y); |
|
141 | + return $this->expressionBuilder->eq($x, $y); |
|
142 | + } |
|
143 | 143 | |
144 | - /** |
|
145 | - * Creates a non equality comparison expression with the given arguments. |
|
146 | - * First argument is considered the left expression and the second is the right expression. |
|
147 | - * When converted to string, it will generated a <left expr> <> <right expr>. Example: |
|
148 | - * |
|
149 | - * [php] |
|
150 | - * // u.id <> 1 |
|
151 | - * $q->where($q->expr()->neq('u.id', '1')); |
|
152 | - * |
|
153 | - * @param mixed $x The left expression. |
|
154 | - * @param mixed $y The right expression. |
|
155 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
156 | - * required when comparing text fields for oci compatibility |
|
157 | - * |
|
158 | - * @return string |
|
159 | - */ |
|
160 | - public function neq($x, $y, $type = null) { |
|
161 | - $x = $this->helper->quoteColumnName($x); |
|
162 | - $y = $this->helper->quoteColumnName($y); |
|
163 | - return $this->expressionBuilder->neq($x, $y); |
|
164 | - } |
|
144 | + /** |
|
145 | + * Creates a non equality comparison expression with the given arguments. |
|
146 | + * First argument is considered the left expression and the second is the right expression. |
|
147 | + * When converted to string, it will generated a <left expr> <> <right expr>. Example: |
|
148 | + * |
|
149 | + * [php] |
|
150 | + * // u.id <> 1 |
|
151 | + * $q->where($q->expr()->neq('u.id', '1')); |
|
152 | + * |
|
153 | + * @param mixed $x The left expression. |
|
154 | + * @param mixed $y The right expression. |
|
155 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
156 | + * required when comparing text fields for oci compatibility |
|
157 | + * |
|
158 | + * @return string |
|
159 | + */ |
|
160 | + public function neq($x, $y, $type = null) { |
|
161 | + $x = $this->helper->quoteColumnName($x); |
|
162 | + $y = $this->helper->quoteColumnName($y); |
|
163 | + return $this->expressionBuilder->neq($x, $y); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * Creates a lower-than comparison expression with the given arguments. |
|
168 | - * First argument is considered the left expression and the second is the right expression. |
|
169 | - * When converted to string, it will generated a <left expr> < <right expr>. Example: |
|
170 | - * |
|
171 | - * [php] |
|
172 | - * // u.id < ? |
|
173 | - * $q->where($q->expr()->lt('u.id', '?')); |
|
174 | - * |
|
175 | - * @param mixed $x The left expression. |
|
176 | - * @param mixed $y The right expression. |
|
177 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
178 | - * required when comparing text fields for oci compatibility |
|
179 | - * |
|
180 | - * @return string |
|
181 | - */ |
|
182 | - public function lt($x, $y, $type = null) { |
|
183 | - $x = $this->helper->quoteColumnName($x); |
|
184 | - $y = $this->helper->quoteColumnName($y); |
|
185 | - return $this->expressionBuilder->lt($x, $y); |
|
186 | - } |
|
166 | + /** |
|
167 | + * Creates a lower-than comparison expression with the given arguments. |
|
168 | + * First argument is considered the left expression and the second is the right expression. |
|
169 | + * When converted to string, it will generated a <left expr> < <right expr>. Example: |
|
170 | + * |
|
171 | + * [php] |
|
172 | + * // u.id < ? |
|
173 | + * $q->where($q->expr()->lt('u.id', '?')); |
|
174 | + * |
|
175 | + * @param mixed $x The left expression. |
|
176 | + * @param mixed $y The right expression. |
|
177 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
178 | + * required when comparing text fields for oci compatibility |
|
179 | + * |
|
180 | + * @return string |
|
181 | + */ |
|
182 | + public function lt($x, $y, $type = null) { |
|
183 | + $x = $this->helper->quoteColumnName($x); |
|
184 | + $y = $this->helper->quoteColumnName($y); |
|
185 | + return $this->expressionBuilder->lt($x, $y); |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * Creates a lower-than-equal comparison expression with the given arguments. |
|
190 | - * First argument is considered the left expression and the second is the right expression. |
|
191 | - * When converted to string, it will generated a <left expr> <= <right expr>. Example: |
|
192 | - * |
|
193 | - * [php] |
|
194 | - * // u.id <= ? |
|
195 | - * $q->where($q->expr()->lte('u.id', '?')); |
|
196 | - * |
|
197 | - * @param mixed $x The left expression. |
|
198 | - * @param mixed $y The right expression. |
|
199 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
200 | - * required when comparing text fields for oci compatibility |
|
201 | - * |
|
202 | - * @return string |
|
203 | - */ |
|
204 | - public function lte($x, $y, $type = null) { |
|
205 | - $x = $this->helper->quoteColumnName($x); |
|
206 | - $y = $this->helper->quoteColumnName($y); |
|
207 | - return $this->expressionBuilder->lte($x, $y); |
|
208 | - } |
|
188 | + /** |
|
189 | + * Creates a lower-than-equal comparison expression with the given arguments. |
|
190 | + * First argument is considered the left expression and the second is the right expression. |
|
191 | + * When converted to string, it will generated a <left expr> <= <right expr>. Example: |
|
192 | + * |
|
193 | + * [php] |
|
194 | + * // u.id <= ? |
|
195 | + * $q->where($q->expr()->lte('u.id', '?')); |
|
196 | + * |
|
197 | + * @param mixed $x The left expression. |
|
198 | + * @param mixed $y The right expression. |
|
199 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
200 | + * required when comparing text fields for oci compatibility |
|
201 | + * |
|
202 | + * @return string |
|
203 | + */ |
|
204 | + public function lte($x, $y, $type = null) { |
|
205 | + $x = $this->helper->quoteColumnName($x); |
|
206 | + $y = $this->helper->quoteColumnName($y); |
|
207 | + return $this->expressionBuilder->lte($x, $y); |
|
208 | + } |
|
209 | 209 | |
210 | - /** |
|
211 | - * Creates a greater-than comparison expression with the given arguments. |
|
212 | - * First argument is considered the left expression and the second is the right expression. |
|
213 | - * When converted to string, it will generated a <left expr> > <right expr>. Example: |
|
214 | - * |
|
215 | - * [php] |
|
216 | - * // u.id > ? |
|
217 | - * $q->where($q->expr()->gt('u.id', '?')); |
|
218 | - * |
|
219 | - * @param mixed $x The left expression. |
|
220 | - * @param mixed $y The right expression. |
|
221 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
222 | - * required when comparing text fields for oci compatibility |
|
223 | - * |
|
224 | - * @return string |
|
225 | - */ |
|
226 | - public function gt($x, $y, $type = null) { |
|
227 | - $x = $this->helper->quoteColumnName($x); |
|
228 | - $y = $this->helper->quoteColumnName($y); |
|
229 | - return $this->expressionBuilder->gt($x, $y); |
|
230 | - } |
|
210 | + /** |
|
211 | + * Creates a greater-than comparison expression with the given arguments. |
|
212 | + * First argument is considered the left expression and the second is the right expression. |
|
213 | + * When converted to string, it will generated a <left expr> > <right expr>. Example: |
|
214 | + * |
|
215 | + * [php] |
|
216 | + * // u.id > ? |
|
217 | + * $q->where($q->expr()->gt('u.id', '?')); |
|
218 | + * |
|
219 | + * @param mixed $x The left expression. |
|
220 | + * @param mixed $y The right expression. |
|
221 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
222 | + * required when comparing text fields for oci compatibility |
|
223 | + * |
|
224 | + * @return string |
|
225 | + */ |
|
226 | + public function gt($x, $y, $type = null) { |
|
227 | + $x = $this->helper->quoteColumnName($x); |
|
228 | + $y = $this->helper->quoteColumnName($y); |
|
229 | + return $this->expressionBuilder->gt($x, $y); |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Creates a greater-than-equal comparison expression with the given arguments. |
|
234 | - * First argument is considered the left expression and the second is the right expression. |
|
235 | - * When converted to string, it will generated a <left expr> >= <right expr>. Example: |
|
236 | - * |
|
237 | - * [php] |
|
238 | - * // u.id >= ? |
|
239 | - * $q->where($q->expr()->gte('u.id', '?')); |
|
240 | - * |
|
241 | - * @param mixed $x The left expression. |
|
242 | - * @param mixed $y The right expression. |
|
243 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
244 | - * required when comparing text fields for oci compatibility |
|
245 | - * |
|
246 | - * @return string |
|
247 | - */ |
|
248 | - public function gte($x, $y, $type = null) { |
|
249 | - $x = $this->helper->quoteColumnName($x); |
|
250 | - $y = $this->helper->quoteColumnName($y); |
|
251 | - return $this->expressionBuilder->gte($x, $y); |
|
252 | - } |
|
232 | + /** |
|
233 | + * Creates a greater-than-equal comparison expression with the given arguments. |
|
234 | + * First argument is considered the left expression and the second is the right expression. |
|
235 | + * When converted to string, it will generated a <left expr> >= <right expr>. Example: |
|
236 | + * |
|
237 | + * [php] |
|
238 | + * // u.id >= ? |
|
239 | + * $q->where($q->expr()->gte('u.id', '?')); |
|
240 | + * |
|
241 | + * @param mixed $x The left expression. |
|
242 | + * @param mixed $y The right expression. |
|
243 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
244 | + * required when comparing text fields for oci compatibility |
|
245 | + * |
|
246 | + * @return string |
|
247 | + */ |
|
248 | + public function gte($x, $y, $type = null) { |
|
249 | + $x = $this->helper->quoteColumnName($x); |
|
250 | + $y = $this->helper->quoteColumnName($y); |
|
251 | + return $this->expressionBuilder->gte($x, $y); |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Creates an IS NULL expression with the given arguments. |
|
256 | - * |
|
257 | - * @param string $x The field in string format to be restricted by IS NULL. |
|
258 | - * |
|
259 | - * @return string |
|
260 | - */ |
|
261 | - public function isNull($x) { |
|
262 | - $x = $this->helper->quoteColumnName($x); |
|
263 | - return $this->expressionBuilder->isNull($x); |
|
264 | - } |
|
254 | + /** |
|
255 | + * Creates an IS NULL expression with the given arguments. |
|
256 | + * |
|
257 | + * @param string $x The field in string format to be restricted by IS NULL. |
|
258 | + * |
|
259 | + * @return string |
|
260 | + */ |
|
261 | + public function isNull($x) { |
|
262 | + $x = $this->helper->quoteColumnName($x); |
|
263 | + return $this->expressionBuilder->isNull($x); |
|
264 | + } |
|
265 | 265 | |
266 | - /** |
|
267 | - * Creates an IS NOT NULL expression with the given arguments. |
|
268 | - * |
|
269 | - * @param string $x The field in string format to be restricted by IS NOT NULL. |
|
270 | - * |
|
271 | - * @return string |
|
272 | - */ |
|
273 | - public function isNotNull($x) { |
|
274 | - $x = $this->helper->quoteColumnName($x); |
|
275 | - return $this->expressionBuilder->isNotNull($x); |
|
276 | - } |
|
266 | + /** |
|
267 | + * Creates an IS NOT NULL expression with the given arguments. |
|
268 | + * |
|
269 | + * @param string $x The field in string format to be restricted by IS NOT NULL. |
|
270 | + * |
|
271 | + * @return string |
|
272 | + */ |
|
273 | + public function isNotNull($x) { |
|
274 | + $x = $this->helper->quoteColumnName($x); |
|
275 | + return $this->expressionBuilder->isNotNull($x); |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * Creates a LIKE() comparison expression with the given arguments. |
|
280 | - * |
|
281 | - * @param string $x Field in string format to be inspected by LIKE() comparison. |
|
282 | - * @param mixed $y Argument to be used in LIKE() comparison. |
|
283 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
284 | - * required when comparing text fields for oci compatibility |
|
285 | - * |
|
286 | - * @return string |
|
287 | - */ |
|
288 | - public function like($x, $y, $type = null) { |
|
289 | - $x = $this->helper->quoteColumnName($x); |
|
290 | - $y = $this->helper->quoteColumnName($y); |
|
291 | - return $this->expressionBuilder->like($x, $y); |
|
292 | - } |
|
278 | + /** |
|
279 | + * Creates a LIKE() comparison expression with the given arguments. |
|
280 | + * |
|
281 | + * @param string $x Field in string format to be inspected by LIKE() comparison. |
|
282 | + * @param mixed $y Argument to be used in LIKE() comparison. |
|
283 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
284 | + * required when comparing text fields for oci compatibility |
|
285 | + * |
|
286 | + * @return string |
|
287 | + */ |
|
288 | + public function like($x, $y, $type = null) { |
|
289 | + $x = $this->helper->quoteColumnName($x); |
|
290 | + $y = $this->helper->quoteColumnName($y); |
|
291 | + return $this->expressionBuilder->like($x, $y); |
|
292 | + } |
|
293 | 293 | |
294 | - /** |
|
295 | - * Creates a ILIKE() comparison expression with the given arguments. |
|
296 | - * |
|
297 | - * @param string $x Field in string format to be inspected by ILIKE() comparison. |
|
298 | - * @param mixed $y Argument to be used in ILIKE() comparison. |
|
299 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
300 | - * required when comparing text fields for oci compatibility |
|
301 | - * |
|
302 | - * @return string |
|
303 | - * @since 9.0.0 |
|
304 | - */ |
|
305 | - public function iLike($x, $y, $type = null) { |
|
306 | - return $this->expressionBuilder->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y)); |
|
307 | - } |
|
294 | + /** |
|
295 | + * Creates a ILIKE() comparison expression with the given arguments. |
|
296 | + * |
|
297 | + * @param string $x Field in string format to be inspected by ILIKE() comparison. |
|
298 | + * @param mixed $y Argument to be used in ILIKE() comparison. |
|
299 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
300 | + * required when comparing text fields for oci compatibility |
|
301 | + * |
|
302 | + * @return string |
|
303 | + * @since 9.0.0 |
|
304 | + */ |
|
305 | + public function iLike($x, $y, $type = null) { |
|
306 | + return $this->expressionBuilder->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y)); |
|
307 | + } |
|
308 | 308 | |
309 | - /** |
|
310 | - * Creates a NOT LIKE() comparison expression with the given arguments. |
|
311 | - * |
|
312 | - * @param string $x Field in string format to be inspected by NOT LIKE() comparison. |
|
313 | - * @param mixed $y Argument to be used in NOT LIKE() comparison. |
|
314 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
315 | - * required when comparing text fields for oci compatibility |
|
316 | - * |
|
317 | - * @return string |
|
318 | - */ |
|
319 | - public function notLike($x, $y, $type = null) { |
|
320 | - $x = $this->helper->quoteColumnName($x); |
|
321 | - $y = $this->helper->quoteColumnName($y); |
|
322 | - return $this->expressionBuilder->notLike($x, $y); |
|
323 | - } |
|
309 | + /** |
|
310 | + * Creates a NOT LIKE() comparison expression with the given arguments. |
|
311 | + * |
|
312 | + * @param string $x Field in string format to be inspected by NOT LIKE() comparison. |
|
313 | + * @param mixed $y Argument to be used in NOT LIKE() comparison. |
|
314 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
315 | + * required when comparing text fields for oci compatibility |
|
316 | + * |
|
317 | + * @return string |
|
318 | + */ |
|
319 | + public function notLike($x, $y, $type = null) { |
|
320 | + $x = $this->helper->quoteColumnName($x); |
|
321 | + $y = $this->helper->quoteColumnName($y); |
|
322 | + return $this->expressionBuilder->notLike($x, $y); |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * Creates a IN () comparison expression with the given arguments. |
|
327 | - * |
|
328 | - * @param string $x The field in string format to be inspected by IN() comparison. |
|
329 | - * @param string|array $y The placeholder or the array of values to be used by IN() comparison. |
|
330 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
331 | - * required when comparing text fields for oci compatibility |
|
332 | - * |
|
333 | - * @return string |
|
334 | - */ |
|
335 | - public function in($x, $y, $type = null) { |
|
336 | - $x = $this->helper->quoteColumnName($x); |
|
337 | - $y = $this->helper->quoteColumnNames($y); |
|
338 | - return $this->expressionBuilder->in($x, $y); |
|
339 | - } |
|
325 | + /** |
|
326 | + * Creates a IN () comparison expression with the given arguments. |
|
327 | + * |
|
328 | + * @param string $x The field in string format to be inspected by IN() comparison. |
|
329 | + * @param string|array $y The placeholder or the array of values to be used by IN() comparison. |
|
330 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
331 | + * required when comparing text fields for oci compatibility |
|
332 | + * |
|
333 | + * @return string |
|
334 | + */ |
|
335 | + public function in($x, $y, $type = null) { |
|
336 | + $x = $this->helper->quoteColumnName($x); |
|
337 | + $y = $this->helper->quoteColumnNames($y); |
|
338 | + return $this->expressionBuilder->in($x, $y); |
|
339 | + } |
|
340 | 340 | |
341 | - /** |
|
342 | - * Creates a NOT IN () comparison expression with the given arguments. |
|
343 | - * |
|
344 | - * @param string $x The field in string format to be inspected by NOT IN() comparison. |
|
345 | - * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison. |
|
346 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
347 | - * required when comparing text fields for oci compatibility |
|
348 | - * |
|
349 | - * @return string |
|
350 | - */ |
|
351 | - public function notIn($x, $y, $type = null) { |
|
352 | - $x = $this->helper->quoteColumnName($x); |
|
353 | - $y = $this->helper->quoteColumnNames($y); |
|
354 | - return $this->expressionBuilder->notIn($x, $y); |
|
355 | - } |
|
341 | + /** |
|
342 | + * Creates a NOT IN () comparison expression with the given arguments. |
|
343 | + * |
|
344 | + * @param string $x The field in string format to be inspected by NOT IN() comparison. |
|
345 | + * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison. |
|
346 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
347 | + * required when comparing text fields for oci compatibility |
|
348 | + * |
|
349 | + * @return string |
|
350 | + */ |
|
351 | + public function notIn($x, $y, $type = null) { |
|
352 | + $x = $this->helper->quoteColumnName($x); |
|
353 | + $y = $this->helper->quoteColumnNames($y); |
|
354 | + return $this->expressionBuilder->notIn($x, $y); |
|
355 | + } |
|
356 | 356 | |
357 | - /** |
|
358 | - * Creates a $x = '' statement, because Oracle needs a different check |
|
359 | - * |
|
360 | - * @param string $x The field in string format to be inspected by the comparison. |
|
361 | - * @return string |
|
362 | - * @since 13.0.0 |
|
363 | - */ |
|
364 | - public function emptyString($x) { |
|
365 | - return $this->eq($x, $this->literal('', IQueryBuilder::PARAM_STR)); |
|
366 | - } |
|
357 | + /** |
|
358 | + * Creates a $x = '' statement, because Oracle needs a different check |
|
359 | + * |
|
360 | + * @param string $x The field in string format to be inspected by the comparison. |
|
361 | + * @return string |
|
362 | + * @since 13.0.0 |
|
363 | + */ |
|
364 | + public function emptyString($x) { |
|
365 | + return $this->eq($x, $this->literal('', IQueryBuilder::PARAM_STR)); |
|
366 | + } |
|
367 | 367 | |
368 | - /** |
|
369 | - * Creates a `$x <> ''` statement, because Oracle needs a different check |
|
370 | - * |
|
371 | - * @param string $x The field in string format to be inspected by the comparison. |
|
372 | - * @return string |
|
373 | - * @since 13.0.0 |
|
374 | - */ |
|
375 | - public function nonEmptyString($x) { |
|
376 | - return $this->neq($x, $this->literal('', IQueryBuilder::PARAM_STR)); |
|
377 | - } |
|
368 | + /** |
|
369 | + * Creates a `$x <> ''` statement, because Oracle needs a different check |
|
370 | + * |
|
371 | + * @param string $x The field in string format to be inspected by the comparison. |
|
372 | + * @return string |
|
373 | + * @since 13.0.0 |
|
374 | + */ |
|
375 | + public function nonEmptyString($x) { |
|
376 | + return $this->neq($x, $this->literal('', IQueryBuilder::PARAM_STR)); |
|
377 | + } |
|
378 | 378 | |
379 | - /** |
|
380 | - * Binary AND Operator copies a bit to the result if it exists in both operands. |
|
381 | - * |
|
382 | - * @param string|ILiteral $x The field or value to check |
|
383 | - * @param int $y Bitmap that must be set |
|
384 | - * @return IQueryFunction |
|
385 | - * @since 12.0.0 |
|
386 | - */ |
|
387 | - public function bitwiseAnd($x, $y) { |
|
388 | - return new QueryFunction($this->connection->getDatabasePlatform()->getBitAndComparisonExpression( |
|
389 | - $this->helper->quoteColumnName($x), |
|
390 | - $y |
|
391 | - )); |
|
392 | - } |
|
379 | + /** |
|
380 | + * Binary AND Operator copies a bit to the result if it exists in both operands. |
|
381 | + * |
|
382 | + * @param string|ILiteral $x The field or value to check |
|
383 | + * @param int $y Bitmap that must be set |
|
384 | + * @return IQueryFunction |
|
385 | + * @since 12.0.0 |
|
386 | + */ |
|
387 | + public function bitwiseAnd($x, $y) { |
|
388 | + return new QueryFunction($this->connection->getDatabasePlatform()->getBitAndComparisonExpression( |
|
389 | + $this->helper->quoteColumnName($x), |
|
390 | + $y |
|
391 | + )); |
|
392 | + } |
|
393 | 393 | |
394 | - /** |
|
395 | - * Binary OR Operator copies a bit if it exists in either operand. |
|
396 | - * |
|
397 | - * @param string|ILiteral $x The field or value to check |
|
398 | - * @param int $y Bitmap that must be set |
|
399 | - * @return IQueryFunction |
|
400 | - * @since 12.0.0 |
|
401 | - */ |
|
402 | - public function bitwiseOr($x, $y) { |
|
403 | - return new QueryFunction($this->connection->getDatabasePlatform()->getBitOrComparisonExpression( |
|
404 | - $this->helper->quoteColumnName($x), |
|
405 | - $y |
|
406 | - )); |
|
407 | - } |
|
394 | + /** |
|
395 | + * Binary OR Operator copies a bit if it exists in either operand. |
|
396 | + * |
|
397 | + * @param string|ILiteral $x The field or value to check |
|
398 | + * @param int $y Bitmap that must be set |
|
399 | + * @return IQueryFunction |
|
400 | + * @since 12.0.0 |
|
401 | + */ |
|
402 | + public function bitwiseOr($x, $y) { |
|
403 | + return new QueryFunction($this->connection->getDatabasePlatform()->getBitOrComparisonExpression( |
|
404 | + $this->helper->quoteColumnName($x), |
|
405 | + $y |
|
406 | + )); |
|
407 | + } |
|
408 | 408 | |
409 | - /** |
|
410 | - * Quotes a given input parameter. |
|
411 | - * |
|
412 | - * @param mixed $input The parameter to be quoted. |
|
413 | - * @param mixed|null $type One of the IQueryBuilder::PARAM_* constants |
|
414 | - * |
|
415 | - * @return ILiteral |
|
416 | - */ |
|
417 | - public function literal($input, $type = null) { |
|
418 | - return new Literal($this->expressionBuilder->literal($input, $type)); |
|
419 | - } |
|
409 | + /** |
|
410 | + * Quotes a given input parameter. |
|
411 | + * |
|
412 | + * @param mixed $input The parameter to be quoted. |
|
413 | + * @param mixed|null $type One of the IQueryBuilder::PARAM_* constants |
|
414 | + * |
|
415 | + * @return ILiteral |
|
416 | + */ |
|
417 | + public function literal($input, $type = null) { |
|
418 | + return new Literal($this->expressionBuilder->literal($input, $type)); |
|
419 | + } |
|
420 | 420 | |
421 | - /** |
|
422 | - * Returns a IQueryFunction that casts the column to the given type |
|
423 | - * |
|
424 | - * @param string $column |
|
425 | - * @param mixed $type One of IQueryBuilder::PARAM_* |
|
426 | - * @return string |
|
427 | - */ |
|
428 | - public function castColumn($column, $type) { |
|
429 | - return new QueryFunction( |
|
430 | - $this->helper->quoteColumnName($column) |
|
431 | - ); |
|
432 | - } |
|
421 | + /** |
|
422 | + * Returns a IQueryFunction that casts the column to the given type |
|
423 | + * |
|
424 | + * @param string $column |
|
425 | + * @param mixed $type One of IQueryBuilder::PARAM_* |
|
426 | + * @return string |
|
427 | + */ |
|
428 | + public function castColumn($column, $type) { |
|
429 | + return new QueryFunction( |
|
430 | + $this->helper->quoteColumnName($column) |
|
431 | + ); |
|
432 | + } |
|
433 | 433 | } |
@@ -25,14 +25,14 @@ |
||
25 | 25 | |
26 | 26 | |
27 | 27 | class SqliteExpressionBuilder extends ExpressionBuilder { |
28 | - /** |
|
29 | - * @inheritdoc |
|
30 | - */ |
|
31 | - public function like($x, $y, $type = null) { |
|
32 | - return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
33 | - } |
|
28 | + /** |
|
29 | + * @inheritdoc |
|
30 | + */ |
|
31 | + public function like($x, $y, $type = null) { |
|
32 | + return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
33 | + } |
|
34 | 34 | |
35 | - public function iLike($x, $y, $type = null) { |
|
36 | - return $this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y), $type); |
|
37 | - } |
|
35 | + public function iLike($x, $y, $type = null) { |
|
36 | + return $this->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y), $type); |
|
37 | + } |
|
38 | 38 | } |
@@ -28,39 +28,39 @@ |
||
28 | 28 | use OCP\DB\QueryBuilder\IFunctionBuilder; |
29 | 29 | |
30 | 30 | class FunctionBuilder implements IFunctionBuilder { |
31 | - /** @var QuoteHelper */ |
|
32 | - protected $helper; |
|
31 | + /** @var QuoteHelper */ |
|
32 | + protected $helper; |
|
33 | 33 | |
34 | - /** |
|
35 | - * ExpressionBuilder constructor. |
|
36 | - * |
|
37 | - * @param QuoteHelper $helper |
|
38 | - */ |
|
39 | - public function __construct(QuoteHelper $helper) { |
|
40 | - $this->helper = $helper; |
|
41 | - } |
|
34 | + /** |
|
35 | + * ExpressionBuilder constructor. |
|
36 | + * |
|
37 | + * @param QuoteHelper $helper |
|
38 | + */ |
|
39 | + public function __construct(QuoteHelper $helper) { |
|
40 | + $this->helper = $helper; |
|
41 | + } |
|
42 | 42 | |
43 | - public function md5($input) { |
|
44 | - return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')'); |
|
45 | - } |
|
43 | + public function md5($input) { |
|
44 | + return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')'); |
|
45 | + } |
|
46 | 46 | |
47 | - public function concat($x, $y) { |
|
48 | - return new QueryFunction('CONCAT(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')'); |
|
49 | - } |
|
47 | + public function concat($x, $y) { |
|
48 | + return new QueryFunction('CONCAT(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')'); |
|
49 | + } |
|
50 | 50 | |
51 | - public function substring($input, $start, $length = null) { |
|
52 | - if ($length) { |
|
53 | - return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')'); |
|
54 | - } else { |
|
55 | - return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')'); |
|
56 | - } |
|
57 | - } |
|
51 | + public function substring($input, $start, $length = null) { |
|
52 | + if ($length) { |
|
53 | + return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')'); |
|
54 | + } else { |
|
55 | + return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')'); |
|
56 | + } |
|
57 | + } |
|
58 | 58 | |
59 | - public function sum($field) { |
|
60 | - return new QueryFunction('SUM(' . $this->helper->quoteColumnName($field) . ')'); |
|
61 | - } |
|
59 | + public function sum($field) { |
|
60 | + return new QueryFunction('SUM(' . $this->helper->quoteColumnName($field) . ')'); |
|
61 | + } |
|
62 | 62 | |
63 | - public function lower($field) { |
|
64 | - return new QueryFunction('LOWER(' . $this->helper->quoteColumnName($field) . ')'); |
|
65 | - } |
|
63 | + public function lower($field) { |
|
64 | + return new QueryFunction('LOWER(' . $this->helper->quoteColumnName($field) . ')'); |
|
65 | + } |
|
66 | 66 | } |
@@ -67,334 +67,334 @@ |
||
67 | 67 | * Class for user management in a SQL Database (e.g. MySQL, SQLite) |
68 | 68 | */ |
69 | 69 | class Database extends Backend implements IUserBackend { |
70 | - /** @var CappedMemoryCache */ |
|
71 | - private $cache; |
|
72 | - |
|
73 | - /** @var EventDispatcher */ |
|
74 | - private $eventDispatcher; |
|
75 | - |
|
76 | - /** |
|
77 | - * \OC\User\Database constructor. |
|
78 | - * |
|
79 | - * @param EventDispatcher $eventDispatcher |
|
80 | - */ |
|
81 | - public function __construct($eventDispatcher = null) { |
|
82 | - $this->cache = new CappedMemoryCache(); |
|
83 | - $this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher(); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Create a new user |
|
88 | - * |
|
89 | - * @param string $uid The username of the user to create |
|
90 | - * @param string $password The password of the new user |
|
91 | - * @return bool |
|
92 | - * |
|
93 | - * Creates a new user. Basic checking of username is done in OC_User |
|
94 | - * itself, not in its subclasses. |
|
95 | - */ |
|
96 | - public function createUser($uid, $password) { |
|
97 | - if (!$this->userExists($uid)) { |
|
98 | - $event = new GenericEvent($password); |
|
99 | - $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
100 | - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )'); |
|
101 | - try { |
|
102 | - $result = $query->execute(array($uid, \OC::$server->getHasher()->hash($password))); |
|
103 | - } catch (\Exception $e) { |
|
104 | - $result = false; |
|
105 | - } |
|
106 | - |
|
107 | - // Clear cache |
|
108 | - unset($this->cache[$uid]); |
|
109 | - |
|
110 | - return $result ? true : false; |
|
111 | - } |
|
112 | - |
|
113 | - return false; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * delete a user |
|
118 | - * |
|
119 | - * @param string $uid The username of the user to delete |
|
120 | - * @return bool |
|
121 | - * |
|
122 | - * Deletes a user |
|
123 | - */ |
|
124 | - public function deleteUser($uid) { |
|
125 | - // Delete user-group-relation |
|
126 | - $query = \OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); |
|
127 | - $result = $query->execute(array($uid)); |
|
128 | - |
|
129 | - if (isset($this->cache[$uid])) { |
|
130 | - unset($this->cache[$uid]); |
|
131 | - } |
|
132 | - |
|
133 | - return $result ? true : false; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Set password |
|
138 | - * |
|
139 | - * @param string $uid The username |
|
140 | - * @param string $password The new password |
|
141 | - * @return bool |
|
142 | - * |
|
143 | - * Change the password of a user |
|
144 | - */ |
|
145 | - public function setPassword($uid, $password) { |
|
146 | - if ($this->userExists($uid)) { |
|
147 | - $event = new GenericEvent($password); |
|
148 | - $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
149 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?'); |
|
150 | - $result = $query->execute(array(\OC::$server->getHasher()->hash($password), $uid)); |
|
151 | - |
|
152 | - return $result ? true : false; |
|
153 | - } |
|
154 | - |
|
155 | - return false; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Set display name |
|
160 | - * |
|
161 | - * @param string $uid The username |
|
162 | - * @param string $displayName The new display name |
|
163 | - * @return bool |
|
164 | - * |
|
165 | - * Change the display name of a user |
|
166 | - */ |
|
167 | - public function setDisplayName($uid, $displayName) { |
|
168 | - if ($this->userExists($uid)) { |
|
169 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)'); |
|
170 | - $query->execute(array($displayName, $uid)); |
|
171 | - $this->cache[$uid]['displayname'] = $displayName; |
|
172 | - |
|
173 | - return true; |
|
174 | - } |
|
175 | - |
|
176 | - return false; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * get display name of the user |
|
181 | - * |
|
182 | - * @param string $uid user ID of the user |
|
183 | - * @return string display name |
|
184 | - */ |
|
185 | - public function getDisplayName($uid) { |
|
186 | - $this->loadUser($uid); |
|
187 | - return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname']; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Get a list of all display names and user ids. |
|
192 | - * |
|
193 | - * @param string $search |
|
194 | - * @param string|null $limit |
|
195 | - * @param string|null $offset |
|
196 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
197 | - */ |
|
198 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
199 | - $connection = \OC::$server->getDatabaseConnection(); |
|
200 | - |
|
201 | - $query = $connection->getQueryBuilder(); |
|
202 | - |
|
203 | - $query->select('uid', 'displayname') |
|
204 | - ->from('users', 'u') |
|
205 | - ->leftJoin('u', 'preferences', 'p', $query->expr()->andX( |
|
206 | - $query->expr()->eq('userid', 'uid')), |
|
207 | - $query->expr()->eq('appid', new Literal('settings')), |
|
208 | - $query->expr()->eq('configkey', new Literal('email')) |
|
209 | - ) |
|
210 | - // sqlite doesn't like re-using a single named parameter here |
|
211 | - ->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%'))) |
|
212 | - ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%'))) |
|
213 | - ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%'))) |
|
214 | - ->orderBy($query->func()->lower('displayname'), 'ASC') |
|
215 | - ->orderBy($query->func()->lower('uid'), 'ASC') |
|
216 | - ->setMaxResults($limit) |
|
217 | - ->setFirstResult($offset); |
|
218 | - |
|
219 | - $result = $query->execute(); |
|
220 | - $displayNames = []; |
|
221 | - while ($row = $result->fetch()) { |
|
222 | - $displayNames[$row['uid']] = $row['displayname']; |
|
223 | - } |
|
224 | - |
|
225 | - return $displayNames; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * Check if the password is correct |
|
230 | - * |
|
231 | - * @param string $uid The username |
|
232 | - * @param string $password The password |
|
233 | - * @return string |
|
234 | - * |
|
235 | - * Check if the password is correct without logging in the user |
|
236 | - * returns the user id or false |
|
237 | - */ |
|
238 | - public function checkPassword($uid, $password) { |
|
239 | - $query = \OC_DB::prepare('SELECT `uid`, `password` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); |
|
240 | - $result = $query->execute(array($uid)); |
|
241 | - |
|
242 | - $row = $result->fetchRow(); |
|
243 | - if ($row) { |
|
244 | - $storedHash = $row['password']; |
|
245 | - $newHash = ''; |
|
246 | - if (\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) { |
|
247 | - if (!empty($newHash)) { |
|
248 | - $this->setPassword($uid, $password); |
|
249 | - } |
|
250 | - return $row['uid']; |
|
251 | - } |
|
252 | - |
|
253 | - } |
|
254 | - |
|
255 | - return false; |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Load an user in the cache |
|
260 | - * |
|
261 | - * @param string $uid the username |
|
262 | - * @return boolean true if user was found, false otherwise |
|
263 | - */ |
|
264 | - private function loadUser($uid) { |
|
265 | - $uid = (string)$uid; |
|
266 | - if (!isset($this->cache[$uid])) { |
|
267 | - //guests $uid could be NULL or '' |
|
268 | - if ($uid === '') { |
|
269 | - $this->cache[$uid] = false; |
|
270 | - return true; |
|
271 | - } |
|
272 | - |
|
273 | - $query = \OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); |
|
274 | - $result = $query->execute(array($uid)); |
|
275 | - |
|
276 | - if ($result === false) { |
|
277 | - Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
278 | - return false; |
|
279 | - } |
|
280 | - |
|
281 | - $this->cache[$uid] = false; |
|
282 | - |
|
283 | - // "uid" is primary key, so there can only be a single result |
|
284 | - if ($row = $result->fetchRow()) { |
|
285 | - $this->cache[$uid]['uid'] = $row['uid']; |
|
286 | - $this->cache[$uid]['displayname'] = $row['displayname']; |
|
287 | - $result->closeCursor(); |
|
288 | - } else { |
|
289 | - $result->closeCursor(); |
|
290 | - return false; |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - return true; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Get a list of all users |
|
299 | - * |
|
300 | - * @param string $search |
|
301 | - * @param null|int $limit |
|
302 | - * @param null|int $offset |
|
303 | - * @return string[] an array of all uids |
|
304 | - */ |
|
305 | - public function getUsers($search = '', $limit = null, $offset = null) { |
|
306 | - $users = $this->getDisplayNames($search, $limit, $offset); |
|
307 | - $userIds = array_keys($users); |
|
308 | - sort($userIds, SORT_STRING | SORT_FLAG_CASE); |
|
309 | - return $userIds; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * check if a user exists |
|
314 | - * |
|
315 | - * @param string $uid the username |
|
316 | - * @return boolean |
|
317 | - */ |
|
318 | - public function userExists($uid) { |
|
319 | - $this->loadUser($uid); |
|
320 | - return $this->cache[$uid] !== false; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * get the user's home directory |
|
325 | - * |
|
326 | - * @param string $uid the username |
|
327 | - * @return string|false |
|
328 | - */ |
|
329 | - public function getHome($uid) { |
|
330 | - if ($this->userExists($uid)) { |
|
331 | - return \OC::$server->getConfig()->getSystemValue("datadirectory", \OC::$SERVERROOT . "/data") . '/' . $uid; |
|
332 | - } |
|
333 | - |
|
334 | - return false; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @return bool |
|
339 | - */ |
|
340 | - public function hasUserListings() { |
|
341 | - return true; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * counts the users in the database |
|
346 | - * |
|
347 | - * @return int|bool |
|
348 | - */ |
|
349 | - public function countUsers() { |
|
350 | - $query = \OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); |
|
351 | - $result = $query->execute(); |
|
352 | - if ($result === false) { |
|
353 | - Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
354 | - return false; |
|
355 | - } |
|
356 | - return $result->fetchOne(); |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * returns the username for the given login name in the correct casing |
|
361 | - * |
|
362 | - * @param string $loginName |
|
363 | - * @return string|false |
|
364 | - */ |
|
365 | - public function loginName2UserName($loginName) { |
|
366 | - if ($this->userExists($loginName)) { |
|
367 | - return $this->cache[$loginName]['uid']; |
|
368 | - } |
|
369 | - |
|
370 | - return false; |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Backend name to be shown in user management |
|
375 | - * |
|
376 | - * @return string the name of the backend to be shown |
|
377 | - */ |
|
378 | - public function getBackendName() { |
|
379 | - return 'Database'; |
|
380 | - } |
|
381 | - |
|
382 | - public static function preLoginNameUsedAsUserName($param) { |
|
383 | - if (!isset($param['uid'])) { |
|
384 | - throw new \Exception('key uid is expected to be set in $param'); |
|
385 | - } |
|
386 | - |
|
387 | - $backends = \OC::$server->getUserManager()->getBackends(); |
|
388 | - foreach ($backends as $backend) { |
|
389 | - if ($backend instanceof Database) { |
|
390 | - /** @var \OC\User\Database $backend */ |
|
391 | - $uid = $backend->loginName2UserName($param['uid']); |
|
392 | - if ($uid !== false) { |
|
393 | - $param['uid'] = $uid; |
|
394 | - return; |
|
395 | - } |
|
396 | - } |
|
397 | - } |
|
398 | - |
|
399 | - } |
|
70 | + /** @var CappedMemoryCache */ |
|
71 | + private $cache; |
|
72 | + |
|
73 | + /** @var EventDispatcher */ |
|
74 | + private $eventDispatcher; |
|
75 | + |
|
76 | + /** |
|
77 | + * \OC\User\Database constructor. |
|
78 | + * |
|
79 | + * @param EventDispatcher $eventDispatcher |
|
80 | + */ |
|
81 | + public function __construct($eventDispatcher = null) { |
|
82 | + $this->cache = new CappedMemoryCache(); |
|
83 | + $this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher(); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Create a new user |
|
88 | + * |
|
89 | + * @param string $uid The username of the user to create |
|
90 | + * @param string $password The password of the new user |
|
91 | + * @return bool |
|
92 | + * |
|
93 | + * Creates a new user. Basic checking of username is done in OC_User |
|
94 | + * itself, not in its subclasses. |
|
95 | + */ |
|
96 | + public function createUser($uid, $password) { |
|
97 | + if (!$this->userExists($uid)) { |
|
98 | + $event = new GenericEvent($password); |
|
99 | + $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
100 | + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )'); |
|
101 | + try { |
|
102 | + $result = $query->execute(array($uid, \OC::$server->getHasher()->hash($password))); |
|
103 | + } catch (\Exception $e) { |
|
104 | + $result = false; |
|
105 | + } |
|
106 | + |
|
107 | + // Clear cache |
|
108 | + unset($this->cache[$uid]); |
|
109 | + |
|
110 | + return $result ? true : false; |
|
111 | + } |
|
112 | + |
|
113 | + return false; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * delete a user |
|
118 | + * |
|
119 | + * @param string $uid The username of the user to delete |
|
120 | + * @return bool |
|
121 | + * |
|
122 | + * Deletes a user |
|
123 | + */ |
|
124 | + public function deleteUser($uid) { |
|
125 | + // Delete user-group-relation |
|
126 | + $query = \OC_DB::prepare('DELETE FROM `*PREFIX*users` WHERE `uid` = ?'); |
|
127 | + $result = $query->execute(array($uid)); |
|
128 | + |
|
129 | + if (isset($this->cache[$uid])) { |
|
130 | + unset($this->cache[$uid]); |
|
131 | + } |
|
132 | + |
|
133 | + return $result ? true : false; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Set password |
|
138 | + * |
|
139 | + * @param string $uid The username |
|
140 | + * @param string $password The new password |
|
141 | + * @return bool |
|
142 | + * |
|
143 | + * Change the password of a user |
|
144 | + */ |
|
145 | + public function setPassword($uid, $password) { |
|
146 | + if ($this->userExists($uid)) { |
|
147 | + $event = new GenericEvent($password); |
|
148 | + $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); |
|
149 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `password` = ? WHERE `uid` = ?'); |
|
150 | + $result = $query->execute(array(\OC::$server->getHasher()->hash($password), $uid)); |
|
151 | + |
|
152 | + return $result ? true : false; |
|
153 | + } |
|
154 | + |
|
155 | + return false; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Set display name |
|
160 | + * |
|
161 | + * @param string $uid The username |
|
162 | + * @param string $displayName The new display name |
|
163 | + * @return bool |
|
164 | + * |
|
165 | + * Change the display name of a user |
|
166 | + */ |
|
167 | + public function setDisplayName($uid, $displayName) { |
|
168 | + if ($this->userExists($uid)) { |
|
169 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*users` SET `displayname` = ? WHERE LOWER(`uid`) = LOWER(?)'); |
|
170 | + $query->execute(array($displayName, $uid)); |
|
171 | + $this->cache[$uid]['displayname'] = $displayName; |
|
172 | + |
|
173 | + return true; |
|
174 | + } |
|
175 | + |
|
176 | + return false; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * get display name of the user |
|
181 | + * |
|
182 | + * @param string $uid user ID of the user |
|
183 | + * @return string display name |
|
184 | + */ |
|
185 | + public function getDisplayName($uid) { |
|
186 | + $this->loadUser($uid); |
|
187 | + return empty($this->cache[$uid]['displayname']) ? $uid : $this->cache[$uid]['displayname']; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Get a list of all display names and user ids. |
|
192 | + * |
|
193 | + * @param string $search |
|
194 | + * @param string|null $limit |
|
195 | + * @param string|null $offset |
|
196 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
197 | + */ |
|
198 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
199 | + $connection = \OC::$server->getDatabaseConnection(); |
|
200 | + |
|
201 | + $query = $connection->getQueryBuilder(); |
|
202 | + |
|
203 | + $query->select('uid', 'displayname') |
|
204 | + ->from('users', 'u') |
|
205 | + ->leftJoin('u', 'preferences', 'p', $query->expr()->andX( |
|
206 | + $query->expr()->eq('userid', 'uid')), |
|
207 | + $query->expr()->eq('appid', new Literal('settings')), |
|
208 | + $query->expr()->eq('configkey', new Literal('email')) |
|
209 | + ) |
|
210 | + // sqlite doesn't like re-using a single named parameter here |
|
211 | + ->where($query->expr()->iLike('uid', $query->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%'))) |
|
212 | + ->orWhere($query->expr()->iLike('displayname', $query->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%'))) |
|
213 | + ->orWhere($query->expr()->iLike('configvalue', $query->createPositionalParameter('%' . $connection->escapeLikeParameter($search) . '%'))) |
|
214 | + ->orderBy($query->func()->lower('displayname'), 'ASC') |
|
215 | + ->orderBy($query->func()->lower('uid'), 'ASC') |
|
216 | + ->setMaxResults($limit) |
|
217 | + ->setFirstResult($offset); |
|
218 | + |
|
219 | + $result = $query->execute(); |
|
220 | + $displayNames = []; |
|
221 | + while ($row = $result->fetch()) { |
|
222 | + $displayNames[$row['uid']] = $row['displayname']; |
|
223 | + } |
|
224 | + |
|
225 | + return $displayNames; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * Check if the password is correct |
|
230 | + * |
|
231 | + * @param string $uid The username |
|
232 | + * @param string $password The password |
|
233 | + * @return string |
|
234 | + * |
|
235 | + * Check if the password is correct without logging in the user |
|
236 | + * returns the user id or false |
|
237 | + */ |
|
238 | + public function checkPassword($uid, $password) { |
|
239 | + $query = \OC_DB::prepare('SELECT `uid`, `password` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); |
|
240 | + $result = $query->execute(array($uid)); |
|
241 | + |
|
242 | + $row = $result->fetchRow(); |
|
243 | + if ($row) { |
|
244 | + $storedHash = $row['password']; |
|
245 | + $newHash = ''; |
|
246 | + if (\OC::$server->getHasher()->verify($password, $storedHash, $newHash)) { |
|
247 | + if (!empty($newHash)) { |
|
248 | + $this->setPassword($uid, $password); |
|
249 | + } |
|
250 | + return $row['uid']; |
|
251 | + } |
|
252 | + |
|
253 | + } |
|
254 | + |
|
255 | + return false; |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Load an user in the cache |
|
260 | + * |
|
261 | + * @param string $uid the username |
|
262 | + * @return boolean true if user was found, false otherwise |
|
263 | + */ |
|
264 | + private function loadUser($uid) { |
|
265 | + $uid = (string)$uid; |
|
266 | + if (!isset($this->cache[$uid])) { |
|
267 | + //guests $uid could be NULL or '' |
|
268 | + if ($uid === '') { |
|
269 | + $this->cache[$uid] = false; |
|
270 | + return true; |
|
271 | + } |
|
272 | + |
|
273 | + $query = \OC_DB::prepare('SELECT `uid`, `displayname` FROM `*PREFIX*users` WHERE LOWER(`uid`) = LOWER(?)'); |
|
274 | + $result = $query->execute(array($uid)); |
|
275 | + |
|
276 | + if ($result === false) { |
|
277 | + Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
278 | + return false; |
|
279 | + } |
|
280 | + |
|
281 | + $this->cache[$uid] = false; |
|
282 | + |
|
283 | + // "uid" is primary key, so there can only be a single result |
|
284 | + if ($row = $result->fetchRow()) { |
|
285 | + $this->cache[$uid]['uid'] = $row['uid']; |
|
286 | + $this->cache[$uid]['displayname'] = $row['displayname']; |
|
287 | + $result->closeCursor(); |
|
288 | + } else { |
|
289 | + $result->closeCursor(); |
|
290 | + return false; |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + return true; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Get a list of all users |
|
299 | + * |
|
300 | + * @param string $search |
|
301 | + * @param null|int $limit |
|
302 | + * @param null|int $offset |
|
303 | + * @return string[] an array of all uids |
|
304 | + */ |
|
305 | + public function getUsers($search = '', $limit = null, $offset = null) { |
|
306 | + $users = $this->getDisplayNames($search, $limit, $offset); |
|
307 | + $userIds = array_keys($users); |
|
308 | + sort($userIds, SORT_STRING | SORT_FLAG_CASE); |
|
309 | + return $userIds; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * check if a user exists |
|
314 | + * |
|
315 | + * @param string $uid the username |
|
316 | + * @return boolean |
|
317 | + */ |
|
318 | + public function userExists($uid) { |
|
319 | + $this->loadUser($uid); |
|
320 | + return $this->cache[$uid] !== false; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * get the user's home directory |
|
325 | + * |
|
326 | + * @param string $uid the username |
|
327 | + * @return string|false |
|
328 | + */ |
|
329 | + public function getHome($uid) { |
|
330 | + if ($this->userExists($uid)) { |
|
331 | + return \OC::$server->getConfig()->getSystemValue("datadirectory", \OC::$SERVERROOT . "/data") . '/' . $uid; |
|
332 | + } |
|
333 | + |
|
334 | + return false; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @return bool |
|
339 | + */ |
|
340 | + public function hasUserListings() { |
|
341 | + return true; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * counts the users in the database |
|
346 | + * |
|
347 | + * @return int|bool |
|
348 | + */ |
|
349 | + public function countUsers() { |
|
350 | + $query = \OC_DB::prepare('SELECT COUNT(*) FROM `*PREFIX*users`'); |
|
351 | + $result = $query->execute(); |
|
352 | + if ($result === false) { |
|
353 | + Util::writeLog('core', \OC_DB::getErrorMessage(), Util::ERROR); |
|
354 | + return false; |
|
355 | + } |
|
356 | + return $result->fetchOne(); |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * returns the username for the given login name in the correct casing |
|
361 | + * |
|
362 | + * @param string $loginName |
|
363 | + * @return string|false |
|
364 | + */ |
|
365 | + public function loginName2UserName($loginName) { |
|
366 | + if ($this->userExists($loginName)) { |
|
367 | + return $this->cache[$loginName]['uid']; |
|
368 | + } |
|
369 | + |
|
370 | + return false; |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Backend name to be shown in user management |
|
375 | + * |
|
376 | + * @return string the name of the backend to be shown |
|
377 | + */ |
|
378 | + public function getBackendName() { |
|
379 | + return 'Database'; |
|
380 | + } |
|
381 | + |
|
382 | + public static function preLoginNameUsedAsUserName($param) { |
|
383 | + if (!isset($param['uid'])) { |
|
384 | + throw new \Exception('key uid is expected to be set in $param'); |
|
385 | + } |
|
386 | + |
|
387 | + $backends = \OC::$server->getUserManager()->getBackends(); |
|
388 | + foreach ($backends as $backend) { |
|
389 | + if ($backend instanceof Database) { |
|
390 | + /** @var \OC\User\Database $backend */ |
|
391 | + $uid = $backend->loginName2UserName($param['uid']); |
|
392 | + if ($uid !== false) { |
|
393 | + $param['uid'] = $uid; |
|
394 | + return; |
|
395 | + } |
|
396 | + } |
|
397 | + } |
|
398 | + |
|
399 | + } |
|
400 | 400 | } |