Strip drift data class name from `CREATE VIEW`

This commit is contained in:
Simon Binder 2023-01-02 20:04:22 +01:00
parent ecf6679518
commit 4cd7616f24
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 30 additions and 10 deletions

View File

@ -1,4 +1,6 @@
import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart';
import 'package:sqlparser/utils/node_to_text.dart';
import '../../driver/state.dart';
import '../../results/results.dart';
@ -65,12 +67,20 @@ class DriftViewResolver extends DriftElementResolver<DiscoveredDriftView> {
}
}
final createStmtForDatabase = CreateViewStatement(
ifNotExists: stmt.ifNotExists,
viewName: stmt.viewName,
columns: stmt.columns,
query: stmt.query,
// Remove drift-specific syntax
driftTableName: null,
).toSql();
return DriftView(
discovered.ownId,
DriftDeclaration.driftFile(stmt, file.ownUri),
columns: columns,
source: SqlViewSource(
source.substring(stmt.firstPosition, stmt.lastPosition)),
source: SqlViewSource('$createStmtForDatabase;'),
customParentClass: null,
entityInfoName: entityInfoName,
existingRowClass: existingRowClass,

View File

@ -62,8 +62,12 @@ class DriftView extends DriftElementWithResultSet {
abstract class DriftViewSource {}
class SqlViewSource extends DriftViewSource {
/// The `CREATE VIEW` statement as it appears in the `.drift` file.
final String createView;
/// The `CREATE VIEW` statement like it appears in the database, with drift-
/// specific syntax stripped out.
///
/// In particular, the [sqlCreateViewStmt] will not have a
/// [CreateViewStatement.driftTableName] set.
final String sqlCreateViewStmt;
/// The parsed `CREATE VIEW` statement from [createView].
///
@ -71,7 +75,7 @@ class SqlViewSource extends DriftViewSource {
/// analysis.
CreateViewStatement? parsedStatement;
SqlViewSource(this.createView);
SqlViewSource(this.sqlCreateViewStmt);
}
/// A table added to a view via a getter.

View File

@ -94,7 +94,8 @@ class ElementSerializer {
if (source is SqlViewSource) {
serializedSource = {
'kind': 'sql',
'sql': source.createView,
'sql': source.sqlCreateViewStmt,
'schema_sql': source.sqlCreateViewStmt,
};
} else if (source is DartViewSource) {
serializedSource = {

View File

@ -79,7 +79,7 @@ class SchemaWriter {
type = 'view';
data = {
'name': entity.schemaName,
'sql': source.createView,
'sql': source.sqlCreateViewStmt,
'dart_data_name': entity.nameOfRowClass,
'dart_info_name': entity.entityInfoName,
'columns': [for (final column in entity.columns) _columnData(column)],

View File

@ -82,7 +82,7 @@ class ViewWriter extends TableOrViewWriter {
if (astNode != null) {
emitter.writeSqlAsDartLiteral(astNode);
} else {
emitter.write(asDartLiteral(source.createView));
emitter.write(asDartLiteral(source.sqlCreateViewStmt));
}
buffer.writeln(';');
} else {

View File

@ -42,7 +42,7 @@ END;
CREATE INDEX groups_name ON "groups"(name);
CREATE VIEW my_view AS SELECT id FROM "groups";
CREATE VIEW my_view WITH MyViewRow AS SELECT id FROM "groups";
simple_query: SELECT * FROM my_view; -- not part of the schema
''',
@ -67,6 +67,11 @@ class SettingsConverter extends TypeConverter<Settings, String> {
Settings fromSql(String db) => Settings();
}
class MyViewRow {
final int id;
MyViewRow(this.id);
}
@DriftDatabase(include: {'a.drift'}, tables: [Users])
class Database {}
''',
@ -375,7 +380,7 @@ const expected = r'''
"data": {
"name": "my_view",
"sql": "CREATE VIEW my_view AS SELECT id FROM \"groups\";",
"dart_data_name": "MyViewData",
"dart_data_name": "MyViewRow",
"dart_info_name": "MyView",
"columns": [
{