1: public partial class PetaPocoDB : Database
2: {
3: public PetaPocoDB()
4: : base("")
5: {
6: CommonConstruct();
7: }
8:
9: public PetaPocoDB(string connectionStringName)
10: : base(connectionStringName)
11: {
12: CommonConstruct();
13: }
14:
15: partial void CommonConstruct();
16:
17: public interface IFactory
18: {
19: PetaPocoDB GetInstance();
20: }
21:
22: public static IFactory Factory { get; set; }
23: public static PetaPocoDB GetInstance()
24: {
25: if (_instance!=null)
26: return _instance;
27:
28: if (Factory!=null)
29: return Factory.GetInstance();
30: else
31: return new PetaPocoDB();
32: }
33:
34: [ThreadStatic] static PetaPocoDB _instance;
35:
36: public override void OnBeginTransaction()
37: {
38: if (_instance==null)
39: _instance=this;
40: }
41:
42: public override void OnEndTransaction()
43: {
44: if (_instance==this)
45: _instance=null;
46: }
47:
48: public class Record<T> where T:new()
49: {
50: public static PetaPocoDB repo { get { return PetaPocoDB.GetInstance(); } }
51: public bool IsNew() { return repo.IsNew(this); }
52: public object Insert() { return repo.Insert(this); }
53: public int Update(IEnumerable<string> columns) { return repo.Update(this, columns); }
54: public static int Update(string sql, params object[] args) { return repo.Update<T>(sql, args); }
55: public static int Update(Sql sql) { return repo.Update<T>(sql); }
56: public int Delete() { return repo.Delete(this); }
57: public static int Delete(string sql, params object[] args) { return repo.Delete<T>(sql, args); }
58: public static int Delete(Sql sql) { return repo.Delete<T>(sql); }
59: public static int Delete(object primaryKey) { return repo.Delete<T>(primaryKey); }
60: public static bool Exists(object primaryKey) { return repo.Exists<T>(primaryKey); }
61: public static T SingleOrDefault(object primaryKey) { return repo.SingleOrDefault<T>(primaryKey); }
62: public static T SingleOrDefault(string sql, params object[] args) { return repo.SingleOrDefault<T>(sql, args); }
63: public static T SingleOrDefault(Sql sql) { return repo.SingleOrDefault<T>(sql); }
64: public static T FirstOrDefault(string sql, params object[] args) { return repo.FirstOrDefault<T>(sql, args); }
65: public static T FirstOrDefault(Sql sql) { return repo.FirstOrDefault<T>(sql); }
66: public static T Single(object primaryKey) { return repo.Single<T>(primaryKey); }
67: public static T Single(string sql, params object[] args) { return repo.Single<T>(sql, args); }
68: public static T Single(Sql sql) { return repo.Single<T>(sql); }
69: public static T First(string sql, params object[] args) { return repo.First<T>(sql, args); }
70: public static T First(Sql sql) { return repo.First<T>(sql); }
71: public static List<T> Fetch(string sql, params object[] args) { return repo.Fetch<T>(sql, args); }
72: public static List<T> Fetch(Sql sql) { return repo.Fetch<T>(sql); }
73: public static List<T> Fetch(long page, long itemsPerPage, string sql, params object[] args) { return repo.Fetch<T>(page, itemsPerPage, sql, args); }
74: public static List<T> Fetch(long page, long itemsPerPage, Sql sql) { return repo.Fetch<T>(page, itemsPerPage, sql); }
75: public static List<T> SkipTake(long skip, long take, string sql, params object[] args) { return repo.SkipTake<T>(skip, take, sql, args); }
76: public static List<T> SkipTake(long skip, long take, Sql sql) { return repo.SkipTake<T>(skip, take, sql); }
77: public static Page<T> Page(long page, long itemsPerPage, string sql, params object[] args) { return repo.Page<T>(page, itemsPerPage, sql, args); }
78: public static Page<T> Page(long page, long itemsPerPage, Sql sql) { return repo.Page<T>(page, itemsPerPage, sql); }
79: public static IEnumerable<T> Query(string sql, params object[] args) { return repo.Query<T>(sql, args); }
80: public static IEnumerable<T> Query(Sql sql) { return repo.Query<T>(sql); }
81:
82: private Dictionary<string,bool> ModifiedColumns;
83: private void OnLoaded()
84: {
85: ModifiedColumns = new Dictionary<string,bool>();
86: }
87: protected void MarkColumnModified(string column_name)
88: {
89: if (ModifiedColumns!=null)
90: ModifiedColumns[column_name]=true;
91: }
92: public int Update()
93: {
94: if (ModifiedColumns==null)
95: return repo.Update(this);
96:
97: int retv = repo.Update(this, ModifiedColumns.Keys);
98: ModifiedColumns.Clear();
99: return retv;
100: }
101: public void Save()
102: {
103: if (repo.IsNew(this))
104: repo.Insert(this);
105: else
106: Update();
107: }
108: }
109: }