Skip to content

Commit

Permalink
#813 HasTrigger is not working with synonymous tables
Browse files Browse the repository at this point in the history
  • Loading branch information
sjh37 committed Sep 7, 2023
1 parent 0aebda9 commit 8e03292
Show file tree
Hide file tree
Showing 15 changed files with 4,966 additions and 4,557 deletions.
62 changes: 47 additions & 15 deletions EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Runtime.Remoting.Messaging" #>
<#@ import namespace="System.Security" #>
<#@ import namespace="System.Security.Cryptography" #>
<#@ import namespace="System.Text" #>
Expand Down Expand Up @@ -12681,6 +12680,7 @@ and limitations under the License.
ChangeType<int>(rdr["PrimaryKeyOrdinal"]),
ChangeType<bool>(rdr["PrimaryKey"]),
ChangeType<bool>(rdr["IsForeignKey"]),
ChangeType<string>(rdr["SynonymTriggerName"]),
ChangeType<int>(rdr["Ordinal"]),
rdr["ColumnName"].ToString().Trim(),
rdr["Default"].ToString().Trim()
Expand Down Expand Up @@ -13483,6 +13483,9 @@ and limitations under the License.

public Column CreateColumn(RawTable rt, Table table, IDbContextFilter filter)
{
if (!string.IsNullOrEmpty(rt.SynonymTriggerName) && string.IsNullOrEmpty(table.TriggerName))
table.TriggerName = rt.SynonymTriggerName;

var col = new Column
{
Scale = rt.Scale,
Expand Down Expand Up @@ -14165,14 +14168,15 @@ SELECT T.TABLE_SCHEMA AS ""SchemaName"",
AND tcfk.TABLE_NAME = fk.TABLE_NAME
AND tcfk.CONSTRAINT_NAME = fk.CONSTRAINT_NAME
AND tcfk.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND C.TABLE_SCHEMA = fk.TABLE_SCHEMA
AND C.TABLE_SCHEMA = fk.TABLE_SCHEMA
AND C.TABLE_NAME = fk.TABLE_NAME
AND C.COLUMN_NAME = fk.COLUMN_NAME) THEN 1 ELSE 0 END AS bit) AS ""IsForeignKey""
AND C.COLUMN_NAME = fk.COLUMN_NAME) THEN 1 ELSE 0 END AS bit) AS ""IsForeignKey"",
NULL AS ""SynonymTriggerName"",
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
ON T.TABLE_SCHEMA = C.TABLE_SCHEMA
AND C.TABLE_NAME = T.TABLE_NAME
AND C.table_catalog = T.table_catalog
AND C.table_catalog = T.table_catalog
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE pk
ON tc.CONSTRAINT_SCHEMA = pk.TABLE_SCHEMA
Expand Down Expand Up @@ -14689,20 +14693,22 @@ ORDER BY SchemaName, TableName, TriggerName;";
public readonly int PrimaryKeyOrdinal;
public readonly bool PrimaryKey;
public readonly bool IsForeignKey;
public readonly string SynonymTriggerName;
public readonly int Ordinal;
public readonly string ColumnName;
public readonly string Default;

