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

Commit d9bc1a5

Browse files
committed
Merge pull request #445 from mikepugh/patch-1
(MSSQL) Add support for Table Valued Functions
2 parents 6ca6dad + e0d828f commit d9bc1a5

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

src/T4/OrmLite.Core.ttinclude

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<#@ template language="C#" hostspecific="True" #>
23
<#@ assembly name="EnvDTE" #>
34
<#@ assembly name="System.Core.dll" #>
@@ -86,6 +87,7 @@ string ClassPrefix = "";
8687
string ClassSuffix = "";
8788
string SchemaName = null;
8889
bool IncludeViews = false;
90+
bool IncludeFunctions = false;
8991

9092
public class Table
9193
{
@@ -95,6 +97,7 @@ public class Table
9597
public string Name;
9698
public string Schema;
9799
public bool IsView;
100+
public bool IsFunction;
98101
public string CleanName;
99102
public string ClassName;
100103
public string SequenceName;
@@ -534,11 +537,11 @@ Tables LoadTables(bool makeSingular)
534537
result.RemoveAt(i);
535538
continue;
536539
}
537-
if (!IncludeViews && result[i].IsView)
540+
if ((!IncludeViews && result[i].IsView) ||(!IncludeFunctions && result[i].IsFunction))
538541
{
539542
result.RemoveAt(i);
540543
continue;
541-
}
544+
}
542545
}
543546
}
544547

@@ -781,9 +784,12 @@ static int GetDatatypeSize(string type)
781784
return -1;
782785
}
783786

787+
// Edit here to get a method to read the proc
784788
class SqlServerSchemaReader : SchemaReader
785789
{
786790
// SchemaReader.ReadSchema
791+
792+
787793
public override Tables ReadSchema(DbConnection connection, DbProviderFactory factory)
788794
{
789795
var result=new Tables();
@@ -807,6 +813,7 @@ class SqlServerSchemaReader : SchemaReader
807813
tbl.Name=rdr["TABLE_NAME"].ToString();
808814
tbl.Schema=rdr["TABLE_SCHEMA"].ToString();
809815
tbl.IsView=string.Compare(rdr["TABLE_TYPE"].ToString(), "View", true)==0;
816+
tbl.IsFunction=string.Compare(rdr["TABLE_TYPE"].ToString(), "TVF", true)==0;
810817
tbl.CleanName=CleanUp(tbl.Name);
811818
tbl.ClassName=Inflector.MakeSingular(tbl.CleanName);
812819

@@ -1156,25 +1163,53 @@ class SqlServerSchemaReader : SchemaReader
11561163
}
11571164

11581165

1159-
const string TABLE_SQL=@"SELECT *
1160-
FROM INFORMATION_SCHEMA.TABLES
1161-
WHERE TABLE_TYPE='BASE TABLE' OR TABLE_TYPE='VIEW'";
1162-
1163-
const string COLUMN_SQL=@"SELECT
1164-
TABLE_CATALOG AS [Database],
1165-
TABLE_SCHEMA AS Owner,
1166-
TABLE_NAME AS TableName,
1167-
COLUMN_NAME AS ColumnName,
1168-
ORDINAL_POSITION AS OrdinalPosition,
1169-
COLUMN_DEFAULT AS DefaultSetting,
1170-
IS_NULLABLE AS IsNullable, DATA_TYPE AS DataType,
1171-
CHARACTER_MAXIMUM_LENGTH AS MaxLength,
1172-
DATETIME_PRECISION AS DatePrecision,
1173-
COLUMNPROPERTY(object_id('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), COLUMN_NAME, 'IsIdentity') AS IsIdentity,
1174-
COLUMNPROPERTY(object_id('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), COLUMN_NAME, 'IsComputed') as IsComputed
1175-
FROM INFORMATION_SCHEMA.COLUMNS
1176-
WHERE TABLE_NAME=@tableName AND TABLE_SCHEMA=@schemaName
1177-
ORDER BY OrdinalPosition ASC";
1166+
const string TABLE_SQL=@"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' OR TABLE_TYPE='VIEW'
1167+
UNION
1168+
SELECT SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME, 'TVF' FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION' AND DATA_TYPE = 'TABLE'";
1169+
1170+
const string COLUMN_SQL=@"SELECT T.[Database] ,
1171+
T.Owner ,
1172+
T.TableName ,
1173+
T.ColumnName ,
1174+
T.OrdinalPosition ,
1175+
T.DefaultSetting ,
1176+
T.IsNullable ,
1177+
T.DataType ,
1178+
T.MaxLength ,
1179+
T.DatePrecision ,
1180+
T.IsIdentity ,
1181+
T.IsComputed FROM (
1182+
SELECT
1183+
TABLE_CATALOG AS [Database],
1184+
TABLE_SCHEMA AS Owner,
1185+
TABLE_NAME AS TableName,
1186+
COLUMN_NAME AS ColumnName,
1187+
ORDINAL_POSITION AS OrdinalPosition,
1188+
COLUMN_DEFAULT AS DefaultSetting,
1189+
IS_NULLABLE AS IsNullable, DATA_TYPE AS DataType,
1190+
CHARACTER_MAXIMUM_LENGTH AS MaxLength,
1191+
DATETIME_PRECISION AS DatePrecision,
1192+
COLUMNPROPERTY(object_id('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), COLUMN_NAME, 'IsIdentity') AS IsIdentity,
1193+
COLUMNPROPERTY(object_id('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), COLUMN_NAME, 'IsComputed') as IsComputed
1194+
FROM INFORMATION_SCHEMA.COLUMNS
1195+
WHERE TABLE_NAME=@tableName AND TABLE_SCHEMA=@schemaName
1196+
--ORDER BY OrdinalPosition ASC
1197+
UNION
1198+
SELECT TABLE_CATALOG AS [Database],
1199+
TABLE_SCHEMA AS Owner,
1200+
TABLE_NAME AS TableName,
1201+
COLUMN_NAME AS ColumnName,
1202+
ORDINAL_POSITION AS OrdinalPosition,
1203+
COLUMN_DEFAULT AS DefaultSetting,
1204+
IS_NULLABLE AS IsNullable, DATA_TYPE AS DataType,
1205+
CHARACTER_MAXIMUM_LENGTH AS MaxLength,
1206+
DATETIME_PRECISION AS DatePrecision,
1207+
COLUMNPROPERTY(object_id('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), COLUMN_NAME, 'IsIdentity') AS IsIdentity,
1208+
COLUMNPROPERTY(object_id('[' + TABLE_SCHEMA + '].[' + TABLE_NAME + ']'), COLUMN_NAME, 'IsComputed') as IsComputed
1209+
FROM INFORMATION_SCHEMA.ROUTINE_COLUMNS
1210+
WHERE TABLE_NAME=@tableName AND TABLE_SCHEMA=@schemaName
1211+
) T
1212+
ORDER BY T.OrdinalPosition ASC";
11781213

11791214
const string SP_NAMES_SQL=@"SELECT o.name AS sp_name, s.name AS schema_name
11801215
FROM sys.objects o
@@ -2603,4 +2638,4 @@ class Manager {
26032638
/*
26042639
End of Manager.tt
26052640
*/
2606-
#>
2641+
#>

0 commit comments

Comments
 (0)