@@ -52,7 +52,7 @@ macro filter(sqlquery, conditions...)
5252
5353 if isa (sq, SQLQuery)
5454 if ! sq. is_aggregated
55- if sq. post_join
55+ if sq. post_join || sq . post_mutate
5656 combined_conditions = String[]
5757 for condition in $ (esc (conditions))
5858 condition_str = string (expr_to_sql (condition, sq))
@@ -76,7 +76,7 @@ macro filter(sqlquery, conditions...)
7676 sq. where = combined_condition_str
7777 # println(sq.from)
7878 build_cte! (sq)
79- sq. select = " * "
79+ # sq.select = " * "
8080 end
8181 else
8282 aggregated_columns = Set {String} ()
@@ -168,7 +168,8 @@ macro arrange(sqlquery, columns...)
168168 sq = $ (esc (sqlquery))
169169 sq = sq. post_first ? t ($ (esc (sqlquery))) : sq
170170 sq. post_first = false ;
171- sq. orderBy = " ORDER BY " * $ order_clause
171+
172+ sq. orderBy = " ORDER BY " * $ order_clause
172173 sq
173174 end
174175end
@@ -360,6 +361,7 @@ macro rename(sqlquery, renamings...)
360361end
361362
362363# COV_EXCL_START
364+ # will delete
363365cyan_crayon = Crayon (foreground = :cyan , bold = true ) # for FROM and SELECT
364366blue_crayon = Crayon (foreground = :blue , bold = true ) # for JOINs
365367yellow_crayon = Crayon (foreground = :yellow , bold = true ) # for GROUP BY
@@ -371,64 +373,77 @@ light_gray = Crayon(foreground = :red, bold = true)
371373green = Crayon (foreground = :green , bold = false )
372374# COV_EXCL_STOP
373375
376+ mutable struct DBQuery
377+ val:: String
378+ end
379+
380+ function Base. show (io:: IO , :: MIME"text/plain" , mytype:: DBQuery )
381+ print (io, mytype. val)
382+ end
374383
375384macro show_query (sqlquery)
376385 return quote
377- final_query = finalize_query ($ (esc (sqlquery)))
378-
379- formatted_query = replace (final_query, r" (?<=\) ), " => " ,\n " )
380- formatted_query = replace (formatted_query, " SELECT " => " \n SELECT " )
381- formatted_query = replace (formatted_query, " AS (SELECT " => " AS ( \n\t SELECT " )
382- formatted_query = replace (formatted_query, " FROM " => " \n\t FROM " )
383- formatted_query = replace (formatted_query, " WHERE " => " \n\t WHERE " )
384- formatted_query = replace (formatted_query, " GROUP BY " => " \n\t GROUP BY " )
385- formatted_query = replace (formatted_query, " ORDER BY " => " \n\t ORDER BY " )
386- formatted_query = replace (formatted_query, " HAVING " => " \n\t HAVING " )
387- formatted_query = replace (formatted_query, " LEFT JOIN " => " \n\t LEFT JOIN " )
388- formatted_query = replace (formatted_query, " RIGHT JOIN " => " \n\t RIGHT JOIN " )
389- formatted_query = replace (formatted_query, " INNER JOIN " => " \n\t INNER JOIN " )
390- formatted_query = replace (formatted_query, " OUTER JOIN " => " \n\t OUTER JOIN " )
391- formatted_query = replace (formatted_query, " ASOF " => " \n\t ASOF " )
392- formatted_query = replace (formatted_query, " LIMIT " => " \n\t LIMIT " )
393- formatted_query = replace (formatted_query, " ANY_VALUE" => " \n\t ANY_VALUE" )
394-
395- pattern = r" \b (cte_\w +|WITH|FROM|SELECT|AS|LEFT|JOIN|RIGHT|OUTER|UNION|INNER|ASOF|GROUP\s +BY|CASE|WHEN|THEN|ELSE|END|WHERE|HAVING|ORDER\s +BY|PARTITION|ASC|DESC|INNER)\b "
396- # COV_EXCL_START
397- if TidierDB. color[]
398- formatted_query = replace (formatted_query, pattern => s -> begin
399- token = String (s)
400- token_upper = uppercase (strip (token))
401-
402- if token_upper in [" FROM" , " SELECT" , " WITH" ]
403- return $ cyan_crayon (token)
404- elseif token_upper in [" AS" ]
405- return $ green (token)
406- elseif token_upper in [" ASOF" , " RIGHT" , " LEFT" , " OUTER" , " SEMI" , " JOIN" , " INNER" ]
407- return $ blue_crayon (token)
408- elseif occursin (r" ^GROUP\s +BY$" , token_upper)
409- return $ yellow_crayon (token)
410- elseif token_upper in [" CASE" , " WHEN" , " THEN" , " ELSE" , " END" ]
411- return $ orange_crayon (token)
412- elseif token_upper in [" WHERE" , " HAVING" ]
413- return $ lightblue_crayon (token)
414- elseif occursin (r" ^ORDER\s +BY$" , token_upper)
415- return $ pink_crayon (token)
416- elseif token_upper in [" ASC" , " DESC" , " PARTITION" ]
417- return $ pink_crayon (token)
418- # elseif occursin(r"^CTE_\w+$", token_upper)
419- # return $light_magenta(token)
420- else
421- return token
422- end
423- end )
424- end
425- # COV_EXCL_STOP
426- println (formatted_query)
386+ final_query = finalize_query ($ (esc (sqlquery)))
387+ formatted_query = format_sql_query (final_query)
388+
389+ display (DBQuery (formatted_query))
390+ # $(esc(sqlquery));
427391 end
392+
428393end
429394
430-
431-
395+ # COV_EXCL_START
396+ function format_sql_query (final_query:: String )
397+ # Format basic SQL structure with newlines and indentation
398+ formatted_query = replace (final_query, r" (?<=\) ), " => " ,\n " )
399+ formatted_query = replace (formatted_query, " SELECT " => " \n SELECT " )
400+ formatted_query = replace (formatted_query, " AS (SELECT " => " AS ( \n\t SELECT " )
401+ formatted_query = replace (formatted_query, " FROM " => " \n\t FROM " )
402+ formatted_query = replace (formatted_query, " WHERE " => " \n\t WHERE " )
403+ formatted_query = replace (formatted_query, " GROUP BY " => " \n\t GROUP BY " )
404+ formatted_query = replace (formatted_query, " ORDER BY " => " \n\t ORDER BY " )
405+ formatted_query = replace (formatted_query, " HAVING " => " \n\t HAVING " )
406+ formatted_query = replace (formatted_query, " LEFT JOIN " => " \n\t LEFT JOIN " )
407+ formatted_query = replace (formatted_query, " RIGHT JOIN " => " \n\t RIGHT JOIN " )
408+ formatted_query = replace (formatted_query, " INNER JOIN " => " \n\t INNER JOIN " )
409+ formatted_query = replace (formatted_query, " OUTER JOIN " => " \n\t OUTER JOIN " )
410+ formatted_query = replace (formatted_query, " ASOF " => " \n\t ASOF " )
411+ formatted_query = replace (formatted_query, " LIMIT " => " \n\t LIMIT " )
412+ formatted_query = replace (formatted_query, " ANY_VALUE" => " \n\t ANY_VALUE" )
413+
414+ # pattern for SQL keywords
415+ pattern = r" \b (cte_\w +|WITH|FROM|SELECT|AS|LEFT|JOIN|RIGHT|OUTER|UNION|INNER|ASOF|GROUP\s +BY|CASE|WHEN|THEN|ELSE|END|WHERE|HAVING|ORDER\s +BY|PARTITION|ASC|DESC|INNER)\b "
416+
417+ if TidierDB. color[]
418+ formatted_query = replace (formatted_query, pattern => s -> begin
419+ token = String (s)
420+ token_upper = uppercase (strip (token))
421+
422+ if token_upper in [" FROM" , " SELECT" , " WITH" ]
423+ " \e [36m$(token) \e [0m" # Cyan
424+ elseif token_upper in [" AS" ]
425+ " \e [32m$(token) \e [0m" # Green
426+ elseif token_upper in [" ASOF" , " RIGHT" , " LEFT" , " OUTER" , " SEMI" , " JOIN" , " INNER" ]
427+ " \e [34m$(token) \e [0m" # Blue
428+ elseif occursin (r" ^GROUP\s +BY$" , token_upper)
429+ " \e [33m$(token) \e [0m" # Yellow
430+ elseif token_upper in [" CASE" , " WHEN" , " THEN" , " ELSE" , " END" ]
431+ " \e [38;5;208m$(token) \e [0m" # Orange
432+ elseif token_upper in [" WHERE" , " HAVING" ]
433+ " \e [94m$(token) \e [0m" # Light Blue
434+ elseif occursin (r" ^ORDER\s +BY$" , token_upper)
435+ " \e [35m$(token) \e [0m" # Pink
436+ elseif token_upper in [" ASC" , " DESC" , " PARTITION" ]
437+ " \e [35m$(token) \e [0m" # Pink
438+ else
439+ token
440+ end
441+ end )
442+ end
443+
444+ return formatted_query
445+ end
446+ # COV_EXCL_STOP
432447
433448function final_collect (sqlquery:: SQLQuery , :: Type{<:duckdb} )
434449 final_query = finalize_query (sqlquery)
@@ -489,7 +504,6 @@ $docstring_collect
489504"""
490505macro collect (sqlquery, stream = false )
491506 return quote
492-
493507 backend = current_sql_mode[]
494508 if backend == duckdb ()
495509 if $ stream
0 commit comments