/PetaPoco/Models/Generated/PetaPoco.Generator.ttinclude

http://github.com/toptensoftware/PetaPoco · Unknown · 190 lines · 176 code · 14 blank · 0 comment · 0 complexity · 57f38347860df895bdbbfb71be3ef2b0 MD5 · raw file

  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 T SingleOrDefault(object primaryKey) { return repo.SingleOrDefault<T>(primaryKey); }
  72. public static T SingleOrDefault(string sql, params object[] args) { return repo.SingleOrDefault<T>(sql, args); }
  73. public static T SingleOrDefault(Sql sql) { return repo.SingleOrDefault<T>(sql); }
  74. public static T FirstOrDefault(string sql, params object[] args) { return repo.FirstOrDefault<T>(sql, args); }
  75. public static T FirstOrDefault(Sql sql) { return repo.FirstOrDefault<T>(sql); }
  76. public static T Single(object primaryKey) { return repo.Single<T>(primaryKey); }
  77. public static T Single(string sql, params object[] args) { return repo.Single<T>(sql, args); }
  78. public static T Single(Sql sql) { return repo.Single<T>(sql); }
  79. public static T First(string sql, params object[] args) { return repo.First<T>(sql, args); }
  80. public static T First(Sql sql) { return repo.First<T>(sql); }
  81. public static List<T> Fetch(string sql, params object[] args) { return repo.Fetch<T>(sql, args); }
  82. public static List<T> Fetch(Sql sql) { return repo.Fetch<T>(sql); }
  83. public static List<T> Fetch(long page, long itemsPerPage, string sql, params object[] args) { return repo.Fetch<T>(page, itemsPerPage, sql, args); }
  84. public static List<T> Fetch(long page, long itemsPerPage, Sql sql) { return repo.Fetch<T>(page, itemsPerPage, sql); }
  85. public static List<T> SkipTake(long skip, long take, string sql, params object[] args) { return repo.SkipTake<T>(skip, take, sql, args); }
  86. public static List<T> SkipTake(long skip, long take, Sql sql) { return repo.SkipTake<T>(skip, take, sql); }
  87. public static Page<T> Page(long page, long itemsPerPage, string sql, params object[] args) { return repo.Page<T>(page, itemsPerPage, sql, args); }
  88. public static Page<T> Page(long page, long itemsPerPage, Sql sql) { return repo.Page<T>(page, itemsPerPage, sql); }
  89. public static IEnumerable<T> Query(string sql, params object[] args) { return repo.Query<T>(sql, args); }
  90. public static IEnumerable<T> Query(Sql sql) { return repo.Query<T>(sql); }
  91. <# if (TrackModifiedColumns) { #>
  92. private Dictionary<string,bool> ModifiedColumns;
  93. private void OnLoaded()
  94. {
  95. ModifiedColumns = new Dictionary<string,bool>();
  96. }
  97. protected void MarkColumnModified(string column_name)
  98. {
  99. if (ModifiedColumns!=null)
  100. ModifiedColumns[column_name]=true;
  101. }
  102. public int Update()
  103. {
  104. if (ModifiedColumns==null)
  105. return repo.Update(this);
  106. int retv = repo.Update(this, ModifiedColumns.Keys);
  107. ModifiedColumns.Clear();
  108. return retv;
  109. }
  110. public void Save()
  111. {
  112. if (repo.IsNew(this))
  113. repo.Insert(this);
  114. else
  115. Update();
  116. }
  117. <# } #>
  118. }
  119. <# } #>
  120. }
  121. <# } #>
  122. <# if (GeneratePocos) { #>
  123. <#
  124. foreach(Table tbl in from t in tables where !t.Ignore select t)
  125. {
  126. #>
  127. [TableName("<#=tbl.Name#>")]
  128. <# if (tbl.PK!=null && tbl.PK.IsAutoIncrement) { #>
  129. <# if (tbl.SequenceName==null) { #>
  130. [PrimaryKey("<#=tbl.PK.Name#>")]
  131. <# } else { #>
  132. [PrimaryKey("<#=tbl.PK.Name#>", sequenceName="<#=tbl.SequenceName#>")]
  133. <# } #>
  134. <# } #>
  135. <# if (tbl.PK!=null && !tbl.PK.IsAutoIncrement) { #>
  136. [PrimaryKey("<#=tbl.PK.Name#>", autoIncrement=false)]
  137. <# } #>
  138. [ExplicitColumns]
  139. public partial class <#=tbl.ClassName#> <# if (GenerateOperations) { #>: <#=RepoName#>.Record<<#=tbl.ClassName#>> <# } #>
  140. {
  141. <#
  142. foreach(Column col in from c in tbl.Columns where !c.Ignore select c)
  143. {
  144. // Column bindings
  145. #>
  146. <# if (TrackModifiedColumns) { #>
  147. <# if (col.Name!=col.PropertyName) { #>
  148. [Column("<#=col.Name#>")]
  149. <# } else { #>
  150. [Column]
  151. <# } #>
  152. public <#=col.PropertyType #><#=CheckNullable(col)#> <#=col.PropertyName #>
  153. {
  154. get
  155. {
  156. return _<#=col.PropertyName #>;
  157. }
  158. set
  159. {
  160. _<#=col.PropertyName #> = value;
  161. MarkColumnModified("<#=col.Name#>");
  162. }
  163. }
  164. <#=col.PropertyType #><#=CheckNullable(col)#> _<#=col.PropertyName #>;
  165. <# } else { #>
  166. <# if (col.Name!=col.PropertyName) { #>
  167. [Column("<#=col.Name#>")] public <#=col.PropertyType #><#=CheckNullable(col)#> <#=col.PropertyName #> { get; set; }
  168. <# } else { #>
  169. [Column] public <#=col.PropertyType #><#=CheckNullable(col)#> <#=col.PropertyName #> { get; set; }
  170. <# } #>
  171. <# } #>
  172. <# } #>
  173. }
  174. <# } #>
  175. <# } #>
  176. }