PageRenderTime 14ms CodeModel.GetById 3ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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