Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust to new dbal #605

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
[#620](https://github.com/nextcloud/cookbook/pull/620) @christianlupus
- Avoid descending of CS_fixer into non-code folders
[#621](https://github.com/nextcloud/cookbook/pull/621) @christianlupus
- Fixed compatiblity with Nextcloud 21
[#605](https://github.com/nextcloud/cookbook/pull/605) @icewind1991

## Deprecated
- Obsolete routes to old user interface, see `appinfo/routes.php`
Expand Down
46 changes: 46 additions & 0 deletions lib/Db/DbTypesPolyfillHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace OCA\Cookbook\Db;

use OCP\Util;

class DbTypesPolyfillHelper {
/**
* @var String
*/
private $int;
/**
* @var String
*/
private $string;

public function __construct(Util $util) {
switch ($util->getVersion()[0]) {
case 18:
case 19:
case 20:
$this->int = \Doctrine\DBAL\Types\Type::INTEGER;
$this->string = \Doctrine\DBAL\Types\Type::STRING;
break;

default:
$this->int = \OCP\DB\Types::INTEGER;
$this->string = \OCP\DB\Types::STRING;
break;
}
}

/**
* @return string
*/
final public function INT() {
return $this->int;
}

/**
* @return string
*/
final public function STRING() {
return $this->string;
}
}
63 changes: 34 additions & 29 deletions lib/Db/RecipeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace OCA\Cookbook\Db;

use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Types\Type;
use OCP\IDBConnection;
use OCP\AppFramework\Db\DoesNotExistException;

Expand All @@ -14,8 +13,14 @@ class RecipeDb {

private $db;

public function __construct(IDBConnection $db) {
/**
* @var DbTypesPolyfillHelper
*/
private $types;

public function __construct(IDBConnection $db, DbTypesPolyfillHelper $polyfillTypes) {
$this->db = $db;
$this->types = $polyfillTypes;
}

/**
Expand Down Expand Up @@ -79,7 +84,7 @@ public function findAllRecipes(string $user_id) {
->from(self::DB_TABLE_RECIPES, 'r')
->where('r.user_id = :user')
->orderBy('r.name');
$qb->setParameter('user', $user_id, TYPE::STRING);
$qb->setParameter('user', $user_id, $this->types->STRING());
$qb->leftJoin('r', self::DB_TABLE_KEYWORDS, 'k',
$qb->expr()->andX(
'r.recipe_id = k.recipe_id',
Expand Down Expand Up @@ -136,7 +141,7 @@ public function findAllKeywords(string $user_id) {
->where('user_id = :user AND k.name != \'\'')
->groupBy('k.name')
->orderBy('k.name');
$qb->setParameter('user', $user_id, TYPE::STRING);
$qb->setParameter('user', $user_id, $this->types->STRING());

$cursor = $qb->execute();
$result = $cursor->fetchAll();
Expand All @@ -160,7 +165,7 @@ public function findAllCategories(string $user_id) {
->where('user_id = :user')
->groupBy('c.name')
->orderBy('c.name');
$qb->setParameter('user', $user_id, TYPE::STRING);
$qb->setParameter('user', $user_id, $this->types->STRING());

$cursor = $qb->execute();
$result = $cursor->fetchAll();
Expand Down Expand Up @@ -217,8 +222,8 @@ public function getRecipesByCategory(string $category, string $user_id) {
->from(self::DB_TABLE_CATEGORIES, 'c')
->where('c.name = :category')
->andWhere('c.user_id = :user')
->setParameter('category', $category, TYPE::STRING)
->setParameter('user', $user_id, TYPE::STRING);
->setParameter('category', $category, $this->types->STRING())
->setParameter('user', $user_id, $this->types->STRING());

$qb->join('c', self::DB_TABLE_RECIPES, 'r', 'c.recipe_id = r.recipe_id');
$qb->leftJoin('c', self::DB_TABLE_KEYWORDS, 'k', 'c.recipe_id = k.recipe_id');
Expand Down Expand Up @@ -268,9 +273,9 @@ public function getRecipesByKeywords(string $keywords, string $user_id) {
->where('k.name IN (:keywords)')
->andWhere('k.user_id = :user')
->having('COUNT(DISTINCT k.name) = :keywordsCount')
->setParameter('user', $user_id, TYPE::INTEGER)
->setParameter('user', $user_id, $this->types->INT())
->setParameter('keywords', $keywords_arr, IQueryBuilder::PARAM_STR_ARRAY)
->setParameter('keywordsCount', sizeof($keywords_arr), TYPE::INTEGER);
->setParameter('keywordsCount', sizeof($keywords_arr), $this->types->INT());
$qb->join('k', self::DB_TABLE_RECIPES, 'r', 'k.recipe_id = r.recipe_id');
$qb->join('r', self::DB_TABLE_KEYWORDS, 'kk', 'kk.recipe_id = r.recipe_id');
$qb->groupBy(['r.name', 'r.recipe_id', 'kk.name']);
Expand Down Expand Up @@ -316,14 +321,14 @@ public function findRecipes(array $keywords, string $user_id) {
$qb->orWhere("LOWER(c.name) LIKE :keyword$paramIdx");

$params["keyword$paramIdx"] = "%$lowerKeyword%";
$types["keyword$paramIdx"] = Type::STRING;
$types["keyword$paramIdx"] = $this->types->STRING();
$paramIdx++;
}

$qb->andWhere('r.user_id = :user');

$qb->setParameters($params, $types);
$qb->setParameter('user', $user_id, TYPE::STRING);
$qb->setParameter('user', $user_id, $this->types->STRING());

$qb->groupBy(['r.name', 'r.recipe_id', 'k.name']);
$qb->orderBy('r.name');
Expand Down Expand Up @@ -366,22 +371,22 @@ public function emptySearchIndex(string $user_id) {
$qb->delete(self::DB_TABLE_RECIPES)
->where('user_id = :user')
->orWhere('user_id = :empty');
$qb->setParameter('user', $user_id, TYPE::STRING);
$qb->setParameter('empty', 'empty', TYPE::STRING);
$qb->setParameter('user', $user_id, $this->types->STRING());
$qb->setParameter('empty', 'empty', $this->types->STRING());

$qb->execute();

$qb->delete(self::DB_TABLE_KEYWORDS)
->where('user_id = :user')
->orWhere('user_id = :empty');
$qb->setParameter('user', $user_id, TYPE::STRING);
$qb->setParameter('empty', 'empty', TYPE::STRING);
$qb->setParameter('user', $user_id, $this->types->STRING());
$qb->setParameter('empty', 'empty', $this->types->STRING());

$qb->delete(self::DB_TABLE_CATEGORIES)
->where('user_id = :user')
->orWhere('user_id = :empty');
$qb->setParameter('user', $user_id, TYPE::STRING);
$qb->setParameter('empty', 'empty', TYPE::STRING);
$qb->setParameter('user', $user_id, $this->types->STRING());
$qb->setParameter('empty', 'empty', $this->types->STRING());

$qb->execute();
}
Expand Down Expand Up @@ -444,8 +449,8 @@ public function insertRecipes(array $recipes, string $userId) {
$qb->setParameter('userid', $userId);

foreach ($recipes as $recipe) {
$qb->setParameter('id', $recipe['id'], Type::INTEGER);
$qb->setParameter('name', $recipe['name'], Type::STRING);
$qb->setParameter('id', $recipe['id'], $this->types->INT());
$qb->setParameter('name', $recipe['name'], $this->types->STRING());

$qb->execute();
}
Expand Down Expand Up @@ -521,8 +526,8 @@ public function updateCategoryOfRecipe(int $recipeId, string $categoryName, stri
$qb->update(self::DB_TABLE_CATEGORIES)
->where('recipe_id = :rid', 'user_id = :user');
$qb->set('name', $qb->expr()->literal($categoryName, IQueryBuilder::PARAM_STR));
$qb->setParameter('rid', $recipeId, Type::INTEGER);
$qb->setParameter('user', $userId, Type::STRING);
$qb->setParameter('rid', $recipeId, $this->types->INT());
$qb->setParameter('user', $userId, $this->types->STRING());
$qb->execute();
}

Expand All @@ -539,9 +544,9 @@ public function addCategoryOfRecipe(int $recipeId, string $categoryName, string
$qb = $this->db->getQueryBuilder();
$qb->insert(self::DB_TABLE_CATEGORIES)
->values(['recipe_id' => ':rid', 'name' => ':name', 'user_id' => ':user']);
$qb->setParameter('rid', $recipeId, Type::INTEGER);
$qb->setParameter('name', $categoryName, Type::STRING);
$qb->setParameter('user', $userId, Type::STRING);
$qb->setParameter('rid', $recipeId, $this->types->INT());
$qb->setParameter('name', $categoryName, $this->types->STRING());
$qb->setParameter('user', $userId, $this->types->STRING());

try {
$qb->execute();
Expand All @@ -554,8 +559,8 @@ public function removeCategoryOfRecipe(int $recipeId, string $userId) {
$qb = $this->db->getQueryBuilder();
$qb->delete(self::DB_TABLE_CATEGORIES)
->where('recipe_id = :rid', 'user_id = :user');
$qb->setParameter('rid', $recipeId, Type::INTEGER);
$qb->setParameter('user', $userId, Type::STRING);
$qb->setParameter('rid', $recipeId, $this->types->INT());
$qb->setParameter('user', $userId, $this->types->STRING());
$qb->execute();
}

Expand All @@ -567,11 +572,11 @@ public function addKeywordPairs(array $pairs, string $userId) {
$qb = $this->db->getQueryBuilder();
$qb->insert(self::DB_TABLE_KEYWORDS)
->values(['recipe_id' => ':rid', 'name' => ':name', 'user_id' => ':user']);
$qb->setParameter('user', $userId, Type::STRING);
$qb->setParameter('user', $userId, $this->types->STRING());

foreach ($pairs as $p) {
$qb->setParameter('rid', $p['recipeId'], Type::INTEGER);
$qb->setParameter('name', $p['name'], Type::STRING);
$qb->setParameter('rid', $p['recipeId'], $this->types->INT());
$qb->setParameter('name', $p['name'], $this->types->STRING());

try {
$qb->execute();
Expand Down
5 changes: 2 additions & 3 deletions lib/Migration/Version000000Date20190910100911.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
use Doctrine\DBAL\Types\Type;

class Version000000Date20190910100911 extends SimpleMigrationStep {
/**
Expand All @@ -22,7 +21,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$recipes_table = $schema->getTable('cookbook_recipes');

if (!$recipes_table->hasColumn('user_id')) {
$recipes_table->addColumn('user_id', Type::STRING, [
$recipes_table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64,
'default' => 'empty',
Expand All @@ -32,7 +31,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
$keywords_table = $schema->getTable('cookbook_keywords');

if (!$keywords_table->hasColumn('user_id')) {
$keywords_table->addColumn('user_id', Type::STRING, [
$keywords_table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64,
'default' => 'empty',
Expand Down