Skip to content

Commit 6f9cfd3

Browse files
CommandType (#12)
* working * more consistent comments
1 parent 37850cf commit 6f9cfd3

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

example/lib/main.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ Future<Response> fetch(Request _) async {
1616
);
1717
await client.end();
1818

19-
return Response(result.rows.map(rowToPrettyString).join('\n\n'));
19+
return Response(
20+
[
21+
result.command == CommandType.select,
22+
...result.rows.map(rowToPrettyString),
23+
].join('\n\n'),
24+
);
2025
}
2126

2227
Future<QueryObjectResult<dynamic>> transaction(Transaction transaction) async {

lib/deno_postgres_interop.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
library;
33

44
export 'src/client.dart';
5+
export 'src/command_type.dart';
56
export 'src/isolation_level.dart';
67
export 'src/query_client.dart';
78
export 'src/query_object.dart' show QueryArguments;
89
export 'src/query_object_result.dart';
10+
export 'src/query_result.dart';
911
export 'src/transaction.dart';
1012
export 'src/transaction_options.dart';

lib/src/command_type.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// All sql commands.
2+
enum CommandType {
3+
/// insert.
4+
insert,
5+
6+
/// delete.
7+
delete,
8+
9+
/// update.
10+
update,
11+
12+
/// select.
13+
select,
14+
15+
/// move.
16+
move,
17+
18+
/// fetch.
19+
fetch,
20+
21+
/// copy.
22+
copy;
23+
24+
/// Parses a string containing an [CommandType] literal into its instance.
25+
static CommandType parse(String string) =>
26+
values.firstWhere((e) => e.name.toUpperCase() == string);
27+
}

lib/src/query.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'dart:js_interop';
2+
3+
/// [[email protected]/Query](https://deno.land/x/[email protected]/query/query.ts?s=Query).
4+
@JS()
5+
class Query<T> {}

lib/src/query_object_result.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import 'dart:js_interop';
22
import 'dart:js_util';
33

4+
import 'package:deno_postgres_interop/src/query.dart';
5+
import 'package:deno_postgres_interop/src/query_result.dart';
6+
47
/// [[email protected]/QueryObjectResult](https://deno.land/x/[email protected]/query/query.ts?s=QueryObjectResult).
58
@JS()
6-
class QueryObjectResult<T> {
9+
class QueryObjectResult<T> extends QueryResult {
710
/// [[email protected]/QueryObjectResult/columns](https://deno.land/x/[email protected]/query/query.ts?s=QueryObjectResult#prop_columns).
811
external List<String>? get columns;
12+
13+
/// [[email protected]/QueryResult/constructor](https://deno.land/x/[email protected]/query/query.ts?s=QueryResult#ctor_0).
14+
external factory QueryObjectResult(Query<dynamic> query);
915
}
1016

1117
/// [[email protected]/QueryObjectResult](https://deno.land/x/[email protected]/query/query.ts?s=QueryObjectResult).

lib/src/query_result.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'dart:js_interop';
2+
import 'dart:js_util';
3+
4+
import 'package:deno_postgres_interop/src/command_type.dart';
5+
6+
/// [[email protected]/QueryResult](https://deno.land/x/[email protected]/query/query.ts?s=QueryResult).
7+
@JS()
8+
class QueryResult {}
9+
10+
/// [[email protected]/QueryResult](https://deno.land/x/[email protected]/query/query.ts?s=QueryResult).
11+
extension QueryResultProps on QueryResult {
12+
/// [[email protected]/QueryResult/command](https://deno.land/x/[email protected]/query/query.ts?s=QueryResult#prop_command).
13+
CommandType get command => CommandType.parse(
14+
getProperty(this, 'command'),
15+
);
16+
}

0 commit comments

Comments
 (0)