Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 7d50afa

Browse files
committed
Add IncludeSPReturnTypes flag into T4 template. if true will create {StoredProcedureName}_Result classes for output columns of first table returned.
1 parent d99a054 commit 7d50afa

File tree

2 files changed

+81
-20
lines changed

2 files changed

+81
-20
lines changed

src/T4/OrmLite.Core.ttinclude

Lines changed: 67 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
<#@ import namespace="System.Text" #>
1818
<#@ import namespace="System.Text.RegularExpressions" #>
1919
<#@ import namespace="System.Configuration" #>
20-
<#@ import namespace="System.Windows.Forms" #>
20+
<#@ import namespace="System.Windows.Forms" #>
2121
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
2222
<#+
2323

2424
/*
25-
This is code is based on the T4 template from the PetaPoco project which in turn is based on the subsonic project.
25+
This code is based on the T4 template from the PetaPoco project which in turn is based on the subsonic project.
2626

2727
-----------------------------------------------------------------------------------------
2828

@@ -88,6 +88,7 @@ string ClassSuffix = "";
8888
string SchemaName = null;
8989
bool IncludeViews = false;
9090
bool IncludeFunctions = false;
91+
bool IncludeSPReturnTypes = false; //Will create {StoredProcName}_Result classes for any SPs with output results
9192

9293
public class Table
9394
{
@@ -211,9 +212,11 @@ public class SP
211212
public string Schema;
212213
public string SchemaQualifiedName {get{return Schema+"."+Name;}}
213214
public List<SPParam> Parameters;
215+
public List<SPOutputColumn> SPOutputColumns;
214216
public SP()
215217
{
216218
Parameters=new List<SPParam>();
219+
SPOutputColumns = new List<SPOutputColumn>();
217220
}
218221
public string ArgList
219222
{
@@ -233,6 +236,12 @@ public class SP
233236
}
234237
}
235238

239+
public class SPOutputColumn
240+
{
241+
public string Name ;
242+
public string DotNetType ;
243+
}
244+
236245
public enum SPParamDir
237246
{
238247
OutDirection,
@@ -286,6 +295,8 @@ static string CheckNullable(Column col)
286295
return result;
287296
}
288297

298+
299+
289300
string GetConnectionString(ref string connectionStringName, out string providerName)
290301
{
291302
var _CurrentProject = GetCurrentProject();
@@ -523,7 +534,7 @@ Tables LoadTables(bool makeSingular)
523534
else
524535
{
525536
// Assume SQL Server
526-
reader=new SqlServerSchemaReader();
537+
reader=new SqlServerSchemaReader(IncludeSPReturnTypes);
527538
}
528539

529540
reader.outer=this;
@@ -593,19 +604,19 @@ List<SP> SPsNotSupported(string providerName)
593604
}
594605

595606
string GetParamDirection(SPParamDir direction)
596-
{
597-
switch(direction)
598-
{
599-
case SPParamDir.InAndOutDirection:
600-
return "ParameterDirection.InputOutput";
601-
case SPParamDir.OutDirection:
602-
return "ParameterDirection.Output";
603-
case SPParamDir.InDirection:
604-
default:
605-
return "ParameterDirection.Input";
606-
}
607-
}
608-
607+
{
608+
switch(direction)
609+
{
610+
case SPParamDir.InAndOutDirection:
611+
return "ParameterDirection.InputOutput";
612+
case SPParamDir.OutDirection:
613+
return "ParameterDirection.Output";
614+
case SPParamDir.InDirection:
615+
default:
616+
return "ParameterDirection.Input";
617+
}
618+
}
619+
609620

610621
List<SP> LoadSPs()
611622
{
@@ -684,7 +695,7 @@ List<SP> LoadSPs()
684695
else
685696
{
686697
// Assume SQL Server
687-
reader=new SqlServerSchemaReader();
698+
reader=new SqlServerSchemaReader(IncludeSPReturnTypes);
688699
}
689700

690701
reader.outer=this;
@@ -802,6 +813,11 @@ static int GetDatatypeSize(string type)
802813
// Edit here to get a method to read the proc
803814
class SqlServerSchemaReader : SchemaReader
804815
{
816+
private bool IncludeSPReturnTypes;
817+
public SqlServerSchemaReader(bool includeSPReturnTypes)
818+
{
819+
IncludeSPReturnTypes = includeSPReturnTypes;
820+
}
805821
// SchemaReader.ReadSchema
806822

807823

@@ -884,6 +900,7 @@ class SqlServerSchemaReader : SchemaReader
884900
foreach (var sp in result)
885901
{
886902
sp.Parameters=LoadSPParams(sp);
903+
if (IncludeSPReturnTypes) sp.SPOutputColumns = LoadSPOutputColumns(sp);
887904
}
888905
return result;
889906
}
@@ -969,6 +986,37 @@ class SqlServerSchemaReader : SchemaReader
969986
}
970987
}
971988

989+
List<SPOutputColumn> LoadSPOutputColumns(SP sp)
990+
{
991+
var result=new List<SPOutputColumn>();
992+
using (var cmd=_factory.CreateCommand())
993+
{
994+
cmd.Connection=_connection;
995+
cmd.CommandText=string.Format(@"SELECT name,CAST(is_nullable as VARCHAR(1)) is_nullable,system_type_name FROM sys.dm_exec_describe_first_result_set_for_object(OBJECT_ID('{0}'), 1)",sp.SchemaQualifiedName);
996+
using (IDataReader rdr=cmd.ExecuteReader())
997+
{
998+
while(rdr.Read())
999+
{
1000+
if (!rdr.IsDBNull(0))
1001+
{
1002+
SPOutputColumn param=new SPOutputColumn();
1003+
param.Name = rdr["name"].ToString();
1004+
if (rdr["is_nullable"] == "0")
1005+
{
1006+
param.DotNetType = GetPropertyType(rdr["system_type_name"].ToString());
1007+
}
1008+
else
1009+
{
1010+
param.DotNetType = GetNullablePropertyType(rdr["system_type_name"].ToString());
1011+
}
1012+
result.Add(param);
1013+
}
1014+
}
1015+
}
1016+
}
1017+
return result;
1018+
}
1019+
9721020

9731021
string GetPK(string table){
9741022

@@ -2647,6 +2695,8 @@ class Manager {
26472695
if (sc != null && sc.IsItemUnderSCC(fileName) && !sc.IsItemCheckedOut(fileName))
26482696
checkOutAction.EndInvoke(checkOutAction.BeginInvoke(fileName, null, null));
26492697
}
2698+
2699+
26502700
}
26512701
}
26522702

src/T4/OrmLite.SP.tt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
var SPNamespace = "StoredProcedures";
66
ClassPrefix = "";
77
ClassSuffix = "";
8-
8+
99
// Read schema
1010
var sps = LoadSPs();
1111

@@ -51,6 +51,17 @@ namespace <#=SPNamespace#>
5151

5252
<#}#>
5353
}
54-
}
55-
<# } #>
5654

55+
<#if (IncludeSPReturnTypes) foreach(var sp in sps)
56+
{
57+
if (!sp.SPOutputColumns.Any()) continue; #>
58+
public class <#=sp.CleanName#>_Result
59+
{
60+
<#foreach(var prop in sp.SPOutputColumns){#>
61+
public <#=prop.DotNetType#> <#=prop.Name#> { get; set; }
62+
<#}#>
63+
}
64+
<#}#>
65+
66+
}
67+
<#}#>

0 commit comments

Comments
 (0)