private Settings() { _cache = new Dictionary<string, CachedItemContainer>(); ConnectionPool pool = ConnectionPoolManager.GetPool(typeof(SystemSetting)); if (pool==null) Log.Trace("Unable to obtain a pool for the type " + typeof(SystemSetting).FullName); _conn = pool.GetConnection(); }
private void LoadToIndex(int index) { if (_conn == null) _conn = ConnectionPoolManager.GetConnection(_tableType); while((_data.Count-1<index)&&(_data.Count<Count)) { _data.AddRange(_conn.SelectPaged(TableType,SelectParams,(ulong)_data.Count,(ulong)PageSize)); } if (!_transationSafe) { _conn.CloseConnection(); _conn=null; } }
public PagedTableList(Type type,int? PageSize,SelectParameter[] SelectParams,bool transationSafe) { if (!type.IsSubclassOf(typeof(Table))) throw new Exception("cannot produce a Paged Table List from a class that does not inherit Table"); _tableType=type; _pageSize = PageSize; _pars = SelectParams; _transationSafe=transationSafe; _conn = ConnectionPoolManager.GetConnection(type); _count = (int)_conn.SelectCount(type,_pars); if (!_pageSize.HasValue) _pageSize=20; _data = new List<Table>(); LoadToIndex(_pageSize.Value); }
internal ViewResultRow(Connection conn,ClassQuery cq) { _conn = conn; _cq = cq; }
internal virtual List<Index> ExtractTableIndexes(string tableName, Connection conn) { throw new Exception("Method Not Implemented"); }
internal static void RunTriggers(Connection conn, Type tableType, Dictionary<string, object> updateFields, SelectParameter[] parameters, TriggerTypes type, out bool abort) { abort = false; ITrigger[] tmp = new ITrigger[0]; Monitor.Enter(_triggers); if (_triggers.ContainsKey(tableType)) { tmp = new ITrigger[_triggers[tableType].Count]; _triggers[tableType].CopyTo(tmp); } Monitor.Exit(_triggers); foreach (ITrigger tr in tmp) { switch (type) { case TriggerTypes.PRE_UPDATE: tr.PreUpdate(conn,tableType, updateFields, parameters,out abort); break; case TriggerTypes.POST_UPDATE: tr.PostUpdate(conn,tableType, updateFields, parameters); break; } } }
//recursively sets values private void RecurSetValues(sTable map,Connection conn) { Type ty = conn.Pool.Mapping[map.Name]; List<string> extFields = new List<string>(map.ForeignTableProperties); foreach (string prop in map.Properties) { if (!map.ArrayProperties.Contains(prop)) { PropertyInfo pi = ty.GetProperty(prop, Utility._BINDING_FLAGS); if (pi != null) { if (extFields.Contains(prop) && !Utility.IsEnum(pi.PropertyType)) { Table t = (Table)pi.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]); t._loadStatus = LoadStatus.Partial; t = (Table)LazyProxy.Instance(t); bool setValue = false; t = SetExternalValues(map, prop, conn, out setValue, t); if (!t.AllPrimaryKeysNull && setValue) { t.InitPrimaryKeys(); this.SetField(prop, t); } } else { sTableField fld = map[prop][0]; if (conn.ContainsField(fld.Name)) { if (conn.IsDBNull(conn.GetOrdinal(fld.Name))) { try { this.SetField(prop, null); } catch (Exception e) { } } else { if (fld.Type == FieldType.ENUM) this.SetField(prop, conn.Pool.GetEnumValue(pi.PropertyType, (int)conn[fld.Name])); else this.SetField(prop, conn[fld.Name]); } } } } } } if (conn.Pool.Mapping.IsMappableType(ty.BaseType)) { RecurSetValues(conn.Pool.Mapping[ty.BaseType],conn); } this.InitPrimaryKeys(); }
//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 void GetPkFksCollection(Connection conn,out Dictionary<PrimaryKey, List<ForeignKey>> primaryKeys, out Dictionary<ForeignKey, PrimaryKey> foreignKeys) { primaryKeys = new Dictionary<PrimaryKey, List<ForeignKey>>(); foreignKeys = new Dictionary<ForeignKey, PrimaryKey>(); List<PrimaryKey> pks = ExtractExpectedPrimaryKeys(conn); List<ForeignKey> fks = ExtractExpectedForeignKeys(conn); foreach (PrimaryKey pk in pks) { primaryKeys.Add(pk, new List<ForeignKey>()); foreach (ForeignKey fk in fks) { if (pk.IsForForeignRelation(fk)) primaryKeys[pk].Add(fk); } } foreach (ForeignKey fk in fks) { foreach (PrimaryKey pk in pks) { if (pk.ContainsForeignFields(fk)) { foreignKeys.Add(fk, pk); break; } } } }
internal void EmptyAllTables(Connection conn) { List<string> tables = new List<string>(); conn.ExecuteQuery(conn.queryBuilder.SelectTableNames()); while (conn.Read()) tables.Add(conn[0].ToString()); conn.Close(); foreach (string str in tables){ conn.ExecuteNonQuery(conn.queryBuilder.DeleteAll(str)); conn.Commit(); } }
private List<PrimaryKey> ExtractExpectedPrimaryKeys(Connection conn) { List<PrimaryKey> keys = new List<PrimaryKey>(); foreach (Type t in _updater.CreatedTypes) { sTable tbl = _mapping[t]; if (tbl.PrimaryKeyFields.Length > 0) keys.Add(new PrimaryKey(tbl.Name, new List<string>(tbl.PrimaryKeyFields))); string[] props = tbl.Properties; foreach (string prop in props) { if (_mapping.PropertyHasIntermediateTable(t, prop)) { tbl = _mapping[t, prop]; if (tbl.PrimaryKeyFields.Length > 0) keys.Add(new PrimaryKey(tbl.Name, new List<string>(tbl.PrimaryKeyFields))); } } } return keys; }
private List<ForeignKey> ExtractExpectedForeignKeys(Connection conn) { List<ForeignKey> keys = new List<ForeignKey>(); foreach (Type t in _updater.CreatedTypes) { sTable tbl = _mapping[t]; foreach (string prop in tbl.Properties) { List<string> ifields = new List<string>(); List<string> efields = new List<string>(); if (tbl.GetRelationForProperty(prop).HasValue) { sTableRelation rel = tbl.GetRelationForProperty(prop).Value; foreach (sTableField fld in tbl[prop]) { ifields.Add(fld.Name); efields.Add(fld.ExternalField); } keys.Add(new ForeignKey(tbl.Name, ifields, rel.ExternalTable, efields, rel.OnUpdate.ToString(), rel.OnDelete.ToString())); } if (_mapping.PropertyHasIntermediateTable(t, prop)) { sTable itbl = _mapping[t, prop]; ifields.Clear(); efields.Clear(); foreach (sTableField fld in itbl["PARENT"]) { ifields.Add(fld.Name); efields.Add(fld.ExternalField); } keys.Add(new ForeignKey(itbl.Name, ifields, itbl.Relations[0].ExternalTable, efields, ForeignField.UpdateDeleteAction.CASCADE.ToString(), ForeignField.UpdateDeleteAction.CASCADE.ToString())); ifields.Clear(); efields.Clear(); foreach (sTableField fld in itbl["CHILD"]) { ifields.Add(fld.Name); efields.Add(fld.ExternalField); } keys.Add(new ForeignKey(itbl.Name, ifields, itbl.Relations[1].ExternalTable, efields, ForeignField.UpdateDeleteAction.CASCADE.ToString(), ForeignField.UpdateDeleteAction.CASCADE.ToString())); } } } for (int x = 0; x < keys.Count; x++) { for (int y = x + 1; y < keys.Count; y++) { if (keys[x].Equals(keys[y])) { keys.RemoveAt(y); y--; } } } return keys; }
internal void returnConnection(Connection conn) { Logger.LogLine(string.Format("Connection {0} return to pool", conn.ID)); _lock.WaitOne(); _conns.Remove(conn); _lock.Set(); conn.Disconnect(); }
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 void RecurDropPK(PrimaryKey pk, Dictionary<PrimaryKey, List<ForeignKey>> pks, Dictionary<ForeignKey, PrimaryKey> fks, ref List<string> queries, Connection conn) { string query; bool add = true; query = conn.queryBuilder.DropPrimaryKey(pk); if (query.Contains("\n")) { foreach (string str in query.Split('\n')) { if (str.Trim().Length > 0) { if (queries.Contains(str)) { add = false; break; } } } } else add = !queries.Contains(query); if (add) { foreach (ForeignKey fk in pks[pk]) { if (!fks.ContainsKey(fk)) { query = conn.queryBuilder.DropForeignKey(fk.InternalTable, fk.ExternalTable, fk.ExternalFields[0], fk.InternalFields[0]); if (query.Contains("\n")) queries.AddRange(query.Split('\n')); else queries.Add(query); } else { if (fks[fk].Name != fk.ExternalTable) RecurDropPK(fks[fk], pks, fks, ref queries, conn); query = conn.queryBuilder.DropForeignKey(fk.InternalTable, fk.ExternalTable, fk.ExternalFields[0], fk.InternalFields[0]); if (query.Contains("\n")) queries.AddRange(query.Split('\n')); else queries.Add(query); } } query = conn.queryBuilder.DropPrimaryKey(pk); if (query.Contains("\n")) queries.AddRange(query.Split('\n')); else queries.Add(query); } }
//called by a connection to set the values in the table object from the generated query. internal void SetValues(Connection conn) { _initialPrimaryKeys.Clear(); Logger.LogLine("Obtaining table map for " + this.GetType().FullName + " to allow setting of values from query"); sTable map = conn.Pool.Mapping[this.GetType()]; Logger.LogLine("Recursively setting values from query for " + this.GetType().FullName); RecurSetValues(map, conn); _isSaved = true; }
internal void DisableRelationships(Connection conn) { List<string> queries = new List<string>(); string query; Dictionary<PrimaryKey, List<ForeignKey>> pks; Dictionary<ForeignKey, PrimaryKey> fks; GetPkFksCollection(conn, out pks, out fks); foreach (PrimaryKey pk in pks.Keys) { if (pks[pk].Count == 0) { query = conn.queryBuilder.DropPrimaryKey(pk); if (query.Contains("\n")) queries.AddRange(query.Split('\n')); else queries.Add(query); } } foreach (PrimaryKey pk in pks.Keys) { if (pks[pk].Count > 0) { RecurDropPK(pk, pks, fks, ref queries, conn); } } Utility.RemoveEmptyStrings(ref queries); Utility.RemoveDuplicateStrings(ref queries, null); foreach (string str in queries) { conn.ExecuteNonQuery(str); System.Threading.Thread.Sleep(50); conn.Commit(); } }
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; } } }
internal void EnableRelationships(Connection conn) { List<string> queries = new List<string>(); foreach (PrimaryKey pk in ExtractExpectedPrimaryKeys(conn)) queries.Add(conn.queryBuilder.CreatePrimaryKey(pk)); foreach (ForeignKey fk in ExtractExpectedForeignKeys(conn)) queries.Add(conn.queryBuilder.CreateForeignKey(fk)); Utility.RemoveEmptyStrings(ref queries); Utility.RemoveDuplicateStrings(ref queries, null); foreach (string str in queries) { conn.ExecuteNonQuery(str); System.Threading.Thread.Sleep(50); } }
//used to load the original data to be used for update triggers internal Table LoadCopyOfOriginal(Connection conn) { List<SelectParameter> pars = new List<SelectParameter>(); foreach (string str in _initialPrimaryKeys.Keys) pars.Add(new EqualParameter(str, _initialPrimaryKeys[str])); List<Org.Reddragonit.Dbpro.Structure.Table> tmp = conn.Select(this.GetType(), pars.ToArray()); if (tmp.Count > 0) return tmp[0]; return this; }
internal static void RunTriggers(Connection conn, Type tblType, TriggerTypes type, out bool abort) { abort = false; ITrigger[] tmp = new ITrigger[0]; Monitor.Enter(_triggers); if (_triggers.ContainsKey(tblType)) { tmp = new ITrigger[_triggers[tblType].Count]; _triggers[tblType].CopyTo(tmp); } Monitor.Exit(_triggers); foreach (ITrigger tr in tmp) { switch (type) { case TriggerTypes.PRE_DELETE_ALL: tr.PreDeleteAll(conn,out abort); break; case TriggerTypes.POST_DELETE_ALL: tr.PostDeleteAll(conn); break; } } }