Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        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));
        }