Skip to content

Commit

Permalink
Rewrite tag repository to use query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Nov 11, 2024
1 parent ad94bf3 commit 13f05d0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
68 changes: 28 additions & 40 deletions lib/src/repository/tag.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -102,25 +94,21 @@ class TagRepository {
}

Future<Iterable<TagUsedEvent>> 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<void> 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,
Expand Down
4 changes: 3 additions & 1 deletion lib/src/util/query_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -161,7 +162,8 @@ class SelectQuery extends Query with _WhereQuery, _JoinQuery {
final List<String> _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);

Expand Down
23 changes: 23 additions & 0 deletions test/query_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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", () {
Expand All @@ -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", () {
Expand Down

0 comments on commit 13f05d0

Please sign in to comment.