public RawTable(string schemaName, string tableName, bool isView, int scale,
string typeName, bool isNullable, int maxLength, int dateTimePrecision, int precision,
bool isIdentity, bool isComputed, bool isRowGuid, byte generatedAlwaysType,
bool isStoreGenerated, int primaryKeyOrdinal, bool primaryKey, bool isForeignKey,
bool isStoreGenerated, int primaryKeyOrdinal, bool primaryKey, bool isForeignKey, string synonymTriggerName,
int ordinal, string columnName, string @default)
{
// Table
SchemaName = schemaName;
TableName = tableName;
IsView = isView;
SchemaName = schemaName;
TableName = tableName;
IsView = isView;
SynonymTriggerName = synonymTriggerName;

// Column
Scale = scale;
Expand Down Expand Up @@ -14796,7 +14802,8 @@ SELECT 'main' AS SchemaName,
0 AS IsStoreGenerated,
pk AS PrimaryKey,
pk AS PrimaryKeyOrdinal,
0 AS IsForeignKey
0 AS IsForeignKey,
NULL AS SynonymTriggerName
FROM (SELECT m.tbl_name AS TableName,
CASE WHEN m.type = 'table' THEN 'BASE TABLE' ELSE 'VIEW' END AS TableType,
c.cid AS Ordinal,
Expand Down Expand Up @@ -15066,7 +15073,8 @@ SELECT '' AS SchemaName,
CAST(CASE WHEN c.DATA_TYPE = N'rowversion' THEN 1 ELSE 0 END AS BIT) AS IsStoreGenerated,
0 AS PrimaryKeyOrdinal,
CAST(CASE WHEN u.TABLE_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS PrimaryKey,
CONVERT( bit, 0 ) as IsForeignKey
CONVERT( bit, 0 ) as IsForeignKey,
NULL as SynonymTriggerName
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN INFORMATION_SCHEMA.TABLES t ON c.TABLE_NAME = t.TABLE_NAME
Expand Down Expand Up @@ -15428,7 +15436,8 @@ SELECT

CONVERT( bit, ISNULL( pk.ORDINAL_POSITION, 0 ) ) AS PrimaryKey,
ISNULL(pk.ORDINAL_POSITION, 0) PrimaryKeyOrdinal,
CONVERT( bit, CASE WHEN fk.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END ) AS IsForeignKey
CONVERT( bit, CASE WHEN fk.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END ) AS IsForeignKey,
NULL AS SynonymTriggerName

FROM
#Columns c
Expand Down Expand Up @@ -15788,7 +15797,18 @@ SELECT TOP (0)
END AS BIT) AS IsStoreGenerated,
CAST(CASE WHEN pk.ORDINAL_POSITION IS NULL THEN 0 ELSE 1 END AS BIT) AS PrimaryKey,
ISNULL(pk.ORDINAL_POSITION, 0) PrimaryKeyOrdinal,
CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS IsForeignKey
CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS IsForeignKey,
( SELECT TOP(1) T.name AS TriggerName
FROM sys.triggers T
LEFT JOIN sys.all_objects TOBJ
ON T.parent_id = TOBJ.object_id
LEFT JOIN sys.schemas TSCH
ON TSCH.schema_id = TOBJ.schema_id
WHERE T.type = 'TR'
AND T.is_disabled = 0
AND TSCH.name IS NOT NULL
AND TOBJ.name IS NOT NULL
AND sc.NAME = TSCH.name AND sn.name = TOBJ.name) AS SynonymTriggerName
INTO
#SynonymDetails
FROM
Expand Down Expand Up @@ -15829,7 +15849,7 @@ FROM
DECLARE @synonymDetailsQueryTemplate nvarchar(max) = 'USE [@synonmymDatabaseName];
INSERT INTO #SynonymDetails (
SchemaName, TableName, TableType, TableTemporalType, Ordinal, ColumnName, IsNullable, TypeName, [MaxLength], [Precision], [Default], DateTimePrecision, Scale,
IsIdentity, IsRowGuid, IsComputed, GeneratedAlwaysType, IsStoreGenerated, PrimaryKey, PrimaryKeyOrdinal, IsForeignKey
IsIdentity, IsRowGuid, IsComputed, GeneratedAlwaysType, IsStoreGenerated, PrimaryKey, PrimaryKeyOrdinal, IsForeignKey, SynonymTriggerName
)
SELECT
st.SynonymSchemaName AS SchemaName,
Expand Down Expand Up @@ -15868,7 +15888,18 @@ SELECT

CAST(CASE WHEN pk.ORDINAL_POSITION IS NULL THEN 0 ELSE 1 END AS BIT) AS PrimaryKey,
ISNULL(pk.ORDINAL_POSITION, 0) PrimaryKeyOrdinal,
CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS IsForeignKey
CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS IsForeignKey,
( SELECT TOP(1) T.name AS TriggerName
FROM sys.triggers T
LEFT JOIN sys.all_objects TOBJ
ON T.parent_id = TOBJ.object_id
LEFT JOIN sys.schemas TSCH
ON TSCH.schema_id = TOBJ.schema_id
WHERE T.type = ''TR''
AND T.is_disabled = 0
AND TSCH.name IS NOT NULL
AND TOBJ.name IS NOT NULL
AND st.SynonymSchemaName = TSCH.name AND st.SynonymName = TOBJ.name) AS SynonymTriggerName
FROM
#SynonymTargets st

Expand Down Expand Up @@ -15992,7 +16023,8 @@ SELECT
IsStoreGenerated,
PrimaryKey,
PrimaryKeyOrdinal,
IsForeignKey
IsForeignKey,
SynonymTriggerName
FROM
#SynonymDetails";
}
Expand Down
8 changes: 4 additions & 4 deletions Generator.Tests.Unit/ViewTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Generator.Tests.Unit
{
using System.Collections.Generic;
using Efrpg;
using Efrpg.FileManagement;
using Efrpg.Generators;
using Efrpg.Readers;
using Generator.Tests.Common;
using Common;
using NUnit.Framework;
using System.Collections.Generic;

[TestFixture, NonParallelizable]
[Category(Constants.CI)]
Expand All @@ -31,7 +31,7 @@ public void OneTimeSetUp()
[TestCase("a b c")]
public void InvalidSchema(string schema)
{
_rawTables.Add(new RawTable(schema, "test", false, 0, "int", false, 1, 1, 1, false, false, false, 0, false, 1, true, false, 1, "name", "123"));
_rawTables.Add(new RawTable(schema, "test", false, 0, "int", false, 1, 1, 1, false, false, false, 0, false, 1, true, false, null, 1, "name", "123"));

_sut.LoadTables();
}
Expand All @@ -41,7 +41,7 @@ public void InvalidSchema(string schema)
[TestCase("view with multiple spaces")]
public void InvalidName(string name)
{
_rawTables.Add(new RawTable("dbo", name, false, 0, "int", false, 1, 1, 1, false, false, false, 0, false, 1, true, false, 1, "name", "123"));
_rawTables.Add(new RawTable("dbo", name, false, 0, "int", false, 1, 1, 1, false, false, false, 0, false, 1, true, false, null, 1, "name", "123"));
_sut.LoadTables();
}

Expand Down
4 changes: 4 additions & 0 deletions Generator/Readers/DatabaseReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public List<RawTable> ReadTables(bool includeSynonyms)
ChangeType<int>(rdr["PrimaryKeyOrdinal"]),
ChangeType<bool>(rdr["PrimaryKey"]),
ChangeType<bool>(rdr["IsForeignKey"]),
ChangeType<string>(rdr["SynonymTriggerName"]),
ChangeType<int>(rdr["Ordinal"]),
rdr["ColumnName"].ToString().Trim(),
rdr["Default"].ToString().Trim()
Expand Down Expand Up @@ -1060,6 +1061,9 @@ private static string GetReaderString(DbDataReader rdr, string name)

public Column CreateColumn(RawTable rt, Table table, IDbContextFilter filter)
{
if (!string.IsNullOrEmpty(rt.SynonymTriggerName) && string.IsNullOrEmpty(table.TriggerName))
table.TriggerName = rt.SynonymTriggerName;

var col = new Column
{
Scale = rt.Scale,
Expand Down
4 changes: 1 addition & 3 deletions Generator/Readers/MinMaxValueCache.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Collections.Generic;

namespace Efrpg.Readers
{
Expand Down
7 changes: 4 additions & 3 deletions Generator/Readers/PostgreSqlDatabaseReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE fk
AND tcfk.TABLE_NAME = fk.TABLE_NAME
AND tcfk.CONSTRAINT_NAME = fk.CONSTRAINT_NAME
AND tcfk.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND C.TABLE_SCHEMA = fk.TABLE_SCHEMA
AND C.TABLE_SCHEMA = fk.TABLE_SCHEMA
AND C.TABLE_NAME = fk.TABLE_NAME
AND C.COLUMN_NAME = fk.COLUMN_NAME) THEN 1 ELSE 0 END AS bit) AS ""IsForeignKey""
AND C.COLUMN_NAME = fk.COLUMN_NAME) THEN 1 ELSE 0 END AS bit) AS ""IsForeignKey"",
NULL AS ""SynonymTriggerName"",
FROM INFORMATION_SCHEMA.TABLES T
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
ON T.TABLE_SCHEMA = C.TABLE_SCHEMA
AND C.TABLE_NAME = T.TABLE_NAME
AND C.table_catalog = T.table_catalog
AND C.table_catalog = T.table_catalog
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE pk
ON tc.CONSTRAINT_SCHEMA = pk.TABLE_SCHEMA
Expand Down
10 changes: 6 additions & 4 deletions Generator/Readers/RawTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ public class RawTable
public readonly int PrimaryKeyOrdinal;
public readonly bool PrimaryKey;
public readonly bool IsForeignKey;
public readonly string SynonymTriggerName;
public readonly int Ordinal;
public readonly string ColumnName;
public readonly string Default;

