Skip to content

Commit ccb7d89

Browse files
committed
VNaviForeignKey - Traverses all referenced tables from the root ForeignKeyBaseTable.
1 parent 263cdd1 commit ccb7d89

File tree

14 files changed

+640
-27
lines changed

14 files changed

+640
-27
lines changed

T4SQLTemplateLibrary/DataAccess/DbExtensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ public static T Field<T>(this DbDataReader reader, int ordinal)
5454

5555
public static T Parameter<T>(this DbCommand cmd, string parameterName)
5656
{
57-
return TryConvert<T>(cmd.Parameters[parameterName].Value);
57+
return cmd.Parameters.Parameter<T>(parameterName);
58+
}
59+
60+
public static T Parameter<T>(this DbParameterCollection parameters, string parameterName)
61+
{
62+
return parameters[parameterName].Parameter<T>();
5863
}
5964

6065
public static T Parameter<T>(this DbParameter parameter)

T4SQLTemplateLibrary/DataAccess/DbParameterBuilder.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ namespace DbParallel.DataAccess
77
public partial class DbParameterBuilder
88
{
99
private readonly DbCommand _DbCommand;
10-
11-
public DbParameterCollection Parameters
12-
{
13-
get { return _DbCommand.Parameters; }
14-
}
10+
public DbCommand Command { get { return _DbCommand; } }
1511

1612
public DbParameterBuilder(DbCommand dbCommand)
1713
{

T4SQLTemplateLibrary/DataAccess/Properties/AssemblyInfo.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
// General Information about an assembly is controlled through the following
66
// set of attributes. Change these attribute values to modify the information
77
// associated with an assembly.
8-
[assembly: AssemblyTitle("DbParallel.DataAccess")]
9-
[assembly: AssemblyDescription("dbParallel Database Access Extensions")]
8+
[assembly: AssemblyTitle("DataBooster")]
9+
[assembly: AssemblyDescription("DataBooster is a high-performance extension to ADO.NET Data Provider, includes a light encapsulation to minimize your DAL code and a booster to maximize throughput for writing mass data onto database.")]
1010
[assembly: AssemblyConfiguration("")]
11-
[assembly: AssemblyCompany("http://dbparallel.codeplex.com/")]
11+
[assembly: AssemblyCompany("Abel Cheng")]
1212
[assembly: AssemblyProduct("dbParallel Database Task Parallel Foundation")]
13-
[assembly: AssemblyCopyright("Abel Cheng. All rights reserved.")]
13+
[assembly: AssemblyCopyright("Copyright © 2012 Abel Cheng")]
1414
[assembly: AssemblyTrademark("")]
1515
[assembly: AssemblyCulture("")]
1616

@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.2")]
36-
[assembly: AssemblyFileVersion("1.0.0.2")]
35+
[assembly: AssemblyVersion("1.0.0.4")]
36+
[assembly: AssemblyFileVersion("1.0.0.4")]

T4SQLTemplateLibrary/T4SQL.Base/MetaData/DbmsRelationTree.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ internal TableLink LinkProperty
7878
get { return _LinkProperty; }
7979
set { _LinkProperty = value; }
8080
}
81+
82+
internal IEnumerable<DbmsColumn> DisplayColumns
83+
{
84+
get
85+
{
86+
if (_ParentForeignKey == null)
87+
return _Columns;
88+
else
89+
{
90+
var pkCols = _ParentForeignKey.PrimaryUniqueKeyColumns.Select(c => c.ColumnName);
91+
92+
return _Columns.Where(c => !pkCols.Contains(c.ColumnName, StringComparer.OrdinalIgnoreCase));
93+
}
94+
}
95+
}
8196
}
8297
}
8398

T4SQLTemplateLibrary/T4SQL.Base/MetaData/JoinedTable.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public string ColumnExpression
6868
return string.Format("{0}.{1} AS {2}", TableAlias, ColumnName, Alias);
6969
}
7070
}
71+
72+
public override string ToString()
73+
{
74+
return ColumnExpression;
75+
}
7176
}
7277

