PetaPoco.Generator.ttinclude 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <#
  2. if (string.IsNullOrEmpty(Namespace)) Namespace=ConnectionStringName;
  3. if (string.IsNullOrEmpty(RepoName) && !string.IsNullOrEmpty(ConnectionStringName)) RepoName=ConnectionStringName + "DB";
  4. if (string.IsNullOrEmpty(Namespace)) Namespace="PetaPoco";
  5. if (string.IsNullOrEmpty(RepoName)) RepoName="PetaPocoDB";
  6. #>
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Web;
  11. using PetaPoco;
  12. namespace <#=Namespace #>
  13. {
  14. <# if (GenerateCommon) { #>
  15. public partial class <#=RepoName#> : Database
  16. {
  17. public <#=RepoName#>()
  18. : base("<#=ConnectionStringName#>")
  19. {
  20. CommonConstruct();
  21. }
  22. public <#=RepoName#>(string connectionStringName)
  23. : base(connectionStringName)
  24. {
  25. CommonConstruct();
  26. }
  27. partial void CommonConstruct();
  28. public interface IFactory
  29. {
  30. <#=RepoName#> GetInstance();
  31. }
  32. public static IFactory Factory { get; set; }
  33. public static <#=RepoName#> GetInstance()
  34. {
  35. if (_instance!=null)
  36. return _instance;
  37. if (Factory!=null)
  38. return Factory.GetInstance();
  39. else
  40. return new <#=RepoName#>();
  41. }
  42. [ThreadStatic] static <#=RepoName#> _instance;
  43. public override void OnBeginTransaction()
  44. {
  45. if (_instance==null)
  46. _instance=this;
  47. }
  48. public override void OnEndTransaction()
  49. {
  50. if (_instance==this)
  51. _instance=null;
  52. }
  53. <# if (GenerateOperations) { #>
  54. public class Record<T> where T:new()
  55. {
  56. public static <#=RepoName#> repo { get { return <#=RepoName#>.GetInstance(); } }
  57. public bool IsNew() { return repo.IsNew(this); }
  58. public object Insert() { return repo.Insert(this); }
  59. <# if (!TrackModifiedColumns) { #>
  60. public void Save() { repo.Save(this); }
  61. public int Update() { return repo.Update(this); }
  62. <# } #>
  63. public int Update(IEnumerable<string> columns) { return repo.Update(this, columns); }
  64. public static int Update(string sql, params object[] args) { return repo.Update<T>(sql, args); }
  65. public static int Update(Sql sql) { return repo.Update<T>(sql); }
  66. public int Delete() { return repo.Delete(this); }
  67. public static int Delete(string sql, params object[] args) { return repo.Delete<T>(sql, args); }
  68. public static int Delete(Sql sql) { return repo.Delete<T>(sql); }
  69. public static int Delete(object primaryKey) { return repo.Delete<T>(primaryKey); }
  70. public static bool Exists(object primaryKey) { return repo.Exists<T>(primaryKey); }
  71. public static bool Exists(string sql, params object[] args) { return repo.Exists<T>(sql, args); }
  72. public static T SingleOrDefault(object primaryKey) { return repo.SingleOrDefault<T>(primaryKey); }
  73. public static T SingleOrDefault(string sql, params object[] args) { return repo.SingleOrDefault<T>(sql, args); }
  74. public static T SingleOrDefault(Sql sql) { return repo.SingleOrDefault<T>(sql); }
  75. public static T FirstOrDefault(string sql, params object[] args) { return repo.FirstOrDefault<T>(sql, args); }
  76. public static T FirstOrDefault(Sql sql) { return repo.FirstOrDefault<T>(sql); }
  77. public static T Single(object primaryKey) { return repo.Single<T>(primaryKey); }
  78. public static T Single(string sql, params object[] args) { return repo.Single<T>(sql, args); }
  79. public static T Single(Sql sql) { return repo.Single<T>(sql); }
  80. public static T First(string sql, params object[] args) { return repo.First<T>(sql, args); }
  81. public static T First(Sql sql) { return repo.First<T>(sql); }
  82. public static List<T> Fetch(string sql, params object[] args) { return repo.Fetch<T>(sql, args); }
  83. public static List<T> Fetch(Sql sql) { return repo.Fetch<T>(sql); }
  84. public static List<T> Fetch(long page, long itemsPerPage, string sql, params object[] args) { return repo.Fetch<T>(page, itemsPerPage, sql, args); }
  85. public static List<T> Fetch(long page, long itemsPerPage, Sql sql) { return repo.Fetch<T>(page, itemsPerPage, sql); }
  86. public static List<T> SkipTake(long skip, long take, string sql, params object[] args) { return repo.SkipTake<T>(skip, take, sql, args); }
  87. public static List<T> SkipTake(long skip, long take, Sql sql) { return repo.SkipTake<T>(skip, take, sql); }
  88. public static Page<T> Page(long page, long itemsPerPage, string sql, params object[] args) { return repo.Page<T>(page, itemsPerPage, sql, args); }
  89. public static Page<T> Page(long page, long itemsPerPage, Sql sql) { return repo.Page<T>(page, itemsPerPage, sql); }
  90. public static IEnumerable<T> Query(string sql, params object[] args) { return repo.Query<T>(sql, args); }
  91. public static IEnumerable<T> Query(Sql sql) { return repo.Query<T>(sql); }
  92. <# if (TrackModifiedColumns) { #>
  93. private Dictionary<string,bool> ModifiedColumns;
  94. private void OnLoaded()
  95. {
  96. ModifiedColumns = new Dictionary<string,bool>();
  97. }
  98. protected void MarkColumnModified(string column_name)
  99. {
  100. if (ModifiedColumns!=null)
  101. ModifiedColumns[column_name]=true;
  102. }
  103. public int Update()
  104. {
  105. if (ModifiedColumns==null)
  106. return repo.Update(this);
  107. int retv = repo.Update(this, ModifiedColumns.Keys);
  108. ModifiedColumns.Clear();
  109. return retv;
  110. }
  111. public void Save()
  112. {
  113. if (repo.IsNew(this))
  114. repo.Insert(this);
  115. else
  116. Update();
  117. }
  118. <# } #>
  119. }
  120. <# } #>
  121. }
  122. <# } #>
  123. <# if (GeneratePocos) { #>
  124. <#
  125. foreach(Table tbl in from t in tables where !t.Ignore select t)
  126. {
  127. #>
  128. <# if (string.IsNullOrEmpty(tbl.Schema)) { #>
  129. [TableName("<#=tbl.Name#>")]
  130. <# } else { #>
  131. [TableName("<#=tbl.Schema + "." + tbl.Name#>")]
  132. <# } #>
  133. <# if (tbl.PK!=null && tbl.PK.IsAutoIncrement) { #>
  134. <# if (tbl.SequenceName==null) { #>
  135. [PrimaryKey("<#=tbl.PK.Name#>")]
  136. <# } else { #>
  137. [PrimaryKey("<#=tbl.PK.Name#>", sequenceName="<#=tbl.SequenceName#>")]
  138. <# } #>
  139. <# } #>
  140. <# if (tbl.PK!=null && !tbl.PK.IsAutoIncrement) { #>
  141. [PrimaryKey("<#=tbl.PK.Name#>", AutoIncrement=false)]
  142. <# } #>
  143. <# if (ExplicitColumns) { #>
  144. [ExplicitColumns]
  145. <# } #>
  146. public partial class <#=tbl.ClassName#> <# if (GenerateOperations) { #>: <#=RepoName#>.Record<<#=tbl.ClassName#>> <# } #>
  147. {
  148. <#
  149. foreach(Column col in from c in tbl.Columns where !c.Ignore select c)
  150. {
  151. // Column bindings
  152. #>
  153. <# if (TrackModifiedColumns) { #>
  154. <# if (col.Name!=col.PropertyName) { #>
  155. [Column("<#=col.Name#>")]
  156. <# } else { #>
  157. <# if (ExplicitColumns) { #>
  158. [Column]
  159. <# } #>
  160. <# } #>
  161. public <#=col.PropertyType #><#=CheckNullable(col)#> <#=col.PropertyName #>
  162. {
  163. get
  164. {
  165. return _<#=col.PropertyName #>;
  166. }
  167. set
  168. {
  169. _<#=col.PropertyName #> = value;
  170. MarkColumnModified("<#=col.Name#>");
  171. }
  172. }
  173. <#=col.PropertyType #><#=CheckNullable(col)#> _<#=col.PropertyName #>;
  174. <# } else { #>
  175. <# if (col.Name!=col.PropertyName) { #>
  176. [Column("<#=col.Name#>")] public <#=col.PropertyType #><#=CheckNullable(col)#> <#=col.PropertyName #> { get; set; }
  177. <# } else { #>
  178. <# if (ExplicitColumns) { #>[Column] <# } #>public <#=col.PropertyType #><#=CheckNullable(col)#> <#=col.PropertyName #> { get; set; }
  179. <# } #>
  180. <# } #>
  181. <# } #>
  182. }
  183. <# } #>
  184. <# } #>
  185. }