protected void ReloadCache(string tableName) { DataTableHandler handler = TableDatasets[tableName]; ICache cache = handler.Cache; foreach (object o in cache.Keys) { ReloadObject(cache[o] as DataObject); } }
// Retourne l'objet a partir de sa primary key // Returns the object from its 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); }
// Met a jour ou ajoute un objet dans la DB // Updates or adds an object in the DB 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); }
protected void DeleteFromCache(string tableName, DataObject obj) { DataTableHandler handler = TableDatasets[tableName]; handler.SetCacheObject(obj.ObjectId, null); }
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); }