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

Commit 1aa6f6d

Browse files
committed
Merge pull request #182 from WimAtIHomer/patch-1
Added 3 new options to OrmLite.Poco.tt
2 parents 3100866 + d05650d commit 1aa6f6d

File tree

2 files changed

+104
-38
lines changed

2 files changed

+104
-38
lines changed

src/T4/OrmLite.Core.ttinclude

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ public class Column
150150
{
151151
if(IsNullable)
152152
{
153-
if(PropertyType.Equals("string") == false)
154-
{
155-
return PropertyType +"?";
156-
}
153+
return PropertyType + CheckNullable(this);
157154
}
158155
return PropertyType;
159156
}
@@ -270,7 +267,7 @@ static Func<string, string> CleanUp = (str) =>
270267
return str;
271268
};
272269

273-
string CheckNullable(Column col)
270+
static string CheckNullable(Column col)
274271
{
275272
string result="";
276273
if(col.IsNullable &&
@@ -448,7 +445,7 @@ public string GetColumnDefaultValue(Column col)
448445
return sysType;
449446
}
450447

451-
Tables LoadTables()
448+
Tables LoadTables(bool makeSingular)
452449
{
453450
InitConnectionString();
454451

@@ -547,7 +544,12 @@ Tables LoadTables()
547544
var rxClean = new Regex("^(Equals|GetHashCode|GetType|ToString|repo|Save|IsNew|Insert|Update|Delete|Exists|SingleOrDefault|Single|First|FirstOrDefault|Fetch|Page|Query)$");
548545
foreach (var t in result)
549546
{
547+
if (!makeSingular)
548+
{
549+
t.ClassName = t.CleanName;
550+
}
550551
t.ClassName = ClassPrefix + t.ClassName + ClassSuffix;
552+
551553
foreach (var c in t.Columns)
552554
{
553555
c.PropertyName = rxClean.Replace(c.PropertyName, "_$1");
@@ -956,13 +958,26 @@ class SqlServerSchemaReader : SchemaReader
956958
p.Value=table;
957959
cmd.Parameters.Add(p);
958960

959-
var result=cmd.ExecuteScalar();
960-
961-
if(result!=null)
962-
return result.ToString();
961+
var result = "";
962+
DbDataReader reader = cmd.ExecuteReader();
963+
try
964+
{
965+
if (reader.Read())
966+
{
967+
result = reader[0].ToString();
968+
if (reader.Read())
969+
{
970+
result = "";
971+
}
972+
}
973+
}
974+
finally
975+
{
976+
// Always call Close when done reading.
977+
reader.Close();
978+
}
979+
return result;
963980
}
964-
965-
return "";
966981
}
967982

968983
string GetPropertyType(string sqlType)
@@ -1274,13 +1289,26 @@ class SqlServerCeSchemaReader : SchemaReader
12741289
p.Value=table;
12751290
cmd.Parameters.Add(p);
12761291

1277-
var result=cmd.ExecuteScalar();
1278-
1279-
if(result!=null)
1280-
return result.ToString();
1292+
var result = "";
1293+
DbDataReader reader = cmd.ExecuteReader();
1294+
try
1295+
{
1296+
if (reader.Read())
1297+
{
1298+
result = reader[0].ToString();
1299+
if (reader.Read())
1300+
{
1301+
result = "";
1302+
}
1303+
}
1304+
}
1305+
finally
1306+
{
1307+
// Always call Close when done reading.
1308+
reader.Close();
1309+
}
1310+
return result;
12811311
}
1282-
1283-
return "";
12841312
}
12851313

12861314
string GetPropertyType(string sqlType)
@@ -1467,13 +1495,26 @@ class PostGreSqlSchemaReader : SchemaReader
14671495
p.Value=table;
14681496
cmd.Parameters.Add(p);
14691497

