예제 #1
0
        // public methods
        public static string BuildOrderByQueryString(LambdaExpression expression, bool orderDesc)
        {
            MemberExpression expr        = GetOperand(expression) as MemberExpression;
            string           orderColumn = $"{expr.Expression}.{StringToolkit.PascalToUnderscore(expr.Member.Name.ToString())}";

            return($"ORDER BY {orderColumn} {(orderDesc ? "DESC" : "")}");
        }
예제 #2
0
        internal static string ToWhereString <T>(T item)
        {
            // list of left-hand-side
            IList <PropertyDescriptor> tableColumns = PropertyToolkit.GetInsertProperties <T>();

            string[] columnNames = new string[tableColumns.Count];
            for (int i = 0; i < tableColumns.Count; i++)
            {
                columnNames[i] = StringToolkit.PascalToUnderscore(tableColumns[i].Name);
            }

            // list of right-hand-side
            string[] values = PropertyToolkit.BuildInsertValuesString(item)
                              .Trim().Replace("(", "").Replace(")", "")
                              .Split(", ");

            // build the expression
            StringBuilder queryBuilder = new StringBuilder();

            for (int i = 0; i < values.Length; i++)
            {
                queryBuilder.Append(columnNames[i])
                .Append(" = ")
                .Append(values[i]);
                if (i != values.Length - 1)
                {
                    queryBuilder.Append(" AND ");
                }
            }

            return(queryBuilder.ToString());
        }
예제 #3
0
 /// <summary>
 /// Creates the insert string that is used in SQL insert into syntax.
 /// </summary>
 /// <returns>A string like (col1, col2, ...)</returns>
 internal static string BuildInsertString(IList <PropertyDescriptor> properties)
 {
     string[] insertPropNames = new string[properties.Count];
     for (int i = 0; i < properties.Count; i++)
     {
         insertPropNames[i] = StringToolkit.PascalToUnderscore(properties[i].Name);
     }
     return($"({string.Join(", ", insertPropNames)})");
 }
예제 #4
0
        // sample: SET user = @user, value = @value
        public static string EvaluateToParameterizedSqlString(IDictionary <string, object> propDictionary)
        {
            var            keySet     = propDictionary.Keys.ToArray();
            IList <string> parameters = new List <string>();

            foreach (var key in keySet)
            {
                parameters.Add($"{StringToolkit.PascalToUnderscore(key)} = @{$"{key}"}");
            }
            string output = $"{string.Join(", ", parameters)}";

            return(output);
        }
예제 #5
0
        // since 1.1.0: changed last GetOperand line
        private static string ToQueryString(BinaryExpression simpleExpression)
        {
            var    leftOp       = GetOperand(simpleExpression.Left) as MemberExpression;
            string leftHandSide = $"{leftOp.Expression}" +
                                  $".{StringToolkit.PascalToUnderscore(leftOp.Member.Name)}";
            var    rightOp = GetOperand(simpleExpression.Right);
            string rightHandSide;

            if (rightOp is MemberExpression)
            {
                rightHandSide = $"{(rightOp as MemberExpression).Expression}" +
                                $".{StringToolkit.PascalToUnderscore((rightOp as MemberExpression).Member.Name)}";
            }
            else
            {
                var newRightOp = rightOp is ConstantExpression ?
                                 (rightOp as ConstantExpression).Value
                                : rightOp;
                if (newRightOp is DateTime)
                {
                    rightHandSide = $"'{(rightOp as DateTime?).Value.ToString("yyyy-MM-dd")}'";
                }
                else if (newRightOp is bool)
                {
                    rightHandSide = (bool)newRightOp ? "B'1'" : "B'0'";
                }
                else
                {
                    rightHandSide = rightOp.ToString();
                }
            }
            if (rightHandSide.ContainSqlKeyword())
            {
                throw new SqlInjectionException("Are you trying to perform SQL Injection? :)");
            }
            return(string.Format("{0} {1} {2}",
                                 leftHandSide.Replace("'", "")
                                 .Replace("\"", "")
                                 .Replace("`", ""),
                                 EXPRESSION_MAP[simpleExpression.NodeType],
                                 rightHandSide));
        }
예제 #6
0
        private static JObject UnderscoreToPascalProps(JObject o)
        {
            Regex           rg         = new Regex("\"([a-zA-Z]+_)*([a-zA-Z]+)\"");
            string          serialized = SerializeObject(o);
            MatchCollection matches    = rg.Matches(serialized);

            if (matches.Count == 0)
            {
                return(o);
            }
            else
            {
                foreach (Match m in matches)
                {
                    string snake  = m.Value.ToLower();
                    string pascal = StringToolkit.UnderscoreToPascal(snake);
                    serialized = serialized.Replace(snake, pascal);
                }
                return(DeserializeObject <JObject>(serialized));
            }
        }
예제 #7
0
        public static IDictionary <string, object> ToDictionary(object obj)
        {
            var properties = TypeDescriptor.GetProperties(obj);
            IDictionary <string, object> propDictionary = new Dictionary <string, object>();

            foreach (PropertyDescriptor property in properties)
            {
                if (property.PropertyType.FullName == "System.DateTime")
                {
                    propDictionary.Add(property.Name,
                                       ((DateTime)property.GetValue(obj))
                                       .ToString("yyyy-MM-dd H:mm:ss"));
                }
                else
                {
                    if (!property.Attributes.OfType <ExplicitLoadingAttribute>().Any())
                    {
                        propDictionary.Add(StringToolkit.PascalToUnderscore(property.Name),
                                           property.GetValue(obj));
                    }
                }
            }
            return(propDictionary);
        }