@@ -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 | } |
@@ -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 | } |
@@ -29,7 +29,7 @@ |
||
| 29 | 29 | * @inheritdoc |
| 30 | 30 | */ |
| 31 | 31 | public function like($x, $y, $type = null) { |
| 32 | - return parent::like($x, $y, $type) . " ESCAPE '\\'"; |
|
| 32 | + return parent::like($x, $y, $type)." ESCAPE '\\'"; |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | public function iLike($x, $y, $type = null) { |
@@ -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 | } |
@@ -41,26 +41,26 @@ |
||
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | public function md5($input) { |
| 44 | - return new QueryFunction('MD5(' . $this->helper->quoteColumnName($input) . ')'); |
|
| 44 | + return new QueryFunction('MD5('.$this->helper->quoteColumnName($input).')'); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | public function concat($x, $y) { |
| 48 | - return new QueryFunction('CONCAT(' . $this->helper->quoteColumnName($x) . ', ' . $this->helper->quoteColumnName($y) . ')'); |
|
| 48 | + return new QueryFunction('CONCAT('.$this->helper->quoteColumnName($x).', '.$this->helper->quoteColumnName($y).')'); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | public function substring($input, $start, $length = null) { |
| 52 | 52 | if ($length) { |
| 53 | - return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ', ' . $this->helper->quoteColumnName($length) . ')'); |
|
| 53 | + return new QueryFunction('SUBSTR('.$this->helper->quoteColumnName($input).', '.$this->helper->quoteColumnName($start).', '.$this->helper->quoteColumnName($length).')'); |
|
| 54 | 54 | } else { |
| 55 | - return new QueryFunction('SUBSTR(' . $this->helper->quoteColumnName($input) . ', ' . $this->helper->quoteColumnName($start) . ')'); |
|
| 55 | + return new QueryFunction('SUBSTR('.$this->helper->quoteColumnName($input).', '.$this->helper->quoteColumnName($start).')'); |
|
| 56 | 56 | } |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | public function sum($field) { |
| 60 | - return new QueryFunction('SUM(' . $this->helper->quoteColumnName($field) . ')'); |
|
| 60 | + return new QueryFunction('SUM('.$this->helper->quoteColumnName($field).')'); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | public function lower($field) { |
| 64 | - return new QueryFunction('LOWER(' . $this->helper->quoteColumnName($field) . ')'); |
|
| 64 | + return new QueryFunction('LOWER('.$this->helper->quoteColumnName($field).')'); |
|
| 65 | 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 | } |
@@ -208,9 +208,9 @@ discard block |
||
| 208 | 208 | $query->expr()->eq('configkey', new Literal('email')) |
| 209 | 209 | ) |
| 210 | 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) . '%'))) |
|
| 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 | 214 | ->orderBy($query->func()->lower('displayname'), 'ASC') |
| 215 | 215 | ->orderBy($query->func()->lower('uid'), 'ASC') |
| 216 | 216 | ->setMaxResults($limit) |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | * @return boolean true if user was found, false otherwise |
| 263 | 263 | */ |
| 264 | 264 | private function loadUser($uid) { |
| 265 | - $uid = (string)$uid; |
|
| 265 | + $uid = (string) $uid; |
|
| 266 | 266 | if (!isset($this->cache[$uid])) { |
| 267 | 267 | //guests $uid could be NULL or '' |
| 268 | 268 | if ($uid === '') { |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | */ |
| 329 | 329 | public function getHome($uid) { |
| 330 | 330 | if ($this->userExists($uid)) { |
| 331 | - return \OC::$server->getConfig()->getSystemValue("datadirectory", \OC::$SERVERROOT . "/data") . '/' . $uid; |
|
| 331 | + return \OC::$server->getConfig()->getSystemValue("datadirectory", \OC::$SERVERROOT."/data").'/'.$uid; |
|
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | return false; |
@@ -37,398 +37,398 @@ |
||
| 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 IDBConnection $connection |
|
| 56 | - * @param IQueryBuilder $queryBuilder |
|
| 57 | - */ |
|
| 58 | - public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder) { |
|
| 59 | - $this->connection = $connection; |
|
| 60 | - $this->helper = new QuoteHelper(); |
|
| 61 | - $this->expressionBuilder = new DoctrineExpressionBuilder($connection); |
|
| 62 | - $this->functionBuilder = $queryBuilder->func(); |
|
| 63 | - } |
|
| 52 | + /** |
|
| 53 | + * Initializes a new <tt>ExpressionBuilder</tt>. |
|
| 54 | + * |
|
| 55 | + * @param IDBConnection $connection |
|
| 56 | + * @param IQueryBuilder $queryBuilder |
|
| 57 | + */ |
|
| 58 | + public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder) { |
|
| 59 | + $this->connection = $connection; |
|
| 60 | + $this->helper = new QuoteHelper(); |
|
| 61 | + $this->expressionBuilder = new DoctrineExpressionBuilder($connection); |
|
| 62 | + $this->functionBuilder = $queryBuilder->func(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Creates a conjunction of the given boolean expressions. |
|
| 67 | - * |
|
| 68 | - * Example: |
|
| 69 | - * |
|
| 70 | - * [php] |
|
| 71 | - * // (u.type = ?) AND (u.role = ?) |
|
| 72 | - * $expr->andX('u.type = ?', 'u.role = ?')); |
|
| 73 | - * |
|
| 74 | - * @param mixed $x Optional clause. Defaults = null, but requires |
|
| 75 | - * at least one defined when converting to string. |
|
| 76 | - * |
|
| 77 | - * @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
| 78 | - */ |
|
| 79 | - public function andX($x = null) { |
|
| 80 | - $arguments = func_get_args(); |
|
| 81 | - $compositeExpression = call_user_func_array([$this->expressionBuilder, 'andX'], $arguments); |
|
| 82 | - return new CompositeExpression($compositeExpression); |
|
| 83 | - } |
|
| 65 | + /** |
|
| 66 | + * Creates a conjunction of the given boolean expressions. |
|
| 67 | + * |
|
| 68 | + * Example: |
|
| 69 | + * |
|
| 70 | + * [php] |
|
| 71 | + * // (u.type = ?) AND (u.role = ?) |
|
| 72 | + * $expr->andX('u.type = ?', 'u.role = ?')); |
|
| 73 | + * |
|
| 74 | + * @param mixed $x Optional clause. Defaults = null, but requires |
|
| 75 | + * at least one defined when converting to string. |
|
| 76 | + * |
|
| 77 | + * @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
| 78 | + */ |
|
| 79 | + public function andX($x = null) { |
|
| 80 | + $arguments = func_get_args(); |
|
| 81 | + $compositeExpression = call_user_func_array([$this->expressionBuilder, 'andX'], $arguments); |
|
| 82 | + return new CompositeExpression($compositeExpression); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Creates a disjunction of the given boolean expressions. |
|
| 87 | - * |
|
| 88 | - * Example: |
|
| 89 | - * |
|
| 90 | - * [php] |
|
| 91 | - * // (u.type = ?) OR (u.role = ?) |
|
| 92 | - * $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?')); |
|
| 93 | - * |
|
| 94 | - * @param mixed $x Optional clause. Defaults = null, but requires |
|
| 95 | - * at least one defined when converting to string. |
|
| 96 | - * |
|
| 97 | - * @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
| 98 | - */ |
|
| 99 | - public function orX($x = null) { |
|
| 100 | - $arguments = func_get_args(); |
|
| 101 | - $compositeExpression = call_user_func_array([$this->expressionBuilder, 'orX'], $arguments); |
|
| 102 | - return new CompositeExpression($compositeExpression); |
|
| 103 | - } |
|
| 85 | + /** |
|
| 86 | + * Creates a disjunction of the given boolean expressions. |
|
| 87 | + * |
|
| 88 | + * Example: |
|
| 89 | + * |
|
| 90 | + * [php] |
|
| 91 | + * // (u.type = ?) OR (u.role = ?) |
|
| 92 | + * $qb->where($qb->expr()->orX('u.type = ?', 'u.role = ?')); |
|
| 93 | + * |
|
| 94 | + * @param mixed $x Optional clause. Defaults = null, but requires |
|
| 95 | + * at least one defined when converting to string. |
|
| 96 | + * |
|
| 97 | + * @return \OCP\DB\QueryBuilder\ICompositeExpression |
|
| 98 | + */ |
|
| 99 | + public function orX($x = null) { |
|
| 100 | + $arguments = func_get_args(); |
|
| 101 | + $compositeExpression = call_user_func_array([$this->expressionBuilder, 'orX'], $arguments); |
|
| 102 | + return new CompositeExpression($compositeExpression); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Creates a comparison expression. |
|
| 107 | - * |
|
| 108 | - * @param mixed $x The left expression. |
|
| 109 | - * @param string $operator One of the IExpressionBuilder::* constants. |
|
| 110 | - * @param mixed $y The right expression. |
|
| 111 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 112 | - * required when comparing text fields for oci compatibility |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function comparison($x, $operator, $y, $type = null) { |
|
| 117 | - $x = $this->helper->quoteColumnName($x); |
|
| 118 | - $y = $this->helper->quoteColumnName($y); |
|
| 119 | - return $this->expressionBuilder->comparison($x, $operator, $y); |
|
| 120 | - } |
|
| 105 | + /** |
|
| 106 | + * Creates a comparison expression. |
|
| 107 | + * |
|
| 108 | + * @param mixed $x The left expression. |
|
| 109 | + * @param string $operator One of the IExpressionBuilder::* constants. |
|
| 110 | + * @param mixed $y The right expression. |
|
| 111 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 112 | + * required when comparing text fields for oci compatibility |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function comparison($x, $operator, $y, $type = null) { |
|
| 117 | + $x = $this->helper->quoteColumnName($x); |
|
| 118 | + $y = $this->helper->quoteColumnName($y); |
|
| 119 | + return $this->expressionBuilder->comparison($x, $operator, $y); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Creates an equality comparison expression with the given arguments. |
|
| 124 | - * |
|
| 125 | - * First argument is considered the left expression and the second is the right expression. |
|
| 126 | - * When converted to string, it will generated a <left expr> = <right expr>. Example: |
|
| 127 | - * |
|
| 128 | - * [php] |
|
| 129 | - * // u.id = ? |
|
| 130 | - * $expr->eq('u.id', '?'); |
|
| 131 | - * |
|
| 132 | - * @param mixed $x The left expression. |
|
| 133 | - * @param mixed $y The right expression. |
|
| 134 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 135 | - * required when comparing text fields for oci compatibility |
|
| 136 | - * |
|
| 137 | - * @return string |
|
| 138 | - */ |
|
| 139 | - public function eq($x, $y, $type = null) { |
|
| 140 | - $x = $this->helper->quoteColumnName($x); |
|
| 141 | - $y = $this->helper->quoteColumnName($y); |
|
| 142 | - return $this->expressionBuilder->eq($x, $y); |
|
| 143 | - } |
|
| 122 | + /** |
|
| 123 | + * Creates an equality comparison expression with the given arguments. |
|
| 124 | + * |
|
| 125 | + * First argument is considered the left expression and the second is the right expression. |
|
| 126 | + * When converted to string, it will generated a <left expr> = <right expr>. Example: |
|
| 127 | + * |
|
| 128 | + * [php] |
|
| 129 | + * // u.id = ? |
|
| 130 | + * $expr->eq('u.id', '?'); |
|
| 131 | + * |
|
| 132 | + * @param mixed $x The left expression. |
|
| 133 | + * @param mixed $y The right expression. |
|
| 134 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 135 | + * required when comparing text fields for oci compatibility |
|
| 136 | + * |
|
| 137 | + * @return string |
|
| 138 | + */ |
|
| 139 | + public function eq($x, $y, $type = null) { |
|
| 140 | + $x = $this->helper->quoteColumnName($x); |
|
| 141 | + $y = $this->helper->quoteColumnName($y); |
|
| 142 | + return $this->expressionBuilder->eq($x, $y); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * Creates a non equality comparison expression with the given arguments. |
|
| 147 | - * First argument is considered the left expression and the second is the right expression. |
|
| 148 | - * When converted to string, it will generated a <left expr> <> <right expr>. Example: |
|
| 149 | - * |
|
| 150 | - * [php] |
|
| 151 | - * // u.id <> 1 |
|
| 152 | - * $q->where($q->expr()->neq('u.id', '1')); |
|
| 153 | - * |
|
| 154 | - * @param mixed $x The left expression. |
|
| 155 | - * @param mixed $y The right expression. |
|
| 156 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 157 | - * required when comparing text fields for oci compatibility |
|
| 158 | - * |
|
| 159 | - * @return string |
|
| 160 | - */ |
|
| 161 | - public function neq($x, $y, $type = null) { |
|
| 162 | - $x = $this->helper->quoteColumnName($x); |
|
| 163 | - $y = $this->helper->quoteColumnName($y); |
|
| 164 | - return $this->expressionBuilder->neq($x, $y); |
|
| 165 | - } |
|
| 145 | + /** |
|
| 146 | + * Creates a non equality comparison expression with the given arguments. |
|
| 147 | + * First argument is considered the left expression and the second is the right expression. |
|
| 148 | + * When converted to string, it will generated a <left expr> <> <right expr>. Example: |
|
| 149 | + * |
|
| 150 | + * [php] |
|
| 151 | + * // u.id <> 1 |
|
| 152 | + * $q->where($q->expr()->neq('u.id', '1')); |
|
| 153 | + * |
|
| 154 | + * @param mixed $x The left expression. |
|
| 155 | + * @param mixed $y The right expression. |
|
| 156 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 157 | + * required when comparing text fields for oci compatibility |
|
| 158 | + * |
|
| 159 | + * @return string |
|
| 160 | + */ |
|
| 161 | + public function neq($x, $y, $type = null) { |
|
| 162 | + $x = $this->helper->quoteColumnName($x); |
|
| 163 | + $y = $this->helper->quoteColumnName($y); |
|
| 164 | + return $this->expressionBuilder->neq($x, $y); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * Creates a lower-than comparison expression with the given arguments. |
|
| 169 | - * First argument is considered the left expression and the second is the right expression. |
|
| 170 | - * When converted to string, it will generated a <left expr> < <right expr>. Example: |
|
| 171 | - * |
|
| 172 | - * [php] |
|
| 173 | - * // u.id < ? |
|
| 174 | - * $q->where($q->expr()->lt('u.id', '?')); |
|
| 175 | - * |
|
| 176 | - * @param mixed $x The left expression. |
|
| 177 | - * @param mixed $y The right expression. |
|
| 178 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 179 | - * required when comparing text fields for oci compatibility |
|
| 180 | - * |
|
| 181 | - * @return string |
|
| 182 | - */ |
|
| 183 | - public function lt($x, $y, $type = null) { |
|
| 184 | - $x = $this->helper->quoteColumnName($x); |
|
| 185 | - $y = $this->helper->quoteColumnName($y); |
|
| 186 | - return $this->expressionBuilder->lt($x, $y); |
|
| 187 | - } |
|
| 167 | + /** |
|
| 168 | + * Creates a lower-than comparison expression with the given arguments. |
|
| 169 | + * First argument is considered the left expression and the second is the right expression. |
|
| 170 | + * When converted to string, it will generated a <left expr> < <right expr>. Example: |
|
| 171 | + * |
|
| 172 | + * [php] |
|
| 173 | + * // u.id < ? |
|
| 174 | + * $q->where($q->expr()->lt('u.id', '?')); |
|
| 175 | + * |
|
| 176 | + * @param mixed $x The left expression. |
|
| 177 | + * @param mixed $y The right expression. |
|
| 178 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 179 | + * required when comparing text fields for oci compatibility |
|
| 180 | + * |
|
| 181 | + * @return string |
|
| 182 | + */ |
|
| 183 | + public function lt($x, $y, $type = null) { |
|
| 184 | + $x = $this->helper->quoteColumnName($x); |
|
| 185 | + $y = $this->helper->quoteColumnName($y); |
|
| 186 | + return $this->expressionBuilder->lt($x, $y); |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - /** |
|
| 190 | - * Creates a lower-than-equal comparison expression with the given arguments. |
|
| 191 | - * First argument is considered the left expression and the second is the right expression. |
|
| 192 | - * When converted to string, it will generated a <left expr> <= <right expr>. Example: |
|
| 193 | - * |
|
| 194 | - * [php] |
|
| 195 | - * // u.id <= ? |
|
| 196 | - * $q->where($q->expr()->lte('u.id', '?')); |
|
| 197 | - * |
|
| 198 | - * @param mixed $x The left expression. |
|
| 199 | - * @param mixed $y The right expression. |
|
| 200 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 201 | - * required when comparing text fields for oci compatibility |
|
| 202 | - * |
|
| 203 | - * @return string |
|
| 204 | - */ |
|
| 205 | - public function lte($x, $y, $type = null) { |
|
| 206 | - $x = $this->helper->quoteColumnName($x); |
|
| 207 | - $y = $this->helper->quoteColumnName($y); |
|
| 208 | - return $this->expressionBuilder->lte($x, $y); |
|
| 209 | - } |
|
| 189 | + /** |
|
| 190 | + * Creates a lower-than-equal comparison expression with the given arguments. |
|
| 191 | + * First argument is considered the left expression and the second is the right expression. |
|
| 192 | + * When converted to string, it will generated a <left expr> <= <right expr>. Example: |
|
| 193 | + * |
|
| 194 | + * [php] |
|
| 195 | + * // u.id <= ? |
|
| 196 | + * $q->where($q->expr()->lte('u.id', '?')); |
|
| 197 | + * |
|
| 198 | + * @param mixed $x The left expression. |
|
| 199 | + * @param mixed $y The right expression. |
|
| 200 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 201 | + * required when comparing text fields for oci compatibility |
|
| 202 | + * |
|
| 203 | + * @return string |
|
| 204 | + */ |
|
| 205 | + public function lte($x, $y, $type = null) { |
|
| 206 | + $x = $this->helper->quoteColumnName($x); |
|
| 207 | + $y = $this->helper->quoteColumnName($y); |
|
| 208 | + return $this->expressionBuilder->lte($x, $y); |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * Creates a greater-than comparison expression with the given arguments. |
|
| 213 | - * First argument is considered the left expression and the second is the right expression. |
|
| 214 | - * When converted to string, it will generated a <left expr> > <right expr>. Example: |
|
| 215 | - * |
|
| 216 | - * [php] |
|
| 217 | - * // u.id > ? |
|
| 218 | - * $q->where($q->expr()->gt('u.id', '?')); |
|
| 219 | - * |
|
| 220 | - * @param mixed $x The left expression. |
|
| 221 | - * @param mixed $y The right expression. |
|
| 222 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 223 | - * required when comparing text fields for oci compatibility |
|
| 224 | - * |
|
| 225 | - * @return string |
|
| 226 | - */ |
|
| 227 | - public function gt($x, $y, $type = null) { |
|
| 228 | - $x = $this->helper->quoteColumnName($x); |
|
| 229 | - $y = $this->helper->quoteColumnName($y); |
|
| 230 | - return $this->expressionBuilder->gt($x, $y); |
|
| 231 | - } |
|
| 211 | + /** |
|
| 212 | + * Creates a greater-than comparison expression with the given arguments. |
|
| 213 | + * First argument is considered the left expression and the second is the right expression. |
|
| 214 | + * When converted to string, it will generated a <left expr> > <right expr>. Example: |
|
| 215 | + * |
|
| 216 | + * [php] |
|
| 217 | + * // u.id > ? |
|
| 218 | + * $q->where($q->expr()->gt('u.id', '?')); |
|
| 219 | + * |
|
| 220 | + * @param mixed $x The left expression. |
|
| 221 | + * @param mixed $y The right expression. |
|
| 222 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 223 | + * required when comparing text fields for oci compatibility |
|
| 224 | + * |
|
| 225 | + * @return string |
|
| 226 | + */ |
|
| 227 | + public function gt($x, $y, $type = null) { |
|
| 228 | + $x = $this->helper->quoteColumnName($x); |
|
| 229 | + $y = $this->helper->quoteColumnName($y); |
|
| 230 | + return $this->expressionBuilder->gt($x, $y); |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * Creates a greater-than-equal comparison expression with the given arguments. |
|
| 235 | - * First argument is considered the left expression and the second is the right expression. |
|
| 236 | - * When converted to string, it will generated a <left expr> >= <right expr>. Example: |
|
| 237 | - * |
|
| 238 | - * [php] |
|
| 239 | - * // u.id >= ? |
|
| 240 | - * $q->where($q->expr()->gte('u.id', '?')); |
|
| 241 | - * |
|
| 242 | - * @param mixed $x The left expression. |
|
| 243 | - * @param mixed $y The right expression. |
|
| 244 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 245 | - * required when comparing text fields for oci compatibility |
|
| 246 | - * |
|
| 247 | - * @return string |
|
| 248 | - */ |
|
| 249 | - public function gte($x, $y, $type = null) { |
|
| 250 | - $x = $this->helper->quoteColumnName($x); |
|
| 251 | - $y = $this->helper->quoteColumnName($y); |
|
| 252 | - return $this->expressionBuilder->gte($x, $y); |
|
| 253 | - } |
|
| 233 | + /** |
|
| 234 | + * Creates a greater-than-equal comparison expression with the given arguments. |
|
| 235 | + * First argument is considered the left expression and the second is the right expression. |
|
| 236 | + * When converted to string, it will generated a <left expr> >= <right expr>. Example: |
|
| 237 | + * |
|
| 238 | + * [php] |
|
| 239 | + * // u.id >= ? |
|
| 240 | + * $q->where($q->expr()->gte('u.id', '?')); |
|
| 241 | + * |
|
| 242 | + * @param mixed $x The left expression. |
|
| 243 | + * @param mixed $y The right expression. |
|
| 244 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 245 | + * required when comparing text fields for oci compatibility |
|
| 246 | + * |
|
| 247 | + * @return string |
|
| 248 | + */ |
|
| 249 | + public function gte($x, $y, $type = null) { |
|
| 250 | + $x = $this->helper->quoteColumnName($x); |
|
| 251 | + $y = $this->helper->quoteColumnName($y); |
|
| 252 | + return $this->expressionBuilder->gte($x, $y); |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Creates an IS NULL expression with the given arguments. |
|
| 257 | - * |
|
| 258 | - * @param string $x The field in string format to be restricted by IS NULL. |
|
| 259 | - * |
|
| 260 | - * @return string |
|
| 261 | - */ |
|
| 262 | - public function isNull($x) { |
|
| 263 | - $x = $this->helper->quoteColumnName($x); |
|
| 264 | - return $this->expressionBuilder->isNull($x); |
|
| 265 | - } |
|
| 255 | + /** |
|
| 256 | + * Creates an IS NULL expression with the given arguments. |
|
| 257 | + * |
|
| 258 | + * @param string $x The field in string format to be restricted by IS NULL. |
|
| 259 | + * |
|
| 260 | + * @return string |
|
| 261 | + */ |
|
| 262 | + public function isNull($x) { |
|
| 263 | + $x = $this->helper->quoteColumnName($x); |
|
| 264 | + return $this->expressionBuilder->isNull($x); |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - /** |
|
| 268 | - * Creates an IS NOT NULL expression with the given arguments. |
|
| 269 | - * |
|
| 270 | - * @param string $x The field in string format to be restricted by IS NOT NULL. |
|
| 271 | - * |
|
| 272 | - * @return string |
|
| 273 | - */ |
|
| 274 | - public function isNotNull($x) { |
|
| 275 | - $x = $this->helper->quoteColumnName($x); |
|
| 276 | - return $this->expressionBuilder->isNotNull($x); |
|
| 277 | - } |
|
| 267 | + /** |
|
| 268 | + * Creates an IS NOT NULL expression with the given arguments. |
|
| 269 | + * |
|
| 270 | + * @param string $x The field in string format to be restricted by IS NOT NULL. |
|
| 271 | + * |
|
| 272 | + * @return string |
|
| 273 | + */ |
|
| 274 | + public function isNotNull($x) { |
|
| 275 | + $x = $this->helper->quoteColumnName($x); |
|
| 276 | + return $this->expressionBuilder->isNotNull($x); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - /** |
|
| 280 | - * Creates a LIKE() comparison expression with the given arguments. |
|
| 281 | - * |
|
| 282 | - * @param string $x Field in string format to be inspected by LIKE() comparison. |
|
| 283 | - * @param mixed $y Argument to be used in LIKE() comparison. |
|
| 284 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 285 | - * required when comparing text fields for oci compatibility |
|
| 286 | - * |
|
| 287 | - * @return string |
|
| 288 | - */ |
|
| 289 | - public function like($x, $y, $type = null) { |
|
| 290 | - $x = $this->helper->quoteColumnName($x); |
|
| 291 | - $y = $this->helper->quoteColumnName($y); |
|
| 292 | - return $this->expressionBuilder->like($x, $y); |
|
| 293 | - } |
|
| 279 | + /** |
|
| 280 | + * Creates a LIKE() comparison expression with the given arguments. |
|
| 281 | + * |
|
| 282 | + * @param string $x Field in string format to be inspected by LIKE() comparison. |
|
| 283 | + * @param mixed $y Argument to be used in LIKE() comparison. |
|
| 284 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 285 | + * required when comparing text fields for oci compatibility |
|
| 286 | + * |
|
| 287 | + * @return string |
|
| 288 | + */ |
|
| 289 | + public function like($x, $y, $type = null) { |
|
| 290 | + $x = $this->helper->quoteColumnName($x); |
|
| 291 | + $y = $this->helper->quoteColumnName($y); |
|
| 292 | + return $this->expressionBuilder->like($x, $y); |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - /** |
|
| 296 | - * Creates a ILIKE() comparison expression with the given arguments. |
|
| 297 | - * |
|
| 298 | - * @param string $x Field in string format to be inspected by ILIKE() comparison. |
|
| 299 | - * @param mixed $y Argument to be used in ILIKE() comparison. |
|
| 300 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 301 | - * required when comparing text fields for oci compatibility |
|
| 302 | - * |
|
| 303 | - * @return string |
|
| 304 | - * @since 9.0.0 |
|
| 305 | - */ |
|
| 306 | - public function iLike($x, $y, $type = null) { |
|
| 307 | - return $this->expressionBuilder->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y)); |
|
| 308 | - } |
|
| 295 | + /** |
|
| 296 | + * Creates a ILIKE() comparison expression with the given arguments. |
|
| 297 | + * |
|
| 298 | + * @param string $x Field in string format to be inspected by ILIKE() comparison. |
|
| 299 | + * @param mixed $y Argument to be used in ILIKE() comparison. |
|
| 300 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 301 | + * required when comparing text fields for oci compatibility |
|
| 302 | + * |
|
| 303 | + * @return string |
|
| 304 | + * @since 9.0.0 |
|
| 305 | + */ |
|
| 306 | + public function iLike($x, $y, $type = null) { |
|
| 307 | + return $this->expressionBuilder->like($this->functionBuilder->lower($x), $this->functionBuilder->lower($y)); |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | - /** |
|
| 311 | - * Creates a NOT LIKE() comparison expression with the given arguments. |
|
| 312 | - * |
|
| 313 | - * @param string $x Field in string format to be inspected by NOT LIKE() comparison. |
|
| 314 | - * @param mixed $y Argument to be used in NOT LIKE() comparison. |
|
| 315 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 316 | - * required when comparing text fields for oci compatibility |
|
| 317 | - * |
|
| 318 | - * @return string |
|
| 319 | - */ |
|
| 320 | - public function notLike($x, $y, $type = null) { |
|
| 321 | - $x = $this->helper->quoteColumnName($x); |
|
| 322 | - $y = $this->helper->quoteColumnName($y); |
|
| 323 | - return $this->expressionBuilder->notLike($x, $y); |
|
| 324 | - } |
|
| 310 | + /** |
|
| 311 | + * Creates a NOT LIKE() comparison expression with the given arguments. |
|
| 312 | + * |
|
| 313 | + * @param string $x Field in string format to be inspected by NOT LIKE() comparison. |
|
| 314 | + * @param mixed $y Argument to be used in NOT LIKE() comparison. |
|
| 315 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 316 | + * required when comparing text fields for oci compatibility |
|
| 317 | + * |
|
| 318 | + * @return string |
|
| 319 | + */ |
|
| 320 | + public function notLike($x, $y, $type = null) { |
|
| 321 | + $x = $this->helper->quoteColumnName($x); |
|
| 322 | + $y = $this->helper->quoteColumnName($y); |
|
| 323 | + return $this->expressionBuilder->notLike($x, $y); |
|
| 324 | + } |
|
| 325 | 325 | |
| 326 | - /** |
|
| 327 | - * Creates a IN () comparison expression with the given arguments. |
|
| 328 | - * |
|
| 329 | - * @param string $x The field in string format to be inspected by IN() comparison. |
|
| 330 | - * @param string|array $y The placeholder or the array of values to be used by IN() comparison. |
|
| 331 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 332 | - * required when comparing text fields for oci compatibility |
|
| 333 | - * |
|
| 334 | - * @return string |
|
| 335 | - */ |
|
| 336 | - public function in($x, $y, $type = null) { |
|
| 337 | - $x = $this->helper->quoteColumnName($x); |
|
| 338 | - $y = $this->helper->quoteColumnNames($y); |
|
| 339 | - return $this->expressionBuilder->in($x, $y); |
|
| 340 | - } |
|
| 326 | + /** |
|
| 327 | + * Creates a IN () comparison expression with the given arguments. |
|
| 328 | + * |
|
| 329 | + * @param string $x The field in string format to be inspected by IN() comparison. |
|
| 330 | + * @param string|array $y The placeholder or the array of values to be used by IN() comparison. |
|
| 331 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 332 | + * required when comparing text fields for oci compatibility |
|
| 333 | + * |
|
| 334 | + * @return string |
|
| 335 | + */ |
|
| 336 | + public function in($x, $y, $type = null) { |
|
| 337 | + $x = $this->helper->quoteColumnName($x); |
|
| 338 | + $y = $this->helper->quoteColumnNames($y); |
|
| 339 | + return $this->expressionBuilder->in($x, $y); |
|
| 340 | + } |
|
| 341 | 341 | |
| 342 | - /** |
|
| 343 | - * Creates a NOT IN () comparison expression with the given arguments. |
|
| 344 | - * |
|
| 345 | - * @param string $x The field in string format to be inspected by NOT IN() comparison. |
|
| 346 | - * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison. |
|
| 347 | - * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 348 | - * required when comparing text fields for oci compatibility |
|
| 349 | - * |
|
| 350 | - * @return string |
|
| 351 | - */ |
|
| 352 | - public function notIn($x, $y, $type = null) { |
|
| 353 | - $x = $this->helper->quoteColumnName($x); |
|
| 354 | - $y = $this->helper->quoteColumnNames($y); |
|
| 355 | - return $this->expressionBuilder->notIn($x, $y); |
|
| 356 | - } |
|
| 342 | + /** |
|
| 343 | + * Creates a NOT IN () comparison expression with the given arguments. |
|
| 344 | + * |
|
| 345 | + * @param string $x The field in string format to be inspected by NOT IN() comparison. |
|
| 346 | + * @param string|array $y The placeholder or the array of values to be used by NOT IN() comparison. |
|
| 347 | + * @param mixed|null $type one of the IQueryBuilder::PARAM_* constants |
|
| 348 | + * required when comparing text fields for oci compatibility |
|
| 349 | + * |
|
| 350 | + * @return string |
|
| 351 | + */ |
|
| 352 | + public function notIn($x, $y, $type = null) { |
|
| 353 | + $x = $this->helper->quoteColumnName($x); |
|
| 354 | + $y = $this->helper->quoteColumnNames($y); |
|
| 355 | + return $this->expressionBuilder->notIn($x, $y); |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Creates a $x = '' statement, because Oracle needs a different check |
|
| 360 | - * |
|
| 361 | - * @param string $x The field in string format to be inspected by the comparison. |
|
| 362 | - * @return string |
|
| 363 | - * @since 13.0.0 |
|
| 364 | - */ |
|
| 365 | - public function emptyString($x) { |
|
| 366 | - return $this->eq($x, $this->literal('', IQueryBuilder::PARAM_STR)); |
|
| 367 | - } |
|
| 358 | + /** |
|
| 359 | + * Creates a $x = '' statement, because Oracle needs a different check |
|
| 360 | + * |
|
| 361 | + * @param string $x The field in string format to be inspected by the comparison. |
|
| 362 | + * @return string |
|
| 363 | + * @since 13.0.0 |
|
| 364 | + */ |
|
| 365 | + public function emptyString($x) { |
|
| 366 | + return $this->eq($x, $this->literal('', IQueryBuilder::PARAM_STR)); |
|
| 367 | + } |
|
| 368 | 368 | |
| 369 | - /** |
|
| 370 | - * Creates a `$x <> ''` statement, because Oracle needs a different check |
|
| 371 | - * |
|
| 372 | - * @param string $x The field in string format to be inspected by the comparison. |
|
| 373 | - * @return string |
|
| 374 | - * @since 13.0.0 |
|
| 375 | - */ |
|
| 376 | - public function nonEmptyString($x) { |
|
| 377 | - return $this->neq($x, $this->literal('', IQueryBuilder::PARAM_STR)); |
|
| 378 | - } |
|
| 369 | + /** |
|
| 370 | + * Creates a `$x <> ''` statement, because Oracle needs a different check |
|
| 371 | + * |
|
| 372 | + * @param string $x The field in string format to be inspected by the comparison. |
|
| 373 | + * @return string |
|
| 374 | + * @since 13.0.0 |
|
| 375 | + */ |
|
| 376 | + public function nonEmptyString($x) { |
|
| 377 | + return $this->neq($x, $this->literal('', IQueryBuilder::PARAM_STR)); |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * Binary AND Operator copies a bit to the result if it exists in both operands. |
|
| 382 | - * |
|
| 383 | - * @param string|ILiteral $x The field or value to check |
|
| 384 | - * @param int $y Bitmap that must be set |
|
| 385 | - * @return IQueryFunction |
|
| 386 | - * @since 12.0.0 |
|
| 387 | - */ |
|
| 388 | - public function bitwiseAnd($x, $y) { |
|
| 389 | - return new QueryFunction($this->connection->getDatabasePlatform()->getBitAndComparisonExpression( |
|
| 390 | - $this->helper->quoteColumnName($x), |
|
| 391 | - $y |
|
| 392 | - )); |
|
| 393 | - } |
|
| 380 | + /** |
|
| 381 | + * Binary AND Operator copies a bit to the result if it exists in both operands. |
|
| 382 | + * |
|
| 383 | + * @param string|ILiteral $x The field or value to check |
|
| 384 | + * @param int $y Bitmap that must be set |
|
| 385 | + * @return IQueryFunction |
|
| 386 | + * @since 12.0.0 |
|
| 387 | + */ |
|
| 388 | + public function bitwiseAnd($x, $y) { |
|
| 389 | + return new QueryFunction($this->connection->getDatabasePlatform()->getBitAndComparisonExpression( |
|
| 390 | + $this->helper->quoteColumnName($x), |
|
| 391 | + $y |
|
| 392 | + )); |
|
| 393 | + } |
|
| 394 | 394 | |
| 395 | - /** |
|
| 396 | - * Binary OR Operator copies a bit if it exists in either operand. |
|
| 397 | - * |
|
| 398 | - * @param string|ILiteral $x The field or value to check |
|
| 399 | - * @param int $y Bitmap that must be set |
|
| 400 | - * @return IQueryFunction |
|
| 401 | - * @since 12.0.0 |
|
| 402 | - */ |
|
| 403 | - public function bitwiseOr($x, $y) { |
|
| 404 | - return new QueryFunction($this->connection->getDatabasePlatform()->getBitOrComparisonExpression( |
|
| 405 | - $this->helper->quoteColumnName($x), |
|
| 406 | - $y |
|
| 407 | - )); |
|
| 408 | - } |
|
| 395 | + /** |
|
| 396 | + * Binary OR Operator copies a bit if it exists in either operand. |
|
| 397 | + * |
|
| 398 | + * @param string|ILiteral $x The field or value to check |
|
| 399 | + * @param int $y Bitmap that must be set |
|
| 400 | + * @return IQueryFunction |
|
| 401 | + * @since 12.0.0 |
|
| 402 | + */ |
|
| 403 | + public function bitwiseOr($x, $y) { |
|
| 404 | + return new QueryFunction($this->connection->getDatabasePlatform()->getBitOrComparisonExpression( |
|
| 405 | + $this->helper->quoteColumnName($x), |
|
| 406 | + $y |
|
| 407 | + )); |
|
| 408 | + } |
|
| 409 | 409 | |
| 410 | - /** |
|
| 411 | - * Quotes a given input parameter. |
|
| 412 | - * |
|
| 413 | - * @param mixed $input The parameter to be quoted. |
|
| 414 | - * @param mixed|null $type One of the IQueryBuilder::PARAM_* constants |
|
| 415 | - * |
|
| 416 | - * @return ILiteral |
|
| 417 | - */ |
|
| 418 | - public function literal($input, $type = null) { |
|
| 419 | - return new Literal($this->expressionBuilder->literal($input, $type)); |
|
| 420 | - } |
|
| 410 | + /** |
|
| 411 | + * Quotes a given input parameter. |
|
| 412 | + * |
|
| 413 | + * @param mixed $input The parameter to be quoted. |
|
| 414 | + * @param mixed|null $type One of the IQueryBuilder::PARAM_* constants |
|
| 415 | + * |
|
| 416 | + * @return ILiteral |
|
| 417 | + */ |
|
| 418 | + public function literal($input, $type = null) { |
|
| 419 | + return new Literal($this->expressionBuilder->literal($input, $type)); |
|
| 420 | + } |
|
| 421 | 421 | |
| 422 | - /** |
|
| 423 | - * Returns a IQueryFunction that casts the column to the given type |
|
| 424 | - * |
|
| 425 | - * @param string $column |
|
| 426 | - * @param mixed $type One of IQueryBuilder::PARAM_* |
|
| 427 | - * @return string |
|
| 428 | - */ |
|
| 429 | - public function castColumn($column, $type) { |
|
| 430 | - return new QueryFunction( |
|
| 431 | - $this->helper->quoteColumnName($column) |
|
| 432 | - ); |
|
| 433 | - } |
|
| 422 | + /** |
|
| 423 | + * Returns a IQueryFunction that casts the column to the given type |
|
| 424 | + * |
|
| 425 | + * @param string $column |
|
| 426 | + * @param mixed $type One of IQueryBuilder::PARAM_* |
|
| 427 | + * @return string |
|
| 428 | + */ |
|
| 429 | + public function castColumn($column, $type) { |
|
| 430 | + return new QueryFunction( |
|
| 431 | + $this->helper->quoteColumnName($column) |
|
| 432 | + ); |
|
| 433 | + } |
|
| 434 | 434 | } |
@@ -30,27 +30,27 @@ |
||
| 30 | 30 | |
| 31 | 31 | class MySqlExpressionBuilder extends ExpressionBuilder { |
| 32 | 32 | |
| 33 | - /** @var string */ |
|
| 34 | - protected $charset; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @param \OCP\IDBConnection|Connection $connection |
|
| 38 | - * @param IQueryBuilder $queryBuilder |
|
| 39 | - */ |
|
| 40 | - public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder) { |
|
| 41 | - parent::__construct($connection, $queryBuilder); |
|
| 42 | - |
|
| 43 | - $params = $connection->getParams(); |
|
| 44 | - $this->charset = isset($params['charset']) ? $params['charset'] : 'utf8'; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @inheritdoc |
|
| 49 | - */ |
|
| 50 | - public function iLike($x, $y, $type = null) { |
|
| 51 | - $x = $this->helper->quoteColumnName($x); |
|
| 52 | - $y = $this->helper->quoteColumnName($y); |
|
| 53 | - return $this->expressionBuilder->comparison($x, ' COLLATE ' . $this->charset . '_general_ci LIKE', $y); |
|
| 54 | - } |
|
| 33 | + /** @var string */ |
|
| 34 | + protected $charset; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @param \OCP\IDBConnection|Connection $connection |
|
| 38 | + * @param IQueryBuilder $queryBuilder |
|
| 39 | + */ |
|
| 40 | + public function __construct(IDBConnection $connection, IQueryBuilder $queryBuilder) { |
|
| 41 | + parent::__construct($connection, $queryBuilder); |
|
| 42 | + |
|
| 43 | + $params = $connection->getParams(); |
|
| 44 | + $this->charset = isset($params['charset']) ? $params['charset'] : 'utf8'; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @inheritdoc |
|
| 49 | + */ |
|
| 50 | + public function iLike($x, $y, $type = null) { |
|
| 51 | + $x = $this->helper->quoteColumnName($x); |
|
| 52 | + $y = $this->helper->quoteColumnName($y); |
|
| 53 | + return $this->expressionBuilder->comparison($x, ' COLLATE ' . $this->charset . '_general_ci LIKE', $y); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | 56 | } |
@@ -47,1165 +47,1165 @@ |
||
| 47 | 47 | |
| 48 | 48 | class QueryBuilder implements IQueryBuilder { |
| 49 | 49 | |
| 50 | - /** @var \OCP\IDBConnection */ |
|
| 51 | - private $connection; |
|
| 52 | - |
|
| 53 | - /** @var SystemConfig */ |
|
| 54 | - private $systemConfig; |
|
| 55 | - |
|
| 56 | - /** @var ILogger */ |
|
| 57 | - private $logger; |
|
| 58 | - |
|
| 59 | - /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
| 60 | - private $queryBuilder; |
|
| 61 | - |
|
| 62 | - /** @var QuoteHelper */ |
|
| 63 | - private $helper; |
|
| 64 | - |
|
| 65 | - /** @var bool */ |
|
| 66 | - private $automaticTablePrefix = true; |
|
| 67 | - |
|
| 68 | - /** @var string */ |
|
| 69 | - protected $lastInsertedTable; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Initializes a new QueryBuilder. |
|
| 73 | - * |
|
| 74 | - * @param IDBConnection $connection |
|
| 75 | - * @param SystemConfig $systemConfig |
|
| 76 | - * @param ILogger $logger |
|
| 77 | - */ |
|
| 78 | - public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) { |
|
| 79 | - $this->connection = $connection; |
|
| 80 | - $this->systemConfig = $systemConfig; |
|
| 81 | - $this->logger = $logger; |
|
| 82 | - $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); |
|
| 83 | - $this->helper = new QuoteHelper(); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 88 | - * |
|
| 89 | - * @param bool $enabled If set to true table names will be prefixed with the |
|
| 90 | - * owncloud database prefix automatically. |
|
| 91 | - * @since 8.2.0 |
|
| 92 | - */ |
|
| 93 | - public function automaticTablePrefix($enabled) { |
|
| 94 | - $this->automaticTablePrefix = (bool) $enabled; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 99 | - * This producer method is intended for convenient inline usage. Example: |
|
| 100 | - * |
|
| 101 | - * <code> |
|
| 102 | - * $qb = $conn->getQueryBuilder() |
|
| 103 | - * ->select('u') |
|
| 104 | - * ->from('users', 'u') |
|
| 105 | - * ->where($qb->expr()->eq('u.id', 1)); |
|
| 106 | - * </code> |
|
| 107 | - * |
|
| 108 | - * For more complex expression construction, consider storing the expression |
|
| 109 | - * builder object in a local variable. |
|
| 110 | - * |
|
| 111 | - * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 112 | - */ |
|
| 113 | - public function expr() { |
|
| 114 | - if ($this->connection instanceof OracleConnection) { |
|
| 115 | - return new OCIExpressionBuilder($this->connection, $this); |
|
| 116 | - } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 117 | - return new PgSqlExpressionBuilder($this->connection, $this); |
|
| 118 | - } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 119 | - return new MySqlExpressionBuilder($this->connection, $this); |
|
| 120 | - } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 121 | - return new SqliteExpressionBuilder($this->connection, $this); |
|
| 122 | - } else { |
|
| 123 | - return new ExpressionBuilder($this->connection, $this); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 129 | - * This producer method is intended for convenient inline usage. Example: |
|
| 130 | - * |
|
| 131 | - * <code> |
|
| 132 | - * $qb = $conn->getQueryBuilder() |
|
| 133 | - * ->select('u') |
|
| 134 | - * ->from('users', 'u') |
|
| 135 | - * ->where($qb->fun()->md5('u.id')); |
|
| 136 | - * </code> |
|
| 137 | - * |
|
| 138 | - * For more complex function construction, consider storing the function |
|
| 139 | - * builder object in a local variable. |
|
| 140 | - * |
|
| 141 | - * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 142 | - */ |
|
| 143 | - public function func() { |
|
| 144 | - if ($this->connection instanceof OracleConnection) { |
|
| 145 | - return new OCIFunctionBuilder($this->helper); |
|
| 146 | - } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 147 | - return new SqliteFunctionBuilder($this->helper); |
|
| 148 | - } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 149 | - return new PgSqlFunctionBuilder($this->helper); |
|
| 150 | - } else { |
|
| 151 | - return new FunctionBuilder($this->helper); |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Gets the type of the currently built query. |
|
| 157 | - * |
|
| 158 | - * @return integer |
|
| 159 | - */ |
|
| 160 | - public function getType() { |
|
| 161 | - return $this->queryBuilder->getType(); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Gets the associated DBAL Connection for this query builder. |
|
| 166 | - * |
|
| 167 | - * @return \OCP\IDBConnection |
|
| 168 | - */ |
|
| 169 | - public function getConnection() { |
|
| 170 | - return $this->connection; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Gets the state of this query builder instance. |
|
| 175 | - * |
|
| 176 | - * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
| 177 | - */ |
|
| 178 | - public function getState() { |
|
| 179 | - return $this->queryBuilder->getState(); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Executes this query using the bound parameters and their types. |
|
| 184 | - * |
|
| 185 | - * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 186 | - * for insert, update and delete statements. |
|
| 187 | - * |
|
| 188 | - * @return \Doctrine\DBAL\Driver\Statement|int |
|
| 189 | - */ |
|
| 190 | - public function execute() { |
|
| 191 | - if ($this->systemConfig->getValue('log_query', false)) { |
|
| 192 | - $params = []; |
|
| 193 | - foreach ($this->getParameters() as $placeholder => $value) { |
|
| 194 | - if (is_array($value)) { |
|
| 195 | - $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 196 | - } else { |
|
| 197 | - $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - if (empty($params)) { |
|
| 201 | - $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
| 202 | - 'query' => $this->getSQL(), |
|
| 203 | - 'app' => 'core', |
|
| 204 | - ]); |
|
| 205 | - } else { |
|
| 206 | - $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
| 207 | - 'query' => $this->getSQL(), |
|
| 208 | - 'params' => implode(', ', $params), |
|
| 209 | - 'app' => 'core', |
|
| 210 | - ]); |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return $this->queryBuilder->execute(); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 219 | - * |
|
| 220 | - * <code> |
|
| 221 | - * $qb = $conn->getQueryBuilder() |
|
| 222 | - * ->select('u') |
|
| 223 | - * ->from('User', 'u') |
|
| 224 | - * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 225 | - * </code> |
|
| 226 | - * |
|
| 227 | - * @return string The SQL query string. |
|
| 228 | - */ |
|
| 229 | - public function getSQL() { |
|
| 230 | - return $this->queryBuilder->getSQL(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Sets a query parameter for the query being constructed. |
|
| 235 | - * |
|
| 236 | - * <code> |
|
| 237 | - * $qb = $conn->getQueryBuilder() |
|
| 238 | - * ->select('u') |
|
| 239 | - * ->from('users', 'u') |
|
| 240 | - * ->where('u.id = :user_id') |
|
| 241 | - * ->setParameter(':user_id', 1); |
|
| 242 | - * </code> |
|
| 243 | - * |
|
| 244 | - * @param string|integer $key The parameter position or name. |
|
| 245 | - * @param mixed $value The parameter value. |
|
| 246 | - * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants. |
|
| 247 | - * |
|
| 248 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 249 | - */ |
|
| 250 | - public function setParameter($key, $value, $type = null) { |
|
| 251 | - $this->queryBuilder->setParameter($key, $value, $type); |
|
| 252 | - |
|
| 253 | - return $this; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Sets a collection of query parameters for the query being constructed. |
|
| 258 | - * |
|
| 259 | - * <code> |
|
| 260 | - * $qb = $conn->getQueryBuilder() |
|
| 261 | - * ->select('u') |
|
| 262 | - * ->from('users', 'u') |
|
| 263 | - * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 264 | - * ->setParameters(array( |
|
| 265 | - * ':user_id1' => 1, |
|
| 266 | - * ':user_id2' => 2 |
|
| 267 | - * )); |
|
| 268 | - * </code> |
|
| 269 | - * |
|
| 270 | - * @param array $params The query parameters to set. |
|
| 271 | - * @param array $types The query parameters types to set. |
|
| 272 | - * |
|
| 273 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 274 | - */ |
|
| 275 | - public function setParameters(array $params, array $types = array()) { |
|
| 276 | - $this->queryBuilder->setParameters($params, $types); |
|
| 277 | - |
|
| 278 | - return $this; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 283 | - * |
|
| 284 | - * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 285 | - */ |
|
| 286 | - public function getParameters() { |
|
| 287 | - return $this->queryBuilder->getParameters(); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Gets a (previously set) query parameter of the query being constructed. |
|
| 292 | - * |
|
| 293 | - * @param mixed $key The key (index or name) of the bound parameter. |
|
| 294 | - * |
|
| 295 | - * @return mixed The value of the bound parameter. |
|
| 296 | - */ |
|
| 297 | - public function getParameter($key) { |
|
| 298 | - return $this->queryBuilder->getParameter($key); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 303 | - * |
|
| 304 | - * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 305 | - */ |
|
| 306 | - public function getParameterTypes() { |
|
| 307 | - return $this->queryBuilder->getParameterTypes(); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Gets a (previously set) query parameter type of the query being constructed. |
|
| 312 | - * |
|
| 313 | - * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 314 | - * |
|
| 315 | - * @return mixed The value of the bound parameter type. |
|
| 316 | - */ |
|
| 317 | - public function getParameterType($key) { |
|
| 318 | - return $this->queryBuilder->getParameterType($key); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Sets the position of the first result to retrieve (the "offset"). |
|
| 323 | - * |
|
| 324 | - * @param integer $firstResult The first result to return. |
|
| 325 | - * |
|
| 326 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 327 | - */ |
|
| 328 | - public function setFirstResult($firstResult) { |
|
| 329 | - $this->queryBuilder->setFirstResult($firstResult); |
|
| 330 | - |
|
| 331 | - return $this; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 336 | - * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 337 | - * |
|
| 338 | - * @return integer The position of the first result. |
|
| 339 | - */ |
|
| 340 | - public function getFirstResult() { |
|
| 341 | - return $this->queryBuilder->getFirstResult(); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Sets the maximum number of results to retrieve (the "limit"). |
|
| 346 | - * |
|
| 347 | - * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
| 348 | - * of the databases will just return an empty result set, Oracle will return |
|
| 349 | - * all entries. |
|
| 350 | - * |
|
| 351 | - * @param integer $maxResults The maximum number of results to retrieve. |
|
| 352 | - * |
|
| 353 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 354 | - */ |
|
| 355 | - public function setMaxResults($maxResults) { |
|
| 356 | - $this->queryBuilder->setMaxResults($maxResults); |
|
| 357 | - |
|
| 358 | - return $this; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - /** |
|
| 362 | - * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 363 | - * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 364 | - * |
|
| 365 | - * @return integer The maximum number of results. |
|
| 366 | - */ |
|
| 367 | - public function getMaxResults() { |
|
| 368 | - return $this->queryBuilder->getMaxResults(); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Specifies an item that is to be returned in the query result. |
|
| 373 | - * Replaces any previously specified selections, if any. |
|
| 374 | - * |
|
| 375 | - * <code> |
|
| 376 | - * $qb = $conn->getQueryBuilder() |
|
| 377 | - * ->select('u.id', 'p.id') |
|
| 378 | - * ->from('users', 'u') |
|
| 379 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 380 | - * </code> |
|
| 381 | - * |
|
| 382 | - * @param mixed $select The selection expressions. |
|
| 383 | - * |
|
| 384 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 385 | - */ |
|
| 386 | - public function select($select = null) { |
|
| 387 | - $selects = is_array($select) ? $select : func_get_args(); |
|
| 388 | - |
|
| 389 | - $this->queryBuilder->select( |
|
| 390 | - $this->helper->quoteColumnNames($selects) |
|
| 391 | - ); |
|
| 392 | - |
|
| 393 | - return $this; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Specifies an item that is to be returned with a different name in the query result. |
|
| 398 | - * |
|
| 399 | - * <code> |
|
| 400 | - * $qb = $conn->getQueryBuilder() |
|
| 401 | - * ->selectAlias('u.id', 'user_id') |
|
| 402 | - * ->from('users', 'u') |
|
| 403 | - * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 404 | - * </code> |
|
| 405 | - * |
|
| 406 | - * @param mixed $select The selection expressions. |
|
| 407 | - * @param string $alias The column alias used in the constructed query. |
|
| 408 | - * |
|
| 409 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 410 | - */ |
|
| 411 | - public function selectAlias($select, $alias) { |
|
| 412 | - |
|
| 413 | - $this->queryBuilder->addSelect( |
|
| 414 | - $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 415 | - ); |
|
| 416 | - |
|
| 417 | - return $this; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Specifies an item that is to be returned uniquely in the query result. |
|
| 422 | - * |
|
| 423 | - * <code> |
|
| 424 | - * $qb = $conn->getQueryBuilder() |
|
| 425 | - * ->selectDistinct('type') |
|
| 426 | - * ->from('users'); |
|
| 427 | - * </code> |
|
| 428 | - * |
|
| 429 | - * @param mixed $select The selection expressions. |
|
| 430 | - * |
|
| 431 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 432 | - */ |
|
| 433 | - public function selectDistinct($select) { |
|
| 434 | - |
|
| 435 | - $this->queryBuilder->addSelect( |
|
| 436 | - 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
| 437 | - ); |
|
| 438 | - |
|
| 439 | - return $this; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Adds an item that is to be returned in the query result. |
|
| 444 | - * |
|
| 445 | - * <code> |
|
| 446 | - * $qb = $conn->getQueryBuilder() |
|
| 447 | - * ->select('u.id') |
|
| 448 | - * ->addSelect('p.id') |
|
| 449 | - * ->from('users', 'u') |
|
| 450 | - * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 451 | - * </code> |
|
| 452 | - * |
|
| 453 | - * @param mixed $select The selection expression. |
|
| 454 | - * |
|
| 455 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 456 | - */ |
|
| 457 | - public function addSelect($select = null) { |
|
| 458 | - $selects = is_array($select) ? $select : func_get_args(); |
|
| 459 | - |
|
| 460 | - $this->queryBuilder->addSelect( |
|
| 461 | - $this->helper->quoteColumnNames($selects) |
|
| 462 | - ); |
|
| 463 | - |
|
| 464 | - return $this; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Turns the query being built into a bulk delete query that ranges over |
|
| 469 | - * a certain table. |
|
| 470 | - * |
|
| 471 | - * <code> |
|
| 472 | - * $qb = $conn->getQueryBuilder() |
|
| 473 | - * ->delete('users', 'u') |
|
| 474 | - * ->where('u.id = :user_id'); |
|
| 475 | - * ->setParameter(':user_id', 1); |
|
| 476 | - * </code> |
|
| 477 | - * |
|
| 478 | - * @param string $delete The table whose rows are subject to the deletion. |
|
| 479 | - * @param string $alias The table alias used in the constructed query. |
|
| 480 | - * |
|
| 481 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 482 | - */ |
|
| 483 | - public function delete($delete = null, $alias = null) { |
|
| 484 | - $this->queryBuilder->delete( |
|
| 485 | - $this->getTableName($delete), |
|
| 486 | - $alias |
|
| 487 | - ); |
|
| 488 | - |
|
| 489 | - return $this; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * Turns the query being built into a bulk update query that ranges over |
|
| 494 | - * a certain table |
|
| 495 | - * |
|
| 496 | - * <code> |
|
| 497 | - * $qb = $conn->getQueryBuilder() |
|
| 498 | - * ->update('users', 'u') |
|
| 499 | - * ->set('u.password', md5('password')) |
|
| 500 | - * ->where('u.id = ?'); |
|
| 501 | - * </code> |
|
| 502 | - * |
|
| 503 | - * @param string $update The table whose rows are subject to the update. |
|
| 504 | - * @param string $alias The table alias used in the constructed query. |
|
| 505 | - * |
|
| 506 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 507 | - */ |
|
| 508 | - public function update($update = null, $alias = null) { |
|
| 509 | - $this->queryBuilder->update( |
|
| 510 | - $this->getTableName($update), |
|
| 511 | - $alias |
|
| 512 | - ); |
|
| 513 | - |
|
| 514 | - return $this; |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * Turns the query being built into an insert query that inserts into |
|
| 519 | - * a certain table |
|
| 520 | - * |
|
| 521 | - * <code> |
|
| 522 | - * $qb = $conn->getQueryBuilder() |
|
| 523 | - * ->insert('users') |
|
| 524 | - * ->values( |
|
| 525 | - * array( |
|
| 526 | - * 'name' => '?', |
|
| 527 | - * 'password' => '?' |
|
| 528 | - * ) |
|
| 529 | - * ); |
|
| 530 | - * </code> |
|
| 531 | - * |
|
| 532 | - * @param string $insert The table into which the rows should be inserted. |
|
| 533 | - * |
|
| 534 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 535 | - */ |
|
| 536 | - public function insert($insert = null) { |
|
| 537 | - $this->queryBuilder->insert( |
|
| 538 | - $this->getTableName($insert) |
|
| 539 | - ); |
|
| 540 | - |
|
| 541 | - $this->lastInsertedTable = $insert; |
|
| 542 | - |
|
| 543 | - return $this; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * Creates and adds a query root corresponding to the table identified by the |
|
| 548 | - * given alias, forming a cartesian product with any existing query roots. |
|
| 549 | - * |
|
| 550 | - * <code> |
|
| 551 | - * $qb = $conn->getQueryBuilder() |
|
| 552 | - * ->select('u.id') |
|
| 553 | - * ->from('users', 'u') |
|
| 554 | - * </code> |
|
| 555 | - * |
|
| 556 | - * @param string $from The table. |
|
| 557 | - * @param string|null $alias The alias of the table. |
|
| 558 | - * |
|
| 559 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 560 | - */ |
|
| 561 | - public function from($from, $alias = null) { |
|
| 562 | - $this->queryBuilder->from( |
|
| 563 | - $this->getTableName($from), |
|
| 564 | - $this->quoteAlias($alias) |
|
| 565 | - ); |
|
| 566 | - |
|
| 567 | - return $this; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * Creates and adds a join to the query. |
|
| 572 | - * |
|
| 573 | - * <code> |
|
| 574 | - * $qb = $conn->getQueryBuilder() |
|
| 575 | - * ->select('u.name') |
|
| 576 | - * ->from('users', 'u') |
|
| 577 | - * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 578 | - * </code> |
|
| 579 | - * |
|
| 580 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 581 | - * @param string $join The table name to join. |
|
| 582 | - * @param string $alias The alias of the join table. |
|
| 583 | - * @param string $condition The condition for the join. |
|
| 584 | - * |
|
| 585 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 586 | - */ |
|
| 587 | - public function join($fromAlias, $join, $alias, $condition = null) { |
|
| 588 | - $this->queryBuilder->join( |
|
| 589 | - $this->quoteAlias($fromAlias), |
|
| 590 | - $this->getTableName($join), |
|
| 591 | - $this->quoteAlias($alias), |
|
| 592 | - $condition |
|
| 593 | - ); |
|
| 594 | - |
|
| 595 | - return $this; |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Creates and adds a join to the query. |
|
| 600 | - * |
|
| 601 | - * <code> |
|
| 602 | - * $qb = $conn->getQueryBuilder() |
|
| 603 | - * ->select('u.name') |
|
| 604 | - * ->from('users', 'u') |
|
| 605 | - * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 606 | - * </code> |
|
| 607 | - * |
|
| 608 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 609 | - * @param string $join The table name to join. |
|
| 610 | - * @param string $alias The alias of the join table. |
|
| 611 | - * @param string $condition The condition for the join. |
|
| 612 | - * |
|
| 613 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 614 | - */ |
|
| 615 | - public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 616 | - $this->queryBuilder->innerJoin( |
|
| 617 | - $this->quoteAlias($fromAlias), |
|
| 618 | - $this->getTableName($join), |
|
| 619 | - $this->quoteAlias($alias), |
|
| 620 | - $condition |
|
| 621 | - ); |
|
| 622 | - |
|
| 623 | - return $this; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * Creates and adds a left join to the query. |
|
| 628 | - * |
|
| 629 | - * <code> |
|
| 630 | - * $qb = $conn->getQueryBuilder() |
|
| 631 | - * ->select('u.name') |
|
| 632 | - * ->from('users', 'u') |
|
| 633 | - * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 634 | - * </code> |
|
| 635 | - * |
|
| 636 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 637 | - * @param string $join The table name to join. |
|
| 638 | - * @param string $alias The alias of the join table. |
|
| 639 | - * @param string $condition The condition for the join. |
|
| 640 | - * |
|
| 641 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 642 | - */ |
|
| 643 | - public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 644 | - $this->queryBuilder->leftJoin( |
|
| 645 | - $this->quoteAlias($fromAlias), |
|
| 646 | - $this->getTableName($join), |
|
| 647 | - $this->quoteAlias($alias), |
|
| 648 | - $condition |
|
| 649 | - ); |
|
| 650 | - |
|
| 651 | - return $this; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - /** |
|
| 655 | - * Creates and adds a right join to the query. |
|
| 656 | - * |
|
| 657 | - * <code> |
|
| 658 | - * $qb = $conn->getQueryBuilder() |
|
| 659 | - * ->select('u.name') |
|
| 660 | - * ->from('users', 'u') |
|
| 661 | - * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 662 | - * </code> |
|
| 663 | - * |
|
| 664 | - * @param string $fromAlias The alias that points to a from clause. |
|
| 665 | - * @param string $join The table name to join. |
|
| 666 | - * @param string $alias The alias of the join table. |
|
| 667 | - * @param string $condition The condition for the join. |
|
| 668 | - * |
|
| 669 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 670 | - */ |
|
| 671 | - public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 672 | - $this->queryBuilder->rightJoin( |
|
| 673 | - $this->quoteAlias($fromAlias), |
|
| 674 | - $this->getTableName($join), |
|
| 675 | - $this->quoteAlias($alias), |
|
| 676 | - $condition |
|
| 677 | - ); |
|
| 678 | - |
|
| 679 | - return $this; |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - /** |
|
| 683 | - * Sets a new value for a column in a bulk update query. |
|
| 684 | - * |
|
| 685 | - * <code> |
|
| 686 | - * $qb = $conn->getQueryBuilder() |
|
| 687 | - * ->update('users', 'u') |
|
| 688 | - * ->set('u.password', md5('password')) |
|
| 689 | - * ->where('u.id = ?'); |
|
| 690 | - * </code> |
|
| 691 | - * |
|
| 692 | - * @param string $key The column to set. |
|
| 693 | - * @param string $value The value, expression, placeholder, etc. |
|
| 694 | - * |
|
| 695 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 696 | - */ |
|
| 697 | - public function set($key, $value) { |
|
| 698 | - $this->queryBuilder->set( |
|
| 699 | - $this->helper->quoteColumnName($key), |
|
| 700 | - $this->helper->quoteColumnName($value) |
|
| 701 | - ); |
|
| 702 | - |
|
| 703 | - return $this; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * Specifies one or more restrictions to the query result. |
|
| 708 | - * Replaces any previously specified restrictions, if any. |
|
| 709 | - * |
|
| 710 | - * <code> |
|
| 711 | - * $qb = $conn->getQueryBuilder() |
|
| 712 | - * ->select('u.name') |
|
| 713 | - * ->from('users', 'u') |
|
| 714 | - * ->where('u.id = ?'); |
|
| 715 | - * |
|
| 716 | - * // You can optionally programatically build and/or expressions |
|
| 717 | - * $qb = $conn->getQueryBuilder(); |
|
| 718 | - * |
|
| 719 | - * $or = $qb->expr()->orx(); |
|
| 720 | - * $or->add($qb->expr()->eq('u.id', 1)); |
|
| 721 | - * $or->add($qb->expr()->eq('u.id', 2)); |
|
| 722 | - * |
|
| 723 | - * $qb->update('users', 'u') |
|
| 724 | - * ->set('u.password', md5('password')) |
|
| 725 | - * ->where($or); |
|
| 726 | - * </code> |
|
| 727 | - * |
|
| 728 | - * @param mixed $predicates The restriction predicates. |
|
| 729 | - * |
|
| 730 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 731 | - */ |
|
| 732 | - public function where($predicates) { |
|
| 733 | - call_user_func_array( |
|
| 734 | - [$this->queryBuilder, 'where'], |
|
| 735 | - func_get_args() |
|
| 736 | - ); |
|
| 737 | - |
|
| 738 | - return $this; |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - /** |
|
| 742 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 743 | - * conjunction with any previously specified restrictions. |
|
| 744 | - * |
|
| 745 | - * <code> |
|
| 746 | - * $qb = $conn->getQueryBuilder() |
|
| 747 | - * ->select('u') |
|
| 748 | - * ->from('users', 'u') |
|
| 749 | - * ->where('u.username LIKE ?') |
|
| 750 | - * ->andWhere('u.is_active = 1'); |
|
| 751 | - * </code> |
|
| 752 | - * |
|
| 753 | - * @param mixed $where The query restrictions. |
|
| 754 | - * |
|
| 755 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 756 | - * |
|
| 757 | - * @see where() |
|
| 758 | - */ |
|
| 759 | - public function andWhere($where) { |
|
| 760 | - call_user_func_array( |
|
| 761 | - [$this->queryBuilder, 'andWhere'], |
|
| 762 | - func_get_args() |
|
| 763 | - ); |
|
| 764 | - |
|
| 765 | - return $this; |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - /** |
|
| 769 | - * Adds one or more restrictions to the query results, forming a logical |
|
| 770 | - * disjunction with any previously specified restrictions. |
|
| 771 | - * |
|
| 772 | - * <code> |
|
| 773 | - * $qb = $conn->getQueryBuilder() |
|
| 774 | - * ->select('u.name') |
|
| 775 | - * ->from('users', 'u') |
|
| 776 | - * ->where('u.id = 1') |
|
| 777 | - * ->orWhere('u.id = 2'); |
|
| 778 | - * </code> |
|
| 779 | - * |
|
| 780 | - * @param mixed $where The WHERE statement. |
|
| 781 | - * |
|
| 782 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 783 | - * |
|
| 784 | - * @see where() |
|
| 785 | - */ |
|
| 786 | - public function orWhere($where) { |
|
| 787 | - call_user_func_array( |
|
| 788 | - [$this->queryBuilder, 'orWhere'], |
|
| 789 | - func_get_args() |
|
| 790 | - ); |
|
| 791 | - |
|
| 792 | - return $this; |
|
| 793 | - } |
|
| 794 | - |
|
| 795 | - /** |
|
| 796 | - * Specifies a grouping over the results of the query. |
|
| 797 | - * Replaces any previously specified groupings, if any. |
|
| 798 | - * |
|
| 799 | - * <code> |
|
| 800 | - * $qb = $conn->getQueryBuilder() |
|
| 801 | - * ->select('u.name') |
|
| 802 | - * ->from('users', 'u') |
|
| 803 | - * ->groupBy('u.id'); |
|
| 804 | - * </code> |
|
| 805 | - * |
|
| 806 | - * @param mixed $groupBy The grouping expression. |
|
| 807 | - * |
|
| 808 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 809 | - */ |
|
| 810 | - public function groupBy($groupBy) { |
|
| 811 | - $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 812 | - |
|
| 813 | - call_user_func_array( |
|
| 814 | - [$this->queryBuilder, 'groupBy'], |
|
| 815 | - $this->helper->quoteColumnNames($groupBys) |
|
| 816 | - ); |
|
| 817 | - |
|
| 818 | - return $this; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - /** |
|
| 822 | - * Adds a grouping expression to the query. |
|
| 823 | - * |
|
| 824 | - * <code> |
|
| 825 | - * $qb = $conn->getQueryBuilder() |
|
| 826 | - * ->select('u.name') |
|
| 827 | - * ->from('users', 'u') |
|
| 828 | - * ->groupBy('u.lastLogin'); |
|
| 829 | - * ->addGroupBy('u.createdAt') |
|
| 830 | - * </code> |
|
| 831 | - * |
|
| 832 | - * @param mixed $groupBy The grouping expression. |
|
| 833 | - * |
|
| 834 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 835 | - */ |
|
| 836 | - public function addGroupBy($groupBy) { |
|
| 837 | - $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 838 | - |
|
| 839 | - call_user_func_array( |
|
| 840 | - [$this->queryBuilder, 'addGroupBy'], |
|
| 841 | - $this->helper->quoteColumnNames($groupBys) |
|
| 842 | - ); |
|
| 843 | - |
|
| 844 | - return $this; |
|
| 845 | - } |
|
| 846 | - |
|
| 847 | - /** |
|
| 848 | - * Sets a value for a column in an insert query. |
|
| 849 | - * |
|
| 850 | - * <code> |
|
| 851 | - * $qb = $conn->getQueryBuilder() |
|
| 852 | - * ->insert('users') |
|
| 853 | - * ->values( |
|
| 854 | - * array( |
|
| 855 | - * 'name' => '?' |
|
| 856 | - * ) |
|
| 857 | - * ) |
|
| 858 | - * ->setValue('password', '?'); |
|
| 859 | - * </code> |
|
| 860 | - * |
|
| 861 | - * @param string $column The column into which the value should be inserted. |
|
| 862 | - * @param string $value The value that should be inserted into the column. |
|
| 863 | - * |
|
| 864 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 865 | - */ |
|
| 866 | - public function setValue($column, $value) { |
|
| 867 | - $this->queryBuilder->setValue( |
|
| 868 | - $this->helper->quoteColumnName($column), |
|
| 869 | - $value |
|
| 870 | - ); |
|
| 871 | - |
|
| 872 | - return $this; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - /** |
|
| 876 | - * Specifies values for an insert query indexed by column names. |
|
| 877 | - * Replaces any previous values, if any. |
|
| 878 | - * |
|
| 879 | - * <code> |
|
| 880 | - * $qb = $conn->getQueryBuilder() |
|
| 881 | - * ->insert('users') |
|
| 882 | - * ->values( |
|
| 883 | - * array( |
|
| 884 | - * 'name' => '?', |
|
| 885 | - * 'password' => '?' |
|
| 886 | - * ) |
|
| 887 | - * ); |
|
| 888 | - * </code> |
|
| 889 | - * |
|
| 890 | - * @param array $values The values to specify for the insert query indexed by column names. |
|
| 891 | - * |
|
| 892 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 893 | - */ |
|
| 894 | - public function values(array $values) { |
|
| 895 | - $quotedValues = []; |
|
| 896 | - foreach ($values as $key => $value) { |
|
| 897 | - $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - $this->queryBuilder->values($quotedValues); |
|
| 901 | - |
|
| 902 | - return $this; |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - /** |
|
| 906 | - * Specifies a restriction over the groups of the query. |
|
| 907 | - * Replaces any previous having restrictions, if any. |
|
| 908 | - * |
|
| 909 | - * @param mixed $having The restriction over the groups. |
|
| 910 | - * |
|
| 911 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 912 | - */ |
|
| 913 | - public function having($having) { |
|
| 914 | - call_user_func_array( |
|
| 915 | - [$this->queryBuilder, 'having'], |
|
| 916 | - func_get_args() |
|
| 917 | - ); |
|
| 918 | - |
|
| 919 | - return $this; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - /** |
|
| 923 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 924 | - * conjunction with any existing having restrictions. |
|
| 925 | - * |
|
| 926 | - * @param mixed $having The restriction to append. |
|
| 927 | - * |
|
| 928 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 929 | - */ |
|
| 930 | - public function andHaving($having) { |
|
| 931 | - call_user_func_array( |
|
| 932 | - [$this->queryBuilder, 'andHaving'], |
|
| 933 | - func_get_args() |
|
| 934 | - ); |
|
| 935 | - |
|
| 936 | - return $this; |
|
| 937 | - } |
|
| 938 | - |
|
| 939 | - /** |
|
| 940 | - * Adds a restriction over the groups of the query, forming a logical |
|
| 941 | - * disjunction with any existing having restrictions. |
|
| 942 | - * |
|
| 943 | - * @param mixed $having The restriction to add. |
|
| 944 | - * |
|
| 945 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 946 | - */ |
|
| 947 | - public function orHaving($having) { |
|
| 948 | - call_user_func_array( |
|
| 949 | - [$this->queryBuilder, 'orHaving'], |
|
| 950 | - func_get_args() |
|
| 951 | - ); |
|
| 952 | - |
|
| 953 | - return $this; |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - /** |
|
| 957 | - * Specifies an ordering for the query results. |
|
| 958 | - * Replaces any previously specified orderings, if any. |
|
| 959 | - * |
|
| 960 | - * @param string $sort The ordering expression. |
|
| 961 | - * @param string $order The ordering direction. |
|
| 962 | - * |
|
| 963 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 964 | - */ |
|
| 965 | - public function orderBy($sort, $order = null) { |
|
| 966 | - $this->queryBuilder->orderBy( |
|
| 967 | - $this->helper->quoteColumnName($sort), |
|
| 968 | - $order |
|
| 969 | - ); |
|
| 970 | - |
|
| 971 | - return $this; |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - /** |
|
| 975 | - * Adds an ordering to the query results. |
|
| 976 | - * |
|
| 977 | - * @param string $sort The ordering expression. |
|
| 978 | - * @param string $order The ordering direction. |
|
| 979 | - * |
|
| 980 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 981 | - */ |
|
| 982 | - public function addOrderBy($sort, $order = null) { |
|
| 983 | - $this->queryBuilder->addOrderBy( |
|
| 984 | - $this->helper->quoteColumnName($sort), |
|
| 985 | - $order |
|
| 986 | - ); |
|
| 987 | - |
|
| 988 | - return $this; |
|
| 989 | - } |
|
| 990 | - |
|
| 991 | - /** |
|
| 992 | - * Gets a query part by its name. |
|
| 993 | - * |
|
| 994 | - * @param string $queryPartName |
|
| 995 | - * |
|
| 996 | - * @return mixed |
|
| 997 | - */ |
|
| 998 | - public function getQueryPart($queryPartName) { |
|
| 999 | - return $this->queryBuilder->getQueryPart($queryPartName); |
|
| 1000 | - } |
|
| 1001 | - |
|
| 1002 | - /** |
|
| 1003 | - * Gets all query parts. |
|
| 1004 | - * |
|
| 1005 | - * @return array |
|
| 1006 | - */ |
|
| 1007 | - public function getQueryParts() { |
|
| 1008 | - return $this->queryBuilder->getQueryParts(); |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - /** |
|
| 1012 | - * Resets SQL parts. |
|
| 1013 | - * |
|
| 1014 | - * @param array|null $queryPartNames |
|
| 1015 | - * |
|
| 1016 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1017 | - */ |
|
| 1018 | - public function resetQueryParts($queryPartNames = null) { |
|
| 1019 | - $this->queryBuilder->resetQueryParts($queryPartNames); |
|
| 1020 | - |
|
| 1021 | - return $this; |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - /** |
|
| 1025 | - * Resets a single SQL part. |
|
| 1026 | - * |
|
| 1027 | - * @param string $queryPartName |
|
| 1028 | - * |
|
| 1029 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1030 | - */ |
|
| 1031 | - public function resetQueryPart($queryPartName) { |
|
| 1032 | - $this->queryBuilder->resetQueryPart($queryPartName); |
|
| 1033 | - |
|
| 1034 | - return $this; |
|
| 1035 | - } |
|
| 1036 | - |
|
| 1037 | - /** |
|
| 1038 | - * Creates a new named parameter and bind the value $value to it. |
|
| 1039 | - * |
|
| 1040 | - * This method provides a shortcut for PDOStatement::bindValue |
|
| 1041 | - * when using prepared statements. |
|
| 1042 | - * |
|
| 1043 | - * The parameter $value specifies the value that you want to bind. If |
|
| 1044 | - * $placeholder is not provided bindValue() will automatically create a |
|
| 1045 | - * placeholder for you. An automatic placeholder will be of the name |
|
| 1046 | - * ':dcValue1', ':dcValue2' etc. |
|
| 1047 | - * |
|
| 1048 | - * For more information see {@link http://php.net/pdostatement-bindparam} |
|
| 1049 | - * |
|
| 1050 | - * Example: |
|
| 1051 | - * <code> |
|
| 1052 | - * $value = 2; |
|
| 1053 | - * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 1054 | - * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 1055 | - * </code> |
|
| 1056 | - * |
|
| 1057 | - * @license New BSD License |
|
| 1058 | - * @link http://www.zetacomponents.org |
|
| 1059 | - * |
|
| 1060 | - * @param mixed $value |
|
| 1061 | - * @param mixed $type |
|
| 1062 | - * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 1063 | - * |
|
| 1064 | - * @return IParameter the placeholder name used. |
|
| 1065 | - */ |
|
| 1066 | - public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
| 1067 | - return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - /** |
|
| 1071 | - * Creates a new positional parameter and bind the given value to it. |
|
| 1072 | - * |
|
| 1073 | - * Attention: If you are using positional parameters with the query builder you have |
|
| 1074 | - * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 1075 | - * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 1076 | - * bugs in your code. |
|
| 1077 | - * |
|
| 1078 | - * Example: |
|
| 1079 | - * <code> |
|
| 1080 | - * $qb = $conn->getQueryBuilder(); |
|
| 1081 | - * $qb->select('u.*') |
|
| 1082 | - * ->from('users', 'u') |
|
| 1083 | - * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 1084 | - * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 1085 | - * </code> |
|
| 1086 | - * |
|
| 1087 | - * @param mixed $value |
|
| 1088 | - * @param integer $type |
|
| 1089 | - * |
|
| 1090 | - * @return IParameter |
|
| 1091 | - */ |
|
| 1092 | - public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
| 1093 | - return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
| 1094 | - } |
|
| 1095 | - |
|
| 1096 | - /** |
|
| 1097 | - * Creates a new parameter |
|
| 1098 | - * |
|
| 1099 | - * Example: |
|
| 1100 | - * <code> |
|
| 1101 | - * $qb = $conn->getQueryBuilder(); |
|
| 1102 | - * $qb->select('u.*') |
|
| 1103 | - * ->from('users', 'u') |
|
| 1104 | - * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 1105 | - * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 1106 | - * </code> |
|
| 1107 | - * |
|
| 1108 | - * @param string $name |
|
| 1109 | - * |
|
| 1110 | - * @return IParameter |
|
| 1111 | - */ |
|
| 1112 | - public function createParameter($name) { |
|
| 1113 | - return new Parameter(':' . $name); |
|
| 1114 | - } |
|
| 1115 | - |
|
| 1116 | - /** |
|
| 1117 | - * Creates a new function |
|
| 1118 | - * |
|
| 1119 | - * Attention: Column names inside the call have to be quoted before hand |
|
| 1120 | - * |
|
| 1121 | - * Example: |
|
| 1122 | - * <code> |
|
| 1123 | - * $qb = $conn->getQueryBuilder(); |
|
| 1124 | - * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 1125 | - * ->from('users', 'u') |
|
| 1126 | - * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 1127 | - * </code> |
|
| 1128 | - * <code> |
|
| 1129 | - * $qb = $conn->getQueryBuilder(); |
|
| 1130 | - * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 1131 | - * ->from('users', 'u') |
|
| 1132 | - * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 1133 | - * </code> |
|
| 1134 | - * |
|
| 1135 | - * @param string $call |
|
| 1136 | - * |
|
| 1137 | - * @return IQueryFunction |
|
| 1138 | - */ |
|
| 1139 | - public function createFunction($call) { |
|
| 1140 | - return new QueryFunction($call); |
|
| 1141 | - } |
|
| 1142 | - |
|
| 1143 | - /** |
|
| 1144 | - * Used to get the id of the last inserted element |
|
| 1145 | - * @return int |
|
| 1146 | - * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 1147 | - */ |
|
| 1148 | - public function getLastInsertId() { |
|
| 1149 | - if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
| 1150 | - // lastInsertId() needs the prefix but no quotes |
|
| 1151 | - $table = $this->prefixTableName($this->lastInsertedTable); |
|
| 1152 | - return (int) $this->connection->lastInsertId($table); |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
| 1156 | - } |
|
| 1157 | - |
|
| 1158 | - /** |
|
| 1159 | - * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 1160 | - * |
|
| 1161 | - * @param string $table |
|
| 1162 | - * @return string |
|
| 1163 | - */ |
|
| 1164 | - public function getTableName($table) { |
|
| 1165 | - $table = $this->prefixTableName($table); |
|
| 1166 | - return $this->helper->quoteColumnName($table); |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - /** |
|
| 1170 | - * Returns the table name with database prefix as needed by the implementation |
|
| 1171 | - * |
|
| 1172 | - * @param string $table |
|
| 1173 | - * @return string |
|
| 1174 | - */ |
|
| 1175 | - protected function prefixTableName($table) { |
|
| 1176 | - if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) { |
|
| 1177 | - return $table; |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - return '*PREFIX*' . $table; |
|
| 1181 | - } |
|
| 1182 | - |
|
| 1183 | - /** |
|
| 1184 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1185 | - * |
|
| 1186 | - * @param string $column |
|
| 1187 | - * @param string $tableAlias |
|
| 1188 | - * @return string |
|
| 1189 | - */ |
|
| 1190 | - public function getColumnName($column, $tableAlias = '') { |
|
| 1191 | - if ($tableAlias !== '') { |
|
| 1192 | - $tableAlias .= '.'; |
|
| 1193 | - } |
|
| 1194 | - |
|
| 1195 | - return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1196 | - } |
|
| 1197 | - |
|
| 1198 | - /** |
|
| 1199 | - * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1200 | - * |
|
| 1201 | - * @param string $alias |
|
| 1202 | - * @return string |
|
| 1203 | - */ |
|
| 1204 | - public function quoteAlias($alias) { |
|
| 1205 | - if ($alias === '' || $alias === null) { |
|
| 1206 | - return $alias; |
|
| 1207 | - } |
|
| 1208 | - |
|
| 1209 | - return $this->helper->quoteColumnName($alias); |
|
| 1210 | - } |
|
| 50 | + /** @var \OCP\IDBConnection */ |
|
| 51 | + private $connection; |
|
| 52 | + |
|
| 53 | + /** @var SystemConfig */ |
|
| 54 | + private $systemConfig; |
|
| 55 | + |
|
| 56 | + /** @var ILogger */ |
|
| 57 | + private $logger; |
|
| 58 | + |
|
| 59 | + /** @var \Doctrine\DBAL\Query\QueryBuilder */ |
|
| 60 | + private $queryBuilder; |
|
| 61 | + |
|
| 62 | + /** @var QuoteHelper */ |
|
| 63 | + private $helper; |
|
| 64 | + |
|
| 65 | + /** @var bool */ |
|
| 66 | + private $automaticTablePrefix = true; |
|
| 67 | + |
|
| 68 | + /** @var string */ |
|
| 69 | + protected $lastInsertedTable; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Initializes a new QueryBuilder. |
|
| 73 | + * |
|
| 74 | + * @param IDBConnection $connection |
|
| 75 | + * @param SystemConfig $systemConfig |
|
| 76 | + * @param ILogger $logger |
|
| 77 | + */ |
|
| 78 | + public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) { |
|
| 79 | + $this->connection = $connection; |
|
| 80 | + $this->systemConfig = $systemConfig; |
|
| 81 | + $this->logger = $logger; |
|
| 82 | + $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection); |
|
| 83 | + $this->helper = new QuoteHelper(); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Enable/disable automatic prefixing of table names with the oc_ prefix |
|
| 88 | + * |
|
| 89 | + * @param bool $enabled If set to true table names will be prefixed with the |
|
| 90 | + * owncloud database prefix automatically. |
|
| 91 | + * @since 8.2.0 |
|
| 92 | + */ |
|
| 93 | + public function automaticTablePrefix($enabled) { |
|
| 94 | + $this->automaticTablePrefix = (bool) $enabled; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Gets an ExpressionBuilder used for object-oriented construction of query expressions. |
|
| 99 | + * This producer method is intended for convenient inline usage. Example: |
|
| 100 | + * |
|
| 101 | + * <code> |
|
| 102 | + * $qb = $conn->getQueryBuilder() |
|
| 103 | + * ->select('u') |
|
| 104 | + * ->from('users', 'u') |
|
| 105 | + * ->where($qb->expr()->eq('u.id', 1)); |
|
| 106 | + * </code> |
|
| 107 | + * |
|
| 108 | + * For more complex expression construction, consider storing the expression |
|
| 109 | + * builder object in a local variable. |
|
| 110 | + * |
|
| 111 | + * @return \OCP\DB\QueryBuilder\IExpressionBuilder |
|
| 112 | + */ |
|
| 113 | + public function expr() { |
|
| 114 | + if ($this->connection instanceof OracleConnection) { |
|
| 115 | + return new OCIExpressionBuilder($this->connection, $this); |
|
| 116 | + } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 117 | + return new PgSqlExpressionBuilder($this->connection, $this); |
|
| 118 | + } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) { |
|
| 119 | + return new MySqlExpressionBuilder($this->connection, $this); |
|
| 120 | + } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 121 | + return new SqliteExpressionBuilder($this->connection, $this); |
|
| 122 | + } else { |
|
| 123 | + return new ExpressionBuilder($this->connection, $this); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Gets an FunctionBuilder used for object-oriented construction of query functions. |
|
| 129 | + * This producer method is intended for convenient inline usage. Example: |
|
| 130 | + * |
|
| 131 | + * <code> |
|
| 132 | + * $qb = $conn->getQueryBuilder() |
|
| 133 | + * ->select('u') |
|
| 134 | + * ->from('users', 'u') |
|
| 135 | + * ->where($qb->fun()->md5('u.id')); |
|
| 136 | + * </code> |
|
| 137 | + * |
|
| 138 | + * For more complex function construction, consider storing the function |
|
| 139 | + * builder object in a local variable. |
|
| 140 | + * |
|
| 141 | + * @return \OCP\DB\QueryBuilder\IFunctionBuilder |
|
| 142 | + */ |
|
| 143 | + public function func() { |
|
| 144 | + if ($this->connection instanceof OracleConnection) { |
|
| 145 | + return new OCIFunctionBuilder($this->helper); |
|
| 146 | + } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) { |
|
| 147 | + return new SqliteFunctionBuilder($this->helper); |
|
| 148 | + } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) { |
|
| 149 | + return new PgSqlFunctionBuilder($this->helper); |
|
| 150 | + } else { |
|
| 151 | + return new FunctionBuilder($this->helper); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Gets the type of the currently built query. |
|
| 157 | + * |
|
| 158 | + * @return integer |
|
| 159 | + */ |
|
| 160 | + public function getType() { |
|
| 161 | + return $this->queryBuilder->getType(); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Gets the associated DBAL Connection for this query builder. |
|
| 166 | + * |
|
| 167 | + * @return \OCP\IDBConnection |
|
| 168 | + */ |
|
| 169 | + public function getConnection() { |
|
| 170 | + return $this->connection; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Gets the state of this query builder instance. |
|
| 175 | + * |
|
| 176 | + * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN. |
|
| 177 | + */ |
|
| 178 | + public function getState() { |
|
| 179 | + return $this->queryBuilder->getState(); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Executes this query using the bound parameters and their types. |
|
| 184 | + * |
|
| 185 | + * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate} |
|
| 186 | + * for insert, update and delete statements. |
|
| 187 | + * |
|
| 188 | + * @return \Doctrine\DBAL\Driver\Statement|int |
|
| 189 | + */ |
|
| 190 | + public function execute() { |
|
| 191 | + if ($this->systemConfig->getValue('log_query', false)) { |
|
| 192 | + $params = []; |
|
| 193 | + foreach ($this->getParameters() as $placeholder => $value) { |
|
| 194 | + if (is_array($value)) { |
|
| 195 | + $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')'; |
|
| 196 | + } else { |
|
| 197 | + $params[] = $placeholder . ' => \'' . $value . '\''; |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + if (empty($params)) { |
|
| 201 | + $this->logger->debug('DB QueryBuilder: \'{query}\'', [ |
|
| 202 | + 'query' => $this->getSQL(), |
|
| 203 | + 'app' => 'core', |
|
| 204 | + ]); |
|
| 205 | + } else { |
|
| 206 | + $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [ |
|
| 207 | + 'query' => $this->getSQL(), |
|
| 208 | + 'params' => implode(', ', $params), |
|
| 209 | + 'app' => 'core', |
|
| 210 | + ]); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return $this->queryBuilder->execute(); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Gets the complete SQL string formed by the current specifications of this QueryBuilder. |
|
| 219 | + * |
|
| 220 | + * <code> |
|
| 221 | + * $qb = $conn->getQueryBuilder() |
|
| 222 | + * ->select('u') |
|
| 223 | + * ->from('User', 'u') |
|
| 224 | + * echo $qb->getSQL(); // SELECT u FROM User u |
|
| 225 | + * </code> |
|
| 226 | + * |
|
| 227 | + * @return string The SQL query string. |
|
| 228 | + */ |
|
| 229 | + public function getSQL() { |
|
| 230 | + return $this->queryBuilder->getSQL(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Sets a query parameter for the query being constructed. |
|
| 235 | + * |
|
| 236 | + * <code> |
|
| 237 | + * $qb = $conn->getQueryBuilder() |
|
| 238 | + * ->select('u') |
|
| 239 | + * ->from('users', 'u') |
|
| 240 | + * ->where('u.id = :user_id') |
|
| 241 | + * ->setParameter(':user_id', 1); |
|
| 242 | + * </code> |
|
| 243 | + * |
|
| 244 | + * @param string|integer $key The parameter position or name. |
|
| 245 | + * @param mixed $value The parameter value. |
|
| 246 | + * @param string|null|int $type One of the IQueryBuilder::PARAM_* constants. |
|
| 247 | + * |
|
| 248 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 249 | + */ |
|
| 250 | + public function setParameter($key, $value, $type = null) { |
|
| 251 | + $this->queryBuilder->setParameter($key, $value, $type); |
|
| 252 | + |
|
| 253 | + return $this; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Sets a collection of query parameters for the query being constructed. |
|
| 258 | + * |
|
| 259 | + * <code> |
|
| 260 | + * $qb = $conn->getQueryBuilder() |
|
| 261 | + * ->select('u') |
|
| 262 | + * ->from('users', 'u') |
|
| 263 | + * ->where('u.id = :user_id1 OR u.id = :user_id2') |
|
| 264 | + * ->setParameters(array( |
|
| 265 | + * ':user_id1' => 1, |
|
| 266 | + * ':user_id2' => 2 |
|
| 267 | + * )); |
|
| 268 | + * </code> |
|
| 269 | + * |
|
| 270 | + * @param array $params The query parameters to set. |
|
| 271 | + * @param array $types The query parameters types to set. |
|
| 272 | + * |
|
| 273 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 274 | + */ |
|
| 275 | + public function setParameters(array $params, array $types = array()) { |
|
| 276 | + $this->queryBuilder->setParameters($params, $types); |
|
| 277 | + |
|
| 278 | + return $this; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Gets all defined query parameters for the query being constructed indexed by parameter index or name. |
|
| 283 | + * |
|
| 284 | + * @return array The currently defined query parameters indexed by parameter index or name. |
|
| 285 | + */ |
|
| 286 | + public function getParameters() { |
|
| 287 | + return $this->queryBuilder->getParameters(); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Gets a (previously set) query parameter of the query being constructed. |
|
| 292 | + * |
|
| 293 | + * @param mixed $key The key (index or name) of the bound parameter. |
|
| 294 | + * |
|
| 295 | + * @return mixed The value of the bound parameter. |
|
| 296 | + */ |
|
| 297 | + public function getParameter($key) { |
|
| 298 | + return $this->queryBuilder->getParameter($key); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Gets all defined query parameter types for the query being constructed indexed by parameter index or name. |
|
| 303 | + * |
|
| 304 | + * @return array The currently defined query parameter types indexed by parameter index or name. |
|
| 305 | + */ |
|
| 306 | + public function getParameterTypes() { |
|
| 307 | + return $this->queryBuilder->getParameterTypes(); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Gets a (previously set) query parameter type of the query being constructed. |
|
| 312 | + * |
|
| 313 | + * @param mixed $key The key (index or name) of the bound parameter type. |
|
| 314 | + * |
|
| 315 | + * @return mixed The value of the bound parameter type. |
|
| 316 | + */ |
|
| 317 | + public function getParameterType($key) { |
|
| 318 | + return $this->queryBuilder->getParameterType($key); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Sets the position of the first result to retrieve (the "offset"). |
|
| 323 | + * |
|
| 324 | + * @param integer $firstResult The first result to return. |
|
| 325 | + * |
|
| 326 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 327 | + */ |
|
| 328 | + public function setFirstResult($firstResult) { |
|
| 329 | + $this->queryBuilder->setFirstResult($firstResult); |
|
| 330 | + |
|
| 331 | + return $this; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Gets the position of the first result the query object was set to retrieve (the "offset"). |
|
| 336 | + * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder. |
|
| 337 | + * |
|
| 338 | + * @return integer The position of the first result. |
|
| 339 | + */ |
|
| 340 | + public function getFirstResult() { |
|
| 341 | + return $this->queryBuilder->getFirstResult(); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Sets the maximum number of results to retrieve (the "limit"). |
|
| 346 | + * |
|
| 347 | + * NOTE: Setting max results to "0" will cause mixed behaviour. While most |
|
| 348 | + * of the databases will just return an empty result set, Oracle will return |
|
| 349 | + * all entries. |
|
| 350 | + * |
|
| 351 | + * @param integer $maxResults The maximum number of results to retrieve. |
|
| 352 | + * |
|
| 353 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 354 | + */ |
|
| 355 | + public function setMaxResults($maxResults) { |
|
| 356 | + $this->queryBuilder->setMaxResults($maxResults); |
|
| 357 | + |
|
| 358 | + return $this; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + /** |
|
| 362 | + * Gets the maximum number of results the query object was set to retrieve (the "limit"). |
|
| 363 | + * Returns NULL if {@link setMaxResults} was not applied to this query builder. |
|
| 364 | + * |
|
| 365 | + * @return integer The maximum number of results. |
|
| 366 | + */ |
|
| 367 | + public function getMaxResults() { |
|
| 368 | + return $this->queryBuilder->getMaxResults(); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Specifies an item that is to be returned in the query result. |
|
| 373 | + * Replaces any previously specified selections, if any. |
|
| 374 | + * |
|
| 375 | + * <code> |
|
| 376 | + * $qb = $conn->getQueryBuilder() |
|
| 377 | + * ->select('u.id', 'p.id') |
|
| 378 | + * ->from('users', 'u') |
|
| 379 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 380 | + * </code> |
|
| 381 | + * |
|
| 382 | + * @param mixed $select The selection expressions. |
|
| 383 | + * |
|
| 384 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 385 | + */ |
|
| 386 | + public function select($select = null) { |
|
| 387 | + $selects = is_array($select) ? $select : func_get_args(); |
|
| 388 | + |
|
| 389 | + $this->queryBuilder->select( |
|
| 390 | + $this->helper->quoteColumnNames($selects) |
|
| 391 | + ); |
|
| 392 | + |
|
| 393 | + return $this; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Specifies an item that is to be returned with a different name in the query result. |
|
| 398 | + * |
|
| 399 | + * <code> |
|
| 400 | + * $qb = $conn->getQueryBuilder() |
|
| 401 | + * ->selectAlias('u.id', 'user_id') |
|
| 402 | + * ->from('users', 'u') |
|
| 403 | + * ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id'); |
|
| 404 | + * </code> |
|
| 405 | + * |
|
| 406 | + * @param mixed $select The selection expressions. |
|
| 407 | + * @param string $alias The column alias used in the constructed query. |
|
| 408 | + * |
|
| 409 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 410 | + */ |
|
| 411 | + public function selectAlias($select, $alias) { |
|
| 412 | + |
|
| 413 | + $this->queryBuilder->addSelect( |
|
| 414 | + $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias) |
|
| 415 | + ); |
|
| 416 | + |
|
| 417 | + return $this; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Specifies an item that is to be returned uniquely in the query result. |
|
| 422 | + * |
|
| 423 | + * <code> |
|
| 424 | + * $qb = $conn->getQueryBuilder() |
|
| 425 | + * ->selectDistinct('type') |
|
| 426 | + * ->from('users'); |
|
| 427 | + * </code> |
|
| 428 | + * |
|
| 429 | + * @param mixed $select The selection expressions. |
|
| 430 | + * |
|
| 431 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 432 | + */ |
|
| 433 | + public function selectDistinct($select) { |
|
| 434 | + |
|
| 435 | + $this->queryBuilder->addSelect( |
|
| 436 | + 'DISTINCT ' . $this->helper->quoteColumnName($select) |
|
| 437 | + ); |
|
| 438 | + |
|
| 439 | + return $this; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Adds an item that is to be returned in the query result. |
|
| 444 | + * |
|
| 445 | + * <code> |
|
| 446 | + * $qb = $conn->getQueryBuilder() |
|
| 447 | + * ->select('u.id') |
|
| 448 | + * ->addSelect('p.id') |
|
| 449 | + * ->from('users', 'u') |
|
| 450 | + * ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id'); |
|
| 451 | + * </code> |
|
| 452 | + * |
|
| 453 | + * @param mixed $select The selection expression. |
|
| 454 | + * |
|
| 455 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 456 | + */ |
|
| 457 | + public function addSelect($select = null) { |
|
| 458 | + $selects = is_array($select) ? $select : func_get_args(); |
|
| 459 | + |
|
| 460 | + $this->queryBuilder->addSelect( |
|
| 461 | + $this->helper->quoteColumnNames($selects) |
|
| 462 | + ); |
|
| 463 | + |
|
| 464 | + return $this; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Turns the query being built into a bulk delete query that ranges over |
|
| 469 | + * a certain table. |
|
| 470 | + * |
|
| 471 | + * <code> |
|
| 472 | + * $qb = $conn->getQueryBuilder() |
|
| 473 | + * ->delete('users', 'u') |
|
| 474 | + * ->where('u.id = :user_id'); |
|
| 475 | + * ->setParameter(':user_id', 1); |
|
| 476 | + * </code> |
|
| 477 | + * |
|
| 478 | + * @param string $delete The table whose rows are subject to the deletion. |
|
| 479 | + * @param string $alias The table alias used in the constructed query. |
|
| 480 | + * |
|
| 481 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 482 | + */ |
|
| 483 | + public function delete($delete = null, $alias = null) { |
|
| 484 | + $this->queryBuilder->delete( |
|
| 485 | + $this->getTableName($delete), |
|
| 486 | + $alias |
|
| 487 | + ); |
|
| 488 | + |
|
| 489 | + return $this; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * Turns the query being built into a bulk update query that ranges over |
|
| 494 | + * a certain table |
|
| 495 | + * |
|
| 496 | + * <code> |
|
| 497 | + * $qb = $conn->getQueryBuilder() |
|
| 498 | + * ->update('users', 'u') |
|
| 499 | + * ->set('u.password', md5('password')) |
|
| 500 | + * ->where('u.id = ?'); |
|
| 501 | + * </code> |
|
| 502 | + * |
|
| 503 | + * @param string $update The table whose rows are subject to the update. |
|
| 504 | + * @param string $alias The table alias used in the constructed query. |
|
| 505 | + * |
|
| 506 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 507 | + */ |
|
| 508 | + public function update($update = null, $alias = null) { |
|
| 509 | + $this->queryBuilder->update( |
|
| 510 | + $this->getTableName($update), |
|
| 511 | + $alias |
|
| 512 | + ); |
|
| 513 | + |
|
| 514 | + return $this; |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * Turns the query being built into an insert query that inserts into |
|
| 519 | + * a certain table |
|
| 520 | + * |
|
| 521 | + * <code> |
|
| 522 | + * $qb = $conn->getQueryBuilder() |
|
| 523 | + * ->insert('users') |
|
| 524 | + * ->values( |
|
| 525 | + * array( |
|
| 526 | + * 'name' => '?', |
|
| 527 | + * 'password' => '?' |
|
| 528 | + * ) |
|
| 529 | + * ); |
|
| 530 | + * </code> |
|
| 531 | + * |
|
| 532 | + * @param string $insert The table into which the rows should be inserted. |
|
| 533 | + * |
|
| 534 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 535 | + */ |
|
| 536 | + public function insert($insert = null) { |
|
| 537 | + $this->queryBuilder->insert( |
|
| 538 | + $this->getTableName($insert) |
|
| 539 | + ); |
|
| 540 | + |
|
| 541 | + $this->lastInsertedTable = $insert; |
|
| 542 | + |
|
| 543 | + return $this; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * Creates and adds a query root corresponding to the table identified by the |
|
| 548 | + * given alias, forming a cartesian product with any existing query roots. |
|
| 549 | + * |
|
| 550 | + * <code> |
|
| 551 | + * $qb = $conn->getQueryBuilder() |
|
| 552 | + * ->select('u.id') |
|
| 553 | + * ->from('users', 'u') |
|
| 554 | + * </code> |
|
| 555 | + * |
|
| 556 | + * @param string $from The table. |
|
| 557 | + * @param string|null $alias The alias of the table. |
|
| 558 | + * |
|
| 559 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 560 | + */ |
|
| 561 | + public function from($from, $alias = null) { |
|
| 562 | + $this->queryBuilder->from( |
|
| 563 | + $this->getTableName($from), |
|
| 564 | + $this->quoteAlias($alias) |
|
| 565 | + ); |
|
| 566 | + |
|
| 567 | + return $this; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * Creates and adds a join to the query. |
|
| 572 | + * |
|
| 573 | + * <code> |
|
| 574 | + * $qb = $conn->getQueryBuilder() |
|
| 575 | + * ->select('u.name') |
|
| 576 | + * ->from('users', 'u') |
|
| 577 | + * ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 578 | + * </code> |
|
| 579 | + * |
|
| 580 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 581 | + * @param string $join The table name to join. |
|
| 582 | + * @param string $alias The alias of the join table. |
|
| 583 | + * @param string $condition The condition for the join. |
|
| 584 | + * |
|
| 585 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 586 | + */ |
|
| 587 | + public function join($fromAlias, $join, $alias, $condition = null) { |
|
| 588 | + $this->queryBuilder->join( |
|
| 589 | + $this->quoteAlias($fromAlias), |
|
| 590 | + $this->getTableName($join), |
|
| 591 | + $this->quoteAlias($alias), |
|
| 592 | + $condition |
|
| 593 | + ); |
|
| 594 | + |
|
| 595 | + return $this; |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Creates and adds a join to the query. |
|
| 600 | + * |
|
| 601 | + * <code> |
|
| 602 | + * $qb = $conn->getQueryBuilder() |
|
| 603 | + * ->select('u.name') |
|
| 604 | + * ->from('users', 'u') |
|
| 605 | + * ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 606 | + * </code> |
|
| 607 | + * |
|
| 608 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 609 | + * @param string $join The table name to join. |
|
| 610 | + * @param string $alias The alias of the join table. |
|
| 611 | + * @param string $condition The condition for the join. |
|
| 612 | + * |
|
| 613 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 614 | + */ |
|
| 615 | + public function innerJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 616 | + $this->queryBuilder->innerJoin( |
|
| 617 | + $this->quoteAlias($fromAlias), |
|
| 618 | + $this->getTableName($join), |
|
| 619 | + $this->quoteAlias($alias), |
|
| 620 | + $condition |
|
| 621 | + ); |
|
| 622 | + |
|
| 623 | + return $this; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * Creates and adds a left join to the query. |
|
| 628 | + * |
|
| 629 | + * <code> |
|
| 630 | + * $qb = $conn->getQueryBuilder() |
|
| 631 | + * ->select('u.name') |
|
| 632 | + * ->from('users', 'u') |
|
| 633 | + * ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 634 | + * </code> |
|
| 635 | + * |
|
| 636 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 637 | + * @param string $join The table name to join. |
|
| 638 | + * @param string $alias The alias of the join table. |
|
| 639 | + * @param string $condition The condition for the join. |
|
| 640 | + * |
|
| 641 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 642 | + */ |
|
| 643 | + public function leftJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 644 | + $this->queryBuilder->leftJoin( |
|
| 645 | + $this->quoteAlias($fromAlias), |
|
| 646 | + $this->getTableName($join), |
|
| 647 | + $this->quoteAlias($alias), |
|
| 648 | + $condition |
|
| 649 | + ); |
|
| 650 | + |
|
| 651 | + return $this; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + /** |
|
| 655 | + * Creates and adds a right join to the query. |
|
| 656 | + * |
|
| 657 | + * <code> |
|
| 658 | + * $qb = $conn->getQueryBuilder() |
|
| 659 | + * ->select('u.name') |
|
| 660 | + * ->from('users', 'u') |
|
| 661 | + * ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1'); |
|
| 662 | + * </code> |
|
| 663 | + * |
|
| 664 | + * @param string $fromAlias The alias that points to a from clause. |
|
| 665 | + * @param string $join The table name to join. |
|
| 666 | + * @param string $alias The alias of the join table. |
|
| 667 | + * @param string $condition The condition for the join. |
|
| 668 | + * |
|
| 669 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 670 | + */ |
|
| 671 | + public function rightJoin($fromAlias, $join, $alias, $condition = null) { |
|
| 672 | + $this->queryBuilder->rightJoin( |
|
| 673 | + $this->quoteAlias($fromAlias), |
|
| 674 | + $this->getTableName($join), |
|
| 675 | + $this->quoteAlias($alias), |
|
| 676 | + $condition |
|
| 677 | + ); |
|
| 678 | + |
|
| 679 | + return $this; |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * Sets a new value for a column in a bulk update query. |
|
| 684 | + * |
|
| 685 | + * <code> |
|
| 686 | + * $qb = $conn->getQueryBuilder() |
|
| 687 | + * ->update('users', 'u') |
|
| 688 | + * ->set('u.password', md5('password')) |
|
| 689 | + * ->where('u.id = ?'); |
|
| 690 | + * </code> |
|
| 691 | + * |
|
| 692 | + * @param string $key The column to set. |
|
| 693 | + * @param string $value The value, expression, placeholder, etc. |
|
| 694 | + * |
|
| 695 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 696 | + */ |
|
| 697 | + public function set($key, $value) { |
|
| 698 | + $this->queryBuilder->set( |
|
| 699 | + $this->helper->quoteColumnName($key), |
|
| 700 | + $this->helper->quoteColumnName($value) |
|
| 701 | + ); |
|
| 702 | + |
|
| 703 | + return $this; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * Specifies one or more restrictions to the query result. |
|
| 708 | + * Replaces any previously specified restrictions, if any. |
|
| 709 | + * |
|
| 710 | + * <code> |
|
| 711 | + * $qb = $conn->getQueryBuilder() |
|
| 712 | + * ->select('u.name') |
|
| 713 | + * ->from('users', 'u') |
|
| 714 | + * ->where('u.id = ?'); |
|
| 715 | + * |
|
| 716 | + * // You can optionally programatically build and/or expressions |
|
| 717 | + * $qb = $conn->getQueryBuilder(); |
|
| 718 | + * |
|
| 719 | + * $or = $qb->expr()->orx(); |
|
| 720 | + * $or->add($qb->expr()->eq('u.id', 1)); |
|
| 721 | + * $or->add($qb->expr()->eq('u.id', 2)); |
|
| 722 | + * |
|
| 723 | + * $qb->update('users', 'u') |
|
| 724 | + * ->set('u.password', md5('password')) |
|
| 725 | + * ->where($or); |
|
| 726 | + * </code> |
|
| 727 | + * |
|
| 728 | + * @param mixed $predicates The restriction predicates. |
|
| 729 | + * |
|
| 730 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 731 | + */ |
|
| 732 | + public function where($predicates) { |
|
| 733 | + call_user_func_array( |
|
| 734 | + [$this->queryBuilder, 'where'], |
|
| 735 | + func_get_args() |
|
| 736 | + ); |
|
| 737 | + |
|
| 738 | + return $this; |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + /** |
|
| 742 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 743 | + * conjunction with any previously specified restrictions. |
|
| 744 | + * |
|
| 745 | + * <code> |
|
| 746 | + * $qb = $conn->getQueryBuilder() |
|
| 747 | + * ->select('u') |
|
| 748 | + * ->from('users', 'u') |
|
| 749 | + * ->where('u.username LIKE ?') |
|
| 750 | + * ->andWhere('u.is_active = 1'); |
|
| 751 | + * </code> |
|
| 752 | + * |
|
| 753 | + * @param mixed $where The query restrictions. |
|
| 754 | + * |
|
| 755 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 756 | + * |
|
| 757 | + * @see where() |
|
| 758 | + */ |
|
| 759 | + public function andWhere($where) { |
|
| 760 | + call_user_func_array( |
|
| 761 | + [$this->queryBuilder, 'andWhere'], |
|
| 762 | + func_get_args() |
|
| 763 | + ); |
|
| 764 | + |
|
| 765 | + return $this; |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + /** |
|
| 769 | + * Adds one or more restrictions to the query results, forming a logical |
|
| 770 | + * disjunction with any previously specified restrictions. |
|
| 771 | + * |
|
| 772 | + * <code> |
|
| 773 | + * $qb = $conn->getQueryBuilder() |
|
| 774 | + * ->select('u.name') |
|
| 775 | + * ->from('users', 'u') |
|
| 776 | + * ->where('u.id = 1') |
|
| 777 | + * ->orWhere('u.id = 2'); |
|
| 778 | + * </code> |
|
| 779 | + * |
|
| 780 | + * @param mixed $where The WHERE statement. |
|
| 781 | + * |
|
| 782 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 783 | + * |
|
| 784 | + * @see where() |
|
| 785 | + */ |
|
| 786 | + public function orWhere($where) { |
|
| 787 | + call_user_func_array( |
|
| 788 | + [$this->queryBuilder, 'orWhere'], |
|
| 789 | + func_get_args() |
|
| 790 | + ); |
|
| 791 | + |
|
| 792 | + return $this; |
|
| 793 | + } |
|
| 794 | + |
|
| 795 | + /** |
|
| 796 | + * Specifies a grouping over the results of the query. |
|
| 797 | + * Replaces any previously specified groupings, if any. |
|
| 798 | + * |
|
| 799 | + * <code> |
|
| 800 | + * $qb = $conn->getQueryBuilder() |
|
| 801 | + * ->select('u.name') |
|
| 802 | + * ->from('users', 'u') |
|
| 803 | + * ->groupBy('u.id'); |
|
| 804 | + * </code> |
|
| 805 | + * |
|
| 806 | + * @param mixed $groupBy The grouping expression. |
|
| 807 | + * |
|
| 808 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 809 | + */ |
|
| 810 | + public function groupBy($groupBy) { |
|
| 811 | + $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 812 | + |
|
| 813 | + call_user_func_array( |
|
| 814 | + [$this->queryBuilder, 'groupBy'], |
|
| 815 | + $this->helper->quoteColumnNames($groupBys) |
|
| 816 | + ); |
|
| 817 | + |
|
| 818 | + return $this; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + /** |
|
| 822 | + * Adds a grouping expression to the query. |
|
| 823 | + * |
|
| 824 | + * <code> |
|
| 825 | + * $qb = $conn->getQueryBuilder() |
|
| 826 | + * ->select('u.name') |
|
| 827 | + * ->from('users', 'u') |
|
| 828 | + * ->groupBy('u.lastLogin'); |
|
| 829 | + * ->addGroupBy('u.createdAt') |
|
| 830 | + * </code> |
|
| 831 | + * |
|
| 832 | + * @param mixed $groupBy The grouping expression. |
|
| 833 | + * |
|
| 834 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 835 | + */ |
|
| 836 | + public function addGroupBy($groupBy) { |
|
| 837 | + $groupBys = is_array($groupBy) ? $groupBy : func_get_args(); |
|
| 838 | + |
|
| 839 | + call_user_func_array( |
|
| 840 | + [$this->queryBuilder, 'addGroupBy'], |
|
| 841 | + $this->helper->quoteColumnNames($groupBys) |
|
| 842 | + ); |
|
| 843 | + |
|
| 844 | + return $this; |
|
| 845 | + } |
|
| 846 | + |
|
| 847 | + /** |
|
| 848 | + * Sets a value for a column in an insert query. |
|
| 849 | + * |
|
| 850 | + * <code> |
|
| 851 | + * $qb = $conn->getQueryBuilder() |
|
| 852 | + * ->insert('users') |
|
| 853 | + * ->values( |
|
| 854 | + * array( |
|
| 855 | + * 'name' => '?' |
|
| 856 | + * ) |
|
| 857 | + * ) |
|
| 858 | + * ->setValue('password', '?'); |
|
| 859 | + * </code> |
|
| 860 | + * |
|
| 861 | + * @param string $column The column into which the value should be inserted. |
|
| 862 | + * @param string $value The value that should be inserted into the column. |
|
| 863 | + * |
|
| 864 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 865 | + */ |
|
| 866 | + public function setValue($column, $value) { |
|
| 867 | + $this->queryBuilder->setValue( |
|
| 868 | + $this->helper->quoteColumnName($column), |
|
| 869 | + $value |
|
| 870 | + ); |
|
| 871 | + |
|
| 872 | + return $this; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + /** |
|
| 876 | + * Specifies values for an insert query indexed by column names. |
|
| 877 | + * Replaces any previous values, if any. |
|
| 878 | + * |
|
| 879 | + * <code> |
|
| 880 | + * $qb = $conn->getQueryBuilder() |
|
| 881 | + * ->insert('users') |
|
| 882 | + * ->values( |
|
| 883 | + * array( |
|
| 884 | + * 'name' => '?', |
|
| 885 | + * 'password' => '?' |
|
| 886 | + * ) |
|
| 887 | + * ); |
|
| 888 | + * </code> |
|
| 889 | + * |
|
| 890 | + * @param array $values The values to specify for the insert query indexed by column names. |
|
| 891 | + * |
|
| 892 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 893 | + */ |
|
| 894 | + public function values(array $values) { |
|
| 895 | + $quotedValues = []; |
|
| 896 | + foreach ($values as $key => $value) { |
|
| 897 | + $quotedValues[$this->helper->quoteColumnName($key)] = $value; |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + $this->queryBuilder->values($quotedValues); |
|
| 901 | + |
|
| 902 | + return $this; |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + /** |
|
| 906 | + * Specifies a restriction over the groups of the query. |
|
| 907 | + * Replaces any previous having restrictions, if any. |
|
| 908 | + * |
|
| 909 | + * @param mixed $having The restriction over the groups. |
|
| 910 | + * |
|
| 911 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 912 | + */ |
|
| 913 | + public function having($having) { |
|
| 914 | + call_user_func_array( |
|
| 915 | + [$this->queryBuilder, 'having'], |
|
| 916 | + func_get_args() |
|
| 917 | + ); |
|
| 918 | + |
|
| 919 | + return $this; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + /** |
|
| 923 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 924 | + * conjunction with any existing having restrictions. |
|
| 925 | + * |
|
| 926 | + * @param mixed $having The restriction to append. |
|
| 927 | + * |
|
| 928 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 929 | + */ |
|
| 930 | + public function andHaving($having) { |
|
| 931 | + call_user_func_array( |
|
| 932 | + [$this->queryBuilder, 'andHaving'], |
|
| 933 | + func_get_args() |
|
| 934 | + ); |
|
| 935 | + |
|
| 936 | + return $this; |
|
| 937 | + } |
|
| 938 | + |
|
| 939 | + /** |
|
| 940 | + * Adds a restriction over the groups of the query, forming a logical |
|
| 941 | + * disjunction with any existing having restrictions. |
|
| 942 | + * |
|
| 943 | + * @param mixed $having The restriction to add. |
|
| 944 | + * |
|
| 945 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 946 | + */ |
|
| 947 | + public function orHaving($having) { |
|
| 948 | + call_user_func_array( |
|
| 949 | + [$this->queryBuilder, 'orHaving'], |
|
| 950 | + func_get_args() |
|
| 951 | + ); |
|
| 952 | + |
|
| 953 | + return $this; |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + /** |
|
| 957 | + * Specifies an ordering for the query results. |
|
| 958 | + * Replaces any previously specified orderings, if any. |
|
| 959 | + * |
|
| 960 | + * @param string $sort The ordering expression. |
|
| 961 | + * @param string $order The ordering direction. |
|
| 962 | + * |
|
| 963 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 964 | + */ |
|
| 965 | + public function orderBy($sort, $order = null) { |
|
| 966 | + $this->queryBuilder->orderBy( |
|
| 967 | + $this->helper->quoteColumnName($sort), |
|
| 968 | + $order |
|
| 969 | + ); |
|
| 970 | + |
|
| 971 | + return $this; |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + /** |
|
| 975 | + * Adds an ordering to the query results. |
|
| 976 | + * |
|
| 977 | + * @param string $sort The ordering expression. |
|
| 978 | + * @param string $order The ordering direction. |
|
| 979 | + * |
|
| 980 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 981 | + */ |
|
| 982 | + public function addOrderBy($sort, $order = null) { |
|
| 983 | + $this->queryBuilder->addOrderBy( |
|
| 984 | + $this->helper->quoteColumnName($sort), |
|
| 985 | + $order |
|
| 986 | + ); |
|
| 987 | + |
|
| 988 | + return $this; |
|
| 989 | + } |
|
| 990 | + |
|
| 991 | + /** |
|
| 992 | + * Gets a query part by its name. |
|
| 993 | + * |
|
| 994 | + * @param string $queryPartName |
|
| 995 | + * |
|
| 996 | + * @return mixed |
|
| 997 | + */ |
|
| 998 | + public function getQueryPart($queryPartName) { |
|
| 999 | + return $this->queryBuilder->getQueryPart($queryPartName); |
|
| 1000 | + } |
|
| 1001 | + |
|
| 1002 | + /** |
|
| 1003 | + * Gets all query parts. |
|
| 1004 | + * |
|
| 1005 | + * @return array |
|
| 1006 | + */ |
|
| 1007 | + public function getQueryParts() { |
|
| 1008 | + return $this->queryBuilder->getQueryParts(); |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + /** |
|
| 1012 | + * Resets SQL parts. |
|
| 1013 | + * |
|
| 1014 | + * @param array|null $queryPartNames |
|
| 1015 | + * |
|
| 1016 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1017 | + */ |
|
| 1018 | + public function resetQueryParts($queryPartNames = null) { |
|
| 1019 | + $this->queryBuilder->resetQueryParts($queryPartNames); |
|
| 1020 | + |
|
| 1021 | + return $this; |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + /** |
|
| 1025 | + * Resets a single SQL part. |
|
| 1026 | + * |
|
| 1027 | + * @param string $queryPartName |
|
| 1028 | + * |
|
| 1029 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance. |
|
| 1030 | + */ |
|
| 1031 | + public function resetQueryPart($queryPartName) { |
|
| 1032 | + $this->queryBuilder->resetQueryPart($queryPartName); |
|
| 1033 | + |
|
| 1034 | + return $this; |
|
| 1035 | + } |
|
| 1036 | + |
|
| 1037 | + /** |
|
| 1038 | + * Creates a new named parameter and bind the value $value to it. |
|
| 1039 | + * |
|
| 1040 | + * This method provides a shortcut for PDOStatement::bindValue |
|
| 1041 | + * when using prepared statements. |
|
| 1042 | + * |
|
| 1043 | + * The parameter $value specifies the value that you want to bind. If |
|
| 1044 | + * $placeholder is not provided bindValue() will automatically create a |
|
| 1045 | + * placeholder for you. An automatic placeholder will be of the name |
|
| 1046 | + * ':dcValue1', ':dcValue2' etc. |
|
| 1047 | + * |
|
| 1048 | + * For more information see {@link http://php.net/pdostatement-bindparam} |
|
| 1049 | + * |
|
| 1050 | + * Example: |
|
| 1051 | + * <code> |
|
| 1052 | + * $value = 2; |
|
| 1053 | + * $q->eq( 'id', $q->bindValue( $value ) ); |
|
| 1054 | + * $stmt = $q->executeQuery(); // executed with 'id = 2' |
|
| 1055 | + * </code> |
|
| 1056 | + * |
|
| 1057 | + * @license New BSD License |
|
| 1058 | + * @link http://www.zetacomponents.org |
|
| 1059 | + * |
|
| 1060 | + * @param mixed $value |
|
| 1061 | + * @param mixed $type |
|
| 1062 | + * @param string $placeHolder The name to bind with. The string must start with a colon ':'. |
|
| 1063 | + * |
|
| 1064 | + * @return IParameter the placeholder name used. |
|
| 1065 | + */ |
|
| 1066 | + public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) { |
|
| 1067 | + return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder)); |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + /** |
|
| 1071 | + * Creates a new positional parameter and bind the given value to it. |
|
| 1072 | + * |
|
| 1073 | + * Attention: If you are using positional parameters with the query builder you have |
|
| 1074 | + * to be very careful to bind all parameters in the order they appear in the SQL |
|
| 1075 | + * statement , otherwise they get bound in the wrong order which can lead to serious |
|
| 1076 | + * bugs in your code. |
|
| 1077 | + * |
|
| 1078 | + * Example: |
|
| 1079 | + * <code> |
|
| 1080 | + * $qb = $conn->getQueryBuilder(); |
|
| 1081 | + * $qb->select('u.*') |
|
| 1082 | + * ->from('users', 'u') |
|
| 1083 | + * ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR)) |
|
| 1084 | + * ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR)) |
|
| 1085 | + * </code> |
|
| 1086 | + * |
|
| 1087 | + * @param mixed $value |
|
| 1088 | + * @param integer $type |
|
| 1089 | + * |
|
| 1090 | + * @return IParameter |
|
| 1091 | + */ |
|
| 1092 | + public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) { |
|
| 1093 | + return new Parameter($this->queryBuilder->createPositionalParameter($value, $type)); |
|
| 1094 | + } |
|
| 1095 | + |
|
| 1096 | + /** |
|
| 1097 | + * Creates a new parameter |
|
| 1098 | + * |
|
| 1099 | + * Example: |
|
| 1100 | + * <code> |
|
| 1101 | + * $qb = $conn->getQueryBuilder(); |
|
| 1102 | + * $qb->select('u.*') |
|
| 1103 | + * ->from('users', 'u') |
|
| 1104 | + * ->where('u.username = ' . $qb->createParameter('name')) |
|
| 1105 | + * ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR)) |
|
| 1106 | + * </code> |
|
| 1107 | + * |
|
| 1108 | + * @param string $name |
|
| 1109 | + * |
|
| 1110 | + * @return IParameter |
|
| 1111 | + */ |
|
| 1112 | + public function createParameter($name) { |
|
| 1113 | + return new Parameter(':' . $name); |
|
| 1114 | + } |
|
| 1115 | + |
|
| 1116 | + /** |
|
| 1117 | + * Creates a new function |
|
| 1118 | + * |
|
| 1119 | + * Attention: Column names inside the call have to be quoted before hand |
|
| 1120 | + * |
|
| 1121 | + * Example: |
|
| 1122 | + * <code> |
|
| 1123 | + * $qb = $conn->getQueryBuilder(); |
|
| 1124 | + * $qb->select($qb->createFunction('COUNT(*)')) |
|
| 1125 | + * ->from('users', 'u') |
|
| 1126 | + * echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u |
|
| 1127 | + * </code> |
|
| 1128 | + * <code> |
|
| 1129 | + * $qb = $conn->getQueryBuilder(); |
|
| 1130 | + * $qb->select($qb->createFunction('COUNT(`column`)')) |
|
| 1131 | + * ->from('users', 'u') |
|
| 1132 | + * echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u |
|
| 1133 | + * </code> |
|
| 1134 | + * |
|
| 1135 | + * @param string $call |
|
| 1136 | + * |
|
| 1137 | + * @return IQueryFunction |
|
| 1138 | + */ |
|
| 1139 | + public function createFunction($call) { |
|
| 1140 | + return new QueryFunction($call); |
|
| 1141 | + } |
|
| 1142 | + |
|
| 1143 | + /** |
|
| 1144 | + * Used to get the id of the last inserted element |
|
| 1145 | + * @return int |
|
| 1146 | + * @throws \BadMethodCallException When being called before an insert query has been run. |
|
| 1147 | + */ |
|
| 1148 | + public function getLastInsertId() { |
|
| 1149 | + if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) { |
|
| 1150 | + // lastInsertId() needs the prefix but no quotes |
|
| 1151 | + $table = $this->prefixTableName($this->lastInsertedTable); |
|
| 1152 | + return (int) $this->connection->lastInsertId($table); |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.'); |
|
| 1156 | + } |
|
| 1157 | + |
|
| 1158 | + /** |
|
| 1159 | + * Returns the table name quoted and with database prefix as needed by the implementation |
|
| 1160 | + * |
|
| 1161 | + * @param string $table |
|
| 1162 | + * @return string |
|
| 1163 | + */ |
|
| 1164 | + public function getTableName($table) { |
|
| 1165 | + $table = $this->prefixTableName($table); |
|
| 1166 | + return $this->helper->quoteColumnName($table); |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + /** |
|
| 1170 | + * Returns the table name with database prefix as needed by the implementation |
|
| 1171 | + * |
|
| 1172 | + * @param string $table |
|
| 1173 | + * @return string |
|
| 1174 | + */ |
|
| 1175 | + protected function prefixTableName($table) { |
|
| 1176 | + if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) { |
|
| 1177 | + return $table; |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + return '*PREFIX*' . $table; |
|
| 1181 | + } |
|
| 1182 | + |
|
| 1183 | + /** |
|
| 1184 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1185 | + * |
|
| 1186 | + * @param string $column |
|
| 1187 | + * @param string $tableAlias |
|
| 1188 | + * @return string |
|
| 1189 | + */ |
|
| 1190 | + public function getColumnName($column, $tableAlias = '') { |
|
| 1191 | + if ($tableAlias !== '') { |
|
| 1192 | + $tableAlias .= '.'; |
|
| 1193 | + } |
|
| 1194 | + |
|
| 1195 | + return $this->helper->quoteColumnName($tableAlias . $column); |
|
| 1196 | + } |
|
| 1197 | + |
|
| 1198 | + /** |
|
| 1199 | + * Returns the column name quoted and with table alias prefix as needed by the implementation |
|
| 1200 | + * |
|
| 1201 | + * @param string $alias |
|
| 1202 | + * @return string |
|
| 1203 | + */ |
|
| 1204 | + public function quoteAlias($alias) { |
|
| 1205 | + if ($alias === '' || $alias === null) { |
|
| 1206 | + return $alias; |
|
| 1207 | + } |
|
| 1208 | + |
|
| 1209 | + return $this->helper->quoteColumnName($alias); |
|
| 1210 | + } |
|
| 1211 | 1211 | } |