Пример #1
0
        public void RegisterDataObject(Type objType)
        {
            if (TableDatasets.ContainsKey(GetTableOrViewName(objType)))
            {
                return;
            }

            bool       primaryKeySpecified    = false;
            bool       useAutoIncrementColumn = false;
            bool       relations          = false;
            MemberInfo primaryIndexMember = null;

            string tableName = GetTableOrViewName(objType);
            var    ds        = new DataSet();
            var    table     = new System.Data.DataTable(tableName);

            MemberInfo[] myMembers = objType.GetMembers();

            for (int i = 0; i < myMembers.Length; i++)
            {
                //object[] myAttributes = myMembers[i].GetCustomAttributes(true);
                //object[] myAttributes = myMembers[i].GetCustomAttributes(typeof(DOL.Database.Attributes.DataElement), true);

                object[] myAttributes = myMembers[i].GetCustomAttributes(typeof(PrimaryKey), true);

                if (myAttributes.Length > 0)
                {
                    primaryKeySpecified = true;
                    if (myMembers[i] is PropertyInfo)
                    {
                        table.Columns.Add(myMembers[i].Name, ((PropertyInfo)myMembers[i]).PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(myMembers[i].Name, ((FieldInfo)myMembers[i]).FieldType);
                    }

                    table.Columns[myMembers[i].Name].AutoIncrement = ((PrimaryKey)myAttributes[0]).AutoIncrement;

                    useAutoIncrementColumn = table.Columns[myMembers[i].Name].AutoIncrement;

                    var index = new DataColumn[1];
                    index[0]           = table.Columns[myMembers[i].Name];
                    primaryIndexMember = myMembers[i];
                    table.PrimaryKey   = index;
                    continue;
                }

                myAttributes = myMembers[i].GetCustomAttributes(typeof(DataElement), true);

                if (myAttributes.Length > 0)
                {
                    //if(myAttributes[0] is Attributes.DataElement)
                    //{
                    if (myMembers[i] is PropertyInfo)
                    {
                        table.Columns.Add(myMembers[i].Name, ((PropertyInfo)myMembers[i]).PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(myMembers[i].Name, ((FieldInfo)myMembers[i]).FieldType);
                    }

                    table.Columns[myMembers[i].Name].AllowDBNull = ((DataElement)myAttributes[0]).AllowDbNull;
                    if (((DataElement)myAttributes[0]).Unique)
                    {
                        table.Constraints.Add(new UniqueConstraint("UNIQUE_" + myMembers[i].Name, table.Columns[myMembers[i].Name]));
                    }

                    if (((DataElement)myAttributes[0]).IndexColumns != string.Empty)
                    {
                        table.Columns[myMembers[i].Name].ExtendedProperties.Add("INDEX", true);
                        table.Columns[myMembers[i].Name].ExtendedProperties.Add("INDEXCOLUMNS", ((DataElement)myAttributes[0]).IndexColumns);
                    }
                    else if (((DataElement)myAttributes[0]).Index)
                    {
                        table.Columns[myMembers[i].Name].ExtendedProperties.Add("INDEX", true);
                    }

                    if (((DataElement)myAttributes[0]).Varchar > 0)
                    {
                        table.Columns[myMembers[i].Name].ExtendedProperties.Add("VARCHAR", ((DataElement)myAttributes[0]).Varchar);
                    }

                    //if(myAttributes[0] is Attributes.PrimaryKey)
                    myAttributes = GetRelationAttributes(myMembers[i]);

                    //if(myAttributes[0] is Attributes.Relation)
                    if (myAttributes.Length > 0)
                    {
                        relations = true;
                    }
                }
            }

            if (useAutoIncrementColumn == false)
            {
                // We define the Tablename_ID column that will always contain a generated unique ID

                DataColumn idColumn = table.Columns.Add(tableName + "_ID", typeof(string));

                if (primaryKeySpecified)
                {
                    // if another primary key is defined on this table but the TableName_ID column is still being used then force
                    // the creation of a unique index on the the TableName_ID column
                    table.Constraints.Add(new UniqueConstraint(idColumn.ColumnName, idColumn));
                }
            }

            if (primaryKeySpecified == false)
            {
                var index = new DataColumn[1];
                index[0]         = table.Columns[tableName + "_ID"];
                table.PrimaryKey = index;
            }

            if (Connection.IsSQLConnection)
            {
                Connection.CheckOrCreateTable(table);
            }
            else
            {
                if (!System.IO.File.Exists(Connection.connString + tableName + ".xml"))
                {
                    Connection.SaveDataSet(tableName, ds);
                }
            }

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

            var dth = new DataTableHandler(ds);

            dth.HasRelations   = relations;
            dth.UsesPreCaching = DataObject.GetPreCachedFlag(objType);

            TableDatasets.Add(tableName, dth);

            //if (dth.UsesPreCaching && Connection.IsSQLConnection)
            //{
            //    // not useful for xml connection
            //    if (Log.IsDebugEnabled)
            //        Log.Debug("Precaching of " + table.TableName + "...");

            //    var objects = SQLSelectObjects<TObject>("");

            //    object key;
            //    for (int i = 0; i < objects.Length; i++)
            //    {
            //        key = null;
            //        if (primaryIndexMember == null)
            //        {
            //            key = objects[i].ObjectId;
            //        }
            //        else
            //        {
            //            if (primaryIndexMember is PropertyInfo)
            //            {
            //                key = ((PropertyInfo) primaryIndexMember).GetValue(objects[i], null);
            //            }
            //            else if (primaryIndexMember is FieldInfo)
            //            {
            //                key = ((FieldInfo) primaryIndexMember).GetValue(objects[i]);
            //            }
            //        }
            //        if (key != null)
            //        {
            //            dth.SetPreCachedObject(key, objects[i]);
            //        }
            //        else
            //        {
            //            if (Log.IsErrorEnabled)
            //                Log.Error("Primary key is null! " + ((primaryIndexMember != null) ? primaryIndexMember.Name : ""));
            //        }
            //    }

            //    if (Log.IsDebugEnabled)
            //        Log.Debug("Precaching of " + table.TableName + " finished!");
            //}
        }
Пример #2
0
 // Supprime un objet de la database
 protected abstract void DeleteObjectImpl(DataObject dataObject);
Пример #3
0
        protected void DeleteFromCache(string tableName, DataObject obj)
        {
            DataTableHandler handler = TableDatasets[tableName];

            handler.SetCacheObject(obj.ObjectId, null);
        }
Пример #4
0
        // Persiste l'objet dans la DB
        protected override void SaveObjectImpl(DataObject dataObject)
        {
            try
            {
                string tableName = dataObject.TableName;

                var sb = new StringBuilder("UPDATE `" + tableName + "` SET ");

                BindingInfo[] bindingInfo  = GetBindingInfo(dataObject.GetType());
                bool          hasRelations = false;
                bool          first        = true;
                string        dateFormat   = Connection.GetDBDateFormat();

                for (int i = 0; i < bindingInfo.Length; i++)
                {
                    BindingInfo bind = bindingInfo[i];

                    if (bind.ReadOnly)
                    {
                        continue;
                    }

                    if (!hasRelations)
                    {
                        hasRelations = bind.HasRelation;
                    }
                    if (!bind.HasRelation)
                    {
                        object val = null;
                        if (bind.Member is PropertyInfo)
                        {
                            val = ((PropertyInfo)bind.Member).GetValue(dataObject, null);
                        }
                        else if (bind.Member is FieldInfo)
                        {
                            val = ((FieldInfo)bind.Member).GetValue(dataObject);
                        }
                        else
                        {
                            continue;
                        }

                        if (!first)
                        {
                            sb.Append(", ");
                        }
                        else
                        {
                            first = false;
                        }

                        val = ConvertVal(val, dateFormat);

                        sb.Append("`" + bind.Member.Name + "` = ");
                        sb.Append('\'');
                        sb.Append(val);
                        sb.Append('\'');
                    }
                }

                sb.Append(" WHERE `" + tableName + "_ID` = '" + Escape(dataObject.ObjectId) + "'");

                string sql = sb.ToString();
                Log.Debug("MysqlObject", sql);

                int res = Connection.ExecuteNonQuery(sql);
                if (res == 0)
                {
                    Log.Error("MysqlObject", "Modify error : " + dataObject.TableName + " ID=" + dataObject.ObjectId + " --- keyvalue changed? " + sql + " " + Environment.StackTrace);
                    return;
                }

                if (hasRelations)
                {
                    SaveObjectRelations(dataObject);
                }

                dataObject.Dirty   = false;
                dataObject.IsValid = true;
            }
            catch (Exception e)
            {
                Log.Error("MysqlObject", "Modify error : " + dataObject.TableName + " " + dataObject.ObjectId + e.ToString());
            }
        }
Пример #5
0
 // Sauvegarde un Objet dans la Database
 protected abstract void SaveObjectImpl(DataObject dataObject);
Пример #6
0
 /// <summary>
 /// Deletes an object from the database.
 /// </summary>
 /// <param name="dataObject">the object to delete from the database</param>
 protected abstract bool DeleteObjectImpl(DataObject dataObject);
Пример #7
0
        protected void DeleteObjectRelations(DataObject dataObject)
        {
            try
            {
                object val;

                Type myType = dataObject.GetType();

                MemberInfo[] myMembers = myType.GetMembers();

                for (int i = 0; i < myMembers.Length; i++)
                {
                    Relation[] myAttributes = GetRelationAttributes(myMembers[i]);
                    if (myAttributes.Length > 0)
                    {
                        //if(myAttributes[0] is Attributes.Relation)
                        //{
                        if (myAttributes[0].AutoDelete == false)
                        {
                            continue;
                        }

                        bool array = false;

                        Type type;

                        if (myMembers[i] is PropertyInfo)
                        {
                            type = ((PropertyInfo)myMembers[i]).PropertyType;
                        }
                        else
                        {
                            type = ((FieldInfo)myMembers[i]).FieldType;
                        }

                        if (type.HasElementType)
                        {
                            type  = type.GetElementType();
                            array = true;
                        }

                        val = null;

                        if (array)
                        {
                            if (myMembers[i] is PropertyInfo)
                            {
                                val = ((PropertyInfo)myMembers[i]).GetValue(dataObject, null);
                            }
                            if (myMembers[i] is FieldInfo)
                            {
                                val = ((FieldInfo)myMembers[i]).GetValue(dataObject);
                            }
                            if (val is Array)
                            {
                                var a = val as Array;

                                foreach (object o in a)
                                {
                                    if (o is DataObject)
                                    {
                                        DeleteObject(o as DataObject);
                                    }
                                }
                            }
                            else
                            {
                                if (val is DataObject)
                                {
                                    DeleteObject(val as DataObject);
                                }
                            }
                        }
                        else
                        {
                            if (myMembers[i] is PropertyInfo)
                            {
                                val = ((PropertyInfo)myMembers[i]).GetValue(dataObject, null);
                            }
                            if (myMembers[i] is FieldInfo)
                            {
                                val = ((FieldInfo)myMembers[i]).GetValue(dataObject);
                            }
                            if (val != null && val is DataObject)
                            {
                                DeleteObject(val as DataObject);
                            }
                        }
                        //}
                    }
                }
            }
            catch (Exception e)
            {
                throw new DatabaseException("Resolving Relations failed !", e);
            }
        }
Пример #8
0
        // Sélectionne tous les objets d'une table
        protected override DataObject[] SelectObjectsImpl(Type objectType, string whereClause, IsolationLevel isolation)
        {
            string tableName   = GetTableOrViewName(objectType);
            var    dataObjects = new List <DataObject>(64);
            bool   useObjectID = TableDatasets[tableName].RequiresObjectId;

            // build sql command
            var sb = new StringBuilder("SELECT ");

            if (useObjectID)
            {
                sb.Append("[" + tableName + "_ID], ");
            }

            bool first = true;

            BindingInfo[] bindingInfo = GetBindingInfo(objectType);
            foreach (BindingInfo bind in bindingInfo)
            {
                if (!bind.HasRelation)
                {
                    if (!first)
                    {
                        sb.Append(", ");
                    }
                    else
                    {
                        first = false;
                    }
                    sb.Append("[" + bind.Member.Name + "]");
                }
            }

            sb.Append(" FROM [" + Connection.SchemaName + "].[" + tableName + "]");

            if (whereClause != null && whereClause.Trim().Length > 0)
            {
                sb.Append(" WHERE " + whereClause);
            }

            string sql = sb.ToString();

            Log.Debug("mssqlObject", "DataObject[] SelectObjectsImpl: " + sql);

            int objCount = 0;

            // read data and fill objects
            Connection.ExecuteSelect(sql, (reader) =>
            {
                var data = new object[reader.FieldCount];
                while (reader.Read())
                {
                    objCount++;

                    reader.GetValues(data);

                    DataObject obj = Activator.CreateInstance(objectType) as DataObject;

                    int field = 0;

                    // ObjectID field is in use, so we need to get rid of the first data value and start from index 1 when reading later.
                    if (useObjectID)
                    {
                        ++field;
                        if (data[0] != null)
                        {
                            obj.ObjectId = (string)data[0];
                        }
                    }
                    bool hasRelations = false;

                    // we can use hard index access because we iterate the same order here
                    foreach (BindingInfo bind in bindingInfo)
                    {
                        if (!hasRelations)
                        {
                            hasRelations = bind.HasRelation;
                        }

                        if (!bind.HasRelation)
                        {
                            object val = data[field++];
                            if (val != null && !val.GetType().IsInstanceOfType(DBNull.Value))
                            {
                                Exception e = GetVal(obj, bind.Member, val);
                                if (e != null)
                                {
                                    Log.Error("mssqlObject",
                                              tableName + ": " + bind.Member.Name + " = " + val.GetType().FullName +
                                              " doesnt fit to " + bind.Member.DeclaringType.FullName + e);
                                }
                            }
                        }
                    }

                    dataObjects.Add(obj);
                    obj.Dirty = false;

                    if (hasRelations)
                    {
                        FillLazyObjectRelations(obj, true);
                    }

                    obj.IsValid  = true;
                    obj.AllowAdd = false; // Exists already
                }
            }
                                     , isolation);

            return(dataObjects.ToArray());
        }
Пример #9
0
 public void SetCacheObject(object key, DataObject obj)
 {
     _cache[key] = obj;
 }
Пример #10
0
        private string FormulateInsert(DataObject dataObject, out bool hasRelations)
        {
            string tableName = dataObject.TableName;

            var columns = new StringBuilder();
            var values  = new StringBuilder();

            BindingInfo[] bindInfos = GetBindingInfo(dataObject.GetType());

            hasRelations = false;
            bool   first      = true;
            string dateFormat = Connection.GetDBDateFormat();

            if (TableDatasets[tableName].RequiresObjectId)
            {
                first = false;

                if (dataObject.ObjectId == null)
                {
                    dataObject.ObjectId = IDGenerator.GenerateID();
                }

                columns.Append("[" + tableName + "_ID]");
                values.Append("'" + Escape(dataObject.ObjectId) + "'");
            }

            foreach (BindingInfo bindInfo in bindInfos)
            {
                if (bindInfo.HasRelation)
                {
                    hasRelations = true;
                }

                // Check whether or not this class member is a primary key or a data element
                // and is thus valid for saving
                if (bindInfo.PrimaryKey || bindInfo.DataElementAttribute != null)
                {
                    object val = null;
                    if (bindInfo.Member is PropertyInfo)
                    {
                        val = ((PropertyInfo)bindInfo.Member).GetValue(dataObject, null);
                    }
                    else if (bindInfo.Member is FieldInfo)
                    {
                        val = ((FieldInfo)bindInfo.Member).GetValue(dataObject);
                    }

                    if (!first)
                    {
                        columns.Append(", ");
                        values.Append(", ");
                    }
                    else
                    {
                        first = false;
                    }

                    columns.Append("[" + bindInfo.Member.Name + "]");

                    val = ConvertVal(val, dateFormat);

                    values.Append('\'');
                    values.Append(val);
                    values.Append('\'');
                }
            }

            return("INSERT INTO [" + Connection.SchemaName + "].[" + tableName + "] (" + columns + ") VALUES (" + values + ")");
        }
Пример #11
0
        private string FormulateUpdate(DataObject dataObject, out bool hasRelations)
        {
            string tableName = dataObject.TableName;

            _opBuilder.Clear();
            _opBuilder.Append("UPDATE [" + Connection.SchemaName + "].[" + tableName + "] SET ");

            _whereBuilder.Clear();
            _whereBuilder.Append(" WHERE ");

            BindingInfo[] bindInfos = GetBindingInfo(dataObject.GetType());
            hasRelations = false;
            bool   first      = true;
            bool   firstPK    = true;
            string dateFormat = Connection.GetDBDateFormat();

            foreach (BindingInfo bind in bindInfos)
            {
                if (bind.ReadOnly)
                {
                    continue;
                }

                if (!hasRelations)
                {
                    hasRelations = bind.HasRelation;
                }

                // Add PKs to the WHERE clause.
                if (bind.PrimaryKey)
                {
                    object val;
                    if (bind.Member is PropertyInfo)
                    {
                        val = ((PropertyInfo)bind.Member).GetValue(dataObject, null);
                    }
                    else if (bind.Member is FieldInfo)
                    {
                        val = ((FieldInfo)bind.Member).GetValue(dataObject);
                    }
                    else
                    {
                        continue;
                    }

                    if (!firstPK)
                    {
                        _whereBuilder.Append(" AND ");
                    }
                    else
                    {
                        firstPK = false;
                    }

                    val = ConvertVal(val, dateFormat);

                    _whereBuilder.Append("[" + bind.Member.Name + "] = ");
                    _whereBuilder.Append('\'');
                    _whereBuilder.Append(val);
                    _whereBuilder.Append('\'');
                }

                // Add other elements to the SET clause.
                else if (!bind.HasRelation)
                {
                    object val;
                    if (bind.Member is PropertyInfo)
                    {
                        val = ((PropertyInfo)bind.Member).GetValue(dataObject, null);
                    }
                    else if (bind.Member is FieldInfo)
                    {
                        val = ((FieldInfo)bind.Member).GetValue(dataObject);
                    }
                    else
                    {
                        continue;
                    }

                    if (!first)
                    {
                        _opBuilder.Append(", ");
                    }
                    else
                    {
                        first = false;
                    }

                    val = ConvertVal(val, dateFormat);

                    _opBuilder.Append("[" + bind.Member.Name + "] = ");
                    _opBuilder.Append('\'');
                    _opBuilder.Append(val);
                    _opBuilder.Append('\'');
                }
            }

            // Object has no PKs within its elements, so use the ObjectId to target the save.
            if (firstPK)
            {
                _whereBuilder.Append($"[{tableName}_ID] = '{Escape(dataObject.ObjectId)}'");
            }

            _opBuilder.Append(_whereBuilder);

            return(_opBuilder.ToString());
        }
Пример #12
0
 public void SetPreCachedObject(object key, DataObject obj)
 {
     _precache[key] = obj;
 }
Пример #13
0
 public void SetCacheObject(object key, DataObject obj)
 {
     _cache[key] = obj;
 }
Пример #14
0
        // Ajoute un nouvel objet a la DB
        protected override bool AddObjectImpl(DataObject dataObject)
        {
            try
            {
                string tableName = dataObject.TableName;

                if (dataObject.ObjectId == null)
                {
                    dataObject.ObjectId = IDGenerator.GenerateID();
                }

                var columns = new StringBuilder();
                var values  = new StringBuilder();

                MemberInfo[] objMembers   = dataObject.GetType().GetMembers();
                bool         hasRelations = false;
                string       dateFormat   = Connection.GetDBDateFormat();

                columns.Append("`" + tableName + "_ID`");
                values.Append("'" + Escape(dataObject.ObjectId) + "'");

                for (int i = 0; i < objMembers.Length; i++)
                {
                    if (!hasRelations)
                    {
                        object[] relAttrib = GetRelationAttributes(objMembers[i]);
                        hasRelations = relAttrib.Length > 0;
                    }
                    object[] keyAttrib = objMembers[i].GetCustomAttributes(typeof(PrimaryKey), true);
                    object[] attrib    = objMembers[i].GetCustomAttributes(typeof(DataElement), true);
                    if (attrib.Length > 0 || keyAttrib.Length > 0)
                    {
                        object val = null;
                        if (objMembers[i] is PropertyInfo)
                        {
                            val = ((PropertyInfo)objMembers[i]).GetValue(dataObject, null);
                        }
                        else if (objMembers[i] is FieldInfo)
                        {
                            val = ((FieldInfo)objMembers[i]).GetValue(dataObject);
                        }

                        columns.Append(", ");
                        values.Append(", ");
                        columns.Append("`" + objMembers[i].Name + "`");

                        val = ConvertVal(val, dateFormat);

                        values.Append('\'');
                        values.Append(val);
                        values.Append('\'');
                    }
                }

                string sql = "INSERT INTO `" + tableName + "` (" + columns + ") VALUES (" + values + ")";

                Log.Debug("MysqlObject", sql);

                int res = Connection.ExecuteNonQuery(sql);
                if (res == 0)
                {
                    Log.Error("MysqlObject", "Add Error : " + dataObject.TableName + " ID=" + dataObject.ObjectId + "Query = " + sql);
                    return(false);
                }

                if (hasRelations)
                {
                    SaveObjectRelations(dataObject);
                }

                dataObject.Dirty     = false;
                dataObject.IsValid   = true;
                dataObject.IsDeleted = false;

                return(true);
            }
            catch (Exception e)
            {
                Log.Error("MysqlObject", "Add Error : " + dataObject.TableName + " " + dataObject.ObjectId + e.ToString());
            }

            return(false);
        }
Пример #15
0
 /// <summary>
 /// Adds a new object to the database.
 /// </summary>
 /// <param name="dataObject">the object to add to the database</param>
 /// <returns>true if the object was added successfully; false otherwise</returns>
 protected abstract bool AddObjectImpl(DataObject dataObject);
Пример #16
0
 public void SetPreCachedObject(object key, DataObject obj)
 {
     _precache[key] = obj;
 }
Пример #17
0
 /// <summary>
 /// Persists an object to the database.
 /// </summary>
 /// <param name="dataObject">the object to save to the database</param>
 protected abstract bool SaveObjectImpl(DataObject dataObject);
Пример #18
0
        public void RegisterDataObject(Type objType)
        {
            if (TableDatasets.ContainsKey(GetTableOrViewName(objType)))
            {
                return;
            }

            bool relations = false;

            string tableName = GetTableOrViewName(objType);
            var    table     = new System.Data.DataTable(tableName);

            List <string> primaryKeys = new List <string>();

            MemberInfo[] myMembers = objType.GetMembers();

            // Load the primary keys and data elements into the data table
            foreach (MemberInfo memberInfo in myMembers)
            {
                object[] myAttributes = memberInfo.GetCustomAttributes(typeof(PrimaryKey), true);

                if (myAttributes.Length > 0)
                {
                    if (memberInfo is PropertyInfo)
                    {
                        table.Columns.Add(memberInfo.Name, ((PropertyInfo)memberInfo).PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(memberInfo.Name, ((FieldInfo)memberInfo).FieldType);
                    }

                    table.Columns[memberInfo.Name].AutoIncrement     = ((PrimaryKey)myAttributes[0]).AutoIncrement;
                    table.Columns[memberInfo.Name].AutoIncrementSeed = ((PrimaryKey)myAttributes[0]).IncrementValue;

                    primaryKeys.Add(memberInfo.Name);
                    continue;
                }

                myAttributes = memberInfo.GetCustomAttributes(typeof(DataElement), true);

                if (myAttributes.Length > 0)
                {
                    if (memberInfo is PropertyInfo)
                    {
                        table.Columns.Add(memberInfo.Name, ((PropertyInfo)memberInfo).PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(memberInfo.Name, ((FieldInfo)memberInfo).FieldType);
                    }

                    table.Columns[memberInfo.Name].AllowDBNull = ((DataElement)myAttributes[0]).AllowDbNull;

                    if (((DataElement)myAttributes[0]).Unique)
                    {
                        table.Constraints.Add(new UniqueConstraint("UNIQUE_" + memberInfo.Name, table.Columns[memberInfo.Name]));
                    }

                    if (((DataElement)myAttributes[0]).Index)
                    {
                        table.Columns[memberInfo.Name].ExtendedProperties.Add("INDEX", true);
                    }

                    if (((DataElement)myAttributes[0]).Varchar > 0)
                    {
                        table.Columns[memberInfo.Name].ExtendedProperties.Add("VARCHAR", ((DataElement)myAttributes[0]).Varchar);
                    }

                    if (!relations)
                    {
                        relations = GetRelationAttributes(memberInfo).Length > 0;
                    }
                }
            }

            if (primaryKeys.Count > 0)
            {
                var index = new DataColumn[primaryKeys.Count];
                for (int i = 0; i < primaryKeys.Count; ++i)
                {
                    index[i] = table.Columns[primaryKeys[i]];
                }
                table.PrimaryKey = index;
            }

            else // Add a primary key column to use
            {
                table.Columns.Add(tableName + "_ID", typeof(string));

                DataColumn[] index = new DataColumn[1];
                index[0]         = table.Columns[tableName + "_ID"];
                table.PrimaryKey = index;
            }

            if (Connection.IsSQLConnection)
            {
                Connection.CheckOrCreateTable(table);
            }

            DataSet dataSet = new DataSet
            {
                DataSetName        = tableName,
                EnforceConstraints = true,
                CaseSensitive      = false
            };

            dataSet.Tables.Add(table);

            DataTableHandler dataTableHandler = new DataTableHandler(dataSet)
            {
                HasRelations     = relations,
                UsesPreCaching   = DataObject.GetPreCachedFlag(objType),
                RequiresObjectId = primaryKeys.Count == 0,
                BindingMethod    = DataObject.GetBindingMethod(objType)
            };

            TableDatasets.Add(tableName, dataTableHandler);
        }
Пример #19
0
 public void FillObjectRelations(DataObject dataObject)
 {
     FillLazyObjectRelations(dataObject, false);
 }
Пример #20
0
        public void RegisterDataObject(Type objType)
        {
            if (TableDatasets.ContainsKey(GetTableOrViewName(objType)))
            {
                return;
            }

            bool       primary            = false;
            bool       relations          = false;
            MemberInfo primaryIndexMember = null;

            string tableName = GetTableOrViewName(objType);
            var    ds        = new DataSet();
            var    table     = new System.Data.DataTable(tableName);

            table.Columns.Add(tableName + "_ID", typeof(string));

            MemberInfo[] myMembers = objType.GetMembers();

            for (int i = 0; i < myMembers.Length; i++)
            {
                object[] myAttributes = myMembers[i].GetCustomAttributes(typeof(PrimaryKey), true);

                if (myAttributes.Length > 0)
                {
                    primary = true;
                    if (myMembers[i] is PropertyInfo)
                    {
                        table.Columns.Add(myMembers[i].Name, ((PropertyInfo)myMembers[i]).PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(myMembers[i].Name, ((FieldInfo)myMembers[i]).FieldType);
                    }

                    table.Columns[myMembers[i].Name].AutoIncrement     = ((PrimaryKey)myAttributes[0]).AutoIncrement;
                    table.Columns[myMembers[i].Name].AutoIncrementSeed = ((PrimaryKey)myAttributes[0]).IncrementValue;

                    var index = new DataColumn[1];
                    index[0]           = table.Columns[myMembers[i].Name];
                    primaryIndexMember = myMembers[i];
                    table.PrimaryKey   = index;
                    continue;
                }

                myAttributes = myMembers[i].GetCustomAttributes(typeof(DataElement), true);

                if (myAttributes.Length > 0)
                {
                    if (myMembers[i] is PropertyInfo)
                    {
                        table.Columns.Add(myMembers[i].Name, ((PropertyInfo)myMembers[i]).PropertyType);
                    }
                    else
                    {
                        table.Columns.Add(myMembers[i].Name, ((FieldInfo)myMembers[i]).FieldType);
                    }

                    table.Columns[myMembers[i].Name].AllowDBNull = ((DataElement)myAttributes[0]).AllowDbNull;
                    if (((DataElement)myAttributes[0]).Unique)
                    {
                        table.Constraints.Add(new UniqueConstraint("UNIQUE_" + myMembers[i].Name, table.Columns[myMembers[i].Name]));
                    }
                    if (((DataElement)myAttributes[0]).Index)
                    {
                        table.Columns[myMembers[i].Name].ExtendedProperties.Add("INDEX", true);
                    }
                    if (((DataElement)myAttributes[0]).Varchar > 0)
                    {
                        table.Columns[myMembers[i].Name].ExtendedProperties.Add("VARCHAR", ((DataElement)myAttributes[0]).Varchar);
                    }

                    myAttributes = GetRelationAttributes(myMembers[i]);

                    if (myAttributes.Length > 0)
                    {
                        relations = true;
                    }
                }
            }

            if (primary == false)
            {
                var index = new DataColumn[1];
                index[0]         = table.Columns[tableName + "_ID"];
                table.PrimaryKey = index;
            }

            if (Connection.IsSQLConnection)
            {
                Connection.CheckOrCreateTable(table);
            }

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

            var dth = new DataTableHandler(ds);

            dth.HasRelations   = relations;
            dth.UsesPreCaching = DataObject.GetPreCachedFlag(objType);

            TableDatasets.Add(tableName, dth);
        }
Пример #21
0
        protected void FillLazyObjectRelations(DataObject dataObject, bool autoload)
        {
            try
            {
                var dataObjectType = dataObject.GetType();

                MemberInfo[] myMembers;
                if (!MemberInfoCache.TryGetValue(dataObjectType, out myMembers))
                {
                    myMembers = dataObjectType.GetMembers();
                    MemberInfoCache[dataObjectType] = myMembers;
                }

                for (int i = 0; i < myMembers.Length; i++)
                {
                    Relation[] myAttributes = GetRelationAttributes(myMembers[i]);

                    if (myAttributes.Length > 0)
                    {
                        Relation rel = myAttributes[0];

                        if ((rel.AutoLoad == false) && autoload)
                        {
                            continue;
                        }

                        bool         isArray = false;
                        Type         remoteType;
                        DataObject[] elements;

                        string local  = rel.LocalField;
                        string remote = rel.RemoteField;

                        if (myMembers[i] is PropertyInfo)
                        {
                            remoteType = ((PropertyInfo)myMembers[i]).PropertyType;
                        }
                        else
                        {
                            remoteType = ((FieldInfo)myMembers[i]).FieldType;
                        }

                        if (remoteType.HasElementType)
                        {
                            remoteType = remoteType.GetElementType();
                            isArray    = true;
                        }

                        PropertyInfo prop  = dataObjectType.GetProperty(local);
                        FieldInfo    field = dataObjectType.GetField(local);

                        object val = 0;

                        if (prop != null)
                        {
                            val = prop.GetValue(dataObject, null);
                        }
                        if (field != null)
                        {
                            val = field.GetValue(dataObject);
                        }

                        if (val != null && val.ToString() != string.Empty)
                        {
                            if (DataObject.GetPreCachedFlag(remoteType))
                            {
                                elements    = new DataObject[1];
                                elements[0] = FindObjectByKeyImpl(remoteType, val);
                            }
                            else
                            {
                                elements = SelectObjectsImpl(remoteType, remote + " = '" + Escape(val.ToString()) + "'", IsolationLevel.DEFAULT);
                            }

                            if ((elements != null) && (elements.Length > 0))
                            {
                                if (isArray)
                                {
                                    if (myMembers[i] is PropertyInfo)
                                    {
                                        ((PropertyInfo)myMembers[i]).SetValue(dataObject, elements, null);
                                    }
                                    if (myMembers[i] is FieldInfo)
                                    {
                                        var             currentField = (FieldInfo)myMembers[i];
                                        ConstructorInfo constructor;
                                        if (!ConstructorByFieldType.TryGetValue(currentField.FieldType, out constructor))
                                        {
                                            constructor = currentField.FieldType.GetConstructor(new[] { typeof(int) });
                                            ConstructorByFieldType[currentField.FieldType] = constructor;
                                        }

                                        object elementHolder = constructor.Invoke(new object[] { elements.Length });
                                        var    elementArray  = (object[])elementHolder;

                                        for (int m = 0; m < elementArray.Length; m++)
                                        {
                                            elementArray[m] = elements[m];
                                        }

                                        currentField.SetValue(dataObject, elementArray);
                                    }
                                }
                                else
                                {
                                    if (myMembers[i] is PropertyInfo)
                                    {
                                        ((PropertyInfo)myMembers[i]).SetValue(dataObject, elements[0], null);
                                    }
                                    if (myMembers[i] is FieldInfo)
                                    {
                                        ((FieldInfo)myMembers[i]).SetValue(dataObject, elements[0]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new DatabaseException("Resolving Relations for " + dataObject.TableName + " failed!", e);
            }
        }
Пример #22
0
 public void SaveObject(DataObject Object)
 {
     CharactersDB.SaveObject(Object);
 }