コード例 #1
0
 private static object ExecuteCommand(IDbConnection cnn, string typeName, IDbTransaction transaction, string sql, bool ExecuteScalar, Action <IDbCommand, object> paramReader, object obj, int?commandTimeout, CommandType?commandType)
 {
     using (var cmd = SetupCommand(cnn, typeName, transaction, sql, paramReader, obj, commandTimeout, commandType))
     {
         string providerName = string.Empty;
         var    time = DateTime.Now; IDMSDbProfiler dbProfiler = GetProfiler(out providerName);
         try
         {
             if (ExecuteScalar)
             {
                 dbProfiler.ExecuteStart(providerName, typeName, cmd, DMSQueryType.ExecuteScalar);
                 var retObj = cmd.ExecuteScalar();
                 dbProfiler.ExecuteFinish(providerName, typeName, cmd, DMSQueryType.ExecuteScalar, (DateTime.Now - time).TotalMilliseconds, null);
                 return(retObj);
             }
             dbProfiler.ExecuteStart(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery);
             var retInt = cmd.ExecuteNonQuery();
             dbProfiler.ExecuteFinish(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery, (DateTime.Now - time).TotalMilliseconds, null);
             return(retInt);
         }
         catch (Exception ex)
         {
             dbProfiler.OnError(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery, (DateTime.Now - time).TotalMilliseconds, ex);
             throw ex;
         }
     }
 }
コード例 #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
            }
        }
コード例 #3
0
        private static object Execute(IDbConnection conn, string typeName, string sql, bool ExecuteScalar, dynamic param = null, IDbTransaction transaction = null, int?commandTimeout = 30, CommandType?commandType = null)
#endif
        {
            DMSFrameException.ThrowIfNull(conn, sql);
            DateTime       time         = DateTime.Now;
            string         providerName = string.Empty;
            IDMSDbProfiler dbProfiler   = GetProfiler(out providerName);

            try
            {
                #region Execute
                IEnumerable multiExec = (object)param as IEnumerable;
                Identity    identity;
                CacheInfo   info = null;

                if (multiExec != null && !(multiExec is string))
                {
                    #region multiExec
                    bool isFirst = true;
                    int  total   = 0;
                    using (var cmd = SetupCommand(conn, typeName, transaction, sql, null, null, commandTimeout, commandType))
                    {
                        string masterSql = null;
                        foreach (var obj in multiExec)
                        {
                            if (isFirst)
                            {
                                masterSql = cmd.CommandText;
                                isFirst   = false;
                                identity  = new Identity(sql, cmd.CommandType, conn, null, obj.GetType(), null);
                                info      = CacheMapper.GetCacheInfo(identity);
                            }
                            else
                            {
                                cmd.CommandText = masterSql; // because we do magic replaces on "in" etc
                                cmd.Parameters.Clear();      // current code is Add-tastic
                            }
                            info.ParamReader(cmd, obj);
                            time = DateTime.Now;

                            try
                            {
                                dbProfiler.ExecuteStart(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery);
                                total += cmd.ExecuteNonQuery();
                                dbProfiler.ExecuteFinish(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery, (DateTime.Now - time).TotalMilliseconds, null);
                            }
                            catch (Exception exp)
                            {
                                dbProfiler.OnError(providerName, typeName, cmd, DMSQueryType.ExecuteNonQuery, (DateTime.Now - time).TotalMilliseconds, exp);
                            }
                        }
                    }
                    #endregion
                    return(total);
                }
                // nice and simple
                if ((object)param != null)
                {
                    identity = new Identity(sql, commandType, conn, null, (object)param == null ? null : ((object)param).GetType(), null);
                    info     = CacheMapper.GetCacheInfo(identity);
                }
                return(ExecuteCommand(conn, typeName, transaction, sql, ExecuteScalar, (object)param == null ? null : info.ParamReader, (object)param, commandTimeout, commandType));

                #endregion
            }
            catch (Exception ex)
            {
                Log.Debug(ReflectionUtils.GetMethodBaseInfo(System.Reflection.MethodBase.GetCurrentMethod()), string.Format("msg:{0},sql:{1},param:{2}", ex.Message, sql, param), ex);
                throw ex;
            }
        }