Skip to content

Commit

Permalink
fix: Adjust parameter type usage and add SQLite support
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Aug 20, 2024
1 parent b338e58 commit e62ee82
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ public function iLike($x, $y, $type = null): string {
* @return array|IQueryFunction|string
*/
protected function prepareColumn($column, $type) {
if ($type === IQueryBuilder::PARAM_DATE && !is_array($column) && !($column instanceof IParameter) && !($column instanceof ILiteral)) {
if ($type !== null
&& !is_array($column)
&& !($column instanceof IParameter)
&& !($column instanceof ILiteral)
&& (str_starts_with($type, 'date') || str_starts_with($type, 'time'))) {
return $this->castColumn($column, $type);
}

Expand All @@ -44,9 +48,21 @@ protected function prepareColumn($column, $type) {
* @return IQueryFunction
*/
public function castColumn($column, $type): IQueryFunction {
if ($type === IQueryBuilder::PARAM_DATE) {
$column = $this->helper->quoteColumnName($column);
return new QueryFunction('DATETIME(' . $column . ')');
switch ($type) {
case IQueryBuilder::PARAM_DATE:
case IQueryBuilder::PARAM_DATE_IMMUTABLE:
$column = $this->helper->quoteColumnName($column);
return new QueryFunction('DATE(' . $column . ')');
case IQueryBuilder::PARAM_DATETIME:
case IQueryBuilder::PARAM_DATETIME_TZ:
case IQueryBuilder::PARAM_DATETIME_IMMUTABLE:
case IQueryBuilder::PARAM_DATETIME_TZ_IMMUTABLE:
$column = $this->helper->quoteColumnName($column);
return new QueryFunction('DATETIME(' . $column . ')');
case IQueryBuilder::PARAM_TIME:
case IQueryBuilder::PARAM_TIME_IMMUTABLE:
$column = $this->helper->quoteColumnName($column);
return new QueryFunction('TIME(' . $column . ')');
}

return parent::castColumn($column, $type);
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Security/RateLimiting/Backend/DatabaseBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private function getExistingAttemptCount(
$qb = $this->dbConnection->getQueryBuilder();
$qb->delete(self::TABLE_NAME)
->where(
$qb->expr()->lte('delete_after', $qb->createNamedParameter($currentTime, IQueryBuilder::PARAM_DATE))
$qb->expr()->lte('delete_after', $qb->createNamedParameter($currentTime, IQueryBuilder::PARAM_DATETIME))
)
->executeStatement();

Expand Down Expand Up @@ -87,7 +87,7 @@ public function registerAttempt(
$qb->insert(self::TABLE_NAME)
->values([
'hash' => $qb->createNamedParameter($identifier, IQueryBuilder::PARAM_STR),
'delete_after' => $qb->createNamedParameter($deleteAfter, IQueryBuilder::PARAM_DATE),
'delete_after' => $qb->createNamedParameter($deleteAfter, IQueryBuilder::PARAM_DATETIME),
]);

if (!$this->config->getSystemValueBool('ratelimit.protection.enabled', true)) {
Expand Down
8 changes: 4 additions & 4 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function update(\OCP\Share\IShare $share) {
->set('attributes', $qb->createNamedParameter($shareAttributes))
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATETIME))
->set('note', $qb->createNamedParameter($share->getNote()))
->set('accepted', $qb->createNamedParameter($share->getStatus()))
->execute();
Expand All @@ -234,7 +234,7 @@ public function update(\OCP\Share\IShare $share) {
->set('attributes', $qb->createNamedParameter($shareAttributes))
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATETIME))
->set('note', $qb->createNamedParameter($share->getNote()))
->execute();

Expand All @@ -249,7 +249,7 @@ public function update(\OCP\Share\IShare $share) {
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATETIME))
->set('note', $qb->createNamedParameter($share->getNote()))
->execute();

Expand All @@ -276,7 +276,7 @@ public function update(\OCP\Share\IShare $share) {
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('token', $qb->createNamedParameter($share->getToken()))
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATE))
->set('expiration', $qb->createNamedParameter($expirationDate, IQueryBuilder::PARAM_DATETIME))
->set('note', $qb->createNamedParameter($share->getNote()))
->set('label', $qb->createNamedParameter($share->getLabel()))
->set('hide_download', $qb->createNamedParameter($share->getHideDownload() ? 1 : 0), IQueryBuilder::PARAM_INT)
Expand Down
4 changes: 2 additions & 2 deletions lib/private/TextToImage/Db/TaskMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public function deleteOlderThan(int $timeout): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->tableName)
->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATE)));
->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATETIME)));
$deletedTasks = $this->findEntities($qb);
$qb = $this->db->getQueryBuilder();
$qb->delete($this->tableName)
->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATE)));
->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($datetime, IQueryBuilder::PARAM_DATETIME)));
$qb->executeStatement();
return $deletedTasks;
}
Expand Down
30 changes: 15 additions & 15 deletions lib/public/Migration/Attributes/ColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,45 @@
*/
enum ColumnType : string {
/** @since 30.0.0 */
case BIGINT = Types::BIGINT;
case BIGINT = 'bigint';
/** @since 30.0.0 */
case BINARY = Types::BINARY;
case BINARY = 'binary';
/** @since 30.0.0 */
case BLOB = Types::BLOB;
case BLOB = 'blob';
/** @since 30.0.0 */
case BOOLEAN = Types::BOOLEAN;
case BOOLEAN = 'boolean';
/**
* A column created with `DATE` can be used for both `DATE` and `DATE_IMMUTABLE`
* on the `\OCP\AppFramework\Db\Entity`.
* @since 30.0.0
*/
case DATE = Types::DATE;
case DATE = 'date';
/**
* A column created with `DATETIME` can be used for both `DATETIME` and `DATETIME_IMMUTABLE`
* on the `\OCP\AppFramework\Db\Entity`.
* @since 30.0.0
*/
case DATETIME = Types::DATETIME;
case DATETIME = 'datetime';
/**
* A column created with `DATETIME_TZ` can be used for both `DATETIME_TZ` and `DATETIME_TZ_IMMUTABLE`
* on the `\OCP\AppFramework\Db\Entity`.
* @since 31.0.0
*/
case DATETIME_TZ = Types::DATETIME_TZ;
case DATETIME_TZ = 'datetimetz';
/** @since 30.0.0 */
case DECIMAL = Types::DECIMAL;
case DECIMAL = 'decimal';
/** @since 30.0.0 */
case FLOAT = Types::FLOAT;
case FLOAT = 'float';
/** @since 30.0.0 */
case INTEGER = Types::INTEGER;
case INTEGER = 'integer';
/** @since 30.0.0 */
case SMALLINT = Types::SMALLINT;
case SMALLINT = 'smallint';
/** @since 30.0.0 */
case STRING = Types::STRING;
case STRING = 'string';
/** @since 30.0.0 */
case TEXT = Types::TEXT;
case TEXT = 'text';
/** @since 30.0.0 */
case TIME = Types::TIME;
case TIME = 'time';
/** @since 30.0.0 */
case JSON = Types::JSON;
case JSON = 'json';
}

0 comments on commit e62ee82

Please sign in to comment.