public static async Task <IIndexedDict[]> GetSchema(AsyncExprCtx ctx, QueryTemplate qt) { var conn = (IDbConn)await ctx.GetValue(qt.connName); var schema = (System.Data.DataTable) await conn.ExecCmd(new SqlCommandData() { Kind = CommandKind.GetSchema, SqlText = qt.queryTemplateText.Replace("{0}", string.Empty), ConvertMultiResultsToLists = qt.arrayResults }, ctx.Cancellation); int nCols = schema.Columns.Count; var key2ndx = new Dictionary <string, int>(nCols, StringComparer.OrdinalIgnoreCase); for (int i = 0; i < nCols; i++) { key2ndx[schema.Columns[i].ColumnName] = i; } int nRows = schema.Rows.Count; var res = new IIndexedDict[nRows]; for (int i = 0; i < nRows; i++) { res[i] = ValuesDictionary.New(schema.Rows[i].ItemArray, key2ndx); } return(res); // todo }
IIndexedDict[] ReadGroupedRows(DbDataReader rdr) { int n = rdr.FieldCount; var key2ndx = new Dictionary <string, int>(StringComparer.OrdinalIgnoreCase); for (int i = 0; i < n; i++) { var key = rdr.GetName(i); key2ndx.Add(key, i); } var lst = new OPs.ListOf <IIndexedDict>(); object currKey = null; var currValues = new List <object> [n - 1]; for (int i = 1; i < n; i++) { currValues[i - 1] = new List <object>(); } var values = new object[n]; while (rdr.Read()) { rdr.GetValues(values); bool keyChanged = currKey == null || W.Common.Cmp.CmpKeys(currKey, values[0]) != 0; if (keyChanged) { if (currKey != null) { // store akkumulated values lists var vals = new object[n]; vals[0] = currKey; for (int i = 1; i < n; i++) { vals[i] = currValues[i - 1].ToArray(); currValues[i - 1].Clear(); } lst.Add(ValuesDictionary.New(vals, key2ndx)); } currKey = values[0]; } for (int i = 1; i < n; i++) { currValues[i - 1].Add(values[i]); // akkumulate values in lists } } if (currKey != null) { // store akkumulated values lists var vals = new object[n]; vals[0] = currKey; for (int i = 1; i < n; i++) { vals[i] = currValues[i - 1].ToArray(); currValues[i - 1].Clear(); } lst.Add(ValuesDictionary.New(vals, key2ndx)); } return(lst.ToArray()); }
public async Task <IReadOnlyDictionary <string, object> > NextRowData(Dictionary <string, object> rowValues, bool forRes = false) { var rc = forRes ? meta.ctxRes : meta.ctxRow; var values = new object[rc.values.Count]; rc.values.CopyTo(values, 0); var k2n = rc.name2ndx; values[k2n[CellValsRef.name]] = rowValues; var rowCtx = new AsyncExprCtx(rc, values, this); var prevVals = (OPs.ListOfConst) await this.GetValue(PrevValsRef.name); int nValsInCtx = values.Length; values[iKey] = await rowCtx.GetValue(iKey); values[iSubkey] = await rowCtx.GetValue(iSubkey); values[iRowVals] = rowValues; for (int i = iRowVals + 1; i < values.Length; i++) { values[i] = await rowCtx.GetValue(i); } for (int i = 0; i < values.Length; i++) { prevVals[i] = values[i]; } if (values[iKey] == null) { // skip row if error or empty string datakey return(null); } return(ValuesDictionary.New(values, k2n)); }
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()); }