예제 #1
0
        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;
            }
        }
예제 #2
0
        internal ESetObjectStatus SetDBObject(CDataBaseObject p_object, String p_value_name)
        {
            //if (!m_p_map.m_p_linked_values_names.Contains(p_value_name)) throw new ArgumentException("The given value name was not registered in the Object map.");

            Object p_objs;

            if (!_m_p_values.TryGetValue(p_value_name, out p_objs))
            {
                if (p_object != null)
                {
                    p_objs = new List <CDataBaseObject>()
                    {
                        p_object
                    };
                }
                else
                {
                    p_objs = new List <CDataBaseObject>();
                }
                _m_p_values[p_value_name] = p_objs;
            }
            else
            {
                if (p_object != null)
                {
                    ((List <CDataBaseObject>)p_objs).Add(p_object);
                }
            }
            return(ESetObjectStatus.success);
        }
예제 #3
0
 /// <summary>
 /// Copies all internal values from the given database object into this database object.
 /// </summary>
 /// <param name="p_db_obj">The database object to copy from.</param>
 internal void CopyFrom(CDataBaseObject p_db_obj)
 {
     _m_p_values.Clear();
     foreach (KeyValuePair <String, Object> entry in p_db_obj._m_p_values)
     {
         _m_p_values.Add(entry.Key, entry.Value);
     }
 }
예제 #4
0
        public override Boolean Equals(Object p_obj)
        {
            CDataBaseObject p_db_obj = p_obj as CDataBaseObject;

            if (p_db_obj == null)
            {
                return(false);
            }

            return(GetHashCode() == p_db_obj.GetHashCode());
        }
예제 #5
0
        internal String[] GetPrimitiveNames(CDataBaseObject p_parent)
        {
            List <String> p_primitive_names = new List <String>();

            foreach (KeyValuePair <String, Object> entry in _m_p_values)
            {
                if (!(entry.Value is List <CDataBaseObject>))
                {
                    p_primitive_names.Add(entry.Key);
                }
                else
                {
                    Int32 link_index = m_p_map.m_p_linked_values_names.FindIndex(x => x == entry.Key);
                    if (link_index == -1)
                    {
                        continue;
                    }

                    List <CDataBaseObject> p_linked_objects = (List <CDataBaseObject>)entry.Value;
                    if (p_linked_objects.Count == 0)
                    {
                        continue;
                    }

                    if (m_p_map.m_p_object_links[link_index].m_type == EObjectLinkType.one_to_one)
                    {
                        p_primitive_names.AddRange(m_p_map.m_p_object_links[link_index].m_p_foreign_key.m_p_source_columns);
                    }
                }
            }
            if (p_parent != null)
            {
                List <SObjectLink> p_parent_links = p_parent.m_p_map.m_p_object_links;
                for (Int32 i = 0; i < p_parent_links.Count; ++i)
                {
                    if (p_parent_links[i].m_p_target_map == m_p_map)
                    {
                        p_primitive_names.AddRange(
                            p_parent_links[i].m_p_foreign_key.m_p_target_columns.Where(p_name => !p_primitive_names.Contains(p_name))
                            );
                    }
                }
            }
            return(p_primitive_names.ToArray());
        }
예제 #6
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));
        }
예제 #7
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);
        }
예제 #8
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);
        }
예제 #9
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));
        }
예제 #10
0
        internal Object[] GetPrimitiveValues(CDataBaseObject p_parent)
        {
            List <Object> p_primitive_values = new List <Object>();

            foreach (KeyValuePair <String, Object> entry in _m_p_values)
            {
                if (!(entry.Value is List <CDataBaseObject>))
                {
                    p_primitive_values.Add(entry.Value);
                }
                else
                {
                    Int32 link_index = m_p_map.m_p_linked_values_names.FindIndex(x => x == entry.Key);
                    if (link_index == -1)
                    {
                        continue;
                    }

                    List <CDataBaseObject> p_linked_objects = (List <CDataBaseObject>)entry.Value;
                    if (p_linked_objects.Count == 0)
                    {
                        continue;
                    }

                    if (m_p_map.m_p_object_links[link_index].m_type == EObjectLinkType.one_to_one)
                    {
                        foreach (String p_target_column in m_p_map.m_p_object_links[link_index].m_p_foreign_key.m_p_target_columns)
                        {
                            p_primitive_values.Add(p_linked_objects[0][p_target_column]);
                        }
                    }
                }
            }
            if (p_parent != null)
            {
                List <SObjectLink> p_parent_links = p_parent.m_p_map.m_p_object_links;
                for (Int32 i = 0; i < p_parent_links.Count; ++i)
                {
                    if (p_parent_links[i].m_p_target_map == m_p_map)
                    {
                        ReadOnlyCollection <String> p_source_columns = p_parent_links[i].m_p_foreign_key.m_p_source_columns;
                        for (Int32 j = 0; j < p_source_columns.Count; ++j)
                        {
                            Object p_value;
                            if (p_parent.HasValue(p_source_columns[j]))
                            {
                                p_value = p_parent[p_source_columns[j]];
                            }
                            else if (HasValue(p_parent_links[i].m_p_foreign_key.m_p_target_columns[j]))
                            {
                                p_value = this[p_parent_links[i].m_p_foreign_key.m_p_target_columns[j]];
                            }
                            else
                            {
                                throw new Exception("Could not track foreign key \"" + p_parent.m_p_map.m_p_linked_values_names[i] + "\" from \"" + p_parent.m_p_map.m_p_object_type.ToString() + "\" to \"" + m_p_map.m_p_object_type.ToString() + "\".");
                            }

                            if (p_primitive_values.Contains(p_value))
                            {
                                continue;
                            }
                            else
                            {
                                p_primitive_values.Add(p_value);
                            }
                        }
                    }
                }
            }
            return(p_primitive_values.ToArray());
        }