From f3e6ef26c4b84663ec9016a08dca1b3b375b720f Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 10 Aug 2023 16:38:28 +0200 Subject: [PATCH] Add substr to Dart query builder (#2555) --- drift/CHANGELOG.md | 2 ++ .../runtime/query_builder/expressions/text.dart | 15 +++++++++++++++ .../expressions/expressions_integration_test.dart | 4 ++++ drift/test/database/expressions/text_test.dart | 5 +++++ 4 files changed, 26 insertions(+) diff --git a/drift/CHANGELOG.md b/drift/CHANGELOG.md index e3ffe070..86ed13a7 100644 --- a/drift/CHANGELOG.md +++ b/drift/CHANGELOG.md @@ -3,6 +3,8 @@ - Add support for subqueries in the Dart query builder. - Add `isInExp` and `isNotInExp` to construct `IS IN` expressions with arbitrary expressions. +- Add the `substr` extension on `Expression` to call the sqlite3 function from + the Dart API. - Add `isolateSetup` to `NativeDatabase.createInBackground()` to override libraries. ## 2.10.0 diff --git a/drift/lib/src/runtime/query_builder/expressions/text.dart b/drift/lib/src/runtime/query_builder/expressions/text.dart index f78693c2..57f345cc 100644 --- a/drift/lib/src/runtime/query_builder/expressions/text.dart +++ b/drift/lib/src/runtime/query_builder/expressions/text.dart @@ -128,6 +128,21 @@ extension StringExpressionOperators on Expression { Expression trimRight() { return FunctionCallExpression('RTRIM', [this]); } + + /// Calls the [`substr`](https://sqlite.org/lang_corefunc.html#substr) + /// function on this string. + /// + /// Note that the function has different semantics than the [String.substring] + /// method for Dart strings - for instance, the [start] index starts at one + /// and [length] can be negative to return a section of the string before + /// [start]. + Expression substr(int start, [int? length]) { + return FunctionCallExpression('SUBSTR', [ + this, + Constant(start), + if (length != null) Constant(length), + ]); + } } /// A `text LIKE pattern` expression that will be true if the first expression diff --git a/drift/test/database/expressions/expressions_integration_test.dart b/drift/test/database/expressions/expressions_integration_test.dart index 56a34dee..0b92d8a8 100644 --- a/drift/test/database/expressions/expressions_integration_test.dart +++ b/drift/test/database/expressions/expressions_integration_test.dart @@ -123,6 +123,10 @@ void main() { const literal = Constant(' hello world '); expect(eval(literal.trimRight()), completion(' hello world')); }); + + test('substring', () { + expect(eval(Constant('hello world').substr(7)), completion('world')); + }); }); test('coalesce', () async { diff --git a/drift/test/database/expressions/text_test.dart b/drift/test/database/expressions/text_test.dart index e9fc9e1c..c63342d0 100644 --- a/drift/test/database/expressions/text_test.dart +++ b/drift/test/database/expressions/text_test.dart @@ -48,4 +48,9 @@ void main() { }); }); }); + + test('substr', () { + expect(expression.substr(10), generates('SUBSTR(col, 10)')); + expect(expression.substr(10, 2), generates('SUBSTR(col, 10, 2)')); + }); }