Exemplo n.º 1
0
        private static IDbCommand SetupCommand(IDbConnection cnn, string typeName, IDbTransaction transaction, string sql, Action <IDbCommand, object> paramReader, object obj, int?commandTimeout, CommandType?commandType)
        {
            var cmd = cnn.CreateCommand();

            var bindByName = MapperGenerator.GetBindByName(cmd.GetType());

            if (bindByName != null)
            {
                bindByName(cmd, true);
            }
            cmd.Transaction = transaction;
            cmd.CommandText = sql;
            if (commandTimeout.HasValue)
            {
                cmd.CommandTimeout = commandTimeout.Value;
            }
            if (commandType.HasValue)
            {
                cmd.CommandType = commandType.Value;
            }
            if (paramReader != null)
            {
                paramReader(cmd, obj);
            }
            return(cmd);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Return a typed list of objects, reader is closed after the call
        /// </summary>
        private static IEnumerable <T> QueryInternal <T>(IDbConnection cnn, string typeName, string sql, object param, int totalRecord, IDbTransaction transaction, int?commandTimeout, CommandType?commandType)
        {
            var identity = new Identity(sql, commandType, cnn, typeof(T), param == null ? null : param.GetType(), null);
            var info     = CacheMapper.GetCacheInfo(identity);

            using (var cmd = SetupCommand(cnn, typeName, transaction, sql, info.ParamReader, param, commandTimeout, commandType))
            {
                #region ExecuteReader
                var            time         = DateTime.Now;
                string         providerName = string.Empty;
                IDMSDbProfiler dbProfiler   = GetProfiler(out providerName);
                dbProfiler.ExecuteStart(providerName, typeName, cmd, DMSQueryType.ExecuteReader);
                using (var reader = cmd.ExecuteReader())
                {
                    dbProfiler.ExecuteFinish(providerName, typeName, cmd, DMSQueryType.ExecuteReader, (DateTime.Now - time).TotalMilliseconds, reader);
                    #region cacheDeserializer
                    Func <Func <IDataReader, object> > cacheDeserializer = () =>
                    {
                        info.Deserializer = MapperGenerator.GetDeserializer(typeof(T), reader, 0, -1, false);
                        CacheMapper.SetQueryCache(identity, info);
                        return(info.Deserializer);
                    };
                    #endregion

                    if (info.Deserializer == null)
                    {
                        cacheDeserializer();
                    }

                    var deserializer = info.Deserializer;
                    int readRecord   = totalRecord == 0 ? int.MaxValue : totalRecord;
                    while (reader.Read() && readRecord > 0)
                    {
                        object next;
                        try
                        {
                            next = deserializer(reader);
                        }
                        catch (DataException exp)
                        {
                            // give it another shot, in case the underlying schema changed
                            deserializer = cacheDeserializer();
                            next         = deserializer(reader);
                            dbProfiler.OnError(providerName, typeName, cmd, DMSQueryType.ExecuteReader, (DateTime.Now - time).TotalMilliseconds, exp);
                        }
                        readRecord--;
                        yield return((T)next);
                    }
                    dbProfiler.ReaderFinish(providerName, typeName, reader, (DateTime.Now - time).TotalMilliseconds);
                }

                #endregion
            }
        }
Exemplo n.º 3
0
        public static CacheInfo GetCacheInfo(Identity identity)
        {
            CacheInfo info;

            if (!TryGetQueryCache(identity, out info))
            {
                info = new CacheInfo();
                if (identity.parametersType != null)
                {
                    if (typeof(IDynamicParameters).IsAssignableFrom(identity.parametersType))
                    {
                        info.ParamReader = (cmd, obj) => { (obj as IDynamicParameters).AddParameters(cmd, identity); };
                    }
                    else
                    {
                        info.ParamReader = MapperGenerator.CreateParamInfoGenerator(identity);
                    }
                }
                SetQueryCache(identity, info);
            }
            return(info);
        }