Import multiline SQL query to single string

后端 未结 7 1006
傲寒
傲寒 2020-12-24 03:11

In R, how can I import the contents of a multiline text file (containing SQL) to a single string?

The sql.txt file looks like this:

SELECT TOP 100 
         


        
7条回答
  •  孤独总比滥情好
    2020-12-24 03:54

    Below is an R function that reads in a multiline SQL query (from a text file) and converts it into a single-line string. The function removes formatting and whole-line comments.

    To use it, run the code to define the functions, and your single-line string will be the result of running ONELINEQ("querytextfile.sql","~/path/to/thefile").

    How it works: Inline comments detail this; it reads each line of the query and deletes (replaces with nothing) whatever isn't needed to write out a single-line version of the query (as asked for in the question). The result is a list of lines, some of which are blank and get filtered out; the last step is to paste this (unlisted) list together and return the single line.

    #
    # This set of functions allows us to read in formatted, commented SQL queries
    # Comments must be entire-line comments, not on same line as SQL code, and begun with "--"
    # The parsing function, to be applied to each line:
    LINECLEAN <- function(x) {
      x = gsub("\t+", "", x, perl=TRUE); # remove all tabs
      x = gsub("^\\s+", "", x, perl=TRUE); # remove leading whitespace
      x = gsub("\\s+$", "", x, perl=TRUE); # remove trailing whitespace
      x = gsub("[ ]+", " ", x, perl=TRUE); # collapse multiple spaces to a single space
      x = gsub("^[--]+.*$", "", x, perl=TRUE); # destroy any comments
      return(x)
    }
    # PRETTYQUERY is the filename of your formatted query in quotes, eg "myquery.sql"
    # DIRPATH is the path to that file, eg "~/Documents/queries"
    ONELINEQ <- function(PRETTYQUERY,DIRPATH) { 
      A <- readLines(paste0(DIRPATH,"/",PRETTYQUERY)) # read in the query to a list of lines
      B <- lapply(A,LINECLEAN) # process each line
      C <- Filter(function(x) x != "",B) # remove blank and/or comment lines
      D <- paste(unlist(C),collapse=" ") # paste lines together into one-line string, spaces between.
      return(D)
    }
    # TODO: add eof newline automatically to remove warning
    #############################################################################################
    

提交回复
热议问题