NPoco icon indicating copy to clipboard operation
NPoco copied to clipboard

More than 4 Multiple Result Sets Mapping

Open yilativ opened this issue 10 years ago • 5 comments

Hi, I do see that there's a Pull Request that will add ability to do an unlimited amount of mapping with FetchMultiple? or Even pass our own Query(Type[]) so we are not limited by 4 result sets.

Is there a plan for integrating that into the main library? It seems that pull request has been out for a while now.

Thank you for all the work on this.

yilativ avatar May 30 '16 19:05 yilativ

We have been migrating to NPoco for almost all data access. However this is one area that is preventing us from fully ripping out our internal way of handling multiple items. At the moment we return 8 result sets. I would love to be able to fully migrate everything over 100% in our code base.

Thanks.

ahazelwood avatar Dec 12 '19 12:12 ahazelwood

Do we have this feature implemented? The issue has been closed, but I cannot see in the latest version the possibility to fetch more than 4 result sets using FetchMultiple.

akiftariq avatar Jul 29 '21 14:07 akiftariq

I faced with the same problem. Anybody has a solution for this?

joni1700 avatar Aug 11 '23 21:08 joni1700

I had a similar situation, but internally I have a helper class that handles this logic for me. It was tedious, but I wound up creating a bunch of methods similar to the following. Hopefully that gets you a starting point.

        /// <summary>
        /// Fetches multiple result sets
        /// </summary>
        /// <typeparam name="T1">The type of the 1.</typeparam>
        /// <typeparam name="T2">The type of the 2.</typeparam>
        /// <typeparam name="T3">The type of the 3.</typeparam>
        /// <typeparam name="T4">The type of the 4.</typeparam>
        /// <typeparam name="T5">The type of the 5.</typeparam>
        /// <typeparam name="T6">The type of the 6.</typeparam>
        /// <typeparam name="T7">The type of the 7.</typeparam>
        /// <typeparam name="T8">The type of the 8.</typeparam>
        /// <param name="dsn">The DSN.</param>
        /// <param name="procedure">The procedure.</param>
        /// <param name="args">The arguments.</param>
        /// <returns></returns>
        public static Tuple<List<T1>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>, List<T7>, Tuple<List<T8>>> FetchMultiple<T1, T2, T3, T4, T5, T6, T7, T8>(string dsn, string procedure, params object[] args) {
            var list1 = new List<T1>();
            var list2 = new List<T2>();
            var list3 = new List<T3>();
            var list4 = new List<T4>();
            var list5 = new List<T5>();
            var list6 = new List<T6>();
            var list7 = new List<T7>();
            var list8 = new List<T8>();

            using (var db = new SqlDatabase(dsn)) {
                var sql = new StringBuilder();
                sql.Append(procedure.StartsWith("exec ", StringComparison.InvariantCultureIgnoreCase) ? procedure : $"exec {procedure} ");
                if (!procedure.Contains('@')) {
                    for (var i = 0; i < args.Length; i++) {
                        sql.Append($"@{i}");
                        if (i < args.Length - 1)
                            sql.Append(", ");
                    }
                }

                try {
                    var types = new List<Type> {
                    typeof(T1),
                    typeof(T2),
                    typeof(T3),
                    typeof(T4),
                    typeof(T5),
                    typeof(T6),
                    typeof(T7),
                    typeof(T8)
                };

                    var pdf = new PocoDataFactory(db.Mappers);
                    var conn = db.OpenSharedConnection();
                    using var cmd = db.CreateCommand(conn.Connection, CommandType.Text, sql.ToString(), args);
                    using var rdr = cmd.ExecuteReader();
                    var typeIndex = 1;
                    do {
                        var pd = pdf.ForType(types[typeIndex - 1]);
                        var factory = new MappingFactory(pd, rdr);

                        while (true) {
                            if (!rdr.Read())
                                break;

                            switch (typeIndex) {
                                case 1:
                                    list1.Add((T1)factory.Map(rdr, default(T1)));
                                    break;
                                case 2:
                                    list2.Add((T2)factory.Map(rdr, default(T2)));
                                    break;
                                case 3:
                                    list3.Add((T3)factory.Map(rdr, default(T3)));
                                    break;
                                case 4:
                                    list4.Add((T4)factory.Map(rdr, default(T4)));
                                    break;
                                case 5:
                                    list5.Add((T5)factory.Map(rdr, default(T5)));
                                    break;
                                case 6:
                                    list6.Add((T6)factory.Map(rdr, default(T6)));
                                    break;
                                case 7:
                                    list7.Add((T7)factory.Map(rdr, default(T7)));
                                    break;
                                case 8:
                                    list8.Add((T8)factory.Map(rdr, default(T8)));
                                    break;
                            }
                        }

                        // Increment the Type Index
                        typeIndex++;
                    } while (rdr.NextResult());
                }
                finally {
                    db.CloseSharedConnection();
                }
            }

            return Tuple.Create(list1, list2, list3, list4, list5, list6, list7, list8);
        }

ahazelwood avatar Aug 11 '23 21:08 ahazelwood

Thank you so much! It's saved my day :)

joni1700 avatar Aug 12 '23 08:08 joni1700