internal void RecurSetPropertyValue(string internalFieldName, Connection conn, string queryFieldName, Table table) { sTable map = conn.Pool.Mapping[table.GetType()]; foreach (sTableField fld in map.Fields) { if (fld.Name == internalFieldName) { if (fld.ExternalField == null) table.SetField(fld.ClassProperty, conn[queryFieldName]); else if (fld.Type == FieldType.ENUM) { PropertyInfo pi = table.GetType().GetProperty(fld.ClassProperty, Utility._BINDING_FLAGS); if (pi == null) pi = table.GetType().GetProperty(fld.ClassProperty, Utility._BINDING_FLAGS_WITH_INHERITANCE); table.SetField(fld.ClassProperty, conn.GetEnum(pi.PropertyType, queryFieldName)); } else { if (table.GetField(fld.ClassProperty) == null) { PropertyInfo pi = table.GetType().GetProperty(fld.ClassProperty, Utility._BINDING_FLAGS); if (pi == null) pi = table.GetType().GetProperty(fld.ClassProperty, Utility._BINDING_FLAGS_WITH_INHERITANCE); Table t = (Table)pi.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]); t._loadStatus = LoadStatus.Partial; t = (Table)LazyProxy.Instance(t); table.SetField(fld.Name, t); } RecurSetPropertyValue(fld.ExternalField, conn, queryFieldName, (Table)table.GetField(fld.Name)); } break; } } }
private Table Insert(Table table,bool ignoreAutogen) { if (_readonly) throw new Exception("Unable to insert into a readonly database."); pool.Updater.InitType(table.GetType(), this); sTable map = pool.Mapping[table.GetType()]; if (!ignoreAutogen) { if (pool.Mapping.IsMappableType(table.GetType().BaseType)) { Table tblPar = (Table)table.ToType(table.GetType().BaseType, null); List<SelectParameter> tmpPars = new List<SelectParameter>(); sTable pMap = pool.Mapping[tblPar.GetType()]; foreach (string str in pMap.PrimaryKeyProperties) tmpPars.Add(new EqualParameter(str, tblPar.GetField(str))); List<Org.Reddragonit.Dbpro.Structure.Table> tmpTbls = Select(tblPar.GetType(), tmpPars.ToArray()); if (tmpTbls.Count > 0) { Table ta = tmpTbls[0]; Table orig = ta.LoadCopyOfOriginal(this); List<string> pProps = new List<string>(pMap.PrimaryKeyProperties); foreach (string prop in pMap.Properties) { if (!pProps.Contains(prop)) { ta.SetField(prop, tblPar.GetField(prop)); } } bool abort = false; ConnectionPoolManager.RunTriggers(this, orig, ta, ConnectionPoolManager.TriggerTypes.PRE_UPDATE, out abort); if (!abort) { tblPar = Update(ta); orig = tblPar.LoadCopyOfOriginal(this); ConnectionPoolManager.RunTriggers(this, orig, tblPar, ConnectionPoolManager.TriggerTypes.PRE_UPDATE, out abort); } } else { bool abort = false; ConnectionPoolManager.RunTriggers(this, null, tblPar, ConnectionPoolManager.TriggerTypes.PRE_INSERT, out abort); if (!abort) { tblPar = Insert(tblPar, ignoreAutogen); ConnectionPoolManager.RunTriggers(this, null, tblPar, ConnectionPoolManager.TriggerTypes.POST_INSERT, out abort); } } table.CopyValuesFrom(tblPar); } foreach (string prop in map.ForeignTableProperties) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (!Utility.IsEnum(pi.PropertyType)) { if (pi.PropertyType.IsArray) { Table[] vals = (Table[])table.GetField(prop); if (vals != null) { foreach (Table t in vals) this.Save(t); } } else { Table ext = (Table)table.GetField(prop); if (ext != null) { ext = Save(ext); table.SetField(prop, ext); } } } } foreach (string prop in map.ArrayProperties) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (!Utility.IsEnum(pi.PropertyType)) { if (Pool.Mapping.IsMappableType(pi.PropertyType.GetElementType())) { Table[] vals = (Table[])table.GetField(prop); if (vals != null) { if (vals != null) { foreach (Table t in vals) this.Save(t); } } } } } } string query = ""; List<IDbDataParameter> pars = new List<IDbDataParameter>(); if (ignoreAutogen) query = queryBuilder.InsertWithIdentity(table, out pars); else query = queryBuilder.Insert(table, out pars); ExecuteNonQuery(query,pars); if ((pars[pars.Count-1].Direction!=ParameterDirection.Input)&&(!ignoreAutogen)) { if (pars[pars.Count - 1].Value == DBNull.Value) { string parsError = ""; if (pars != null) { foreach (IDbDataParameter param in pars) { if (param.Value != null) parsError += param.ParameterName + ": " + param.Value.ToString() + "\n"; else parsError += param.ParameterName + ": NULL" + "\n"; } } throw new Exception("An error occured in executing the query: " + comm.CommandText + "\nwith the parameters: " + parsError); } table.SetField(map.AutoGenProperty, pars[pars.Count - 1].Value); } table._isSaved=true; table.LoadStatus= LoadStatus.Complete; foreach (string prop in map.ArrayProperties) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (pool.Mapping.IsMappableType(pi.PropertyType.GetElementType())) { Dictionary<string, List<List<IDbDataParameter>>> queries = queryBuilder.UpdateMapArray(table, prop, ignoreAutogen); if (queries != null) { foreach (string str in queries.Keys) { foreach (List<IDbDataParameter> p in queries[str]) ExecuteNonQuery(str, p); } } } else InsertArrayValue(table,pi,0,null, map, (Array)table.GetField(prop), prop, ignoreAutogen); } return table; }
//called to set values onto a table that is externally mapped to this current table //this is used through lazy loading proxies by only setting the primary key fields. private Table SetExternalValues(sTable map,string propertyName, Connection conn, out bool setValue,Table table) { setValue = false; sTable eMap = conn.Pool.Mapping[table.GetType()]; sTableField[] flds = map[propertyName]; List<string> fProps = new List<string>(eMap.ForeignTableProperties); Type ty = table.GetType().BaseType; while (conn.Pool.Mapping.IsMappableType(ty)) { foreach (string str in conn.Pool.Mapping[ty].ForeignTableProperties) { if (!fProps.Contains(str)) fProps.Add(str); } ty = ty.BaseType; } foreach (string prop in eMap.PrimaryKeyProperties) { if (fProps.Contains(prop) && !eMap.IsEnumProperty(prop)) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (pi == null) pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS_WITH_INHERITANCE); Table t = (Table)pi.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]); t._loadStatus = LoadStatus.Partial; t = (Table)LazyProxy.Instance(t); foreach (sTableField f in eMap[prop]) { foreach (sTableField fld in flds) { if (fld.ExternalField == f.Name) { if (conn.ContainsField(fld.Name) && !conn.IsDBNull(conn.GetOrdinal(fld.Name))) { RecurSetPropertyValue(f.ExternalField, conn, fld.Name, t); } break; } } } if (!t.AllPrimaryKeysNull) { t.InitPrimaryKeys(); table.SetField(prop, t); setValue = true; } } else { foreach (sTableField f in eMap[prop]) { foreach (sTableField fld in flds) { if (fld.ExternalField == f.Name) { if (conn.ContainsField(fld.Name)&&!conn.IsDBNull(conn.GetOrdinal(fld.Name))){ if (f.Type == FieldType.ENUM) table.SetField(prop, conn.Pool.GetEnumValue(table.GetType().GetProperty(f.ClassProperty,Utility._BINDING_FLAGS_WITH_INHERITANCE).PropertyType, (int)conn[fld.Name])); else table.SetField(prop, conn[fld.Name]); setValue = true; } break; } } } } } return table; }
private Table Update(Table table) { if (_readonly) throw new Exception("Unable to update to a readonly database."); if (table.ConnectionName!=ConnectionName) throw new Exception("Cannot update an entry into a table into the database connection that it was not specified for."); if ((table.ChangedFields==null)||(table.ChangedFields.Count==0)) return table; pool.Updater.InitType(table.GetType(), this); sTable map = Pool.Mapping[table.GetType()]; table._changedFields = table.ChangedFields; if (Pool.Mapping.IsMappableType(table.GetType().BaseType)) { Table ta = Update((Table)table.ToType(table.GetType().BaseType,null)); table.CopyValuesFrom(ta); } foreach (string prop in map.ForeignTableProperties) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (!Utility.IsEnum(pi.PropertyType.IsArray ? pi.PropertyType.GetElementType() : pi.PropertyType)) { if (pi.PropertyType.IsArray) { if (table.ChangedFields.Contains(prop)) { Table[] vals = (Table[])table.GetField(prop); bool changed = false; if (vals != null) { for (int x = 0; x < vals.Length; x++) { Table t = vals[x]; if (table._changedFields.Contains(prop) || t.LoadStatus == LoadStatus.NotLoaded || t.ChangedFields.Count > 0) { vals[x] = this.Save(t); changed = true; } } } if (changed) table.SetField(prop, vals); } } else { if (table.ChangedFields.Contains(prop)) { Table ext = (Table)table.GetField(prop); if (ext != null) { if (ext.LoadStatus == LoadStatus.NotLoaded || ext.ChangedFields.Count > 0) { ext = Save(ext); table.SetField(prop, ext); } } } } } } foreach (string prop in map.ArrayProperties) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (!Utility.IsEnum(pi.PropertyType)) { if (Pool.Mapping.IsMappableType(pi.PropertyType.GetElementType())) { if (table.ChangedFields.Contains(prop)) { Table[] vals = (Table[])table.GetField(prop); bool changed = false; if (vals != null) { for (int x = 0; x < vals.Length; x++) { Table t = vals[x]; if (t.LoadStatus == LoadStatus.NotLoaded || t.ChangedFields.Count > 0) { vals[x] = this.Save(t); changed = true; } } } if (changed) table.SetField(prop, vals); } } } } string query = ""; List<IDbDataParameter> pars = new List<IDbDataParameter>(); Org.Reddragonit.Dbpro.Structure.Attributes.Table tbl = (Org.Reddragonit.Dbpro.Structure.Attributes.Table)table.GetType().GetCustomAttributes(typeof(Org.Reddragonit.Dbpro.Structure.Attributes.Table),false)[0]; if (tbl.AlwaysInsert) query = queryBuilder.Insert(table, out pars); else query = queryBuilder.Update(table, out pars); if (query.Length > 0) { ExecuteNonQuery(query, pars); if ((pars[pars.Count-1].Direction!=ParameterDirection.Input) && (tbl.AlwaysInsert)) { table.SetField(map.AutoGenProperty, pars[pars.Count-1].Value); table._isSaved = true; table.LoadStatus = LoadStatus.Complete; } } if (table.ChangedFields != null) { foreach (string prop in map.Properties) { if (map.ArrayProperties.Contains(prop) && table.ChangedFields.Contains(prop)) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (pi == null) pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS_WITH_INHERITANCE); if (Pool.Mapping.IsMappableType(pi.PropertyType.GetElementType()) && !Utility.IsEnum(pi.PropertyType.GetElementType())) { Dictionary<string, List<List<IDbDataParameter>>> queries = queryBuilder.UpdateMapArray(table, prop, false); foreach (string str in queries.Keys) { foreach (List<IDbDataParameter> p in queries[str]) ExecuteNonQuery(str, p); } } else InsertArrayValue(table,pi,table.OriginalArrayLengths[prop],(table.ReplacedArrayIndexes.ContainsKey(prop) ? table.ReplacedArrayIndexes[prop] : (List<int>)null), map, (Array)table.GetField(prop), prop, false); } } } return table; }