コード例 #1
0
ファイル: DbConnPool.cs プロジェクト: WVitek/DotGlue
        private static object ReadRows(DbDataReader rdr)
        {
            int nFields         = rdr.FieldCount;
            var key2ndx         = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase);
            var lstUsedFields   = new List <int>(nFields);
            int iStartTimeField = -1;
            int iEndTimeField   = -1;

            for (int i = 0; i < nFields; i++)
            {
                var key = rdr.GetName(i);
                if (string.Compare(key, nameof(START_TIME), StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    iStartTimeField = i; continue;
                }
                else if (iStartTimeField >= 0 && (
                             string.Compare(key, "end_time__dt", StringComparison.InvariantCultureIgnoreCase) == 0 ||
                             string.Compare(key, nameof(END_TIME), StringComparison.InvariantCultureIgnoreCase) == 0
                             ))
                {
                    iEndTimeField = i; continue;
                }
                key2ndx.Add(key, lstUsedFields.Count);
                lstUsedFields.Add(i);
            }

            var usedFields  = lstUsedFields.ToArray();
            int nUsedFields = usedFields.Length;

            var lst = new OPs.ListOf <IIndexedDict>();

            if (iEndTimeField >= 0)
            {
                var values = new object[nFields];
                while (rdr.Read())
                {
                    rdr.GetValues(values);
                    var endTime = values[iEndTimeField];
                    var vals    = new object[nUsedFields];
                    var dtBeg   = (DateTime)values[iStartTimeField];
                    var dtEnd   = (endTime == DBNull.Value) ? DateTime.MaxValue : (DateTime)endTime;
                    if (dtBeg == dtEnd)
                    {
                        dtBeg = dtEnd;
                    }
                    //vals[0] = TimedObject.Timed(dtBeg, dtEnd, values[0]);
                    for (int i = 0; i < nUsedFields; i++)
                    {
                        vals[i] = TimedObject.Timed(dtBeg, dtEnd, values[usedFields[i]]);
                    }
                    lst.Add(new ValuesDictionary(vals, key2ndx, dtBeg, dtEnd));
                }
            }
            else if (iStartTimeField >= 0)
            {
                var      values   = new object[nFields];
                object[] prevs    = null;
                var      prevTime = DateTime.MinValue;
                while (rdr.Read())
                {
                    rdr.GetValues(values);
                    var time = (DateTime)values[iStartTimeField];
                    if (prevs != null)
                    {
                        var dtBeg = prevTime;
                        var dtEnd = (Cmp.CmpKeys(prevs[0], values[0]) == 0) ? time : DateTime.MaxValue;
                        if (dtBeg == dtEnd)
                        {
                            continue;
                        }
                        for (int i = 0; i < nUsedFields; i++)
                        {
                            prevs[i] = TimedObject.Timed(dtBeg, dtEnd, prevs[i]);
                        }
                        lst.Add(new ValuesDictionary(prevs, key2ndx, dtBeg, dtEnd));
                    }
                    prevTime = time;
                    var vals = new object[nUsedFields];
                    for (int i = 0; i < nUsedFields; i++)
                    {
                        vals[i] = values[usedFields[i]];
                    }
                    prevs = vals;
                }
                if (prevs != null)
                {
                    for (int i = 0; i < nUsedFields; i++)
                    {
                        prevs[i] = TimedObject.Timed(prevTime, DateTime.MaxValue, prevs[i]);
                    }
                    lst.Add(new ValuesDictionary(prevs, key2ndx, prevTime, DateTime.MaxValue));
                }
            }
            else
            {
                System.Diagnostics.Trace.Assert(nFields == nUsedFields);
                while (rdr.Read())
                {
                    var values = new object[nFields];
                    rdr.GetValues(values);
                    for (int i = values.Length - 1; i >= 0; i--)
                    {
                        values[i] = Minimize(values[i]);
                    }
                    lst.Add(ValuesDictionary.New(values, key2ndx));
                }
            }
#if OCP_LOG
            using (var writer = System.IO.File.AppendText("OraConnPool.log"))
            {
                writer.WriteLine(string.Join("\t", key2ndx.Keys));
                foreach (var vals in lst)
                {
                    writer.WriteLine(string.Join("\t", vals.ValuesList));
                }
                writer.Flush();
            }
#endif
            return(lst.ToArray());
        }