Exemple #1
0
        private static string ToSql(MemberExpression Body, TipoOperacion linkingType)
        {
            string propertyValueResult;
            string propertyName;
            string link;

            propertyValueResult = SqlValueUtil.GetValue(true);

            propertyName = Body.Member.Name;

            link = GetOperator(linkingType, propertyValueResult);

            return(string.Format("{0} {1} {2}", propertyName, "=", propertyValueResult));
        }
Exemple #2
0
        public static IDictionary <string, string> GetKeysValues <T>(IClassConfiguration configuration, ICommandConfiguration commandConfiguration, T entity)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var aSqlFields = new List <SqlField>();

            if (configuration.PropsInclude)
            {
                var props = typeof(T).GetProperties()
                            .Where(c => c.GetCustomAttributes(typeof(NonQuerable), false).Count() == 0)
                            .Select(x => new SqlField()
                {
                    name = x.Name, value = entity != null ? SqlValueUtil.GetValue(x.GetValue(entity)) : null
                });

                aSqlFields.AddRange(props);
            }

            if (configuration.FieldsInclude)
            {
                var fields = typeof(T).GetFields()
                             .Where(c => c.GetCustomAttributes(typeof(NonQuerable), false).Count() == 0)
                             .Select(x => new SqlField()
                {
                    name = x.Name, value = entity != null ? SqlValueUtil.GetValue(x.GetValue(entity)) : null
                });

                aSqlFields.AddRange(fields);
            }

            if (commandConfiguration != null)
            {
                if (!commandConfiguration.IncludeId)
                {
                    aSqlFields = aSqlFields
                                 .Where(x => x.name.ToUpper() != commandConfiguration.PrimaryKeyTable.ToUpper())
                                 .ToList();
                }
            }
            var Elementos = aSqlFields.ToDictionary(x => x.name, y => y.value);

            return(Elementos);
        }
Exemple #3
0
        private static string ToSql(BinaryExpression body)
        {
            if (body.NodeType != ExpressionType.AndAlso && body.NodeType != ExpressionType.OrElse)
            {
                string     propertyName        = GetPropertyName(body);
                Expression propertyValue       = body.Right;
                string     propertyValueResult = SqlValueUtil.GetValue(GetValueExpression(propertyValue));
                string     opr = GetOperator((TipoOperacion)body.NodeType, propertyValueResult);



                return(string.Format("{0} {1} {2}", propertyName, opr, propertyValueResult));
            }
            else
            {
                string link = GetOperator((TipoOperacion)body.NodeType, null);

                var Left  = body.Left;
                var Right = body.Right;

                return(string.Format("({0}) {1} ({2})", AnalizePart(Left, body), link, AnalizePart(Right, body)));
            }
        }
Exemple #4
0
        private static string ToSql(MethodCallExpression MethodBody)
        {
            string propertyValueResult;
            string propertyName;
            string sResult = null;

            switch (MethodBody.Method.Name)
            {
            case "StartsWith":
                propertyValueResult = MethodBody.Arguments.First().ToString();
                propertyValueResult = propertyValueResult.Replace("\"", "");
                propertyValueResult = $"{propertyValueResult}%";
                propertyValueResult = SqlValueUtil.GetValue(propertyValueResult);

                propertyName = MethodBody.Object.ToString().Split('.').Last();

                sResult = string.Format("{0} {1} {2}", propertyName, "LIKE", propertyValueResult);

                break;

            case "EndsWith":
                propertyValueResult = MethodBody.Arguments.First().ToString();
                propertyValueResult = propertyValueResult.Replace("\"", "");
                propertyValueResult = $"%{propertyValueResult}";
                propertyValueResult = SqlValueUtil.GetValue(propertyValueResult);

                propertyName = MethodBody.Object.ToString().Split('.').Last();

                sResult = string.Format("{0} {1} {2}", propertyName, "LIKE", propertyValueResult);

                break;

            case "Contains":
                var Nombres = new List <string>()
                {
                    "List", "Enumerable", "Array"
                };

                if (Nombres.Any(MethodBody.Method.DeclaringType.Name.StartsWith))
                {
                    IEnumerable Lista = null;

                    if (MethodBody.Method.DeclaringType.Name.StartsWith("List", StringComparison.Ordinal))
                    {
                        Lista        = GetValueExpression((Expression)MethodBody.Object) as IEnumerable;
                        propertyName = MethodBody.Arguments.First().ToString().Split('.').Last();
                    }
                    else
                    {
                        Lista        = GetValueExpression(MethodBody.Arguments.First() as Expression) as IEnumerable;
                        propertyName = MethodBody.Arguments.Last().ToString().Split('.').Last();
                    }


                    string sValueList = string.Empty;
                    foreach (var oL in Lista)
                    {
                        string sComa = (sValueList == string.Empty) ? "" : ",";
                        sValueList += $"{sComa}{SqlValueUtil.GetValue(oL)}";
                    }
                    if (sValueList == string.Empty)
                    {
                        return(string.Format("{0} {1} {2}", "1", "=", "0"));
                    }
                    else
                    {
                        sResult = string.Format("{0} {1} ({2})", propertyName, "IN", sValueList);
                    }
                }
                else if (MethodBody.Method.DeclaringType.Name == "String")
                {
                    propertyValueResult = MethodBody.Arguments.First().ToString();
                    propertyValueResult = propertyValueResult.Replace("\"", "");
                    propertyValueResult = $"%{propertyValueResult}%";
                    propertyValueResult = SqlValueUtil.GetValue(propertyValueResult);

                    propertyName = MethodBody.Object.ToString().Split('.').Last();

                    sResult = string.Format("{0} {1} {2}", propertyName, "LIKE", propertyValueResult);
                }
                else
                {
                    throw new ExpresionToSqlException("No existe implemenetación para ese tipo de objeto");
                }
                break;
            }

            if (sResult == null)
            {
                throw new ExpresionToSqlException("No existe converisón SQL para ese método");
            }

            return(sResult);
        }