From 13f05d0eedf73267477785a378bc428efcc2fa21 Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Mon, 11 Nov 2024 10:46:04 +0100 Subject: [PATCH] Rewrite tag repository to use query builder --- lib/src/repository/tag.dart | 68 ++++++++++++++------------------- lib/src/util/query_builder.dart | 4 +- test/query_builder_test.dart | 23 +++++++++++ 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/lib/src/repository/tag.dart b/lib/src/repository/tag.dart index 8607717..36bbfd5 100644 --- a/lib/src/repository/tag.dart +++ b/lib/src/repository/tag.dart @@ -1,6 +1,5 @@ import 'package:injector/injector.dart'; import 'package:logging/logging.dart'; -import 'package:postgres/postgres.dart'; import 'package:running_on_dart/running_on_dart.dart'; import 'package:running_on_dart/src/models/tag.dart'; import 'package:running_on_dart/src/util/query_builder.dart'; @@ -51,21 +50,15 @@ class TagRepository { return; } - final result = await _database.getConnection().execute(Sql.named(''' - INSERT INTO tags ( - name, - content, - enabled, - guild_id, - author_id - ) VALUES ( - @name, - @content, - @enabled, - @guild_id, - @author_id - ) RETURNING id; - '''), parameters: { + final query = InsertQuery("tags") + ..addNamedInsert("name") + ..addNamedInsert("content") + ..addNamedInsert("enabled") + ..addNamedInsert("guild_id") + ..addNamedInsert("author_id") + ..addReturning("id"); + + final result = await _database.executeQuery(query, parameters: { 'name': tag.name, 'content': tag.content, 'enabled': tag.enabled, @@ -82,16 +75,15 @@ class TagRepository { return addTag(tag); } - await _database.getConnection().execute(Sql.named(''' - UPDATE tags SET - name = @name, - content = @content, - enabled = @enabled, - guild_id = @guild_id, - author_id = @author_id - WHERE - id = @id - '''), parameters: { + final query = UpdateQuery("tags") + ..addNamedSet('name') + ..addNamedSet('content') + ..addNamedSet('enabled') + ..addNamedSet('guild_id') + ..addNamedSet('author_id') + ..andWhere("id = @id"); + + await _database.executeQuery(query, parameters: { 'id': tag.id, 'name': tag.name, 'content': tag.content, @@ -102,25 +94,21 @@ class TagRepository { } Future> fetchTagUsage() async { - final result = await _database.getConnection().execute(Sql.named(''' - SELECT tu.* FROM tag_usage tu JOIN tags t ON t.id = tu.command_id AND t.enabled = TRUE; - ''')); + final query = SelectQuery.selectAll("tag_usage", alias: "tu") + ..addJoin('tags', 't', ['t.id = tu.command_id', 't.enabled = TRUE']); + + final result = await _database.executeQuery(query); return result.map((row) => row.toColumnMap()).map(TagUsedEvent.fromRow); } Future registerTagUsedEvent(TagUsedEvent event) async { - await _database.getConnection().execute(Sql.named(''' - INSERT INTO tag_usage ( - command_id, - use_date, - hidden - ) VALUES ( - @tag_id, - @use_date, - @hidden - ) - '''), parameters: { + final query = InsertQuery("tag_usage") + ..addNamedInsert("command_id") + ..addNamedInsert("use_date") + ..addNamedInsert("hidden"); + + await _database.executeQuery(query, parameters: { 'tag_id': event.tagId, 'use_date': event.usedAt, 'hidden': event.hidden, diff --git a/lib/src/util/query_builder.dart b/lib/src/util/query_builder.dart index c48361b..0d6fbc4 100644 --- a/lib/src/util/query_builder.dart +++ b/lib/src/util/query_builder.dart @@ -139,6 +139,7 @@ class UpdateQuery extends Query with _WhereQuery { UpdateQuery(super.from, {super.alias}); void addSet(String name, String value) => _sets[name] = value; + void addNamedSet(String name) => _sets[name] = "@$name"; @override Sql build() { @@ -161,7 +162,8 @@ class SelectQuery extends Query with _WhereQuery, _JoinQuery { final List _selects = []; SelectQuery(super.from, {super.alias}); - factory SelectQuery.selectAll(String from) => SelectQuery(from)..select("*"); + factory SelectQuery.selectAll(String from, {String? alias}) => + SelectQuery(from, alias: alias)..select("${alias != null ? '$alias.' : ''}*"); void select(String expression) => _selects.add(expression); diff --git a/test/query_builder_test.dart b/test/query_builder_test.dart index 0501d5c..304411d 100644 --- a/test/query_builder_test.dart +++ b/test/query_builder_test.dart @@ -62,6 +62,20 @@ void main() { expect(query.build().asString(), "SELECT t.*,ot.* FROM test t JOIN other_table ot ON ot.id = t.test_id,LEFT JOIN another_table at ON at.test_id = t.id WHERE t.name = 'test' OR t.model = 'xg';"); }); + + test('Join multiple conditions', () { + final query = SelectQuery.selectAll("tag_usage", alias: "tu") + ..addJoin('tags', 't', ['t.id = tu.command_id', 't.enabled = TRUE']); + + expect(query.build().asString(), + "SELECT tu.* FROM tag_usage tu JOIN tags t ON t.id = tu.command_id AND t.enabled = TRUE;"); + }); + + test("Simple select all with alias", () { + final query = SelectQuery.selectAll("test", alias: 't')..andWhere("t.name = 'test'"); + + expect(query.build().asString(), "SELECT t.* FROM test t WHERE t.name = 'test';"); + }); }); group("Update tests", () { @@ -72,6 +86,15 @@ void main() { expect(query.build().asString(), "UPDATE test SET name = moron WHERE id = 1;"); }); + + test("Named sets", () { + final query = UpdateQuery("test") + ..addNamedSet("name") + ..addNamedSet("model") + ..andWhere("id = 1"); + + expect(query.build().asString(), "UPDATE test SET name = @name,model = @model WHERE id = 1;"); + }); }); group("Insert tests", () {