1470-
var result=cmd.ExecuteScalar();
1471-
1472-
if(result!=null)
1473-
return result.ToString();
1498+
var result = "";
1499+
DbDataReader reader = cmd.ExecuteReader();
1500+
try
1501+
{
1502+
if (reader.Read())
1503+
{
1504+
result = reader[0].ToString();
1505+
if (reader.Read())
1506+
{
1507+
result = "";
1508+
}
1509+
}
1510+
}
1511+
finally
1512+
{
1513+
// Always call Close when done reading.
1514+
reader.Close();
1515+
}
1516+
return result;
14741517
}
1475-
1476-
return "";
14771518
}
14781519

14791520
string GetPropertyType(string sqlType)
@@ -1781,13 +1822,26 @@ and ucc.position = 1";
17811822
p.Value=table;
17821823
cmd.Parameters.Add(p);
17831824

1784-
var result=cmd.ExecuteScalar();
1785-
1786-
if(result!=null)
1787-
return result.ToString();
1825+
var result = "";
1826+
DbDataReader reader = cmd.ExecuteReader();
1827+
try
1828+
{
1829+
if (reader.Read())
1830+
{
1831+
result = reader[0].ToString();
1832+
if (reader.Read())
1833+
{
1834+
result = "";
1835+
}
1836+
}
1837+
}
1838+
finally
1839+
{
1840+
// Always call Close when done reading.
1841+
reader.Close();
1842+
}
1843+
return result;
17881844
}
1789-
1790-
return "";
17911845
}
17921846

17931847
string GetPropertyType(string sqlType, string dataScale)

src/T4/OrmLite.Poco.tt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
Namespace = "";
66
ClassPrefix = "";
77
ClassSuffix = "";
8-
bool SplitIntoMultipleFiles = false;
8+
bool SplitIntoMultipleFiles = false; // if true: Generates one file for every class
9+
bool MakeSingular = true; // if true: Changes the classname to singular if tablename is not singular
10+
bool UseIdAsPK = true; // if true: Changes the primary key property name to Id
11+
bool GenerateConstructor = false; // if true: Generates the default empty constructor
912

1013
// Read schema
11-
var tables = LoadTables();
14+
var tables = LoadTables(MakeSingular);
1215

1316

1417
/*
@@ -53,14 +56,20 @@ foreach(Table tbl in from t in tables where !t.Ignore select t)
5356
{
5457
manager.StartNewFile(tbl.Name + ".cs");
5558
#>
56-
59+
<# if (MakeSingular) {#>
5760
[Alias("<#=tbl.Name#>")]
58-
public partial class <#=tbl.ClassName#><#if (tbl.HasPK()) { #> : IHasId<<#=tbl.PK.PropertyType#>><#}#>
61+
<#}#>
62+
public partial class <#=tbl.ClassName#><#if (tbl.HasPK() && UseIdAsPK) { #> : IHasId<<#=tbl.PK.PropertyType#>><#}#>
5963
{
60-
<#
64+
<# if (GenerateConstructor) { #>
65+
public <#=tbl.ClassName#>()
66+
{
67+
}
68+
69+
<# }
6170
foreach(Column col in from c in tbl.Columns where !c.Ignore select c)
6271
{
63-
if ((col.Name!=col.PropertyName) || (col.IsPK)) { #>
72+
if ((col.Name!=col.PropertyName) || (col.IsPK && UseIdAsPK)) { #>
6473
[Alias("<#=col.Name#>")]
6574
<# } if (col.PropertyType == "string" && col.Size > 0) { #>
6675
[StringLength(<#=col.Size#>)]
@@ -70,9 +79,12 @@ foreach(Column col in from c in tbl.Columns where !c.Ignore select c)
7079
[Required]
7180
<# } if (!col.IsPK){#>
7281
public <#=col.ProperPropertyType#> <#=col.PropertyName#> { get; set;}
73-
<#} else {#>
82+
<# } if (col.IsPK && UseIdAsPK) { #>
7483
public <#=col.ProperPropertyType#> Id { get; set;}
75-
<#}#>
84+
<# } if (col.IsPK && !UseIdAsPK) { #>
85+
[PrimaryKey]
86+
public <#=col.ProperPropertyType#> <#=col.PropertyName#> { get; set;}
87+
<# } #>
7688
<# } #>
7789
}
7890
<# manager.EndBlock(); #>

0 commit comments

Comments
 (0)