Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of custom expressions in drift files #3366

Open
laurensfischer opened this issue Dec 3, 2024 · 0 comments
Open

Usage of custom expressions in drift files #3366

laurensfischer opened this issue Dec 3, 2024 · 0 comments

Comments

@laurensfischer
Copy link

Hey, still a big fan of your library!

we are currently working on migrating an app to the newest drift version, coming from 1.x.

This works fine after upgrading for most queries, but we get an error when using a custom expression within the SELECT statement. The error while generating is Null check operator used on a null value.

We are mainly doing this to reuse the expression (and some code) in multiple queries and drift files.

Is it not possible anymore to use custom expression in SELECT statements?

If not, are there better alternatives than calling the custom functions directly in drift files? Doing that shows a warning ( Function calcStatus could not be found), but the generated code looks okay.

Example

Simplified example of our queries and how it worked before upgrading and the full error that occurs now:

Query:

exampleQuery as Example:
    SELECT CAST($status AS INT) AS status FROM examples;

Generated code:

  Selectable<Example> exampleQuery(
      Expression status) {
    final generatedstatus = $write(status, hasMultipleTables: true);
    return customSelect(
        'SELECT CAST(${generatedstatus.sql} AS INT) AS status',
        variables: [
          ...generatedstatus.introducedVariables,
        ],
        readsFrom: {
          examples,
          ...generatedstatus.watchedTables,
        }).map((QueryRow row) {
      return Example
        status: row.read<int>('status'),
      );
    });
  }
}

Caller:

  Selectable<Example> getExample(
    int param,
  ) {
    return exampleQuery(CustomExpression(
      "calcStatus($param)",
    ));
  }

calcStatus is a custom function added to sql by calling database.createFunction(functionName: "calcStatus", ...)

The full error:

Error:

Null check operator used on a null value
package:drift_dev/src/writer/queries/utils.dart 29:60           FoundElementType.dartType.<fn>
package:drift_dev/src/analysis/results/dart.dart 45:10          new AnnotatedDartCode.build
package:drift_dev/src/writer/queries/utils.dart 10:32           FoundElementType.dartType
package:drift_dev/src/writer/queries/query_writer.dart 400:40   QueryWriter._writeParameters.typeFor
package:drift_dev/src/writer/queries/query_writer.dart 432:20   QueryWriter._writeParameters
package:drift_dev/src/writer/queries/query_writer.dart 300:5    QueryWriter._writeSelectStatementCreator
package:drift_dev/src/writer/queries/query_writer.dart 82:5     QueryWriter._writeSelect
package:drift_dev/src/writer/queries/query_writer.dart 71:7     QueryWriter.write
package:drift_dev/src/writer/drift_accessor_writer.dart 45:39   AccessorWriter.write
package:drift_dev/src/backends/build/drift_builder.dart 446:49  _DriftBuildRun._generateMonolithic
package:drift_dev/src/backends/build/drift_builder.dart 184:7   _DriftBuildRun.run
package:drift_dev/src/backends/build/drift_builder.dart 109:5   DriftBuilder.build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant