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 virtual LeftArray <valueType> Select <valueType, modelType>(Sql.Table <valueType, modelType> sqlTool, ref DbConnection connection, ref SelectQuery <modelType> query)
            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;
                        }
                        using (DbCommand command = getCommand(connection, query.Sql))
                        {
                            DbDataReader reader = null;
                            try
                            {
                                reader = command.ExecuteReader(CommandBehavior.SingleResult);
                            }
                            catch (Exception error)
                            {
                                sqlTool.Log.add(AutoCSer.Log.LogType.Error, error, query.Sql);
                            }
                            if (reader != null)
                            {
                                using (reader)
                                {
                                    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);
                                        }
                                        return(array);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally { query.Free(); }
            return(default(LeftArray <valueType>));
        }