public RawTable(string schemaName, string tableName, bool isView, int scale,
string typeName, bool isNullable, int maxLength, int dateTimePrecision, int precision,
bool isIdentity, bool isComputed, bool isRowGuid, byte generatedAlwaysType,
bool isStoreGenerated, int primaryKeyOrdinal, bool primaryKey, bool isForeignKey,
bool isStoreGenerated, int primaryKeyOrdinal, bool primaryKey, bool isForeignKey, string synonymTriggerName,
int ordinal, string columnName, string @default)
{
// Table
SchemaName = schemaName;
TableName = tableName;
IsView = isView;
SchemaName = schemaName;
TableName = tableName;
IsView = isView;
SynonymTriggerName = synonymTriggerName;

// Column
Scale = scale;
Expand Down
3 changes: 2 additions & 1 deletion Generator/Readers/SQLiteDatabaseReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ protected override string TableSQL()
0 AS IsStoreGenerated,
pk AS PrimaryKey,
pk AS PrimaryKeyOrdinal,
0 AS IsForeignKey
0 AS IsForeignKey,
NULL AS SynonymTriggerName
FROM (SELECT m.tbl_name AS TableName,
CASE WHEN m.type = 'table' THEN 'BASE TABLE' ELSE 'VIEW' END AS TableType,
c.cid AS Ordinal,
Expand Down
3 changes: 2 additions & 1 deletion Generator/Readers/SqlServerCeDatabaseReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ protected override string TableSQL()
CAST(CASE WHEN c.DATA_TYPE = N'rowversion' THEN 1 ELSE 0 END AS BIT) AS IsStoreGenerated,
0 AS PrimaryKeyOrdinal,
CAST(CASE WHEN u.TABLE_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS PrimaryKey,
CONVERT( bit, 0 ) as IsForeignKey
CONVERT( bit, 0 ) as IsForeignKey,
NULL as SynonymTriggerName
FROM
INFORMATION_SCHEMA.COLUMNS c
INNER JOIN INFORMATION_SCHEMA.TABLES t ON c.TABLE_NAME = t.TABLE_NAME
Expand Down
34 changes: 29 additions & 5 deletions Generator/Readers/SqlServerDatabaseReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ THEN 1
CONVERT( bit, ISNULL( pk.ORDINAL_POSITION, 0 ) ) AS PrimaryKey,
ISNULL(pk.ORDINAL_POSITION, 0) PrimaryKeyOrdinal,
CONVERT( bit, CASE WHEN fk.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END ) AS IsForeignKey
CONVERT( bit, CASE WHEN fk.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END ) AS IsForeignKey,
NULL AS SynonymTriggerName
FROM
#Columns c
Expand Down Expand Up @@ -544,7 +545,18 @@ ELSE 0
END AS BIT) AS IsStoreGenerated,
CAST(CASE WHEN pk.ORDINAL_POSITION IS NULL THEN 0 ELSE 1 END AS BIT) AS PrimaryKey,
ISNULL(pk.ORDINAL_POSITION, 0) PrimaryKeyOrdinal,
CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS IsForeignKey
CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS IsForeignKey,
( SELECT TOP(1) T.name AS TriggerName
FROM sys.triggers T
LEFT JOIN sys.all_objects TOBJ
ON T.parent_id = TOBJ.object_id
LEFT JOIN sys.schemas TSCH
ON TSCH.schema_id = TOBJ.schema_id
WHERE T.type = 'TR'
AND T.is_disabled = 0
AND TSCH.name IS NOT NULL
AND TOBJ.name IS NOT NULL
AND sc.NAME = TSCH.name AND sn.name = TOBJ.name) AS SynonymTriggerName
INTO
#SynonymDetails
FROM
Expand Down Expand Up @@ -585,7 +597,7 @@ INFORMATION_SCHEMA.KEY_COLUMN_USAGE u
DECLARE @synonymDetailsQueryTemplate nvarchar(max) = 'USE [@synonmymDatabaseName];
INSERT INTO #SynonymDetails (
SchemaName, TableName, TableType, TableTemporalType, Ordinal, ColumnName, IsNullable, TypeName, [MaxLength], [Precision], [Default], DateTimePrecision, Scale,
IsIdentity, IsRowGuid, IsComputed, GeneratedAlwaysType, IsStoreGenerated, PrimaryKey, PrimaryKeyOrdinal, IsForeignKey
IsIdentity, IsRowGuid, IsComputed, GeneratedAlwaysType, IsStoreGenerated, PrimaryKey, PrimaryKeyOrdinal, IsForeignKey, SynonymTriggerName
)
SELECT
st.SynonymSchemaName AS SchemaName,
Expand Down Expand Up @@ -624,7 +636,18 @@ ELSE 0
CAST(CASE WHEN pk.ORDINAL_POSITION IS NULL THEN 0 ELSE 1 END AS BIT) AS PrimaryKey,
ISNULL(pk.ORDINAL_POSITION, 0) PrimaryKeyOrdinal,
CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS IsForeignKey
CAST(CASE WHEN fk.COLUMN_NAME IS NULL THEN 0 ELSE 1 END AS BIT) AS IsForeignKey,
( SELECT TOP(1) T.name AS TriggerName
FROM sys.triggers T
LEFT JOIN sys.all_objects TOBJ
ON T.parent_id = TOBJ.object_id
LEFT JOIN sys.schemas TSCH
ON TSCH.schema_id = TOBJ.schema_id
WHERE T.type = ''TR''
AND T.is_disabled = 0
AND TSCH.name IS NOT NULL
AND TOBJ.name IS NOT NULL
AND st.SynonymSchemaName = TSCH.name AND st.SynonymName = TOBJ.name) AS SynonymTriggerName
FROM
#SynonymTargets st
Expand Down Expand Up @@ -748,7 +771,8 @@ protected override string SynonymTableSQL()
IsStoreGenerated,
PrimaryKey,
PrimaryKeyOrdinal,
IsForeignKey
IsForeignKey,
SynonymTriggerName
FROM
#SynonymDetails";
}
Expand Down
Loading

0 comments on commit 8e03292

Please sign in to comment.