Skip to content

Commit ddeeed6

Browse files
committed
Preparing for VNaviForeignKey ...
1 parent e767d8d commit ddeeed6

32 files changed

+703
-63
lines changed

T4SQLTemplateLibrary/DataAccess/DbAccess.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ namespace DbParallel.DataAccess
88
{
99
public partial class DbAccess : IDisposable
1010
{
11+
private static CommandType _DefaultCommandType = CommandType.StoredProcedure;
12+
public static CommandType DefaultCommandType
13+
{
14+
get { return _DefaultCommandType; }
15+
set { _DefaultCommandType = value; }
16+
}
17+
1118
private const int _MaxRetryCount = 2;
12-
private DbConnection _Connection;
1319

20+
private DbConnection _Connection;
1421
public DbConnection Connection { get { return _Connection; } }
1522

1623
public DbAccess(DbProviderFactory dbProviderFactory, string connectionString)
@@ -88,7 +95,7 @@ public void ExecuteReader(string commandText, int commandTimeout, CommandType co
8895

8996
public void ExecuteReader(string commandText, Action<DbParameterBuilder> parametersBuilder, Action<DbDataReader> dataReader)
9097
{
91-
ExecuteReader(commandText, 0, CommandType.StoredProcedure, parametersBuilder, dataReader);
98+
ExecuteReader(commandText, 0, _DefaultCommandType, parametersBuilder, dataReader);
9299
}
93100

94101
public void ExecuteReader(string commandText, int commandTimeout, CommandType commandType, Action<DbParameterBuilder> parametersBuilder, Action<DbDataReader, int> dataReaders)
@@ -112,7 +119,7 @@ public void ExecuteReader(string commandText, int commandTimeout, CommandType co
112119

113120
public void ExecuteReader(string commandText, Action<DbParameterBuilder> parametersBuilder, Action<DbDataReader, int> dataReaders)
114121
{
115-
ExecuteReader(commandText, 0, CommandType.StoredProcedure, parametersBuilder, dataReaders);
122+
ExecuteReader(commandText, 0, _DefaultCommandType, parametersBuilder, dataReaders);
116123
}
117124

118125
public void ExecuteReader<T>(string commandText, int commandTimeout, CommandType commandType, Action<DbParameterBuilder> parametersBuilder,
@@ -138,12 +145,12 @@ public void ExecuteReader<T>(string commandText, int commandTimeout, CommandType
138145
public void ExecuteReader<T>(string commandText, Action<DbParameterBuilder> parametersBuilder,
139146
Action<DbFieldMap<T>> resultMap, Action<T> readEntity) where T : new()
140147
{
141-
ExecuteReader<T>(commandText, 0, CommandType.StoredProcedure, parametersBuilder, resultMap, readEntity);
148+
ExecuteReader<T>(commandText, 0, _DefaultCommandType, parametersBuilder, resultMap, readEntity);
142149
}
143150

144151
public void ExecuteReader<T>(string commandText, Action<DbParameterBuilder> parametersBuilder, Action<T> readEntity) where T : new()
145152
{
146-
ExecuteReader<T>(commandText, 0, CommandType.StoredProcedure, parametersBuilder, null, readEntity);
153+
ExecuteReader<T>(commandText, 0, _DefaultCommandType, parametersBuilder, null, readEntity);
147154
}
148155

149156
public IEnumerable<T> ExecuteReader<T>(string commandText, int commandTimeout, CommandType commandType,
@@ -166,7 +173,7 @@ public IEnumerable<T> ExecuteReader<T>(string commandText, int commandTimeout, C
166173
public IEnumerable<T> ExecuteReader<T>(string commandText, Action<DbParameterBuilder> parametersBuilder,
167174
Action<DbFieldMap<T>> resultMap = null) where T : new()
168175
{
169-
return ExecuteReader<T>(commandText, 0, CommandType.StoredProcedure, parametersBuilder, resultMap);
176+
return ExecuteReader<T>(commandText, 0, _DefaultCommandType, parametersBuilder, resultMap);
170177
}
171178

172179
public int ExecuteNonQuery(string commandText, int commandTimeout, CommandType commandType, Action<DbParameterBuilder> parametersBuilder)
@@ -194,7 +201,7 @@ public int ExecuteNonQuery(string commandText, int commandTimeout, CommandType c
194201

195202
public int ExecuteNonQuery(string commandText, Action<DbParameterBuilder> parametersBuilder = null)
196203
{
197-
return ExecuteNonQuery(commandText, 0, CommandType.StoredProcedure, parametersBuilder);
204+
return ExecuteNonQuery(commandText, 0, _DefaultCommandType, parametersBuilder);
198205
}
199206

200207
private void ReConnect()

T4SQLTemplateLibrary/DataAccess/DbParameterBuilder.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Data;
23
using System.Data.Common;
34

45
namespace DbParallel.DataAccess
@@ -29,6 +30,20 @@ public DbParameter Add(string parameterName, object oValue)
2930

3031
return parameter;
3132
}
33+
34+
public DbParameter AddOutput(string parameterName, int nSize = 0)
35+
{
36+
DbParameter parameter = _DbCommand.CreateParameter();
37+
parameter.ParameterName = parameterName;
38+
parameter.Direction = ParameterDirection.Output;
39+
40+
if (nSize > 0)
41+
parameter.Size = nSize;
42+
43+
_DbCommand.Parameters.Add(parameter);
44+
45+
return parameter;
46+
}
3247
}
3348
}
3449

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
CREATE PROCEDURE T4SQL.ENGINE_GET_FOREIGN_KEY
2+
(
3+
@inTable_Name NVARCHAR(64),
4+
@outTable_Name NVARCHAR(128) OUTPUT
5+
)
6+
AS
7+
SET NOCOUNT ON;
8+
DECLARE @tTable_Id INT;
9+
10+
SET @tTable_Id = OBJECT_ID(@inTable_Name);
11+
12+
SELECT
13+
@outTable_Name = QUOTENAME(S.name) + N'.' + QUOTENAME(T.name)
14+
FROM
15+
sys.schemas S,
16+
sys.objects T
17+
WHERE
18+
S.schema_id = T.schema_id
19+
AND T.object_id = @tTable_Id;
20+
21+
SELECT
22+
C.name AS CONSTRAINT_NAME,
23+
B.name AS FOREIGN_KEY_COLUMN,
24+
B.is_nullable AS FOREIGN_KEY_NULLABLE,
25+
QUOTENAME(S.name) + N'.' + QUOTENAME(P.name)
26+
AS REFERENCED_TABLE,
27+
A.name AS REFERENCED_COLUMN,
28+
A.is_nullable AS REFERENCED_NULLABLE
29+
FROM
30+
sys.schemas S, -- Primary/Unique Table/View Schema
31+
sys.objects P, -- Primary/Unique Table/View
32+
sys.columns A, -- Primary/Unique Key Colums
33+
sys.columns B, -- Foreign Key Columns
34+
sys.objects C, -- Constraint Name
35+
sys.foreign_key_columns F -- Driving
36+
WHERE
37+
S.schema_id = P.schema_id
38+
AND P.object_id = F.referenced_object_id
39+
AND A.column_id = F.referenced_column_id
40+
AND A.object_id = F.referenced_object_id
41+
AND B.column_id = F.parent_column_id
42+
AND B.object_id = F.parent_object_id
43+
AND C.object_id = F.constraint_object_id
44+
AND F.parent_object_id = @tTable_Id
45+
ORDER BY
46+
F.constraint_object_id,
47+
F.parent_column_id;
48+
49+
----------------------------------------------------------------------------------------------------
50+
--
51+
-- Copyright 2013 Abel Cheng
52+
-- This source code is subject to terms and conditions of the Apache License, Version 2.0.
53+
-- See http://www.apache.org/licenses/LICENSE-2.0.
54+
-- All other rights reserved.
55+
-- You must not remove this notice, or any other, from this software.
56+
--
57+
-- Original Author: Abel Cheng <[email protected]>
58+
-- Created Date: ‎‎‎July ‎04, ‎2013, ‏‎6:44:27 PM
59+
-- Primary Host: http://t4sql.codeplex.com
60+
-- Change Log:
61+
-- Author Date Comment
62+
--
63+
--
64+
--
65+
--
66+
-- (Keep code clean)
67+
--
68+
----------------------------------------------------------------------------------------------------

T4SQLTemplateLibrary/Databases/SqlServer/Schema Objects/Schemas/T4SQL/Programmability/Stored Procedures/T4SQL.ENGINE_LIST_COLUMN.proc.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ AS
1717
*/
1818

1919
SELECT
20-
name AS COLUMN_NAME
20+
name AS COLUMN_NAME,
21+
IS_NULLABLE
2122
FROM
2223
sys.columns
2324
WHERE

T4SQLTemplateLibrary/Databases/SqlServer/T4SQLDB.sqlproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@
306306
<Build Include="Schema Objects\Schemas\T4SQL\Programmability\Stored Procedures\T4SQL.CMD_BUILD_SCRIPTS.sql" />
307307
<Build Include="Schema Objects\Schemas\T4SQL\Programmability\Stored Procedures\T4SQL.CMD_PRINT_ALL_LINES.sql" />
308308
<Build Include="Schema Objects\Schemas\T4SQL\Programmability\Stored Procedures\T4SQL.META_CREATE_WORKSPACE.sql" />
309+
<Build Include="Schema Objects\Schemas\T4SQL\Programmability\Stored Procedures\T4SQL.ENGINE_GET_FOREIGN_KEY.sql" />
309310
</ItemGroup>
310311
<ItemGroup>
311312
<None Include="Scripts\Post-Deployment\1-T4SQL.ENGINE_CONFIG.data.sql">

T4SQLTemplateLibrary/SqlBuilder/DbPackage.cs

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using System.Data;
33
using System.Data.Common;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using DbParallel.DataAccess;
7+
using T4SQL.MetaData;
68

79
namespace T4SQL.SqlBuilder
810
{
@@ -18,7 +20,7 @@ private static string GetProcedure(string sp)
1820
return EngineConfig.DatabasePackage + sp;
1921
}
2022

21-
public static ServerEnvironment GetDbServerEnv(this DbAccess dbAccess)
23+
public static DbmsEnvironment GetDbServerEnv(this DbAccess dbAccess)
2224
{
2325
const string sp = "GET_DB_SERVER_ENV";
2426
DbParameter outDatabase_Platform = null;
@@ -28,14 +30,16 @@ public static ServerEnvironment GetDbServerEnv(this DbAccess dbAccess)
2830

2931
dbAccess.ExecuteNonQuery(GetProcedure(sp), parameters =>
3032
{
31-
outDatabase_Platform = parameters.Add().SetName("outDatabase_Platform").SetDirection(ParameterDirection.Output).SetSize(32);
32-
outDatabase_Product = parameters.Add().SetName("outDatabase_Product").SetDirection(ParameterDirection.Output).SetSize(256);
33-
outProduct_Version = parameters.Add().SetName("outProduct_Version").SetDirection(ParameterDirection.Output).SetSize(64);
34-
outServer_Name = parameters.Add().SetName("outServer_Name").SetDirection(ParameterDirection.Output).SetSize(64);
33+
outDatabase_Platform = parameters.AddOutput("outDatabase_Platform", 32);
34+
outDatabase_Product = parameters.AddOutput("outDatabase_Product", 256);
35+
outProduct_Version = parameters.AddOutput("outProduct_Version", 64);
36+
outServer_Name = parameters.AddOutput("outServer_Name", 64);
3537
});
3638

37-
return new ServerEnvironment(tableName => dbAccess.ListTableColumns(tableName), outDatabase_Platform.Parameter<string>(),
38-
outDatabase_Product.Parameter<string>(), outProduct_Version.Parameter<string>(), outServer_Name.Parameter<string>());
39+
return new DbmsEnvironment(tableName => dbAccess.ListTableColumns(tableName),
40+
tableName => dbAccess.LoadForeignKeys(tableName),
41+
outDatabase_Platform.Parameter<string>(), outDatabase_Product.Parameter<string>(),
42+
outProduct_Version.Parameter<string>(), outServer_Name.Parameter<string>());
3943
}
4044

4145
public static void LoadEngineConfig(this DbAccess dbAccess)
@@ -45,7 +49,7 @@ public static void LoadEngineConfig(this DbAccess dbAccess)
4549

4650
dbAccess.ExecuteNonQuery(GetProcedure(sp), parameters =>
4751
{
48-
outPollInterval = parameters.Add().SetName("outPoll_Interval").SetDirection(ParameterDirection.Output).SetDbType(DbType.Byte);
52+
outPollInterval = parameters.AddOutput("outPoll_Interval").SetDbType(DbType.Byte);
4953
});
5054

5155
EngineConfig.EnginePollInterval = outPollInterval.Parameter<byte>() * 1000;
@@ -57,7 +61,7 @@ private static EngineMain.ServiceMode Ping(DbAccess dbAccess, string sp)
5761

5862
dbAccess.ExecuteNonQuery(GetProcedure(sp), parameters =>
5963
{
60-
outParameter = parameters.Add().SetName("outSwitch_To_Mode").SetDirection(ParameterDirection.Output).SetSize(EngineMain.ServiceModeMaxLen);
64+
outParameter = parameters.AddOutput("outSwitch_To_Mode", EngineMain.ServiceModeMaxLen);
6165
});
6266

6367
EngineMain.ServiceMode newMode;
@@ -111,20 +115,54 @@ public static void RegisterTemplatesSpec(this DbAccess dbAccess, string inClass_
111115
});
112116
}
113117

114-
internal static List<string> ListTableColumns(this DbAccess dbAccess, string tableName)
118+
internal static IEnumerable<DbmsColumn> ListTableColumns(this DbAccess dbAccess, string tableName)
115119
{
116120
const string sp = "LIST_COLUMN";
117-
List<string> columns = new List<string>();
118121

119-
dbAccess.ExecuteReader(GetProcedure(sp), parameters =>
122+
return dbAccess.ExecuteReader<DbmsColumn>(GetProcedure(sp), parameters =>
120123
{
121124
parameters.Add("inTable_Name", tableName);
122-
}, reader =>
125+
}, map =>
123126
{
124-
columns.Add(reader.Field<string>("COLUMN_NAME"));
127+
map.Add("COLUMN_NAME", t => t.ColumnName);
128+
map.Add("IS_NULLABLE", t => t.IsNullable);
125129
});
130+
}
131+
132+
private static void LoadForeignKeys(this DbAccess dbAccess, DbmsRelationTree foreignKeyBaseTable)
133+
{
134+
const string sp = "GET_FOREIGN_KEY";
135+
DbParameter outTable_Name = null;
136+
137+
dbAccess.ExecuteReader(GetProcedure(sp), parameters =>
138+
{
139+
parameters.Add("inTable_Name", foreignKeyBaseTable.TableName);
140+
outTable_Name = parameters.AddOutput("outTable_Name", 128);
141+
}, reader =>
142+
{
143+
foreignKeyBaseTable.AddForeignKeyColumn(
144+
reader.Field<string>("CONSTRAINT_NAME"),
145+
reader.Field<string>("FOREIGN_KEY_COLUMN"),
146+
reader.Field<bool>("FOREIGN_KEY_NULLABLE"),
147+
reader.Field<string>("REFERENCED_TABLE"),
148+
reader.Field<string>("REFERENCED_COLUMN"),
149+
reader.Field<bool>("REFERENCED_NULLABLE"));
150+
});
151+
152+
foreignKeyBaseTable.TableName = outTable_Name.Parameter<string>();
153+
foreignKeyBaseTable.Columns = ListTableColumns(dbAccess, foreignKeyBaseTable.TableName).ToArray();
154+
155+
foreach (DbmsForeignKey fk in foreignKeyBaseTable.ForeignKeys)
156+
LoadForeignKeys(dbAccess, fk.PrimaryUniqueKeyBaseTable);
157+
}
158+
159+
internal static DbmsRelationTree LoadForeignKeys(this DbAccess dbAccess, string tableName)
160+
{
161+
DbmsRelationTree rootTable = new DbmsRelationTree(tableName);
162+
163+
LoadForeignKeys(dbAccess, rootTable);
126164

127-
return columns;
165+
return rootTable;
128166
}
129167

130168
internal static void LoadDefaultProperties(this DbAccess dbAccess, Action<DbDataReader> setDefaultProperty)

T4SQLTemplateLibrary/SqlBuilder/EngineMain.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static int ServiceModeMaxLen
3232
private List<Workspace> _WorkspaceTasks;
3333
private volatile bool _KeepPolling;
3434
private Task _MainTask;
35-
private ServerEnvironment _DbServerEnv;
35+
private DbmsEnvironment _DbServerEnv;
3636

3737
public EngineMain(EventLog serviceEventLog = null)
3838
{

T4SQLTemplateLibrary/SqlBuilder/SqlBuilder.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
<Project>{0BD1299C-6AE3-407C-844D-F69B90037846}</Project>
6363
<Name>DataAccess</Name>
6464
</ProjectReference>
65+
<ProjectReference Include="..\T4SQL.Base\T4SQL.Base.csproj">
66+
<Project>{A5DD7958-181E-4D43-BE49-BAE5005ADB6D}</Project>
67+
<Name>T4SQL.Base</Name>
68+
</ProjectReference>
6569
<ProjectReference Include="..\T4Templates\T4Templates.csproj">
6670
<Project>{55837841-3D2A-411D-960F-323C12668088}</Project>
6771
<Name>T4Templates</Name>

T4SQLTemplateLibrary/SqlBuilder/TemplateManager.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ protected int RegisterTemplate(Assembly addInAssembly)
5555

5656
public void LoadAddIns()
5757
{
58-
RegisterTemplate(_typeITemplate.Assembly);
58+
Type typeAddInsLocator = typeof(AddInsLocator);
59+
string builtInDll = typeAddInsLocator.Module.FullyQualifiedName;
60+
string addInDir = Path.GetDirectoryName(builtInDll);
5961

60-
string bootDll = _typeITemplate.Module.FullyQualifiedName;
61-
string addInDir = Path.GetDirectoryName(bootDll);
62+
RegisterTemplate(typeAddInsLocator.Assembly); // Register built-in Templates
6263

6364
if (addInDir.StartsWith(AppDomain.CurrentDomain.BaseDirectory))
6465
foreach (string dllFile in Directory.GetFiles(addInDir, "*.dll", SearchOption.AllDirectories))
65-
if (dllFile != bootDll)
66+
if (!dllFile.Equals(builtInDll, StringComparison.OrdinalIgnoreCase)) // Register add-in Templates
6667
{
6768
try
6869
{

T4SQLTemplateLibrary/SqlBuilder/Workspace.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private void SetWorkingProperty(string workitemName, string propertyName, string
5050
}
5151
}
5252

53-
public void BuildWorkitems(DbAccess dbAccess, TemplateManager templateManager, ServerEnvironment dbServerEnv)
53+
public void BuildWorkitems(DbAccess dbAccess, TemplateManager templateManager, DbmsEnvironment dbServerEnv)
5454
{
5555
TemplateContext defaultProperties;
5656
Type templateClass;

0 commit comments

Comments
 (0)