7378
private DbmsRelationTree _RootTable;
@@ -89,7 +94,7 @@ private void TraverseRelationTree(DbmsRelationTree nodeTable, string parentAlias
8994
{
9095
nodeTable.LinkProperty = new TableLink(this, parentAlias, position);
9196

92-
foreach (DbmsColumn col in nodeTable.Columns)
97+
foreach (DbmsColumn col in nodeTable.DisplayColumns)
9398
_ColumnAliases.Add(new ColumnAlias(nodeTable, col));
9499

95100
for (byte pos = 0; pos < nodeTable.ForeignKeys.Count; pos++)
@@ -98,24 +103,32 @@ private void TraverseRelationTree(DbmsRelationTree nodeTable, string parentAlias
98103

99104
public string BuildJoinClause()
100105
{
101-
return BuildJoinClause(_RootTable);
106+
int cntJoin;
107+
return BuildJoinClause(_RootTable, out cntJoin);
102108
}
103109

104-
private string BuildJoinClause(DbmsRelationTree startTable)
110+
private string BuildJoinClause(DbmsRelationTree startTable, out int cntJoin)
105111
{
106112
StringBuilder joinClause = new StringBuilder();
107113

108114
joinClause.AppendFormat(startTable.TableName);
109115
joinClause.Append(" ");
110116
joinClause.AppendLine(startTable.LinkProperty.Alias);
117+
cntJoin = 1;
111118

112119
foreach (DbmsForeignKey fk in startTable.ForeignKeys) // INNER JOIN first
113120
if (!fk.IsNullable) // ForeignKeyColumns is not null
121+
{
114122
AppendJoinSource(joinClause, fk);
123+
cntJoin++;
124+
}
115125

116126
foreach (DbmsForeignKey fk in startTable.ForeignKeys) // Then LEFT JOIN
117127
if (fk.IsNullable) // ForeignKeyColumns is nullable
128+
{
118129
AppendJoinSource(joinClause, fk);
130+
cntJoin++;
131+
}
119132

120133
return joinClause.ToString();
121134
}
@@ -126,16 +139,25 @@ private void AppendJoinSource(StringBuilder joinClause, DbmsForeignKey foreignKe
126139
string pkTableAlias = foreignKey.PrimaryUniqueKeyBaseTable.LinkProperty.Alias;
127140
List<DbmsColumn> fkColumns = foreignKey.ForeignKeyColumns;
128141
List<DbmsColumn> pkColumns = foreignKey.PrimaryUniqueKeyColumns;
129-
int nCols = foreignKey.ForeignKeyColumns.Count;
142+
int cntJoin, nCols = foreignKey.ForeignKeyColumns.Count;
143+
string insideJoin = BuildJoinClause(foreignKey.PrimaryUniqueKeyBaseTable, out cntJoin);
130144

131145
if (foreignKey.IsNullable)
132-
joinClause.AppendLine("LEFT JOIN (");
146+
joinClause.Append("LEFT JOIN");
133147
else
134-
joinClause.AppendLine("INNER JOIN (");
148+
joinClause.Append("INNER JOIN");
149+
150+
if (cntJoin > 1)
151+
joinClause.AppendLine(" (");
152+
else
153+
joinClause.AppendLine();
154+
155+
joinClause.Append(insideJoin.PushIndent());
135156

136-
joinClause.Append(BuildJoinClause(foreignKey.PrimaryUniqueKeyBaseTable).PushIndent());
157+
if (cntJoin > 1)
158+
joinClause.Append(") ");
137159

138-
joinClause.Append(") ON (");
160+
joinClause.Append("ON (");
139161

140162
for (int i = 0; i < nCols; i++)
141163
{
@@ -145,7 +167,7 @@ private void AppendJoinSource(StringBuilder joinClause, DbmsForeignKey foreignKe
145167
joinClause.AppendFormat("{0}.{1} = {2}.{3}", fkTableAlias, fkColumns[i].ColumnName, pkTableAlias, pkColumns[i].ColumnName);
146168
}
147169

148-
joinClause.Append(")");
170+
joinClause.AppendLine(")");
149171
}
150172

151173
public bool SolveNameRepetition(string renameFormat) // renameFormat: "{0}${1}" - {0}: Table; {1}: ColumnName

T4SQLTemplateLibrary/T4SQL.Base/NuGet/Readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ In case the automatic installation fail due to insufficient permissions or other
88
you may need to manually install it by following instructions:
99

1010
Copy the file T4SQLTemplate.CSharp.zip
11-
from the project NuGet package directory (packages\T4SQL.Base.x.x.x.x\Visual Studio\Templates\ItemTemplates\)
11+
from the project NuGet package directory (packages\T4SQL.Base.x.x.x.x\Visual Studio\Templates\ItemTemplates\Visual C#\)
1212
to your Visual Studio Item Templates Directory
1313
(Find Visual Studio [Tools] menu, [Options] dialog box, under [Projects and Solutions]\[General], in [user item templates location])
1414

T4SQLTemplateLibrary/T4SQL.Base/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// set of attributes. Change these attribute values to modify the information
77
// associated with an assembly.
88
[assembly: AssemblyTitle("T4SQL.Base")]
9-
[assembly: AssemblyDescription("T4SQL Template Library Base")]
9+
[assembly: AssemblyDescription("Base Library for Developing T4SQL Templates Plug-ins")]
1010
[assembly: AssemblyConfiguration("")]
1111
[assembly: AssemblyCompany("Abel Cheng")]
1212
[assembly: AssemblyProduct("T4SQL Template Library")]

T4SQLTemplateLibrary/T4SQLTemplateLibrary.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Global
162162
{A5DD7958-181E-4D43-BE49-BAE5005ADB6D} = {FF43D7CC-1C03-4685-936F-69853A5315F4}
163163
{55837841-3D2A-411D-960F-323C12668088} = {FF43D7CC-1C03-4685-936F-69853A5315F4}
164164
{304335BC-9CD9-4D9C-B87B-CE9FBBACAFFA} = {FF43D7CC-1C03-4685-936F-69853A5315F4}
165+
{AD2C96A3-8A0E-4D50-A703-CDDC43D11284} = {FF43D7CC-1C03-4685-936F-69853A5315F4}
165166
{15675A2B-48D5-484F-B908-82A8E744A6C0} = {AD2C96A3-8A0E-4D50-A703-CDDC43D11284}
166167
EndGlobalSection
167168
EndGlobal
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE OR REPLACE VIEW <#= ObjectView #> AS
2+
SELECT
3+
-- This code was generated by <#= TemplateName #> @ <#= DateTime.Now.ToString() #>
4+
<#= ColumnListClause #>
5+
FROM
6+
<#= JoinClause #>
7+
WITH READ ONLY;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
IF OBJECT_ID(N'<#= ObjectView #>', N'V') IS NULL
2+
EXECUTE ('CREATE VIEW <#= ObjectView #> AS SELECT NULL AS CREATE_OR_REPLACE');
3+
GO
4+
5+
ALTER VIEW <#= ObjectView #> AS
6+
-- This code was generated by <#= TemplateName #> @ <#= DateTime.Now.ToString() #>
7+
SELECT
8+
<#= ColumnListClause #>
9+
FROM
10+
<#= JoinClause #>
11+
;
12+
GO

0 commit comments

Comments
 (0)