Skip to content

Commit

Permalink
Add CrdtApi interface to normalize query and execute methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cachapa committed Mar 15, 2024
1 parent a637d08 commit c15660f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.0.1

- Allow setting custom node ids
- Add CrdtApi interface to normalize query and execute methods

## 3.0.0+1

Expand Down
36 changes: 27 additions & 9 deletions lib/src/crdt_executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,35 @@ import 'database_api.dart';

final _sqlEngine = SqlEngine();

/// Interface to normalize CRDT `query` and `execute` methods
abstract class CrdtApi {
/// Performs a SQL query with optional [args] and returns the result as a list
/// of column maps.
/// Use "?" placeholders for parameters to avoid injection vulnerabilities:
///
/// ```
/// final result = await crdt.query(
/// 'SELECT id, name FROM users WHERE id = ?1', [1]);
/// print(result.isEmpty ? 'User not found' : result.first['name']);
/// ```
Future<List<Map<String, Object?>>> query(String sql, [List<Object?>? args]);

/// Executes a SQL query with an optional [args] list.
/// Use "?" placeholders for parameters to avoid injection vulnerabilities:
///
/// ```
/// await crdt.execute(
/// 'INSERT INTO users (id, name) Values (?1, ?2)', [1, 'John Doe']);
/// ```
Future<void> execute(String sql, [List<Object?>? args]);
}

/// Intercepts CREATE TABLE queries to assist with table creation and updates.
/// Does not impact any other query types.
class CrdtTableExecutor extends _CrdtTableExecutor {
class CrdtTableExecutor extends _CrdtTableExecutor implements CrdtApi {
CrdtTableExecutor(ReadWriteApi super._db);

@override
Future<List<Map<String, Object?>>> query(String sql, [List<Object?>? args]) =>
(_db as ReadWriteApi).query(sql, args);
}
Expand All @@ -20,13 +44,6 @@ class _CrdtTableExecutor {

_CrdtTableExecutor(this._db);

/// Executes a SQL query with an optional [args] list.
/// Use "?" placeholders for parameters to avoid injection vulnerabilities:
///
/// ```
/// await crdt.execute(
/// 'INSERT INTO users (id, name) Values (?1, ?2)', [1, 'John Doe']);
/// ```
Future<void> execute(String sql, [List<Object?>? args]) async {
// Break query into individual statements
final statements =
Expand Down Expand Up @@ -100,9 +117,10 @@ class _CrdtTableExecutor {
}
}

class CrdtExecutor extends CrdtWriteExecutor {
class CrdtExecutor extends CrdtWriteExecutor implements CrdtApi {
CrdtExecutor(ReadWriteApi super._db, super.hlc);

@override
Future<List<Map<String, Object?>>> query(String sql, [List<Object?>? args]) =>
(_db as ReadWriteApi).query(sql, args);
}
Expand Down
20 changes: 3 additions & 17 deletions lib/src/sql_crdt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'sql_util.dart';

typedef Query = (String sql, List<Object?> args);

abstract class SqlCrdt extends Crdt {
abstract class SqlCrdt extends Crdt implements CrdtApi {
final DatabaseApi _db;

// final Map<String, Iterable<String>> _tables;
Expand Down Expand Up @@ -38,25 +38,11 @@ abstract class SqlCrdt extends Crdt {
/// Returns all the keys for the specified table.
Future<Iterable<String>> getTableKeys(String table);

/// Performs a SQL query with optional [args] and returns the result as a list
/// of column maps.
/// Use "?" placeholders for parameters to avoid injection vulnerabilities:
///
/// ```
/// final result = await crdt.query(
/// 'SELECT id, name FROM users WHERE id = ?1', [1]);
/// print(result.isEmpty ? 'User not found' : result.first['name']);
/// ```
@override
Future<List<Map<String, Object?>>> query(String sql, [List<Object?>? args]) =>
_db.query(sql, args);

/// Executes a SQL query with optional [args].
/// Use "?" placeholders for parameters to avoid injection vulnerabilities:
///
/// ```
/// await crdt.execute(
/// 'INSERT INTO users (id, name) Values (?1, ?2)', [1, 'John Doe']);
/// ```
@override
Future<void> execute(String sql, [List<Object?>? args]) async {
final executor = CrdtExecutor(_db, canonicalTime.increment());
await executor.execute(sql, args);
Expand Down

0 comments on commit c15660f

Please sign in to comment.