Skip to content

Commit

Permalink
Extraction of all table named to static class constants in DB connect…
Browse files Browse the repository at this point in the history
…ion source
  • Loading branch information
christianlupus committed Aug 14, 2020
1 parent 72c5921 commit 64f85c6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
- Truncate DB to restart from scratch
- Export DB for debugging?
- Abort Search for JSON after fixed numer/time to avoid timeout
- Use of table names from constants in DB table

# Bugs found
26 changes: 13 additions & 13 deletions lib/Db/RecipeDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public function findRecipeById(int $id) {
public function deleteRecipeById(int $id) {
$qb = $this->db->getQueryBuilder();

$qb->delete('cookbook_names')
$qb->delete(self::DB_TABLE_RECIPES)
->where('recipe_id = :id');
$qb->setParameter('id', $id, IQueryBuilder::PARAM_INT);

$qb->execute();

$qb->delete('cookbook_keywords')
$qb->delete(self::DB_TABLE_KEYWORDS)
->where('recipe_id = :id');
$qb->setParameter('id', $id, IQueryBuilder::PARAM_INT);

$qb->delete('cookbook_categories')
$qb->delete(self::DB_TABLE_CATEGORIES)
->where('recipe_id = :id');
$qb->setParameter('id', $id, IQueryBuilder::PARAM_INT);

Expand All @@ -74,7 +74,7 @@ public function findAllRecipes(string $user_id) {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from('cookbook_names', 'r')
->from(self::DB_TABLE_RECIPES, 'r')
->where('user_id = :user')
->orderBy('r.name');
$qb->setParameter('user', $user_id, TYPE::STRING);
Expand Down Expand Up @@ -107,7 +107,7 @@ public function findAllKeywords(string $user_id) {

$qb->select('k.name')
->selectAlias($qb->createFunction('COUNT(k.recipe_id)'), 'recipe_count')
->from('cookbook_keywords', 'k')
->from(self::DB_TABLE_KEYWORDS, 'k')
->where('user_id = :user AND k.name != \'\'')
->groupBy('k.name')
->orderBy('k.name');
Expand All @@ -128,7 +128,7 @@ public function findAllCategories(string $user_id) {

$qb->select('k.name')
->selectAlias($qb->createFunction('COUNT(k.recipe_id)'), 'recipe_count')
->from('cookbook_categories', 'k')
->from(self::DB_TABLE_CATEGORIES, 'k')
->where('user_id = :user AND k.name != \'\'')
->groupBy('k.name')
->orderBy('k.name');
Expand Down Expand Up @@ -178,13 +178,13 @@ public function getRecipesByCategory(string $category, string $user_id) {
$qb = $this->db->getQueryBuilder();

$qb->select(['r.recipe_id', 'r.name'])
->from('cookbook_categories', 'k')
->from(self::DB_TABLE_CATEGORIES, 'k')
->where('k.name = :category')
->andWhere('k.user_id = :user')
->setParameter('category', $category, TYPE::STRING)
->setParameter('user', $user_id, TYPE::STRING);

$qb->join('k', 'cookbook_names', 'r', 'k.recipe_id = r.recipe_id');
$qb->join('k', self::DB_TABLE_RECIPES, 'r', 'k.recipe_id = r.recipe_id');

$qb->groupBy(['r.name', 'r.recipe_id']);
$qb->orderBy('r.name');
Expand All @@ -207,7 +207,7 @@ public function findRecipes(array $keywords, string $user_id) {
$qb = $this->db->getQueryBuilder();

$qb->select(['r.recipe_id', 'r.name'])
->from('cookbook_keywords', 'k');
->from(self::DB_TABLE_KEYWORDS, 'k');

$paramIdx = 1;
$params = [];
Expand All @@ -231,7 +231,7 @@ public function findRecipes(array $keywords, string $user_id) {
$qb->setParameters($params, $types);
$qb->setParameter('user', $user_id, TYPE::STRING);

$qb->join('k', 'cookbook_names', 'r', 'k.recipe_id = r.recipe_id');
$qb->join('k', self::DB_TABLE_RECIPES, 'r', 'k.recipe_id = r.recipe_id');

$qb->groupBy(['r.name', 'r.recipe_id']);
$qb->orderBy('r.name');
Expand All @@ -250,21 +250,21 @@ public function findRecipes(array $keywords, string $user_id) {
public function emptySearchIndex(string $user_id) {
$qb = $this->db->getQueryBuilder();

$qb->delete('cookbook_names')
$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->execute();

$qb->delete('cookbook_keywords')
$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->delete('cookbook_categories')
$qb->delete(self::DB_TABLE_CATEGORIES)
->where('user_id = :user')
->orWhere('user_id = :empty');
$qb->setParameter('user', $user_id, TYPE::STRING);
Expand Down

0 comments on commit 64f85c6

Please sign in to comment.