Example #1
0
        public virtual string Query(string query, object inParameters, out IList <QueryInfo> parameters)
        {
            var columnValueMap = QueryParserUtilities.ParseObjectKeyValues(inParameters);

            parameters = ToQueryInfos(columnValueMap);
            return(query);
        }
Example #2
0
        public virtual string GenerateUpdate <T>(object item, Expression <Func <T, bool> > where, out IList <QueryInfo> queryParameters) where T : class
        {
            var tableName = DotEntityDb.GetTableNameForType <T>();
            var builder   = new StringBuilder();

            // convert the query parms into a SQL string and dynamic property object
            builder.Append("UPDATE ");
            builder.Append(tableName.ToEnclosed());
            builder.Append(" SET ");

            Dictionary <string, object> updateValueMap = QueryParserUtilities.ParseObjectKeyValues(item);
            var updateString = string.Join(",", updateValueMap.Select(x => $"{x.Key.ToEnclosed()} = @{x.Key}"));

            builder.Append(updateString);
            var parser      = new ExpressionTreeParser();
            var whereString = parser.GetWhereString(where);

            queryParameters = parser.QueryInfoList;

            if (!string.IsNullOrEmpty(whereString))
            {
                //update where string to handle common parameters
                var commonKeys = updateValueMap.Keys.Intersect(queryParameters.Select(x => x.PropertyName));
                whereString = commonKeys.Aggregate(whereString, (current, ck) => current.Replace($"@{ck}", $"@{ck}2"));
            }

            queryParameters = MergeParameters(ToQueryInfos(updateValueMap), queryParameters);

            if (!string.IsNullOrEmpty(whereString))
            {
                builder.Append(" WHERE " + whereString);
            }

            return(builder.ToString().Trim() + ";");
        }
Example #3
0
        public virtual string GenerateCount(string tableName, object @where, out IList <QueryInfo> parameters)
        {
            Dictionary <string, object> whereMap = QueryParserUtilities.ParseObjectKeyValues(where);
            var whereString = string.Join(" AND ", whereMap.Select(x => $"{x.Key} = @{x.Key}"));

            parameters = ToQueryInfos(whereMap);
            return($"SELECT COUNT(*) FROM {tableName.ToEnclosed()} WHERE {whereString};");
        }
Example #4
0
        public virtual string GenerateInsert(string tableName, object entity, out IList <QueryInfo> parameters)
        {
            Dictionary <string, object> columnValueMap = QueryParserUtilities.ParseObjectKeyValues(entity, exclude: "Id");
            var insertColumns    = columnValueMap.Keys.ToArray();
            var joinInsertString = string.Join(",", insertColumns.Select(x => x.ToEnclosed()));
            var joinValueString  = "@" + string.Join(",@", insertColumns);;

            parameters = ToQueryInfos(columnValueMap);

            return($"INSERT INTO {tableName.ToEnclosed()} ({joinInsertString}) OUTPUT inserted.Id VALUES ({joinValueString});");
        }
Example #5
0
        public virtual string GenerateUpdate(string tableName, object entity, object where, out IList <QueryInfo> queryParameters, params string[] exclude)
        {
            Dictionary <string, object> updateValueMap = QueryParserUtilities.ParseObjectKeyValues(entity, exclude);
            Dictionary <string, object> whereMap       = QueryParserUtilities.ParseObjectKeyValues(where);

            queryParameters = ToQueryInfos(updateValueMap, whereMap);

            var updateString = string.Join(",", updateValueMap.Select(x => $"{x.Key.ToEnclosed()} = @{x.Key}"));
            //get the common keys
            var commonKeys  = updateValueMap.Keys.Intersect(whereMap.Keys);
            var whereString = string.Join(" AND ", whereMap.Select(x =>
            {
                var prefix = commonKeys.Contains(x.Key) ? "2" : "";
                return($"{x.Key.ToEnclosed()} = @{x.Key}{prefix}");
            }));

            return($"UPDATE {tableName.ToEnclosed()} SET {updateString} WHERE {whereString};");
        }
Example #6
0
        public virtual string GenerateSelectWithTotalMatchingCount <T>(out IList <QueryInfo> parameters, List <Expression <Func <T, bool> > > @where = null, Dictionary <Expression <Func <T, object> >, RowOrder> orderBy = null,
                                                                       int page = 1, int count = Int32.MaxValue) where T : class
        {
            parameters = new List <QueryInfo>();
            var builder   = new StringBuilder();
            var tableName = DotEntityDb.GetTableNameForType <T>();

            var whereString = "";

            if (where != null)
            {
                var parser             = new ExpressionTreeParser();
                var whereStringBuilder = new List <string>();
                foreach (var wh in where)
                {
                    whereStringBuilder.Add(parser.GetWhereString(wh));
                }
                parameters  = parser.QueryInfoList;
                whereString = string.Join(" AND ", whereStringBuilder).Trim();
            }

            var orderByStringBuilder = new List <string>();
            var orderByString        = "";

            if (orderBy != null)
            {
                var parser = new ExpressionTreeParser();
                foreach (var ob in orderBy)
                {
                    orderByStringBuilder.Add(parser.GetOrderByString(ob.Key) + (ob.Value == RowOrder.Descending ? " DESC" : ""));
                }

                orderByString = string.Join(", ", orderByStringBuilder).Trim(',');
            }

            var paginatedSelect = PaginateOrderByString(orderByString, page, count, out string newWhereString);

            if (paginatedSelect != string.Empty)
            {
                paginatedSelect = "," + paginatedSelect;
                orderByString   = string.Empty;
            }
            // make the query now
            builder.Append($"SELECT {QueryParserUtilities.GetSelectColumnString(new List<Type>() { typeof(T) })}{paginatedSelect} FROM ");
            builder.Append(tableName.ToEnclosed());

            if (!string.IsNullOrEmpty(whereString))
            {
                builder.Append(" WHERE " + whereString);
            }

            if (!string.IsNullOrEmpty(orderByString))
            {
                builder.Append(" ORDER BY " + orderByString);
            }
            var query = builder.ToString().Trim();

            if (paginatedSelect != string.Empty)
            {
                //wrap everything
                query = $"SELECT * FROM ({query}) AS __PAGINATEDRESULT__ WHERE {newWhereString};";
            }

            //and the count query
            query = query + $"{Environment.NewLine}SELECT COUNT(*) FROM {tableName.ToEnclosed()}" + (string.IsNullOrEmpty(whereString)
                ? ""
                : $" WHERE {whereString}") + ";";
            return(query);
        }