Exemplo n.º 1
0
 /// <summary>
 /// 获取查询信息
 /// </summary>
 /// <typeparam name="valueType">对象类型</typeparam>
 /// <typeparam name="modelType">模型类型</typeparam>
 /// <param name="sqlTool">SQL操作工具</param>
 /// <param name="connection"></param>
 /// <param name="query">查询信息</param>
 /// <param name="readValue"></param>
 /// <returns>对象集合</returns>
 internal virtual LeftArray <valueType> Select <valueType, modelType>(Sql.Table <valueType, modelType> sqlTool, ref DbConnection connection, ref SelectQuery <modelType> query, Func <DbDataReader, valueType> readValue)
     where valueType : class, modelType
     where modelType : class
 {
     try
     {
         if (query.Sql != null)
         {
             if (connection == null)
             {
                 connection = GetConnection();
             }
             if (connection != null)
             {
                 if (query.IndexFieldName != null)
                 {
                     sqlTool.CreateIndex(connection, query.IndexFieldName, false);
                     query.IndexFieldName = null;
                 }
                 bool isFinally = false;
                 try
                 {
                     using (DbCommand command = getCommand(connection, query.Sql))
                         using (DbDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult))
                         {
                             int skipCount = query.SkipCount;
                             while (skipCount != 0 && reader.Read())
                             {
                                 --skipCount;
                             }
                             if (skipCount == 0)
                             {
                                 LeftArray <valueType> array = new LeftArray <valueType>();
                                 while (reader.Read())
                                 {
                                     valueType value = readValue(reader);
                                     if (value != null)
                                     {
                                         array.Add(value);
                                     }
                                 }
                                 isFinally = true;
                                 return(array.NotNull());
                             }
                         }
                     isFinally = true;
                 }
                 finally
                 {
                     if (!isFinally)
                     {
                         sqlTool.Log.Add(AutoCSer.Log.LogType.Error, query.Sql);
                     }
                 }
             }
         }
     }
     finally { query.Free(); }
     return(default(LeftArray <valueType>));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 获取查询信息
        /// </summary>
        /// <typeparam name="valueType">对象类型</typeparam>
        /// <typeparam name="modelType">模型类型</typeparam>
        /// <param name="sqlTool">SQL操作工具</param>
        /// <param name="connection"></param>
        /// <param name="query">查询信息</param>
        /// <returns>对象集合</returns>
        internal override LeftArray <valueType> Select <valueType, modelType>(Sql.Table <valueType, modelType> sqlTool, ref DbConnection connection, ref SelectQuery <modelType> query)
        {
            try
            {
                if (query.Sql != null)
                {
                    if (connection == null)
                    {
                        connection = GetConnection();
                    }
                    if (connection != null)
                    {
                        bool isFinally = false;
                        try
                        {
                            using (DbCommand command = getCommand(connection, query.Sql))
                                using (DbDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult))
                                {
                                    int skipCount = query.SkipCount;
                                    while (skipCount != 0 && reader.Read())
                                    {
                                        --skipCount;
                                    }
                                    if (skipCount == 0)
                                    {
                                        LeftArray <valueType> array = new LeftArray <valueType>();
                                        while (reader.Read())
                                        {
                                            valueType value = AutoCSer.Emit.Constructor <valueType> .New();

                                            DataModel.Model <modelType> .Setter.Set(reader, value, query.MemberMap);

                                            array.Add(value);
                                        }
                                        isFinally = true;
                                        return(array.NotNull());
                                    }
                                }
                            isFinally = true;
                        }
                        finally
                        {
                            if (!isFinally)
                            {
                                sqlTool.Log.Add(AutoCSer.Log.LogType.Error, query.Sql);
                            }
                        }
                    }
                }
            }
            finally { query.Free(); }
            return(default(LeftArray <valueType>));
        }
Exemplo n.º 3
0
 /// <summary>
 /// 获取查询信息
 /// </summary>
 /// <typeparam name="valueType">对象类型</typeparam>
 /// <param name="connection"></param>
 /// <param name="sql"></param>
 /// <param name="readValue"></param>
 /// <param name="log"></param>
 /// <returns>对象集合</returns>
 internal virtual LeftArray <valueType> Select <valueType>(ref DbConnection connection, string sql, Func <DbDataReader, valueType> readValue, ILog log)
     where valueType : class
 {
     if (connection == null)
     {
         connection = GetConnection();
     }
     if (connection != null)
     {
         bool isFinally = false;
         try
         {
             using (DbCommand command = getCommand(connection, sql))
                 using (DbDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult))
                 {
                     LeftArray <valueType> array = new LeftArray <valueType>();
                     while (reader.Read())
                     {
                         valueType value = readValue(reader);
                         if (value != null)
                         {
                             array.Add(value);
                         }
                     }
                     isFinally = true;
                     return(array.NotNull());
                 }
         }
         finally
         {
             if (!isFinally)
             {
                 (log ?? AutoCSer.Log.Pub.Log).Add(AutoCSer.Log.LogType.Error, sql);
             }
         }
     }
     return(default(LeftArray <valueType>));
 }