public void Insert(int index, Table item) { throw new Exception("Paged Table List is ReadOnly."); }
internal Dictionary<string,List<List<IDbDataParameter>>> UpdateMapArray(Table table,string property,bool ignoreautogen) { Dictionary<string, List<List<IDbDataParameter>>> ret = new Dictionary<string, List<List<IDbDataParameter>>>(); try{ sTable tbl = _pool.Mapping[table.GetType()]; Table[] values = (Table[])table.GetField(property); if (values!=null) { sTable iTable = _pool.Mapping[table.GetType(), property]; string delString = "DELETE FROM " + iTable.Name + " WHERE "; string insertString = "INSERT INTO " + iTable.Name + "("; string valueString = "VALUES("; List<IDbDataParameter> pars = new List<IDbDataParameter>(); List<string> pkeys = new List<string>(tbl.PrimaryKeyFields); foreach (sTableField f in tbl.Fields) { if (pkeys.Contains(f.Name)) { foreach (sTableField fld in iTable.Fields) { if (Utility.StringsEqual(fld.ClassProperty, "PARENT")) { if (Utility.StringsEqual(fld.ExternalField, f.Name)) { delString += fld.Name + " = " + CreateParameterName(fld.Name) + " AND "; pars.Add(pool.CreateParameter(CreateParameterName(fld.Name), LocateFieldValue(table, f, _pool))); insertString += fld.Name + ","; valueString += CreateParameterName(fld.Name) + ","; break; } } } } } ret.Add(delString.Substring(0, delString.Length - 4),new List<List<IDbDataParameter>>(new List<IDbDataParameter>[]{new List<IDbDataParameter>(pars.ToArray())})); sTable relTable = _pool.Mapping[table.GetType().GetProperty(property, Utility._BINDING_FLAGS).PropertyType.GetElementType()]; foreach (string pkey in relTable.PrimaryKeyFields) { foreach (sTableField fld in iTable.Fields) { if (Utility.StringsEqual(fld.ClassProperty,"CHILD")) { if (Utility.StringsEqual(fld.ExternalField, pkey)) { insertString += fld.Name + ","; valueString += CreateParameterName(fld.Name) + ","; break; } } } } insertString = insertString.Substring(0, insertString.Length - 1) + (ignoreautogen ? "," + pool.Translator.GetIntermediateIndexFieldName(table.GetType(),table.GetType().GetProperty(property,Utility._BINDING_FLAGS)) : "") + ") " + valueString.Substring(0, valueString.Length - 1) + (ignoreautogen ? "," + CreateParameterName("index") : "") + ")"; ret.Add(insertString, new List<List<IDbDataParameter>>()); int index = 0; pkeys.Clear(); pkeys.AddRange(relTable.PrimaryKeyFields); foreach (Table t in values) { foreach (sTableField fld in relTable.Fields) { if (pkeys.Contains(fld.Name)) { foreach (sTableField f in iTable.Fields){ if (Utility.StringsEqual(f.ClassProperty, "CHILD")) { if (Utility.StringsEqual(f.ExternalField, fld.Name)) { for (int x = 0; x < pars.Count; x++) { if (pars[x].ParameterName == CreateParameterName(f.Name)) { pars.RemoveAt(x); break; } } object val = LocateFieldValue(t, fld, pool); if (val == null) pars.Add(pool.CreateParameter(CreateParameterName(f.Name), null, f.Type, f.Length)); else pars.Add(pool.CreateParameter(CreateParameterName(f.Name), val)); break; } } } } } if (ignoreautogen) { for (int x = 0; x < pars.Count; x++) { if (pars[x].ParameterName == CreateParameterName("index")) { pars.RemoveAt(x); break; } } pars.Add(pool.CreateParameter(CreateParameterName("index"), index, FieldType.INTEGER, 4)); } ret[insertString].Add(new List<IDbDataParameter>((this.pool is MsSqlConnectionPool ? ((MsSqlConnectionPool)pool).DuplicateParameters(pars).ToArray() : pars.ToArray()))); index++; } } }catch (Exception e) { Logger.LogLine(e.Message); return null; } return ret; }
public int IndexOf(Table item) { LoadToIndex(Count+1); return _data.IndexOf(item); }
internal string Insert(Table table,out List<IDbDataParameter> insertParameters) { sTable tbl = _pool.Mapping[table.GetType()]; insertParameters=new List<IDbDataParameter>(); List<string> fprops = new List<string>(tbl.ForeignTableProperties); try{ string values=""; string parameters=""; foreach (string prop in tbl.Properties) { sTableField[] flds = tbl[prop]; if (flds.Length > 0) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (pi == null) pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS_WITH_INHERITANCE); if ((fprops.Contains(prop)||pool.Mapping.IsMappableType(pi.PropertyType)) && !Utility.IsEnum(pi.PropertyType)) { Table eTable = (Table)table.GetField(prop); if (eTable == null) { foreach (sTableField fld in flds) { values += fld.Name + ","; parameters += "," + CreateParameterName(fld.Name); insertParameters.Add(pool.CreateParameter(CreateParameterName(fld.Name), null, fld.Type, fld.Length)); } } else { foreach (sTableField fld in flds) { values += fld.Name + ","; parameters += "," + CreateParameterName(fld.Name); } Type etype = pi.PropertyType; while (true) { sTable etbl = _pool.Mapping[etype]; foreach (sTableField fld in flds) { foreach (sTableField efld in etbl.Fields) { if (fld.ExternalField == efld.Name) { insertParameters.Add(pool.CreateParameter(CreateParameterName(fld.Name),QueryBuilder.LocateFieldValue(eTable,efld,pool))); break; } } } etype = etype.BaseType; if (etype.Equals(typeof(Table))) break; } } } else if (!Utility.StringsEqual(prop,tbl.AutoGenProperty) && flds[0].ComputedCode==null) { values += flds[0].Name + ","; parameters += "," + CreateParameterName(flds[0].Name); if (table.IsFieldNull(prop)) insertParameters.Add(pool.CreateParameter(CreateParameterName(flds[0].Name), null, flds[0].Type, flds[0].Length)); else { if (flds[0].Type == FieldType.ENUM) insertParameters.Add(pool.CreateParameter(CreateParameterName(flds[0].Name), pool.GetEnumID(table.GetType().GetProperty(prop, Utility._BINDING_FLAGS_WITH_INHERITANCE).PropertyType, table.GetField(prop).ToString()))); else insertParameters.Add(pool.CreateParameter(CreateParameterName(flds[0].Name), table.GetField(prop), flds[0].Type, flds[0].Length)); } } } } values=values.Substring(0,values.Length-1); parameters=parameters.Substring(1); if (tbl.AutoGenProperty != null) { string select = ""; for (int x = 0; x < insertParameters.Count; x++) { for (int y = x + 1; y < insertParameters.Count; y++) { if (insertParameters[x].ParameterName == insertParameters[y].ParameterName) { insertParameters.RemoveAt(y); y--; } } } insertParameters.Add(pool.CreateParameter(CreateParameterName(tbl.AutoGenField), table.GetField(tbl.AutoGenProperty), tbl[tbl.AutoGenProperty][0].Type, tbl[tbl.AutoGenProperty][0].Length)); if (tbl[tbl.AutoGenProperty][0].Type == FieldType.STRING) insertParameters[insertParameters.Count - 1].Size = int.MaxValue; select = _GenerateAutogenIDQuery(tbl, ref insertParameters); if (pool is MsSqlConnectionPool && select.StartsWith("OUTPUT")) return string.Format(InsertString, tbl.Name, values, parameters).Replace(" VALUES ", " " + select + " VALUES "); else return string.Format(InsertString, tbl.Name, values, parameters) + " " + select; } return string.Format(InsertString,tbl.Name,values,parameters); }catch (Exception e) { Logger.LogLine(e.Message); return null; } }
internal string Delete(Table table,out List<IDbDataParameter> parameters) { sTable tbl = _pool.Mapping[table.GetType()]; List<SelectParameter> tmpPars = new List<SelectParameter>(); List<string> pkeys = new List<string>(tbl.PrimaryKeyFields); if (tbl.PrimaryKeyFields.Length == 0) { foreach (string prop in tbl.Properties) { tmpPars.Add(new EqualParameter(prop, table.GetInitialPrimaryValue(prop))); } } else { foreach (string prop in tbl.Properties) { sTableField[] flds = tbl[prop]; if (flds.Length > 0) { if (pkeys.Contains(flds[0].Name)) tmpPars.Add(new EqualParameter(prop, table.GetInitialPrimaryValue(prop))); } } } return Delete(table.GetType(), tmpPars.ToArray(), out parameters); }
public void CopyTo(Table[] array, int arrayIndex) { LoadToIndex(Count+1); _data.CopyTo(array,arrayIndex); }
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; }
internal static void RunTriggers(Connection conn, Table original, Table tbl, TriggerTypes type, out bool abort) { abort = false; ITrigger[] tmp = new ITrigger[0]; Monitor.Enter(_triggers); if (_triggers.ContainsKey(tbl.GetType())){ tmp = new ITrigger[_triggers[tbl.GetType()].Count]; _triggers[tbl.GetType()].CopyTo(tmp); } Monitor.Exit(_triggers); foreach (ITrigger tr in tmp) { switch (type) { case TriggerTypes.PRE_DELETE: tr.PreDelete(conn,tbl,out abort); break; case TriggerTypes.POST_DELETE: tr.PostDelete(conn, tbl); break; case TriggerTypes.PRE_INSERT: tr.PreInsert(conn, tbl, out abort); break; case TriggerTypes.POST_INSERT: tr.PostInsert(conn, tbl); break; case TriggerTypes.PRE_UPDATE: tr.PreUpdate(conn, original, tbl, tbl.ChangedFields, out abort); break; case TriggerTypes.POST_UPDATE: tr.PostUpdate(conn, original, tbl, tbl.ChangedFields); break; case TriggerTypes.PRE_DELETE_ALL: tr.PreDeleteAll(conn, out abort); break; case TriggerTypes.POST_DELETE_ALL: tr.PostDeleteAll(conn); break; } } }
internal static object LocateFieldValue(Table table,sTableField fld,ConnectionPool pool) { if (fld.ExternalField == null || fld.Type == FieldType.ENUM) { object obj = table.GetField(fld.ClassProperty); if (obj == null) return null; else if (obj is Table) { foreach (sTableField field in pool.Mapping[obj.GetType()].Fields) { if (field.Name == fld.ExternalField) { return LocateFieldValue((Table)obj, field, pool); } } } else return obj; } else { Table val = (Table)table.GetField(fld.ClassProperty); if (val == null) return null; else { foreach (sTableField field in pool.Mapping[val.GetType()].Fields) { if (field.Name == fld.ExternalField) { return LocateFieldValue(val, field, pool); } } } } return null; }
private void InsertArrayValue(Table table,PropertyInfo pi,int originalLength,List<int> changedIndexes,sTable map,Array values,string prop,bool ignoreAutogen) { List<IDbDataParameter> pars = new List<IDbDataParameter>(); string query=""; string fields=""; string paramString=""; string conditions = ""; pars = new List<IDbDataParameter>(); sTable arMap = pool.Mapping[table.GetType(), prop]; changedIndexes = (changedIndexes == null ? new List<int>() : changedIndexes); string indexName = Pool.Translator.GetIntermediateIndexFieldName(table.GetType(), pi); string valueName = Pool.Translator.GetIntermediateValueFieldName(table.GetType(), pi); foreach (string pk in map.PrimaryKeyProperties) { foreach (sTableField fld in map[pk]) { foreach (sTableField f in arMap.Fields) { if (f.ExternalField == fld.Name) { fields += f.Name + ", "; paramString += queryBuilder.CreateParameterName(f.Name) + ", "; conditions += f.Name + " = " + queryBuilder.CreateParameterName(f.Name) + " AND "; pars.Add(CreateParameter(queryBuilder.CreateParameterName(f.Name), QueryBuilder.LocateFieldValue(table, fld, pool))); break; } } } } conditions = conditions.Substring(0, conditions.Length - 4); if (originalLength > (values == null ? 0 : values.Length)) { pars.Add(CreateParameter(queryBuilder.CreateParameterName(indexName), values.Length)); query = queryBuilder.Delete(arMap.Name, conditions+" AND "+indexName + " > " + queryBuilder.CreateParameterName(indexName)); ExecuteNonQuery(query, pars); } if (values!=null) { if (changedIndexes.Count>0) { pars.Add(CreateParameter(queryBuilder.CreateParameterName("VALUE"), null)); pars.Add(CreateParameter(queryBuilder.CreateParameterName(indexName), null)); foreach (int index in changedIndexes) { if (index<values.Length){ pars[pars.Count - 2] = CreateParameter(queryBuilder.CreateParameterName("VALUE"), values.GetValue(index)); pars[pars.Count - 1] = CreateParameter(queryBuilder.CreateParameterName(indexName), index); query = queryBuilder.Update(arMap.Name, valueName + "=" + queryBuilder.CreateParameterName("VALUE") + "," + indexName + "=" + queryBuilder.CreateParameterName(indexName), conditions + " AND " + indexName + " = " + queryBuilder.CreateParameterName(indexName)); ExecuteQuery(query, pars); } } } pars.Add(CreateParameter(queryBuilder.CreateParameterName("VALUE"),null)); if (ignoreAutogen) pars.Add(CreateParameter(CreateParameterName("index_id"), null)); fields += valueName + (ignoreAutogen ? ", " + indexName : ""); paramString+=CreateParameterName("VALUE")+(ignoreAutogen ? ", "+CreateParameterName("index_id") : ""); query = queryBuilder.Insert(arMap.Name,fields,paramString); for(int index = originalLength;index<values.Length;index++){ pars.RemoveAt(pars.Count-1); if(ignoreAutogen) pars.RemoveAt(pars.Count - 1); pars.Add(CreateParameter(queryBuilder.CreateParameterName("VALUE"),values.GetValue(index))); if (ignoreAutogen) pars.Add(pool.CreateParameter(CreateParameterName("index_id"), index, FieldType.LONG, 8)); ExecuteNonQuery(query,pars); } } }
private static XmlElement CreateRealtedXMLElement(Table val,XmlDocument doc,string name,ConnectionPool pool) { XmlElement ret = doc.CreateElement(name); sTable map = pool.Mapping[val.GetType()]; object obj; foreach (string str in map.PrimaryKeyProperties) { obj = val.GetField(str); if (obj != null) { if (pool.Mapping.IsMappableType(obj.GetType())) ret.AppendChild(CreateRealtedXMLElement((Table)obj, doc, str,pool)); else ret.AppendChild(CreateElementWithValue(doc, str, obj)); } } return ret; }
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; }
internal Table SaveWithAutogen(Table table) { Table ret = table; if (table.ConnectionName != ConnectionName) { throw new Exception("Cannot insert an entry into a table into the database connection that it was not specified for."); } if (table.LoadStatus == LoadStatus.Partial) ret = table; if (!table.IsSaved) ret = Insert(table,true); return ret; }
public Table Save(Table table) { Table ret = table; if (table.ConnectionName != ConnectionName) { throw new Exception("Cannot insert an entry into a table into the database connection that it was not specified for."); } if (table.LoadStatus == LoadStatus.Partial) ret = table; else { if (table.IsSaved) { if (table.LoadStatus == LoadStatus.Complete) { Table orig = table.LoadCopyOfOriginal(this); bool abort = false; ConnectionPoolManager.RunTriggers(this, orig, table, ConnectionPoolManager.TriggerTypes.PRE_UPDATE, out abort); if (!abort) { ret = Update(table); ConnectionPoolManager.RunTriggers(this, orig, ret, ConnectionPoolManager.TriggerTypes.POST_UPDATE, out abort); } } else ret = table; } else { bool abort = false; ConnectionPoolManager.RunTriggers(this, null, table, ConnectionPoolManager.TriggerTypes.PRE_INSERT, out abort); if (!abort) { ret = Insert(table, false); ConnectionPoolManager.RunTriggers(this, null, ret, ConnectionPoolManager.TriggerTypes.POST_INSERT, out abort); } } } if (!ret.IsProxied) ret = (Table)LazyProxy.Instance(ret); return ret; }
public void Add(Table item) { throw new Exception("Paged Table List is ReadOnly."); }
internal string Update(Table table, out List<IDbDataParameter> queryParameters) { queryParameters = new List<IDbDataParameter>(); if ((table.ChangedFields == null) || (table.ChangedFields.Count == 0)) return ""; sTable tbl = _pool.Mapping[table.GetType()]; List<string> changedFields = table.ChangedFields; if (changedFields == null) changedFields = new List<string>(tbl.Properties); if (changedFields.Count == 0) return ""; Dictionary<string, object> updateFields = new Dictionary<string, object>(); List<SelectParameter> parameters = new List<SelectParameter>(); List<string> pkeys = new List<string>(tbl.PrimaryKeyProperties); foreach (string prop in tbl.Properties) { if (changedFields.Contains(prop) && !tbl.ArrayProperties.Contains(prop)) updateFields.Add(prop, table.GetField(prop)); if (pkeys.Count == 0 || pkeys.Contains(prop)) parameters.Add(new EqualParameter(prop, table.GetInitialPrimaryValue(prop))); } return Update(table.GetType(), updateFields, parameters.ToArray(), out queryParameters); }
public bool Contains(Table item) { LoadToIndex(Count+1); return _data.Contains(item); }
internal string InsertWithIdentity(Table table, out List<IDbDataParameter> insertParameters) { sTable tbl = _pool.Mapping[table.GetType()]; insertParameters = new List<IDbDataParameter>(); try { string values = ""; string parameters = ""; foreach (string prop in tbl.Properties) { sTableField[] flds = tbl[prop]; if (flds.Length > 0) { PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS); if (pi.GetCustomAttributes(false)[0] is Org.Reddragonit.Dbpro.Structure.Attributes.IForeignField) { Table eTable = (Table)table.GetField(prop); if (eTable == null) { foreach (sTableField fld in flds) { values += fld.Name + ","; parameters += "," + CreateParameterName(fld.Name); insertParameters.Add(pool.CreateParameter(CreateParameterName(fld.Name), null, fld.Type, fld.Length)); } } else { foreach (sTableField fld in flds) { values += fld.Name + ","; parameters += "," + CreateParameterName(fld.Name); } Type etype = pi.PropertyType; while (true) { sTable etbl = _pool.Mapping[etype]; foreach (sTableField fld in flds){ foreach (sTableField efld in etbl.Fields) { if (fld.ExternalField == efld.Name) { object val = LocateFieldValue(eTable, fld, pool); if (val==null) insertParameters.Add(pool.CreateParameter(CreateParameterName(fld.Name), null, fld.Type, fld.Length)); else insertParameters.Add(pool.CreateParameter(CreateParameterName(fld.Name), val)); break; } } } etype = etype.BaseType; if (etype.Equals(typeof(Table))) break; } } }else { values += flds[0].Name + ","; parameters += "," + CreateParameterName(prop); if (table.IsFieldNull(prop)) insertParameters.Add(pool.CreateParameter(CreateParameterName(prop), null, flds[0].Type, flds[0].Length)); else { if (flds[0].Type == FieldType.ENUM) insertParameters.Add(pool.CreateParameter(CreateParameterName(prop), pool.GetEnumID(table.GetType().GetProperty(prop, Utility._BINDING_FLAGS).PropertyType, table.GetField(prop).ToString()))); else insertParameters.Add(pool.CreateParameter(CreateParameterName(prop), table.GetField(prop), flds[0].Type, flds[0].Length)); } } } } values = values.Substring(0, values.Length - 1); parameters = parameters.Substring(1); return string.Format(InsertString, tbl.Name, values, parameters); } catch (Exception e) { Logger.LogLine(e.Message); return null; } }
public bool Remove(Table item) { throw new Exception("Paged Table List is ReadOnly."); }
public void Delete(Table table) { if (_readonly) throw new Exception("Unable to delete from a readonly database."); if (!table.IsSaved) throw new Exception("Unable to delete an object from the database that is not saved."); if (table.ConnectionName != ConnectionName) throw new Exception("Unable to delete an object from a database connection it is not part of."); pool.Updater.InitType(table.GetType(), this); bool abort = false; ConnectionPoolManager.RunTriggers(this, null, table, ConnectionPoolManager.TriggerTypes.PRE_DELETE, out abort); if (!abort) { List<IDbDataParameter> pars; string del = queryBuilder.Delete(table, out pars); ExecuteNonQuery(del, pars); ConnectionPoolManager.RunTriggers(this, null, table, ConnectionPoolManager.TriggerTypes.POST_DELETE, out abort); } }