Beispiel #1
0
        private void AsyncInnerQuery(string orginalSql, int cacheTimeout, bool priorMessage)
        {
            if (!(_SqlConnection is HubbleAsyncConnection))
            {
                throw new Exception("Command must bind HubbleAsyncConnection when it call AsyncInnerQuery!");
            }

            HubbleAsyncConnection asyncConnection = _SqlConnection as HubbleAsyncConnection;

            string tableTicks = "";

            bool needCache = cacheTimeout >= 0;

            if (cacheTimeout >= 0)
            {
                DateTime expireTime;
                int      hitCount;

                _QueryResult = DataCacheMgr.Get(_SqlConnection,
                                                orginalSql, out expireTime, out hitCount, out tableTicks);

                if (_QueryResult == null)
                {
                    tableTicks = "";
                }

                if (DateTime.Now < expireTime && _QueryResult != null)
                {
                    //not expire

                    return;
                }
                else
                {
                    if (orginalSql.Contains(";"))
                    {
                        //if multi select and not unionselect, disable data cache

                        if (orginalSql.Trim().IndexOf("[UnionSelect]", 0, StringComparison.CurrentCultureIgnoreCase) != 0)
                        {
                            needCache = false;
                        }
                    }
                }
            }

            string sql = "";

            if (needCache)
            {
                if (ResetDataCacheAfterTimeout)
                {
                    tableTicks = "";
                }

                if (tableTicks == "")
                {
                    sql = "exec SP_InnerDataCache;";
                }
                else
                {
                    sql = "exec SP_InnerDataCache '" + tableTicks.Replace("'", "''") + "';";
                }
            }

            sql += orginalSql;

            asyncConnection.BeginAsyncQuerySql(sql, this, ref _ClassId, priorMessage);
        }
