public static T Insert <T>(this DirectDatabaseBase db, DirectModel model) where T : DirectModel { DirectExecuteResult result = db.Execute(model.ConstructInsertQuery()); if (result.IsSuccessfull && result.LastID.HasValue) { model.ID = (int)result.LastID; model.Snapshot.SetSnapshot(); return((T)model); } return((T)model); }
public static int?Update(this DirectDatabaseBase db, DirectModel model) { if (model.IntegerPrimary && !model.ID.HasValue) { throw new Exception("ID is not set, maybe this table was not loaded"); } DirectExecuteResult result = db.Execute(model.ConstructUpdateQuery()); if (!result.IsSuccessfull) { return(null); } else { model.Snapshot.SetSnapshot(); return(result.NumberOfRowsAffected); } }
public static bool Delete(this DirectDatabaseBase db, DirectModel model) { if (model.IntegerPrimary && !model.ID.HasValue) { throw new Exception("THIS model has not ID"); } model.OnBeforeDelete(); string command = string.Format(DirectModelHelper.GetDatabase(model).QueryDelete, model.GetTableName(), model.GetIdNameValue(), (model.IntegerPrimary ? model.ID.Value.ToString() : string.Format("'{0}'", model.GetStringID()))); DirectExecuteResult result = db.Execute(command); if (result.IsSuccessfull) { model.ID = null; model.Snapshot.DeleteSnapshot(); return(true); } return(false); }