public TDataObject QuerySingleOrDefault <TDataObject>([NotNull] IDBPredicate cond) where TDataObject : IDataObject, new() { using (var scope = DBScope("Custom query for " + typeof(TDataObject))) { using (var c = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(c); DataObjectInfo <TDataObject> .AppendQuerySql(sc, cond); sc.Finish(); using (var r = c.ExecuteReader()) { if (r.Read()) { if (r.Read()) { throw new InvalidOperationException("Too many objects"); } return(DataObjectInfo <TDataObject> .Read(r)); } return(default(TDataObject)); } } } }
public void Delete <TDataObject>(int id) where TDataObject : class, IIntKeyedDataObject, new() { using (var scope = DBScope("Removing " + typeof(TDataObject).Name + ". ID = " + id)) { Removed <TDataObject>(id); RunInTransaction(scope.Connection, (cn, transaction) => { using (var cmd = cn.CreateCommand()) { cmd.Transaction = transaction; var sc = new SqlSerializationContext((SqlCommand)cmd); DataObjectSqlSerializer <TDataObject> .AppendSoftDeleteSql(sc, id); sc.Finish(); cmd.LexExecuteNonQuery(); if (DataObjectInfo <TDataObject> .IsSecured) { var updOp = new UpdateOperation <TblPermissions>( new CompareCondition <int> ( new PropertyCondition <int>(ObjectTypeHelper.GetObjectType(typeof(TDataObject)).GetSecurityAtr().Name + "Ref"), new ValueCondition <int>(id), COMPARE_KIND.EQUAL ), new PropertyAssignement <int>(DataObject.Schema.SysState, new ValueCondition <int>(1))); Update(updOp, cn, transaction); } } }); } }
public List <TDataObject> Query <TDataObject>([CanBeNull] IDBPredicate cond) where TDataObject : IDataObject, new() { using (var scope = DBScope("Custom query for " + typeof(TDataObject))) { using (var c = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(c); DataObjectInfo <TDataObject> .AppendQuerySql(sc, cond); sc.Finish(); using (var r = c.LexExecuteReader()) { var res = DataObjectInfo <TDataObject> .FullRead(r, 1); if (typeof(TDataObject).GetInterface(typeof(IIntKeyedDataObject).Name) != null) { // TODO: Cache them // foreach (var i in res) // { // CacheIt<TDataObject>(i as IIntKeyedDataObject); // } } return(res); } } } }
public void Update <TDataObject>(IList <TDataObject> objs) where TDataObject : class, IIntKeyedDataObject, new() { //TODO: Update cache here. not only delete cached items if (objs.Count > 0) { using (var scope = DBScope("Updating multiple " + typeof(TDataObject).Name + ". Count = " + objs.Count)) { RunInTransaction(scope.Connection, (cn, transaction) => { using (var cmd = cn.CreateCommand()) { var sc = new SqlSerializationContext((SqlCommand)cmd); foreach (var o in objs) { DataObjectSqlSerializer <TDataObject> .AppendUpdateSql(sc, o); Logger.WriteLine("ID = " + o.ID); sc.Next(); CacheIt(o); } sc.Finish(); cmd.Transaction = transaction; cmd.LexExecuteNonQuery(); } }); } } }
public int Insert <TDataObject>(TDataObject obj) where TDataObject : class, IIntKeyedDataObject, new() { using (var scope = DBScope("Inserting " + typeof(TDataObject).Name)) { using (var cmd = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(cmd); DataObjectSqlSerializer <TDataObject> .AppendInsertSql(sc, obj); sc.Finish(); using (var r = cmd.LexExecuteReader()) { r.Read(); var id = Convert.ToInt32(r.GetDecimal(0)); DataObjectSqlSerializer <TDataObject> .KeyColumn.Storage.SetValue(obj, id); Logger.WriteLine("ID = " + id); Added <TDataObject>(); return(id); } } } }
public void Insert <TDataObject>(IList <TDataObject> objs) where TDataObject : class, IIntKeyedDataObject, new() { if (objs.Count > 0) { using (var scope = DBScope("Inserting multiple " + typeof(TDataObject).Name + ". Count = " + objs.Count)) { RunInTransaction(scope.Connection, (cn, transaction) => { using (var cmd = cn.CreateCommand()) { cmd.Transaction = transaction; var sc = new SqlSerializationContext((SqlCommand)cmd); foreach (var o in objs) { if (o.ID != 0) { throw new DMError("DataObject has been already inserted."); } DataObjectSqlSerializer <TDataObject> .AppendInsertSql(sc, o); sc.Next(); } sc.Finish(); using (var r = cmd.LexExecuteReader()) { int i = 0, c = objs.Count; while (true) { if (!r.Read()) { throw new DMError("Invalid Data Reader"); } int id = Convert.ToInt32(r.GetDecimal(0)); TDataObject obj = objs[i]; DataObjectSqlSerializer <TDataObject> .KeyColumn.Storage.SetValue(obj, id); CacheIt(obj); Added <TDataObject>(); Logger.WriteLine("ID = " + id); ++i; if (i < c) { r.NextResult(); } else { break; } } } } }); } } }
public void UnLink(IIntKeyedDataObject do1, IIntKeyedDataObject do2) { using (var scope = DBScope("UnLinking " + do1.GetType().Name + ": " + do1.ID + " and " + do2.GetType().Name + ": " + do2.ID)) { using (var c = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(c); LookupHelper.AppendMMUnLinkSql(sc, do1, do2); sc.Finish(); c.LexExecuteNonQuery(); } } }
public List <int> LookupMany2ManyIds <TDataObject>([NotNull] IIntKeyedDataObject firstPart, [CanBeNull] IDBPredicate condition) { using (var scope = DBScope("Looking up many-2-many ids between " + firstPart.GetType().Name + " and " + typeof(TDataObject).Name)) { using (var c = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(c); LookupHelper.AppendMMLookupSql(sc, firstPart, typeof(TDataObject)); sc.Finish(); return(c.FullReadInts()); } } }
public void UnLink(IIntKeyedDataObject do1, IIntKeyedDataObject do2) { using (var scope = DBScope(Translations.DatabaseModel_UnLink_UnLinking_ + do1.GetType().Name + ": " + do1.ID + Translations.DatabaseModel_LookupMany2ManyIds__and_ + do2.GetType().Name + ": " + do2.ID)) { using (var c = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(c); LookupHelper.AppendMMUnLinkSql(sc, do1, do2); sc.Finish(); c.LexExecuteNonQuery(); } } }
public List <int> LookupMany2ManyIds <TDataObject>([NotNull] IIntKeyedDataObject firstPart, [CanBeNull] IDBPredicate condition) { using (var scope = DBScope(Translations.DatabaseModel_LookupMany2ManyIds_Looking_up_many_2_many_ids_between_ + firstPart.GetType().Name + Translations.DatabaseModel_LookupMany2ManyIds__and_ + typeof(TDataObject).Name)) { using (var c = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(c); LookupHelper.AppendMMLookupSql(sc, firstPart, typeof(TDataObject)); sc.Finish(); return(c.FullReadInts()); } } }
public List <int> LookupIds <TDataObject>([NotNull] IIntKeyedDataObject owner, [CanBeNull] IDBPredicate condition) where TDataObject : IDataObject { using (var scope = DBScope("Looking up ids of " + owner.GetType().Name + " for " + typeof(TDataObject).Name)) { using (var c = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(c); LookupHelper.AppendLookupSql(sc, owner, typeof(TDataObject), condition); sc.Finish(); return(c.FullReadInts()); } } }
public void Update <TDataObject>([NotNull] IDBUpdateOperation <TDataObject> op, [NotNull] IDbConnection cn, [CanBeNull] IDbTransaction transaction) where TDataObject : class, IDataObject, new() { using (DBScope("Executing update operation")) { using (var cmd = cn.CreateCommand()) { cmd.Transaction = transaction; var sc = new SqlSerializationContext((SqlCommand)cmd); op.Write(sc); sc.Finish(); cmd.LexExecuteNonQuery(); } } }
public void Update <TDataObject>(TDataObject obj) where TDataObject : class, IIntKeyedDataObject, new() { using (var scope = DBScope("Updating " + typeof(TDataObject).Name)) { using (var cmd = scope.Connection.CreateCommand()) { var sc = new SqlSerializationContext(cmd); DataObjectSqlSerializer <TDataObject> .AppendUpdateSql(sc, obj); sc.Finish(); cmd.LexExecuteNonQuery(); } CacheIt(obj); } }
public void Delete <TDataObject>(IList <int> ids) where TDataObject : class, IIntKeyedDataObject, new() { if (ids.Count > 0) { using (var scope = DBScope("Removing multiple " + typeof(TDataObject).Name + ". Count = " + ids.Count)) { foreach (var i in ids) { Logger.WriteLine("ID = " + i); Cache.Remove(FormatCacheKey <TDataObject>(i)); } RunInTransaction(scope.Connection, (cn, transaction) => { using (var cmd = cn.CreateCommand()) { cmd.Transaction = transaction; var sc = new SqlSerializationContext((SqlCommand)cmd); DataObjectSqlSerializer <TDataObject> . AppendSoftDeleteSql(sc, ids); sc.Finish(); cmd.LexExecuteNonQuery(); } if (DataObjectInfo <TDataObject> .IsSecured) { var idCond = new ValueCondition <int>(0); var updOp = new UpdateOperation <TblPermissions>( new CompareCondition <int> ( new PropertyCondition <int>(ObjectTypeHelper.GetObjectType(typeof(TDataObject)).GetSecurityAtr().Name + "Ref"), idCond, COMPARE_KIND.EQUAL ), new PropertyAssignement <int>(DataObject.Schema.SysState, new ValueCondition <int>(1))); foreach (var id in ids) { idCond.Value = id; Update(updOp, cn, transaction); } } }); } } }