Summary
clean_query function:
- Removes all mixed comments.
- Creates single line output
- Accepts an SQL path or text string
- Simple
Function
require(tidyverse)
You can attach regular expressions together if you want obscurely long expressions, but I recommend readable code.
Output for "query6.sql"
[1] " select a6.column1, a6.column2, count(a6.column3) as counts from data.table a6 group by a6.column1 "
Additional text input example
query <- " Select COL_A -- with a comment here ,COL_B ,COL_C FROM -- and some helpful comment here Database.Datatable ; -- or wherever "
Call Function:
clean_query(query)
Output:
[1] " Select COL_A ,COL_B ,COL_C FROM Database.Datatable "
If you want to check reading from a .sql file:
temp_path <- path.expand("~/query.sql") cat(query, file = temp_path) clean_query(temp_path) file.remove(temp_path)
source share