Esempio n. 1
0
 private void DeleteObjectRelations(DataObject DataObject)
 {
     try
     {
         String           tableName = DataObject.TableName;
         DataTableHandler handler   = (DataTableHandler)tableDatasets[tableName];
         foreach (MemberValue value in handler.Relations)
         {
             if (((Common.Attributes.Relation)value.Attribute).AutoDelete == false)
             {
                 continue;
             }
             if (value.GetValueType().IsArray)
             {
                 Array array = value.GetValue(DataObject) as Array;
                 foreach (DataObject obj in array)
                 {
                     DeleteObject(obj);
                 }
             }
             else
             {
                 DeleteObject(value.GetValue(DataObject) as DataObject);
             }
         }
     }
     catch (Exception e)
     {
         throw new DatabaseException("Deleting Relations failed !", e);
     }
 }
Esempio n. 2
0
 private void SaveObjectRelations(DataObject DataObject)
 {
     try
     {
         String           tableName = DataObject.TableName;
         DataTableHandler handler   = (DataTableHandler)tableDatasets[tableName];
         foreach (MemberValue value in handler.Relations)
         {
             if (value.GetValueType().IsArray)
             {
                 Array array = value.GetValue(DataObject) as Array;
                 if (array != null)
                 {
                     foreach (DataObject obj in array)
                     {
                         SaveObject(obj);
                     }
                 }
             }
             else
             {
                 object val = value.GetValue(DataObject);
                 if (val != null)
                 {
                     SaveObject(val as DataObject);
                 }
             }
         }
     }
     catch (Exception e)
     {
         throw new DatabaseException("Saving Relations failed !", e);
     }
 }
Esempio n. 3
0
        private void ReloadCache(string TableName)
        {
            DataTableHandler handler = tableDatasets[TableName] as DataTableHandler;

            ICache cache = handler.Cache;

            foreach (object o in cache.Keys)
            {
                ReloadObject(cache[o] as DataObject);
            }
        }
Esempio n. 4
0
        public void RegisterDataObject(Type DataObjectType)
        {
            bool primary = false;

            string TableName = DataObject.GetTableName(DataObjectType);

            DataSet ds = new DataSet();

            DataTable table = new DataTable(TableName);

            table.Columns.Add(TableName + "_ID", typeof(uint));
            MemberValue[] columns      = MemberValue.GetMemberValues(DataObjectType, typeof(Common.Attributes.DataElement), true, true);
            MemberValue[] relations    = MemberValue.GetMemberValues(DataObjectType, typeof(Common.Attributes.Relation), false, false);
            MemberValue   primaryvalue = null;

            foreach (MemberValue column in columns)
            {
                Type valueType = column.GetValueType();
                if (!primary && column.Attribute is Common.Attributes.PrimaryKey)
                {
                    primaryvalue = column;
                    primary      = true;
                    DataColumn[] index = new DataColumn[1];
                    index[0]         = table.Columns.Add(column.GetName(), valueType);
                    table.PrimaryKey = index;
                }
                else
                {
                    table.Columns.Add(column.GetName(), valueType);
                }
            }

            if (primary == false)
            {
                primaryvalue = new PropertyMemberValue(DataObjectType.GetProperty("ObjectId"));
                DataColumn[] index = new DataColumn[1];
                index[0]         = table.Columns[TableName + "_ID"];
                table.PrimaryKey = index;
            }

            ds.DataSetName        = TableName;
            ds.EnforceConstraints = true;
            ds.CaseSensitive      = false;
            ds.Tables.Add(table);

            DataTableHandler dth = new DataTableHandler(ds, DataObjectType, primaryvalue, columns, relations);

            dth.HasRelations = relations.Length != 0;
            tableDatasets.Add(TableName, dth);
            IdGenerator.setIDStart(DataObjectType, 1);
        }
Esempio n. 5
0
        private DataRow FindRowByKey(DataObject DataObject)
        {
            String tableName = DataObject.TableName;

            DataTableHandler handler = (DataTableHandler)tableDatasets[tableName];
            DataTable        table   = GetDataSet(tableName).Tables[tableName];

            DataRow[] rows = table.Select(tableName + "_ID = '" + DataObject.ObjectId + "'");
            if (rows.Length == 0)
            {
                return(null);
            }
            return(rows[0]);
        }