Beispiel #2
0
        private Hubble.Framework.Data.DataSet InnerQuery(string orginalSql, int cacheTimeout)
        {
            if (!_SqlConnection.Connected)
            {
                if (_SqlConnection.ServerBusy)
                {
                    DateTime expireTime;
                    int      hitCount;
                    string   tableTicks1;

                    _QueryResult = DataCacheMgr.Get(_SqlConnection,
                                                    orginalSql, out expireTime, out hitCount, out tableTicks1);
                    if (_QueryResult != null)
                    {
                        return(_QueryResult.DataSet);
                    }
                    else
                    {
                        _SqlConnection.CommandReportNoCache = true;
                    }
                }

                try
                {
                    _SqlConnection.Open();
                }
                catch (Exception e)
                {
                    throw new System.Data.DataException(string.Format("Sql Connection does not connect!, errMsg:{0}",
                                                                      e.Message));
                }
            }

            string tableTicks = "";

            bool needCache = cacheTimeout >= 0;

            if (cacheTimeout >= 0)
            {
                DateTime expireTime;
                int      hitCount;

                _QueryResult = DataCacheMgr.Get(_SqlConnection,
                                                orginalSql, out expireTime, out hitCount, out tableTicks);

                if (_QueryResult == null)
                {
                    tableTicks = "";
                }

                if (DateTime.Now < expireTime && _QueryResult != null)
                {
                    //not expire

                    return(_QueryResult.DataSet);
                }
                else
                {
                    if (orginalSql.Contains(";"))
                    {
                        //if multi select and not unionselect, disable data cache

                        if (orginalSql.Trim().IndexOf("[UnionSelect]", 0, StringComparison.CurrentCultureIgnoreCase) != 0)
                        {
                            needCache = false;
                        }
                    }
                }
            }

            string sql = "";

            if (needCache)
            {
                if (ResetDataCacheAfterTimeout)
                {
                    tableTicks = "";
                }

                if (tableTicks == "")
                {
                    sql = "exec SP_InnerDataCache;";
                }
                else
                {
                    sql = "exec SP_InnerDataCache '" + tableTicks.Replace("'", "''") + "';";
                }
            }

            sql += orginalSql;

            if (_SqlConnection is HubbleAsyncConnection)
            {
                //Async query
                _AsyncException = null;
                HubbleAsyncConnection asyncConnection = _SqlConnection as HubbleAsyncConnection;

                QueryEvent = new System.Threading.ManualResetEvent(false);

                asyncConnection.BeginAsyncQuerySql(sql, this, ref _ClassId);

                if (QueryEvent.WaitOne(CommandTimeout * 1000, false))
                {
                    if (_AsyncException != null)
                    {
                        throw _AsyncException;
                    }
                }
                else
                {
                    this.EndAsyncQuery();
                    throw new System.TimeoutException("Query sql timeout");
                }
            }
            else
            {
                _QueryResult = _SqlConnection.QuerySql(sql);
            }

            //Data cache
            if (cacheTimeout > 0 || needCache)
            {
                if (_QueryResult.DataSet.Tables.Count >= 0)
                {
                    QueryResult qr = new QueryResult();

                    tableTicks = GetTableTicks(_QueryResult);

                    DateTime expireTime;
                    int      hitCount;

                    QueryResult qResult = null;

                    int noChangedTables = 0;

                    List <Hubble.Framework.Data.DataTable> sourceTables = new List <Hubble.Framework.Data.DataTable>();

                    for (int i = 0; i < _QueryResult.DataSet.Tables.Count; i++)
                    {
                        sourceTables.Add(_QueryResult.DataSet.Tables[i]);
                    }

                    for (int i = 0; i < sourceTables.Count; i++)
                    {
                        Hubble.Framework.Data.DataTable table;

                        if (sourceTables[i].MinimumCapacity == int.MaxValue)
                        {
                            if (qResult == null)
                            {
                                qResult = DataCacheMgr.Get(_SqlConnection,
                                                           orginalSql, out expireTime, out hitCount, out tableTicks);
                            }

                            if (qResult != null)
                            {
                                noChangedTables++;

                                table = sourceTables[i];
                                MergeDataCache(qResult, table, qr);
                                //table = qResult.DataSet.Tables[i];
                                //qResult.DataSet.Tables.Remove(table);
                                //qr.AddDataTable(table);
                            }
                            else
                            {
                                table = sourceTables[i];
                                _QueryResult.DataSet.Tables.Remove(table);

                                qr.AddDataTable(table);
                            }
                        }
                        else
                        {
                            table = sourceTables[i];
                            _QueryResult.DataSet.Tables.Remove(table);

                            qr.AddDataTable(table);
                        }
                    }

                    _QueryResult.DataSet = qr.DataSet;

                    if (noChangedTables != _QueryResult.DataSet.Tables.Count)
                    {
                        DataCacheMgr.Insert(_SqlConnection, orginalSql, _QueryResult, DateTime.Now.AddSeconds(cacheTimeout), tableTicks);
                    }
                    else
                    {
                        DataCacheMgr.ChangeExpireTime(_SqlConnection, orginalSql, DateTime.Now.AddSeconds(cacheTimeout));
                    }
                }
            }

            return(_QueryResult.DataSet);
        }
Beispiel #3
0
        /// <summary>
        /// Merge data cache and table to the result
        /// </summary>
        /// <param name="qrDataCache">data cache</param>
        /// <param name="table">table from server</param>
        /// <param name="result">add the merge table to result</param>
        private void MergeDataCache(QueryResult qrDataCache, Hubble.Framework.Data.DataTable table, QueryResult result)
        {
            int  i    = 0;
            bool find = false;

            for (; i < qrDataCache.DataSet.Tables.Count; i++)
            {
                if (qrDataCache.DataSet.Tables[i].TableName.Equals(table.TableName, StringComparison.CurrentCultureIgnoreCase))
                {
                    find = true;
                    break;
                }
            }

            if (find)
            {
                Hubble.Framework.Data.DataTable[] tables = new Hubble.Framework.Data.DataTable[qrDataCache.DataSet.Tables.Count];
                qrDataCache.DataSet.Tables.CopyTo(tables, 0);

                do
                {
                    qrDataCache.DataSet.Tables.Remove(tables[i]);
                    result.AddDataTable(tables[i]);

                    if (++i >= tables.Length)
                    {
                        break;
                    }
                }while (!IsSelectOrSpTalbe(tables[i]));
            }
        }