/// <summary> /// Use this method with precaution: only if your code parse result as IList immediately /// </summary> public static async Task <object> VectorValueOf(AsyncExprCtx ae, object funcOrValue) { while (true) { var asyncF = funcOrValue as LazyAsync; if (asyncF != null) { funcOrValue = await asyncF(ae); } else { var syncF = funcOrValue as LazySync; if (syncF != null) { funcOrValue = syncF(); } else { break; } } } //IList list = funcOrValue as IList; //if (list == null) // funcOrValue = new OneValueList(funcOrValue); return(funcOrValue); }
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 }
/// <summary> /// Function call context /// </summary> public AsyncExprCtx(Generator.Ctx funcCtx, AsyncExprCtx callerCtx, IList args, int nArgs) { if (funcCtx.parent != null) { Trace.Assert(funcCtx.parent.name2ndx == callerCtx.name2ndx); } else { Trace.Assert(funcCtx.values.Count == nArgs); } this.name2ndx = funcCtx.name2ndx; var values = funcCtx.values; int n = values.Count; ctxValues = new CtxValue[n]; //int nArgs = args.Count; for (int i = 0; i < nArgs; i++) { ctxValues[i] = new CtxValue(args[i], callerCtx); } for (int i = nArgs; i < n; i++) { ctxValues[i] = new CtxValue(values[i]); } parent = callerCtx; }
/// <summary> /// Function call context (with closures) /// </summary> /// <param name="name2ndx">Value name -> index</param> /// <param name="values">Values array</param> /// <param name="callerCtx">Calling context</param> /// <param name="args">Arguments array</param> /// <param name="declCtx">Context in which function is declared (for closures)</param> /// <param name="valuesNdx">Descriptor of function context values: /// -(k+1) = value from caller context; /// 0 = local value (from current context); /// +(k+1) = value from declaration context (closures emulation). /// </param> public AsyncExprCtx(IDictionary <string, int> name2ndx, IList values, IList args, AsyncExprCtx callerCtx, AsyncExprCtx declCtx, IList <int> valuesNdx) { this.name2ndx = name2ndx; int n = values.Count; ctxValues = new CtxValue[n]; for (int i = 0; i < valuesNdx.Count; i++) { int j = valuesNdx[i]; if (j < 0) { ctxValues[i] = new CtxValue(args[~j], callerCtx); } else if (j > 0) { ctxValues[i] = new CtxValue(declCtx, j - 1); } else { ctxValues[i] = new CtxValue(values[i]); } } parent = callerCtx; }
/// <summary> /// Nested (child) context with validation /// </summary> public AsyncExprCtx(Generator.Ctx ctx, IList values, AsyncExprCtx parent) : this(ctx.name2ndx, values) { Trace.Assert(parent != null); this.parent = parent; Trace.Assert(parent.name2ndx.Count == 0 || (ctx.parent != null && parent.name2ndx == ctx.parent.name2ndx)); }
public static async Task <object> binaryOpAsync(AsyncExprCtx ae, Fxy op, object a, object b, bool resultCanBeLazy) { var valueA = await ConstValueOf(ae, a); var valueB = await ConstValueOf(ae, b); return(binaryOpConst(op, valueA, valueB, resultCanBeLazy)); }
public static async Task <object> ExecQuery(AsyncExprCtx ctx, string query, string oraConnName, bool arrayResults) { var conn = (IDbConn)await ctx.GetValue(oraConnName); return(await conn.ExecCmd(new SqlCommandData() { Kind = CommandKind.Query, SqlText = query, ConvertMultiResultsToLists = arrayResults }, ctx.Cancellation)); }
public static async Task ConstValueOf(AsyncExprCtx ae, object funcOrValue, Action <object, object> onComplete, object state) { try { var res = await ConstValueOf(ae, funcOrValue); onComplete(res, state); } catch (Exception ex) { onComplete(ex.Wrap(), state); } }
public Task <object> Aggregate(AsyncExprCtx parent, IList data) { int i = aggCtx.name2ndx[sData]; var vals = new object[aggCtx.values.Count]; aggCtx.values.CopyTo(vals, 0); vals[i] = data; var aec = new AsyncExprCtx(aggCtx, vals, parent); return(OPs.ConstValueOf(aec, aggCode)); }
public Task <object> GetValue(AsyncExprCtx ae, int ndx) { if (ndx >= Generator.Ctx.ctxDepthStep) { return(parent.GetValue(ae, ndx - Generator.Ctx.ctxDepthStep)); } else { return(ctxValues[ndx].Get(this)); //aec } }
public static async Task <object> _call_func(AsyncExprCtx ae, int funcIndex, IList args) { var fObj = await ae.GetValue(funcIndex); var f = fObj as AsyncFn ?? ((FuncDef)fObj).func as AsyncFn; if (f == null) { throw new InvalidCastException("_call_func: AsyncFn expected"); } return(await f(ae, args)); }
public static async Task <object> ConstValueOf(AsyncExprCtx ae, object funcOrValue) { while (true) { var asyncF = funcOrValue as LazyAsync; if (asyncF != null) { ae.Cancellation.ThrowIfCancellationRequested(); funcOrValue = await asyncF(ae); } else { var syncF = funcOrValue as LazySync; if (syncF != null) { funcOrValue = syncF(); } else { break; } } } var list = funcOrValue as IList; if (list != null && list.Count > 0) { if (list is ListOfVals) { var res = new object[list.Count]; for (int i = 0; i < list.Count; i++) { object obj = list[i]; res[i] = await ConstValueOf(ae, obj); } return(res); } else if (list is ListOfSync) { return(ConstValueOf(funcOrValue)); } else { return(funcOrValue); } } else { return(funcOrValue); } }
public CtxValue(object valueToCalc, AsyncExprCtx context) { this.value = (LazyAsync) delegate(AsyncExprCtx ae) { if (ae != context) { return(OPs.ConstValueOf(context, valueToCalc)); } else { return(OPs.ConstValueOf(ae, valueToCalc)); } }; }
public static async Task <object> ISERR(AsyncExprCtx ctx, IList args) { try { object value = await OPs.ConstValueOf(ctx, args[0]); var lst = value as IList; if (lst != null) { var res = lst.ToArray(item => item is Exception || item is W.Common.ErrorWrapper); return(res); } else { return(value is Exception || value is W.Common.ErrorWrapper); } } catch (OperationCanceledException) { throw; } catch { return(True); } }
public async Task <object> Get(AsyncExprCtx ae) { var s = sema; if (s != null) { if (s == errFlag) { throw (Exception)value; } await s.WaitAsync(ae.Cancellation); try { if (sema != null) { // enter here only once if (sema == errFlag) { throw (Exception)value; } try { value = await OPs.VectorValueOf(ae, value); sema = null; } catch (OperationCanceledException) { throw; } catch (Exception ex) { value = ex; var tmp = sema; sema = errFlag; //tmp.Dispose(); throw; } } } finally { s.Release(); } } return(value); }
public Task LoadData(AsyncExprCtx rootAec, Expr loadingExpr, CancellationToken ct) { int i = aggCtx.IndexOf(sFactory); if (i >= 0) { var f = (WeightedSums.Factory)aggCtx[i]; foreach (var n2i in aggCtx.name2ndx) { if (n2i.Key.StartsWith(sAkk)) { aggCtx[n2i.Value] = f.NewSums(); } } } var loadingCtx = new Generator.Ctx(aggCtx.parent); loadingCtx.CreateValue(sUsedParamsDict, aggCtx[aggCtx.IndexOf(sUsedParamsDict)]); loadingCtx.CreateValue(sLoadingInfo, this); var loadingCode = Generator.Generate(loadingExpr, loadingCtx); var ae = new AsyncExprCtx(loadingCtx, loadingCtx.values, rootAec); return(OPs.ConstValueOf(ae, loadingCode)); }
/// <summary> /// Context for next loop iteration /// </summary> /// <param name="prevIterationContext"></param> /// <param name="nextIterationValues"></param> /// <param name="ndxOfValsToCalc"></param> public AsyncExprCtx(AsyncExprCtx prevIterationContext, IList nextIterationValues, IEnumerable <int> ndxOfValsToCalc) { int n = nextIterationValues.Count; name2ndx = prevIterationContext.name2ndx; parent = prevIterationContext.parent; Trace.Assert(parent != null); ctxValues = new CtxValue[n]; for (int i = 0; i < n; i++) { ctxValues[i] = prevIterationContext.ctxValues[i]; } foreach (int i in ndxOfValsToCalc) { if (i < 0) { ctxValues[~i] = new CtxValue(nextIterationValues[~i], prevIterationContext); } else { ctxValues[i] = new CtxValue(nextIterationValues[i]); } } }
public CtxValue(AsyncExprCtx context, int index) { this.value = (LazyAsync)((AsyncExprCtx ae) => context.GetValue(ae, index)); }
//public static async Task CommitPreferredConn(AsyncExprCtx ctx) //{ // var conn = (IDbConn)await ctx.GetValue(DefaultDbConnName); // await conn.Commit(ctx.Cancellation); //} //public static async Task<object> ExecNonQuery(AsyncExprCtx ctx, string cmdText, string oraConnName) //{ // var conn = (IOraConn)await ctx.GetValue(ctx, oraConnName); // return await conn.ExecCmd(new OracleCommandData() { Kind = CommandKind.NonQuery, SqlText = cmdText }, ctx.Cancellation); //} public static async Task <object> CachedExecQuery(AsyncExprCtx ctx, string query, string oraConnName, bool arrayResults) { return(await FuncDefs_Core._Cached(ctx, query, (LazyAsync)(aec => ExecQuery(aec, query, oraConnName, arrayResults)), DateTime.Now + TimeSpan.FromMinutes(5), TimeSpan.Zero)); }
public static async Task <object> Exec(AsyncExprCtx ctx, string oraConnName, SqlCommandData oraCmd) { var conn = (IDbConn)await ctx.GetValue(oraConnName); return(await conn.ExecCmd(oraCmd, ctx.Cancellation)); }
public static async Task <object> AND(AsyncExprCtx ae, IList args) { bool[] results = null; int cnt = args.Count; for (int j = 0; j < cnt; j++) { object v = await OPs.ConstValueOf(ae, args[j]); try { IList lst = v as IList; if (lst == null) { if (!OPs.xl2bool(v)) { if (results == null) { return(False); // scalar, scalar } else { // vector, scalar for (int i = results.Length - 1; i >= 0; i--) { results[i] = false; } return(results); } } } else { bool allFalse = true; if (results == null) { // scalar, vector int n = lst.Count; results = new bool[n]; for (int i = n - 1; i >= 0; i--) { bool tmp = OPs.xl2bool(lst[i]); allFalse = allFalse && !tmp; results[i] = tmp; } } else { // vector, vector int n = Math.Min(results.Length, lst.Count); if (n < results.Length) { Array.Resize <bool>(ref results, n); } for (int i = n - 1; i >= 0; i--) { if (!results[i]) { continue; } bool tmp = OPs.xl2bool(lst[i]); allFalse = allFalse && !tmp; results[i] = tmp; } } if (allFalse) { return(results); } } } catch (OperationCanceledException) { throw; } catch { } } return(results ?? True); }
public static async Task <object> CalcAsync(AsyncExprCtx ae, object arg, int nIns, int nOuts, AsyncFn calc, params int[] indexesOfNotNullableArgs) { var dict = (W.Common.IIndexedDict)arg; try { var data = dict.ValuesList; if (data.NotNullsAt(indexesOfNotNullableArgs) == false) { return(null); } var maxTime = DateTime.MinValue; foreach (ITimedObject to in data) { if (to != null && !to.IsEmpty && maxTime < to.Time) { maxTime = to.Time; } } var res = await calc(ae, data); if (res == null) { return(null); } int n = data.Length; if (maxTime > DateTime.MinValue) { var lst = res as IList; if (lst != null) { for (int i = lst.Count - 1; i >= 0; i--) { lst[i] = TimedObject.TryAsTimed(lst[i], maxTime, DateTime.MaxValue); } } else { res = TimedObject.TryAsTimed(res, maxTime, DateTime.MaxValue); } } if (n == nIns) { return(res); } else { var outs = new object[nOuts + n - nIns]; var lst = res as IList; if (lst != null) { lst.CopyTo(outs, 0); } else { outs[0] = res; } for (int i = n - 1; i >= nIns; i--) { outs[nOuts + i - nIns] = data[i]; } return(outs); } } catch (Exception ex) { Utils.ToLog(ex.Message, dict); return(ex.Wrap()); } }
/// <summary> /// Nested (child) context /// </summary> /// <param name="name2ndx"></param> /// <param name="values"></param> /// <param name="parent"></param> public AsyncExprCtx(IDictionary <string, int> name2ndx, IList values, AsyncExprCtx parent) : this(name2ndx, values) { Trace.Assert(parent != null); this.parent = parent; }
async Task <object> Async(AsyncExprCtx ae) { return(Const(await ConstValueOf(ae, argCode))); }
public AsyncExprCtx() { parent = this; ctxValues = EmptyCtxValues; name2ndx = EmptyName2Ndx; }