From 36f1802ae2f78d19b78ae9019c20f193c23766ee Mon Sep 17 00:00:00 2001 From: ErikEJ Date: Sun, 5 May 2019 14:22:15 +0200 Subject: [PATCH] Implement reverse engineering of views for SQLite --- .../Internal/SqliteDatabaseModelFactory.cs | 3 ++- .../SqliteDatabaseModelFactoryTest.cs | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/EFCore.Sqlite.Core/Scaffolding/Internal/SqliteDatabaseModelFactory.cs b/src/EFCore.Sqlite.Core/Scaffolding/Internal/SqliteDatabaseModelFactory.cs index 6f38e38c0a5..134255d731a 100644 --- a/src/EFCore.Sqlite.Core/Scaffolding/Internal/SqliteDatabaseModelFactory.cs +++ b/src/EFCore.Sqlite.Core/Scaffolding/Internal/SqliteDatabaseModelFactory.cs @@ -155,7 +155,7 @@ private IEnumerable GetTables(DbConnection connection, IEnumerabl command.CommandText = new StringBuilder() .AppendLine("SELECT \"name\"") .AppendLine("FROM \"sqlite_master\"") - .Append("WHERE \"type\" = 'table' AND instr(\"name\", 'sqlite_') <> 1 AND \"name\" NOT IN ('") + .Append("WHERE \"type\" IN ('table', 'view') AND instr(\"name\", 'sqlite_') <> 1 AND \"name\" NOT IN ('") .Append(HistoryRepository.DefaultTableName) .Append("', 'ElementaryGeometries', 'geometry_columns', 'geometry_columns_auth', ") .Append("'geometry_columns_field_infos', 'geometry_columns_statistics', 'geometry_columns_time', ") @@ -163,6 +163,7 @@ private IEnumerable GetTables(DbConnection connection, IEnumerabl .Append("'sql_statements_log', 'views_geometry_columns', 'views_geometry_columns_auth', ") .Append("'views_geometry_columns_field_infos', 'views_geometry_columns_statistics', ") .Append("'virts_geometry_columns', 'virts_geometry_columns_auth', ") + .Append("'geom_cols_ref_sys', 'spatial_ref_sys_all', ") .AppendLine("'virts_geometry_columns_field_infos', 'virts_geometry_columns_statistics');") .ToString(); diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/SqliteDatabaseModelFactoryTest.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/SqliteDatabaseModelFactoryTest.cs index 35332930883..ff5b312cdd5 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/SqliteDatabaseModelFactoryTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/SqliteDatabaseModelFactoryTest.cs @@ -164,6 +164,33 @@ Name text NOT NULL "DROP TABLE MountainsColumns;"); } + [Fact] + public void Create_view_columns() + { + Test( + @" +CREATE VIEW MountainsColumnsView + AS +SELECT + CAST(100 AS integer) AS Id, + CAST('' AS text) AS Name;", + Enumerable.Empty(), + Enumerable.Empty(), + dbModel => + { + var table = dbModel.Tables.Single(); + + Assert.Equal(2, table.Columns.Count); + Assert.Equal(null, table.PrimaryKey); + Assert.All( + table.Columns, c => Assert.Equal("MountainsColumnsView", c.Table.Name)); + + Assert.Single(table.Columns.Where(c => c.Name == "Id")); + Assert.Single(table.Columns.Where(c => c.Name == "Name")); + }, + "DROP VIEW MountainsColumnsView;"); + } + [Fact] public void Create_primary_key() {