/// <summary> /// Saves an Object into the database. Returns true if the Object was successfully saved, false otherwise. The objects status is captured immediately, but the queries are run asynchronously. /// </summary> /// <param name="p_obj">The Object to save.</param> /// <returns>A status object indicating the success/failure of the operation.</returns> public async Task <CDBWizardStatus> SaveAsync <T>(T p_obj) { CObjectMap p_object_map = CObjectMap.Get(p_obj.GetType()); if (p_object_map.m_p_begin_delete_call_back != null) { p_object_map.m_p_begin_delete_call_back(p_obj); } if (p_object_map.m_p_unique_keys.Count == 0) { throw new Exception("Cannot save Object without any identifying primary keys defined"); } CDBWizardStatus p_status = await p_object_map.SaveObjectAsync(this, p_obj); if (!p_status.IsError) { if (p_object_map.m_p_end_save_call_back != null) { p_object_map.m_p_end_save_call_back(p_obj); } return(p_status); } return(p_status); }
internal void SetObject(Object p_obj, String p_value_name, Dictionary <Object, CDataBaseObject> p_mapped_objects) { CDataBaseObject p_db_obj = null; if (p_obj != null) { if (!p_mapped_objects.TryGetValue(p_obj, out p_db_obj)) { p_db_obj = new CDataBaseObject(CObjectMap.Get(p_obj.GetType())); p_db_obj.MapFrom(p_obj, p_mapped_objects); if (p_db_obj.IsEmpty) { p_db_obj = null; } } } ESetObjectStatus status = SetDBObject(p_db_obj, p_value_name); switch (status) { case ESetObjectStatus.err_table_link_ambiguous: case ESetObjectStatus.err_table_not_linked: throw new Exception("Error while trying to set database Object from given value: " + status.ToString()); case ESetObjectStatus.success: break; } }
/// <summary> /// Constructs a new database object and sets its internal values according to the given object. /// </summary> /// <param name="p_obj">The object that is used as base for this database object.</param> public CDataBaseObject(Object p_obj) { if (p_obj == null) { throw new ArgumentNullException("The object you provide must not be null."); } m_p_map = CObjectMap.Get(p_obj.GetType()); _m_p_values = new Dictionary <String, Object>(); MapFrom(p_obj, new Dictionary <Object, CDataBaseObject>()); }
public void MapToClass <T>(T p_obj) where T : class { if (p_obj == null) { throw new ArgumentNullException("You must provide an Object."); } Action <CDataBaseObject> p_delegate = (Action <CDataBaseObject>)CObjectMap.Get(p_obj.GetType()).m_p_map_to_method.CreateDelegate(typeof(Action <CDataBaseObject>), p_obj); p_delegate(this); }
private static void ProcessStoreAttributes(Type p_type) { // TODO: Think about inheritance more thoroughly... // Also GetCustomAttribute<T> may throw an ambiguity exception (nice library design, microsoft, vexing exceptions \o/) StoreAttributes.CTableAttribute p_store_table_attribute = p_type.GetCustomAttribute <StoreAttributes.CTableAttribute>(); if (p_store_table_attribute == null) { return; // table store attribute is the only attribute that matters when applied directly to a type. } CObjectMap p_object_map = CObjectMap.Get(p_type); // <- magic is in the ctor }
/// <summary> /// Deletes an object from the database. /// </summary> /// <param name="obj">The object that should be deleted from the database.</param> /// <returns>A status object indicating the success/failure of the operation</returns> public CDBWizardStatus Delete <T>(T obj) { CObjectMap p_object_map = CObjectMap.Get(obj.GetType()); if (p_object_map.m_p_begin_delete_call_back != null) { p_object_map.m_p_begin_delete_call_back(obj); } if (p_object_map.m_p_unique_keys.Count == 0) { throw new Exception("Cannot delete Object without any identifying primary keys defined"); } CDBWizardStatus p_status = p_object_map.DeleteObject(this, obj); return(p_status); }
public async Task <Tuple <CDBWizardStatus, CDataBaseObject> > LoadObjectAsync(Type p_object_type, Object p_primary_key_value) { CObjectMap p_object_map = CObjectMap.Get(p_object_type); CDataBaseObject p_result = new CDataBaseObject(p_object_map); CDBWizardStatus p_status = await p_object_map.LoadObjectAsync( this, new SQL.CWhereCondition(p_object_map.m_p_unique_keys[0].m_p_column_name, "=", p_primary_key_value.ToString()), p_result ); if (p_status.IsError) { p_result = null; } return(new Tuple <CDBWizardStatus, CDataBaseObject>(p_status, p_result)); }
/// <summary> /// Loads an Object of the given type with the given criteria. The criteria must uniquely identify an Object, otherwise an exception is thrown. /// </summary> /// <typeparam name="T">The type of the Object to load.</typeparam> /// <param name="p_object_conditions">The criteria the loaded Object must match.</param> /// <returns>The loaded Object that matched the criteria. If no Object matches the criteria, default(T) is returned, if multiple objects matched, an error is thrown.</returns> public async Task <CDBWizardStatus> LoadClassAsync <T>(T p_obj, params KeyValuePair <String, Object>[] p_object_conditions) where T : class { CObjectMap p_object_map = CObjectMap.Get(p_obj.GetType()); if (p_object_map.m_p_begin_load_call_back != null) { p_object_map.m_p_begin_load_call_back(p_obj); } SQL.CWhereCondition[] p_conditions = new SQL.CWhereCondition[p_object_conditions.Length]; for (Int32 i = 0; i < p_object_conditions.Length; ++i) { p_conditions[i] = new SQL.CWhereCondition(p_object_conditions[i].Key, "=", p_object_conditions[i].Value); } CDataBaseObject p_db_obj = new CDataBaseObject(p_object_map); CDBWizardStatus p_status = await p_object_map.LoadObjectAsync( this, new SQL.CWhereCondition(p_conditions), p_db_obj ); if (!p_status.IsError) { try { p_db_obj.MapToClass(p_obj); if (p_object_map.m_p_end_load_call_back != null) { p_object_map.m_p_end_load_call_back(p_obj); } } catch (Exception p_except) { return(new CDBWizardStatus(EDBWizardStatusCode.err_exception_thrown, p_except)); } } return(p_status); }
/// <summary> /// Loads an Object of the given type with the given primary key. The criteria must uniquely identify an Object, otherwise an exception is thrown. /// </summary> /// <typeparam name="T">The type of the Object to load.</typeparam> /// <param name="p_primary_key_value">The criteria the loaded Object must match.</param> /// <returns>A status Object containing information about the success/failure of the operation.</returns> public async Task <CDBWizardStatus> LoadClassAsync <T>(T p_obj, Object p_primary_key_value) where T : class { CObjectMap p_object_map = CObjectMap.Get(p_obj.GetType()); if (p_object_map.m_p_begin_load_call_back != null) { p_object_map.m_p_begin_load_call_back(p_obj); } CDataBaseObject p_db_obj = new CDataBaseObject(p_object_map); if (p_object_map.m_p_unique_keys.Count == 0) { throw new Exception("The class \"" + typeof(T).FullName + "\" does not define a primary key."); } CDBWizardStatus p_status = await p_object_map.LoadObjectAsync( this, new SQL.CWhereCondition(p_object_map.m_p_unique_keys[0].m_p_column_name, "=", p_primary_key_value.ToString()), p_db_obj ); if (!p_status.IsError) { try { p_db_obj.MapToClass(p_obj); if (p_object_map.m_p_end_load_call_back != null) { p_object_map.m_p_end_load_call_back(p_obj); } } catch (Exception p_except) { return(new CDBWizardStatus(EDBWizardStatusCode.err_exception_thrown, p_except)); } } return(p_status); }
public Tuple <CDBWizardStatus, CDataBaseObject> LoadObject(Type p_object_type, Object p_primary_key_value) { CObjectMap p_object_map = CObjectMap.Get(p_object_type); CDataBaseObject p_result = new CDataBaseObject(p_object_map); if (p_object_map.m_p_unique_keys.Count == 0) { throw new Exception("The class \"" + p_object_type.FullName + "\" does not define a primary key."); } CDBWizardStatus p_status = p_object_map.LoadObject( this, new SQL.CWhereCondition(p_object_map.m_p_unique_keys[0].m_p_column_name, "=", p_primary_key_value.ToString()), p_result ); if (p_status.IsError) { p_result = null; } return(new Tuple <CDBWizardStatus, CDataBaseObject>(p_status, p_result)); }
public void MapToStruct <T>(ref T p_obj) where T : struct { MapToStructDelegate <T> p_delegate = (MapToStructDelegate <T>)CObjectMap.Get(p_obj.GetType()).m_p_map_to_method.CreateDelegate(typeof(MapToStructDelegate <T>)); p_delegate(ref p_obj, this); }