From da9ca61e0c0b638cb794844c6e187e3b52f0fe8c Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Thu, 12 Dec 2019 18:06:56 +0100 Subject: [PATCH] Don't remap tables from sql -> moor -> sql --- extras/plugin_example/README.md | 7 +++++-- .../lib/src/analyzer/moor/create_table_reader.dart | 8 +++++--- .../lib/src/analyzer/sql_queries/meta/declarations.dart | 4 +++- .../lib/src/analyzer/sql_queries/type_mapping.dart | 5 +++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/extras/plugin_example/README.md b/extras/plugin_example/README.md index 2e92c551..a980865c 100644 --- a/extras/plugin_example/README.md +++ b/extras/plugin_example/README.md @@ -10,7 +10,7 @@ Currently, we support - navigation for references in sql queries ## Setup -To debug this plugin, you'll need to perform these steps once. It is assumed that you +To use this plugin, you'll need to perform these steps once. It is assumed that you have already cloned the `moor` repository. 1. Make sure you run version `3.5.0` or later of the Dart extension in VS Code. @@ -23,7 +23,10 @@ have already cloned the `moor` repository. ``` 3. Uncomment the plugin lines in `analysis_options.yaml` -## Running +## Debugging +Note: If you only want to _use_ the plugin and don't care about debugging it, follow the step +from the [user documentation](https://moor.simonbinder.eu/docs/using-sql/sql_ide/). + After you completed the setup, these steps will open an editor instance that runs the plugin. 1. chdir into `moor_generator` and run `lib/plugin.dart`. You can run that file from an IDE if you need debugging capabilities, but starting it from the command line is fine. Keep that diff --git a/moor_generator/lib/src/analyzer/moor/create_table_reader.dart b/moor_generator/lib/src/analyzer/moor/create_table_reader.dart index 761702bf..04349459 100644 --- a/moor_generator/lib/src/analyzer/moor/create_table_reader.dart +++ b/moor_generator/lib/src/analyzer/moor/create_table_reader.dart @@ -121,9 +121,11 @@ class CreateTableReader { overrideDontWriteConstraints: true, ); - return specifiedTable - ..declaration = - TableDeclaration(specifiedTable, step.file, null, table.definition); + final declaration = TableDeclaration( + specifiedTable, step.file, null, table.definition, + tableFromSqlParser: table); + + return specifiedTable..declaration = declaration; } UsedTypeConverter _readTypeConverter(MappedBy mapper) { diff --git a/moor_generator/lib/src/analyzer/sql_queries/meta/declarations.dart b/moor_generator/lib/src/analyzer/sql_queries/meta/declarations.dart index 281849cc..d486120e 100644 --- a/moor_generator/lib/src/analyzer/sql_queries/meta/declarations.dart +++ b/moor_generator/lib/src/analyzer/sql_queries/meta/declarations.dart @@ -39,8 +39,10 @@ class ColumnDeclaration extends BaseDeclaration { /// a referenced table was declared and provide navigation hints. class TableDeclaration extends BaseDeclaration { final SpecifiedTable table; + final Table tableFromSqlParser; TableDeclaration(this.table, FoundFile declarationFile, - Element dartDeclaration, AstNode moorDeclaration) + Element dartDeclaration, AstNode moorDeclaration, + {this.tableFromSqlParser}) : super(declarationFile, dartDeclaration, moorDeclaration); } diff --git a/moor_generator/lib/src/analyzer/sql_queries/type_mapping.dart b/moor_generator/lib/src/analyzer/sql_queries/type_mapping.dart index 1aa7afd9..d3a034d0 100644 --- a/moor_generator/lib/src/analyzer/sql_queries/type_mapping.dart +++ b/moor_generator/lib/src/analyzer/sql_queries/type_mapping.dart @@ -14,6 +14,11 @@ class TypeMapper { /// Convert a [SpecifiedTable] from moor into something that can be understood /// by the sqlparser library. Table extractStructure(SpecifiedTable table) { + final existingTable = table.declaration?.tableFromSqlParser; + if (existingTable != null) { + return existingTable; + } + final columns = []; for (final specified in table.columns) { final hint = specified.typeConverter != null