internal static object ParseCase(MethodCallExpression cx, string[] Schemas, string[] tables, IList <ParameterExpression> parameters, MemberInfo[] members, int memberIndex, ref List <object> paramList) { var condition = cx.Arguments[0]; var strConditoinal = string.Empty; var isEmptyConditional = false; if (condition is ConstantExpression && ((ConstantExpression)condition).Value == null) { isEmptyConditional = true; } if (cx.Arguments[1] is NewArrayExpression) { var nax = cx.Arguments[1] as NewArrayExpression; if (nax.Expressions.Count < 2) { throw new Exception(@"It looks like you missing parameter in Case\r\n . Call Case should be SqlFuncs.Case(<Expr>,SqlFuncs.When(<expr or null>,expr1,..,exprn,<else return value>))"); } var elseExpr = nax.Expressions[nax.Expressions.Count - 1]; string strElse = SelectorCompiler.Gobble <string>(elseExpr, Schemas, tables, parameters, members, memberIndex, ref paramList); var whenList = new List <string>(); if (!isEmptyConditional) { strConditoinal = SelectorCompiler.Gobble <string>(condition, Schemas, tables, parameters, members, memberIndex, ref paramList); } for (var i = 0; i < nax.Expressions.Count - 1; i++) { var x = nax.Expressions[i]; if (x is MethodCallExpression) { var args = ((MethodCallExpression)x).Arguments; var strWhen = SelectorCompiler.Gobble <object>(args[0], Schemas, tables, parameters, members, memberIndex, ref paramList); var strThen = SelectorCompiler.Gobble <string>(args[1], Schemas, tables, parameters, members, memberIndex, ref paramList); whenList.Add(" when " + strWhen + " then " + strThen); } else if (x is UnaryExpression) { var ux = x as UnaryExpression; } else { throw new NotImplementedException(); } } if (isEmptyConditional) { return("Case " + string.Join("", whenList.ToArray()) + " else " + elseExpr + " end "); } else { return("Case " + strConditoinal + " " + string.Join("", whenList.ToArray()) + " else " + elseExpr + " end "); } //var strElse = SelectorCompiler.Gobble<string>(elseExpr, tables,nax.Expressions.Cast<ParameterExpression>().ToList(), Members,-1,ref paramList); } if (cx.Arguments.Count < 3) { } throw new NotImplementedException(); }
public DeleteDataResult <T> DeleteData <T>(string Schema, string TableName, Expression <Func <T, bool> > Where, bool ReturnError = false) { //throw new NotImplementedException(); var ex = Where.Body as BinaryExpression; var lstParams = new List <object>(); string strWhere = SelectorCompiler.Gobble <string>(Where.Body, new string[] { Schema }, new string[] { TableName }, Where.Parameters, null, -1, ref lstParams); var sql = "delete from " + Globals.Compiler.GetQName(Schema, TableName) + " Where " + strWhere; var ret = this.db.ExecCommand(sql, ReturnError, lstParams.ToArray()); return(null); }
internal static object ParseCOALESCE(MethodCallExpression cx, string[] schemas, string[] tables, IList <ParameterExpression> parameters, MemberInfo[] members, int memberIndex, ref List <object> paramList) { //new System.Linq.Expressions.Expression.NewArrayExpressionProxy(new System.Linq.Expressions.Expression.MethodCallExpressionProxy(cx).Arguments[0]).Expressions if (cx.Arguments[0] is NewArrayExpression) { var nax = cx.Arguments[0] as NewArrayExpression; var agrs = new List <string>(); foreach (var x in nax.Expressions) { var strParam = SelectorCompiler.Gobble <string>(x, schemas, tables, parameters, members, memberIndex, ref paramList); agrs.Add(strParam); } return("COALESCE(" + string.Join(",", agrs.ToArray()) + ")"); } throw new NotImplementedException(); }
/// <summary> /// Where clause /// </summary> /// <param name="expr"></param> /// <returns></returns> public QuerySet <T> Filter(Expression <Func <T, bool> > expr) { var ret = this.Clone <T>(); var ex = expr.Body as BinaryExpression; if (this.sequenceJoin > 0) { ret.sequenceJoin++; ret.SetDataSource("(" + this.ToSQLString() + ") as " + Globals.Compiler.GetQName("", "t" + ret.sequenceJoin.ToString())); ret.fields.Clear(); ret.SetTableName("t" + ret.sequenceJoin.ToString()); } var lstParams = ret.Params; ret.where = SelectorCompiler.Gobble <string>(expr.Body, new string[] { ret.Schema }, new string[] { ret.GetTableName() }, expr.Parameters, null, -1, ref lstParams); ret.Params = lstParams; return(ret); }
public QuerySet <T> SortAsc(params Expression <Func <T, object> >[] Expr) { foreach (var x in Expr) { if (x.Body is MemberExpression) { var pList = new List <object>(); var mx = x.Body as MemberExpression; var Fields = SelectorCompiler.Gobble <string>(x.Body, new string[] { this.Schema }, new string[] { this.table_name }, x.Parameters.ToList(), new MemberInfo[] { mx.Member }, 0, ref pList); } //SortCompiler.GetFields(x); //foreach (var f in Fields) //{ // this.sortList.Add(new SortingInfo() // { // FieldName = f, // SortType = "asc" // }); //} } return(this); }
public UpdateDataResult <T> UpdateData <T>(string Schema, string TableName, Expression <Func <T, bool> > Where, object DataItem, bool ReturnError = false) { if (DataItem == null) { throw new Exception("It looks like you forgot set data parameter for update command"); } var ret = new UpdateDataResult <T>(); var properties = DataItem.GetType().GetProperties().ToList(); var Params = properties.Select(p => new ParamInfo() { Name = p.Name, Value = p.GetValue(DataItem), Index = properties.IndexOf(p), DataType = p.PropertyType }).ToList(); var tbl = this.GetTableInfo(this.db.GetConnectionString(), Schema, TableName); //Check require fields: var requiredFields = this.GetRequireFieldsForUpdate(tbl, Params); var invalidDataType = this.GetInvalidDataTypeFields(tbl, Params); if (requiredFields.Count() > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.MissingFields, Fields = requiredFields.Select(p => p.Name).ToArray() }; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Missing fields:" + string.Join(",", err.Fields)) { Detail = err }); } } if (invalidDataType.Count() > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.InvalidDataType, Fields = invalidDataType.Select(p => p.Name).ToArray() }; ret.Error = err; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Invalid data type at fields:" + string.Join(",", err.Fields)) { Detail = err }); } } var exceedFields = this.GetExceedFields(tbl, Params); if (exceedFields.Count > 0) { var err = new ErrorAction() { ErrorType = DataActionErrorTypeEnum.ExceedSize, Fields = exceedFields.Select(p => p.Name).ToArray() }; ret.Error = err; if (ReturnError) { ret.Error = err; return(ret); } else { throw (new DataActionError("Exceed field len at fields:" + string.Join(",", err.Fields)) { Detail = err }); } } var ex = Where.Body as BinaryExpression; var lstParams = new List <object>(); lstParams.AddRange(Params.Select(p => p.Value)); var strWhere = SelectorCompiler.Gobble <string>(Where.Body, new string[] { Schema }, new string[] { TableName }, Where.Parameters, null, -1, ref lstParams); var Sql = "Update " + Globals.Compiler.GetQName(Schema, TableName) + " set " + String.Join(",", Params.Select(p => Globals.Compiler.GetQName("", p.Name) + "={" + p.Index + "}").ToArray()) + " where " + strWhere; var retUpdate = this.db.UpdateData(Sql, Schema, TableName, tbl, lstParams); ret.Data = retUpdate.Data; ret.DataItem = retUpdate.DataItem; ret.EffectedRowCount = retUpdate.EffectedRowCount; ret.Error = retUpdate.Error; ret.NewID = retUpdate.NewID; return(ret); }