-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36f7761
commit c515bbf
Showing
5 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
library sqflite_ext; | ||
|
||
export 'src/boolean_utils.dart'; | ||
export 'src/parse_named_args.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// A parsed raw query + argument values. | ||
class QueryWithArgs { | ||
QueryWithArgs(this.query, this.arguments); | ||
|
||
/// Query with named arguments replace with placeholders (?). | ||
String query; | ||
|
||
/// List of query argument values. | ||
List<Object?> arguments; | ||
} | ||
|
||
/// Parse a query with named arguments into a raw query + argument values. | ||
QueryWithArgs parseNamedArgs(String query, Map<String, dynamic> params) { | ||
final exp = RegExp(r":([\w]+)"); | ||
List<Object?> arguments = []; | ||
RegExpMatch? match; | ||
|
||
while ((match = exp.firstMatch(query)) != null) { | ||
final name = query.substring(match!.start + 1, match.end); | ||
query = query.replaceRange(match.start, match.end, '?'); | ||
arguments.add(params[name]); | ||
} | ||
|
||
return QueryWithArgs(query, arguments); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:sqflite_ext/src/parse_named_args.dart'; | ||
|
||
void main() { | ||
group('parseNamedArgs', () { | ||
test('with no params', () { | ||
final result = parseNamedArgs( | ||
'SELECT * FROM demo', | ||
{}, | ||
); | ||
|
||
expect(result.query, 'SELECT * FROM demo'); | ||
expect(result.arguments, []); | ||
}); | ||
|
||
test('with missing param', () { | ||
final result = parseNamedArgs( | ||
'SELECT * FROM demo WHERE id = :fail', | ||
{}, | ||
); | ||
|
||
expect(result.query, 'SELECT * FROM demo WHERE id = ?'); | ||
expect(result.arguments, [null]); | ||
}); | ||
|
||
test('with a single param', () { | ||
final result = parseNamedArgs( | ||
'SELECT * FROM demo WHERE id = :id', | ||
{'id': 83}, | ||
); | ||
|
||
expect(result.query, 'SELECT * FROM demo WHERE id = ?'); | ||
expect(result.arguments, [83]); | ||
}); | ||
|
||
test('with multiple different params', () { | ||
final result = parseNamedArgs( | ||
'SELECT * FROM demo WHERE a = :a AND b = :b AND c = :c', | ||
{'a': 83, 'b': 'active', 'c': true}, | ||
); | ||
|
||
expect( | ||
result.query, | ||
'SELECT * FROM demo WHERE a = ? AND b = ? AND c = ?', | ||
); | ||
expect(result.arguments, [83, 'active', true]); | ||
}); | ||
|
||
test('with the same param multiple times', () { | ||
final result = parseNamedArgs( | ||
'SELECT * FROM demo WHERE a = :a AND b = :b AND c = :b', | ||
{'a': 83, 'b': 'active'}, | ||
); | ||
|
||
expect( | ||
result.query, | ||
'SELECT * FROM demo WHERE a = ? AND b = ? AND c = ?', | ||
); | ||
expect(result.arguments, [83, 'active', 'active']); | ||
}); | ||
}); | ||
} |