|
| 1 | +import 'package:deno_postgres_interop/src/query_array_result.dart'; |
| 2 | +import 'package:deno_postgres_interop/src/query_object_options.dart'; |
| 3 | +import 'package:deno_postgres_interop/src/query_object_result.dart'; |
| 4 | +import 'package:deno_postgres_interop/src/util.dart'; |
| 5 | + |
| 6 | +/// [[email protected]/QueryArguments](https://deno.land/x/[email protected]/query/query.ts?s=QueryArguments). |
| 7 | +typedef QueryArguments = Object; |
| 8 | + |
| 9 | +/// This class hosts common interops for clients. |
| 10 | +class ClientCommon { |
| 11 | + /// [[email protected]/Transaction/queryArray](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryArray_0). |
| 12 | + /// [[email protected]/QueryClient/queryArray](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryArray_0). |
| 13 | + static Future<QueryArrayResult<T>> queryArray<T extends List<dynamic>>( |
| 14 | + Object queryable, |
| 15 | + String query, [ |
| 16 | + QueryArguments? arguments, |
| 17 | + ]) => |
| 18 | + _query('queryArray', queryable, query, arguments); |
| 19 | + |
| 20 | + /// [[email protected]/Transaction/queryArray](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryArray_1). |
| 21 | + /// [[email protected]/QueryClient/queryArray](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryArray_1). |
| 22 | + static Future<QueryArrayResult<T>> |
| 23 | + queryArrayWithOptions<T extends List<dynamic>>( |
| 24 | + Object queryable, |
| 25 | + QueryObjectOptions config, |
| 26 | + ) => |
| 27 | + callFutureMethod(queryable, 'queryArray', [config]); |
| 28 | + |
| 29 | + // This one won't be implemented because it doesn't make much sense for dart, |
| 30 | + // the query here is of type TemplateStringsArray which is used in |
| 31 | + // [tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates). |
| 32 | + // |
| 33 | + // [related issue](https://github.com/dart-lang/language/issues/1988). |
| 34 | + // |
| 35 | + // queryArray<T extends Array<unknown>>( |
| 36 | + // strings: TemplateStringsArray, |
| 37 | + // ...args: unknown[], |
| 38 | + // ): Promise<QueryArrayResult<T>> |
| 39 | + // https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryArray_2 |
| 40 | + |
| 41 | + /// [[email protected]/Transaction/queryObject](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryObject_0). |
| 42 | + /// [[email protected]/QueryClient/queryObject](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryObject_0). |
| 43 | + static Future<QueryObjectResult<T>> queryObject<T>( |
| 44 | + Object queryable, |
| 45 | + String query, [ |
| 46 | + QueryArguments? arguments, |
| 47 | + ]) => |
| 48 | + _query('queryObject', queryable, query, arguments); |
| 49 | + |
| 50 | + /// [[email protected]/Transaction/queryObject](https://deno.land/x/[email protected]/mod.ts?s=Transaction#method_queryObject_1). |
| 51 | + /// [[email protected]/QueryClient/queryObject](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryObject_1). |
| 52 | + static Future<QueryObjectResult<T>> queryObjectWithOptions<T>( |
| 53 | + Object queryable, |
| 54 | + QueryObjectOptions config, |
| 55 | + ) => |
| 56 | + callFutureMethod(queryable, 'queryObject', [config]); |
| 57 | + |
| 58 | + // This one won't be implemented because it doesn't make much sense for dart, |
| 59 | + // the query here is of type TemplateStringsArray which is used in |
| 60 | + // [tagged templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates). |
| 61 | + // |
| 62 | + // [related issue](https://github.com/dart-lang/language/issues/1988). |
| 63 | + // |
| 64 | + // [[email protected]/QueryClient/queryObject](https://deno.land/x/[email protected]/mod.ts?s=QueryClient#method_queryObject_2). |
| 65 | + // Future<QueryObjectResult<T>> queryObjectWithOptions<T>( |
| 66 | + // List<String> query, |
| 67 | + // List args, |
| 68 | + // ) => |
| 69 | + // throw UnimplementedError(); |
| 70 | + |
| 71 | + static Future<T> _query<T>( |
| 72 | + String function, |
| 73 | + Object queryable, |
| 74 | + String query, [ |
| 75 | + QueryArguments? arguments, |
| 76 | + ]) { |
| 77 | + _assertQueryArgumentsOrNull(arguments); |
| 78 | + |
| 79 | + return callFutureMethod( |
| 80 | + queryable, |
| 81 | + function, |
| 82 | + [ |
| 83 | + query, |
| 84 | + if (arguments != null) arguments, |
| 85 | + ], |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +void _assertQueryArgumentsOrNull(QueryArguments? arguments) { |
| 91 | + final isCorrectType = arguments is List || |
| 92 | + arguments is Map<String, dynamic> || |
| 93 | + arguments == null; |
| 94 | + |
| 95 | + if (!isCorrectType) { |
| 96 | + throw ArgumentError.value( |
| 97 | + arguments, |
| 98 | + 'arguments', |
| 99 | + 'Accepted types are List<any> and Map<String, any>.', |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments