public static bool TryGetCachedData(ISqlQuery query, out IDataReader data) { var key = new CacheKey(query); ICacheItem cached; if (QueryResultCache.TryGetValue(key, out cached)) { if (cached.Expired) { RemoveFromCache(key); data = null; return(false); } else { CachedDataReader reader = ((CacheItem)cached).Data; reader.Reset(); data = new CachedDataReaderView(reader); return(true); } } else { data = null; return(false); } }
private void _LoadList() { const int ConstDisplayName = 0; const int ConstProfileStateProduction = 1; const int ConstProfileStateShadow = 2; const int ConstChgNumber = 3; using (new UpdateHelper(m_TreeList)) { m_Proxy.Clear(); // Daten holen und prüfen ISingleDbObject dbobject = m_MainActivator.DbObject; if (dbobject == null) { return; } var runner = Session.Resolve <IStatementRunner>(); using (IDataReader reader = new CachedDataReader(runner.SqlExecute("SDL-FormAppServerDriverProfile", new List <QueryParameter>() { new QueryParameter("UID_ApplicationServer", ValType.String, FormTool.GetValueSafe(dbobject, "UID_ApplicationServer", "")) }))) { while (reader.Read()) { ITreeListNode node = m_Proxy.AddNode(reader.GetString(ConstDisplayName), (int)StockImage.DriverProfile); m_Proxy.AddItem(node, reader.GetString(ConstProfileStateProduction)); m_Proxy.AddItem(node, reader.GetString(ConstProfileStateShadow)); m_Proxy.AddItem(node, reader.GetString(ConstChgNumber)); } } } }
/// <summary> /// Executes the given SelectQuery and returns the result set transformed back into entity objects. /// Adds each returned object to the internal cache as managed entity /// </summary> /// <param name="query"></param> /// <typeparam name="TEntity"></typeparam> /// <returns></returns> public IEnumerable <TEntity> Select <TEntity>(IQuery <TEntity> query) { var conn = _connection; if (conn.State != ConnectionState.Open) { conn.Open(); } using var cmd = conn.CreateCommand(); cmd.CommandText = query.AsSqlString(); cmd.Transaction = _transaction; foreach (var(type, id, value) in query.GetParameters()) { var param = cmd.CreateParameter(); param.Value = value; param.DbType = type; param.ParameterName = id; cmd.Parameters.Add(param); } using var reader = new CachedDataReader( _ctx.Cache, _ctx.Schema.EntityTypeMap[typeof(TEntity)], cmd.ExecuteReader(), _ctx ); foreach (var cacheEntry in reader.ReadAll()) { RegisterLazyLoader((TEntity)cacheEntry.Object, cacheEntry.Entity, cacheEntry); yield return((TEntity)cacheEntry.Object); } }
/// <summary> /// Executes <see cref="UpdateStatement{TEntity}"/> and updates cached objects /// </summary> /// <param name="stmt"></param> /// <typeparam name="TEntity"></typeparam> public void Update <TEntity>(UpdateStatement <TEntity> stmt) { var conn = _connection; if (conn.State != ConnectionState.Open) { conn.Open(); } using var cmd = conn.CreateCommand(); cmd.CommandText = stmt.AsSqlString(); cmd.Transaction = _transaction; foreach (var(type, id, value) in stmt.GetParameters()) { var param = cmd.CreateParameter(); param.Value = value; param.DbType = type; param.ParameterName = id; cmd.Parameters.Add(param); } using var reader = new CachedDataReader( _ctx.Cache, stmt.Entity, cmd.ExecuteReader(), _ctx ); // consume update query to execute reader foreach (var _ in reader.ReadAll().ToList()) { } }
public CacheItem(CachedDataReader data, int size) { Data = data; Size = size; DateCreated = DateTime.Now; DateUpdated = DateTime.Now; }
private static ObjectBaseHash _GetServerCollection(string Ident_Domain, string UID_Profile, NodeType nodeType) { ISqlFormatter isql = clsMain.Instance.CurrentConnection.Connection.SqlFormatter; SqlExecutor cSQL = clsMain.Instance.CurrentConnection.Connection.CreateSqlExecutor(clsMain.Instance.CurrentConnection.PublicKey); ObjectBaseHash colServers = new ObjectBaseHash(); ObjectServer oServer = null; string strSQL = ""; switch (nodeType) { case NodeType.AppProfile: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ApplicationProfileServer"]; break; case NodeType.DrvProfile: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["DriverProfileServer"]; break; case NodeType.MacType: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["MachineTypeServer"]; break; } // replace the Domain-Variable strSQL = strSQL.Replace("@Ident_Domain", isql.FormatValue(Ident_Domain, ValType.String, true)); // replace UID_Profile strSQL = strSQL.Replace("@UID_Profile", isql.FormatValue(UID_Profile, ValType.String, true)); // and now do it... using (IDataReader rData = new CachedDataReader(cSQL.SqlExecute(strSQL))) { while (rData.Read()) { // create a server-object oServer = new ObjectServer(rData); // and add to our Hash colServers.Add(oServer, "UID_ApplicationServer"); } } return(colServers); }
public static IDataReader AddToCache(ISqlQuery query, IDataReader data) { ICacheKey key = new CacheKey(query); CachedDataReader cached = new CachedDataReader(data); int newSize = cached.Size() + key.Query.Length; ICacheItem item; if (newSize < CacheObjectSizeLimit) { while (CacheSize + newSize > CacheSizeLimit && QueryOrder.Count > 0) { ICacheKey oldestKey = QueryOrder.Peek(); if (!RemoveFromCache(oldestKey)) { break; } } if (QueryOrder.Count == 0 && CacheSize != 0) { CacheSize = 0; QueryResultCache.Clear(); // this should be logged but thrwing an error seems too much... test the hellout of this? } // It is possible that we failed to remove it from the cache, so just bail out // if still won't fit if (CacheSize + newSize <= CacheSizeLimit) { item = new CacheItem(cached, newSize); if (QueryResultCache.TryAdd(key, item)) { QueryOrder.Enqueue(key); } } else { } } // whether or not we end up caching the datareader, we must return the in-memory copy because you can't go back // after reading a datareader. We only try to cache when buffering is enabled, so nothing's really lost. return cached; }
public static IDataReader AddToCache(ISqlQuery query, IDataReader data) { ICacheKey key = new CacheKey(query); CachedDataReader cached = new CachedDataReader(data); int newSize = cached.Size() + key.Query.Length; ICacheItem item; if (newSize < CacheObjectSizeLimit) { while (CacheSize + newSize > CacheSizeLimit && QueryOrder.Count > 0) { ICacheKey oldestKey = QueryOrder.Peek(); if (!RemoveFromCache(oldestKey)) { break; } } if (QueryOrder.Count == 0 && CacheSize != 0) { CacheSize = 0; QueryResultCache.Clear(); // this should be logged but thrwing an error seems too much... test the hellout of this? } // It is possible that we failed to remove it from the cache, so just bail out // if still won't fit if (CacheSize + newSize <= CacheSizeLimit) { item = new CacheItem(cached, newSize); if (QueryResultCache.TryAdd(key, item)) { QueryOrder.Enqueue(key); } } else { } } // whether or not we end up caching the datareader, we must return the in-memory copy because you can't go back // after reading a datareader. We only try to cache when buffering is enabled, so nothing's really lost. return(cached); }
/// <summary> /// Executes the given InsertStatement and maps returned values back into entities /// objects created like this are add into the cache as managed entities /// </summary> /// <param name="stmt"></param> /// <typeparam name="TEntity"></typeparam> /// <exception cref="Exception"></exception> public void Insert <TEntity>(InsertExpression <TEntity> stmt) { var conn = _connection; if (conn.State != ConnectionState.Open) { conn.Open(); } using var cmd = conn.CreateCommand(); cmd.CommandText = stmt.AsSqlString(); cmd.Transaction = _transaction; // set all prepared stmt placeholders foreach (var(type, id, value) in stmt.GetParameters()) { var param = cmd.CreateParameter(); param.Value = value ?? DBNull.Value; param.DbType = type; param.ParameterName = id; cmd.Parameters.Add(param); } using var reader = new CachedDataReader( _ctx.Cache, stmt.Entity, cmd.ExecuteReader(), _ctx ); //reader reads objects into Cache var entries = reader .ReadAllInto(stmt.InsertedObjects.Cast <object>()) .ToList(); //register lazyloaders on newly created objects foreach (var cacheEntry in entries) { RegisterLazyLoader(cacheEntry.Object, cacheEntry.Entity, cacheEntry); } }
private ObjectBaseHash _GetServerOfDomin(string parentDomain) { ISqlFormatter isql = clsMain.Instance.CurrentConnection.Connection.SqlFormatter; SqlExecutor cSQL = clsMain.Instance.CurrentConnection.Connection.CreateSqlExecutor(clsMain.Instance.CurrentConnection.PublicKey); string strSQL; ObjectBaseHash colServers = new ObjectBaseHash(); ObjectServer2 objServer; try { // get the gigantic SQL-Statement strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ApplicationServer"]; // replace the Domain-Variable strSQL = strSQL.Replace("@Ident_DomainRD", isql.FormatValue(parentDomain, ValType.String)); using (IDataReader rData = new CachedDataReader(cSQL.SqlExecute(strSQL))) { while (rData.Read()) { // create a new Serverobject objServer = new ObjectServer2(rData); // appand to our list colServers.Add(objServer, "UID_ApplicationServer"); } } } catch (Exception ex) { ExceptionDialog.Show(this.ParentForm, ex); } return(colServers); }
private void _InsertProfiles(NodeType nodeType, TreeListNode tlnParent) { ISqlFormatter isql = clsMain.Instance.CurrentConnection.Connection.SqlFormatter; SqlExecutor cSQL = clsMain.Instance.CurrentConnection.Connection.CreateSqlExecutor(clsMain.Instance.CurrentConnection.PublicKey); TreeListNode tlnProfile = null; string strSQL = ""; NodeData nd = tlnParent.Tag as NodeData; try { Cursor.Current = Cursors.WaitCursor; tlcProfile.BeginUpdate(); // remove old childs tlnParent.Nodes.Clear(); switch (nodeType) { case NodeType.AppProfiles: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ApplicationProfile"]; break; case NodeType.DrvProfiles: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["DriverProfile"]; break; case NodeType.MacTypes: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["MachineType"]; break; } // replace the Domain-Variable strSQL = strSQL.Replace("@Ident_DomainRD", isql.FormatValue(nd.Data1, ValType.String)); if (clsMain.Instance.ErrorOnly) { strSQL = strSQL.Replace("@ErrorOnly", " and x.fehler > 0" + Environment.NewLine); } else { strSQL = strSQL.Replace("@ErrorOnly", Environment.NewLine); } // and now do it... using (IDataReader rData = new CachedDataReader(cSQL.SqlExecute(strSQL))) { while (rData.Read()) { bool bFehler = rData.GetBoolean(0); switch (nodeType) { case NodeType.AppProfiles: tlnProfile = tlnParent.Nodes.Add(rData.GetString(1), bFehler ? 5 : 2); tlnProfile.Tag = new NodeData(NodeType.AppProfile, nd.Data1, rData.GetString(2)); break; case NodeType.DrvProfiles: tlnProfile = tlnParent.Nodes.Add(rData.GetString(1), bFehler ? 6 : 3); tlnProfile.Tag = new NodeData(NodeType.DrvProfile, nd.Data1, rData.GetString(2)); break; case NodeType.MacTypes: tlnProfile = tlnParent.Nodes.Add(rData.GetString(1), bFehler ? 7 : 4); tlnProfile.Tag = new NodeData(NodeType.MacType, nd.Data1, rData.GetString(2)); break; } tlnProfile.SubItems.Add(rData.GetInt32(3).ToString()); } } // no profiles --> remove + from Parent-Node if (tlnParent.Nodes.Count == 0) { tlnParent.ShowExpansionIndicator = false; } } catch (Exception ex) { ExceptionDialog.Show(this.ParentForm, ex); } finally { tlcProfile.EndUpdate(); Cursor.Current = Cursors.Default; } }
private void _InsertProfiles(NodeType nodeType, string Ident_DomainRD, string UID_ApplicationServer) { ISqlFormatter isql = clsMain.Instance.CurrentConnection.Connection.SqlFormatter; SqlExecutor cSQL = clsMain.Instance.CurrentConnection.Connection.CreateSqlExecutor(clsMain.Instance.CurrentConnection.PublicKey); string strSQL = ""; string strSQLEO = ""; TreeListNode tlnProfile; try { // SQL depents on ProfileType switch (nodeType) { case NodeType.AppProfile: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ServerAppProfile"]; strSQLEO = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ServerAppProfileErrorsOnly"]; break; case NodeType.DrvProfile: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ServerDrvProfile"]; strSQLEO = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ServerDrvProfileErrorsOnly"]; break; case NodeType.MacType: strSQL = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ServerMacType"]; strSQLEO = clsMain.Instance.CurrentConnection.Connection.SqlStrings["ServerMacTypeErrorsOnly"]; break; } // replace the Variables if (clsMain.Instance.ErrorOnly) { strSQL = strSQL.Replace("@ErrorsOnly", strSQLEO); // insert the ErrorOnly part } else { strSQL = strSQL.Replace("@ErrorsOnly", ""); // remove the ErrorOnly part } strSQL = strSQL.Replace("@Ident_DomainRD", isql.FormatValue(Ident_DomainRD, ValType.String)); strSQL = strSQL.Replace("@UID_ApplicationServer", isql.FormatValue(UID_ApplicationServer, ValType.String)); using (IDataReader rData = new CachedDataReader(cSQL.SqlExecute(strSQL))) { while (rData.Read()) { // create a new Node tlnProfile = tlcProfile.Nodes.Add(rData.GetString(1), 0); tlnProfile.Tag = new NodeData(nodeType, rData.GetString(0), ""); tlnProfile.SubItems.Add(rData.GetInt32(2).ToString()); // Nr Ist tlnProfile.SubItems.Add(rData.GetInt32(3).ToString()); // Nr Soll tlnProfile.SubItems.Add(rData.GetString(4).ToString()); // State P tlnProfile.SubItems.Add(rData.GetString(5).ToString()); // State S tlnProfile.SubItems.Add(rData.GetDateTime(6).ToString()); // XDateUpdated tlnProfile.SubItems.Add(""); } } } catch (Exception ex) { ExceptionDialog.Show(this.ParentForm, ex); } }