Esempio n. 6
0
        private void FillObjectWithRow(ref DataObject DataObject, DataRow row, bool reload)
        {
            string tableName = DataObject.TableName;

            //Type myType = DataObject.GetType();

            uint id = (uint)row[tableName + "_ID"];

            DataObject cacheObj = GetObjectInCache(tableName, id);

            if (cacheObj != null)
            {
                DataObject = cacheObj;
                if (reload == false)
                {
                    return;
                }
            }

            DataTableHandler handler = (DataTableHandler)tableDatasets[tableName];

            DataObject.ObjectId = id;
            foreach (MemberValue member in handler.Columns)
            {
                object val = row[member.GetName()];
                if (val != null && !val.GetType().IsInstanceOfType(DBNull.Value))
                {
                    member.SetValue(DataObject, val);
                }
            }
            DataObject.Dirty = false;


            if (handler.Relations.Length > 0)
            {
                FillLazyObjectRelations(DataObject, true);
            }

            if (reload == false)
            {
                PutObjectInCache(tableName, DataObject);
            }

            DataObject.IsValid = true;
        }
Esempio n. 7
0
        /// <summary>
        /// Loads the tables from all datasets
        /// </summary>
        public void LoadDatabaseTables()
        {
            IDictionaryEnumerator i = tableDatasets.GetEnumerator();

            while (i.MoveNext())
            {
                DataTableHandler dth = (DataTableHandler)i.Value;
                connection.LoadDataSet((string)i.Key, dth.DataSet);
                DataTable table = dth.DataSet.Tables[(string)i.Key];
                if (table.Rows.Count == 0)
                {
                    IdGenerator.setIDStart(dth.DataObjectType, 1);
                }
                else
                {
                    IdGenerator.setIDStart(dth.DataObjectType, 1 + (uint)table.Rows[table.Rows.Count - 1][(string)i.Key + "_ID"]);
                }
            }
        }
Esempio n. 8
0
        private void FillRowWithObject(DataObject DataObject, DataRow row)
        {
            string           tableName = DataObject.TableName;
            DataTableHandler handler   = (DataTableHandler)tableDatasets[tableName];

            row[tableName + "_ID"] = DataObject.ObjectId;
            foreach (MemberValue member in handler.Columns)
            {
                object val = member.GetValue(DataObject);
                if (val != null)
                {
                    row[member.GetName()] = val;
                }
            }
            if (handler.Relations.Length > 0)
            {
                SaveObjectRelations(DataObject);
            }
        }
Esempio n. 9
0
        private void FillLazyObjectRelations(DataObject DataObject, bool Autoload)
        {
            try
            {
                Type             myType    = DataObject.GetType();
                String           tableName = DataObject.TableName;
                DataTableHandler handler   = (DataTableHandler)tableDatasets[tableName];
                foreach (MemberValue value in handler.Relations)
                {
                    Common.Attributes.Relation relation = (Common.Attributes.Relation)value.Attribute;
                    if (relation.AutoLoad == false && Autoload == true)
                    {
                        continue;
                    }

                    PropertyInfo prop = myType.GetProperty(relation.LocalField, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                    object       val  = 0;
                    if (prop != null)
                    {
                        val = prop.GetValue(DataObject, null);
                    }
                    else
                    {
                        FieldInfo field = myType.GetField(relation.LocalField, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                        if (field != null)
                        {
                            val = field.GetValue(DataObject);
                        }
                        else
                        {
                            MemberValue member = handler.GetColumnByName(relation.LocalField);
                            val = member.GetValue(DataObject);
                        }
                    }

                    Type type = value.GetValueType();
                    if (type.IsArray)
                    {
                        type = type.GetElementType();
                    }

                    DataObject[] Elements = null;
                    if (val != null)
                    {
                        Elements = SelectObjects(type, relation.RemoteField + " = '" + val.ToString() + "'");
                    }
                    if (Elements != null && Elements.Length > 0)
                    {
                        if (value.GetValueType().IsArray)
                        {
                            object[] args  = { Elements.Length };
                            object[] array = (object[])Activator.CreateInstance(value.GetValueType(), args);;
                            for (int i = 0; i < Elements.Length; i++)
                            {
                                array[i] = Elements[i];
                            }

                            value.SetValue(DataObject, array);
                        }
                        else
                        {
                            value.SetValue(DataObject, Elements[0]);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new DatabaseException("Resolving Relations failed !", e);
            }
        }
Esempio n. 10
0
        private void DeleteObjectInCache(string TableName, DataObject obj)
        {
            DataTableHandler handler = tableDatasets[TableName] as DataTableHandler;

            handler.SetCacheObject(obj.ObjectId, null);
        }
Esempio n. 11
0
        private DataObject GetObjectInCache(string TableName, uint id)
        {
            DataTableHandler handler = tableDatasets[TableName] as DataTableHandler;

            return(handler.GetCacheObject(id));
        }
Esempio n. 12
0
        public DataSet GetDataSet(string TableName)
        {
            DataTableHandler handler = (DataTableHandler)tableDatasets[TableName];

            return(handler.DataSet);
        }