コード例 #1
0
ファイル: LogicalParser.cs プロジェクト: gnoleht/dn-gnol
        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();
        }
コード例 #2
0
        public static QuerySet <T> FromNothing <T>(Expression <Func <object, T> > Selector)
        {
            var ret    = new QuerySet <T>();
            var Params = new List <object>();

            ret.fields        = SelectorCompiler.GetFields(Selector.Body, new string[] { }, new string[] { }, Selector.Parameters, ref Params);
            ret.isFromNothing = true;
            ret.Params.AddRange(Params);
            return(ret);
        }
コード例 #3
0
ファイル: Db.cs プロジェクト: gnoleht/dn-gnol
        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);
        }
コード例 #4
0
ファイル: LogicalParser.cs プロジェクト: gnoleht/dn-gnol
 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();
 }
コード例 #5
0
ファイル: QuerySet.cs プロジェクト: gnoleht/dn-gnol
        /// <summary>
        /// Select clause
        /// </summary>
        /// <typeparam name="T2"></typeparam>
        /// <param name="selector">Example:p=>new {p.Code,p.Name,FullName=p.Code+" "+p.FirstName}</param>
        /// <returns></returns>
        public QuerySet <T2> Select <T2>(Expression <Func <T, T2> > selector)
        {
            var ret    = this.Clone <T2>();
            var Params = this.Params;
            var tbls   = this.table_names;

            if (!string.IsNullOrEmpty(this.table_name))
            {
                tbls = new string[] { this.table_name };
            }
            var selectFields = SelectorCompiler.GetFields(selector.Body, new string[] { ret.Schema }, tbls, selector.Parameters, ref Params);

            ret.Params = Params;
            ret.fields.Clear();
            ret.fields.AddRange(selectFields);
            return(ret);
        }
コード例 #6
0
ファイル: QuerySet.cs プロジェクト: gnoleht/dn-gnol
        public QuerySet <T> GroupBy(Expression <Func <T, object> > GroupFields)
        {
            var ret        = this.Clone <T>();
            var fieldBody  = ((LambdaExpression)GroupFields).Body;
            var ParamList  = ret.Params;
            var x          = GroupFields.Parameters[0].Type == typeof(T);
            var tableNames = new List <string>();

            if (this.table_names != null)
            {
                tableNames.AddRange(this.table_names);
            }
            if (!string.IsNullOrEmpty(this.table_name))
            {
                tableNames.Add(this.table_name);
            }
            var groupFieldsList = SelectorCompiler.GetFields(fieldBody, new string[] { ret.Schema }, tableNames.ToArray(), GroupFields.Parameters.Cast <ParameterExpression>().ToList(), ref ParamList);

            ret.groupbyList.AddRange(groupFieldsList);
            return(ret);
        }
コード例 #7
0
ファイル: QuerySet.cs プロジェクト: gnoleht/dn-gnol
        /// <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);
        }
コード例 #8
0
ファイル: QuerySet.cs プロジェクト: gnoleht/dn-gnol
        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);
        }
コード例 #9
0
        private static QuerySet <T> BuilJoin <T1, T2, T>(QuerySet <T1> qr1, QuerySet <T2> qr2, Expression <Func <T1, T2, bool> > JoinClause, Expression <Func <T1, T2, T> > selecttor, string JoinType)
        {
            var q1 = qr1 as QuerySet <T1>;
            var q2 = qr2 as QuerySet <T2>;

            if (q1.IsSubQuery())
            {
            }
            if (q2.IsSubQuery())
            {
            }
            var ret = new QuerySet <T>();

            ret.joinTimes = (q1.joinTimes + 1) * 10 + q2.joinTimes;

            var ltName = "l" + ret.joinTimes.ToString();
            var rtName = "r" + ret.joinTimes.ToString();
            var Params = new List <object>();

            Params.AddRange(q1.Params);
            Params.AddRange(q2.Params);
            var joinParams = new List <object>();
            var strJoin    = Joinner.GetJoinExpr(JoinClause.Body, new string[] { null, null }, new string[] { ltName, rtName }, JoinClause.Parameters, ref joinParams);

            strJoin = utils.RepairParameters(strJoin, Params, joinParams);
            Params.AddRange(joinParams);
            var leftSource  = (!q1.IsSubQuery())? utils.GetSource(q1):"(" + q1.ToSQLString() + ")";
            var rightSource = (!q2.IsSubQuery()) ? utils.GetSource(q2) : "(" + q2.ToSQLString() + ")";

            rightSource    = utils.RepairParameters(rightSource, q1.Params, q2.Params);
            ret.datasource = leftSource + " as " + Globals.Compiler.GetQName("", ltName) +
                             " " + JoinType + " join " + rightSource + " as " + Globals.Compiler.GetQName("", rtName) + " on " + strJoin;
            var refTables     = new List <string>();
            var refParameters = new List <ParameterExpression>();

            if (q1.table_names != null)
            {
                refTables.AddRange(q1.table_names);
            }

            if (q2.table_names != null)
            {
                refTables.AddRange(q2.table_names);
            }
            refTables.Add(ltName);
            refTables.Add(rtName);
            if (q1.fieldParams != null)
            {
                refParameters.AddRange(q1.fieldParams);
            }
            if (q2.fieldParams != null)
            {
                refParameters.AddRange(q2.fieldParams);
            }
            refParameters.AddRange(selecttor.Parameters);
            var retFields = SelectorCompiler.GetFields(selecttor.Body, new string[] { null, null }, refTables.ToArray(), refParameters.ToArray(), ref Params);

            ret.fields      = retFields;
            ret.fieldParams = new List <ParameterExpression>();
            ret.fieldParams.AddRange(refParameters);
            ret.parametersExpr = selecttor.Parameters;
            ret.Params         = Params;
            ret.table_names    = refTables.ToArray();
            return(ret);
        }
コード例 #10
0
ファイル: Db.cs プロジェクト: gnoleht/dn-gnol
        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);
        }