Пример #1
0
        protected void ReloadCache(string tableName)
        {
            DataTableHandler handler = TableDatasets[tableName];

            ICache cache = handler.Cache;

            foreach (object o in cache.Keys)
            {
                ReloadObject(cache[o] as DataObject);
            }
        }
Пример #2
0
        // Retourne l'objet a partir de sa primary key
        protected override TObject FindObjectByKeyImpl <TObject>(object key)
        {
            MemberInfo[] members = typeof(TObject).GetMembers();
            var          ret     = (TObject)Activator.CreateInstance(typeof(TObject));

            string           tableName   = ret.TableName;
            DataTableHandler dth         = TableDatasets[tableName];
            string           whereClause = null;

            if (dth.UsesPreCaching)
            {
                DataObject obj = dth.GetPreCachedObject(key);
                if (obj != null)
                {
                    return(obj as TObject);
                }
            }

            // Escape PK value
            key = Escape(key.ToString());

            for (int i = 0; i < members.Length; i++)
            {
                object[] keyAttrib = members[i].GetCustomAttributes(typeof(PrimaryKey), true);
                if (keyAttrib.Length > 0)
                {
                    whereClause = "[" + members[i].Name + "] = '" + key + "'";
                    break;
                }
            }

            if (whereClause == null)
            {
                whereClause = "[" + ret.TableName + "_ID] = '" + key + "'";
            }

            var objs = SelectObjectsImpl <TObject>(whereClause, IsolationLevel.DEFAULT);

            if (objs.Count > 0)
            {
                dth.SetPreCachedObject(key, objs[0]);
                return(objs[0]);
            }

            return(null);
        }
Пример #3
0
        /// <summary>
        /// Selects object from the db and updates or adds entry in the pre-cache
        /// </summary>
        /// <param name="objectType"></param>
        /// <param name="key"></param>
        public bool UpdateInCache <TObject>(object key)
            where TObject : DataObject
        {
            MemberInfo[] members = typeof(TObject).GetMembers();
            var          ret     = (TObject)Activator.CreateInstance(typeof(TObject));

            string           tableName   = ret.TableName;
            DataTableHandler dth         = TableDatasets[tableName];
            string           whereClause = null;

            if (!dth.UsesPreCaching || key == null)
            {
                return(false);
            }

            // Escape PK value
            key = Escape(key.ToString());

            for (int i = 0; i < members.Length; i++)
            {
                object[] keyAttrib = members[i].GetCustomAttributes(typeof(PrimaryKey), true);
                if (keyAttrib.Length > 0)
                {
                    whereClause = "`" + members[i].Name + "` = '" + key + "'";
                    break;
                }
            }

            if (whereClause == null)
            {
                whereClause = "`" + ret.TableName + "_ID` = '" + key + "'";
            }

            var objs = SelectObjects <TObject>(whereClause);

            if (objs.Count > 0)
            {
                dth.SetPreCachedObject(key, objs[0]);
                return(true);
            }

            return(false);
        }
Пример #4
0
        protected void DeleteFromCache(string tableName, DataObject obj)
        {
            DataTableHandler handler = TableDatasets[tableName];

            handler.SetCacheObject(obj.ObjectId, null);
        }
Пример #5
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!");
            //}
        }
Пример #6
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);
        }
Пример #7
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);
        }