public static List < FileInfo > parseFileStatement ( FileInfo parentContext , String statement ) { Matcher fileMatcher = FileToken . matcher ( statement ) ; if ( ! fileMatcher . lookingAt ( ) ) { // This input does not start with FILE, // so it's not a file command, it's something else. // Return to caller a null and no errors. return null ; } String remainder = statement . substring ( fileMatcher . end ( ) , statement . length ( ) ) ; List < FileInfo > filesInfo = new ArrayList <> ( ) ; Matcher inlineBatchMatcher = DashInlineBatchToken . matcher ( remainder ) ; if ( inlineBatchMatcher . lookingAt ( ) ) { remainder = remainder . substring ( inlineBatchMatcher . end ( ) , remainder . length ( ) ) ; Matcher delimiterMatcher = DelimiterToken . matcher ( remainder ) ; // use matches here (not lookingAt) because we want to match // all of the remainder, not just beginning if ( delimiterMatcher . matches ( ) ) { String delimiter = delimiterMatcher . group ( 1 ) ; filesInfo . add ( new FileInfo ( parentContext , FileOption . INLINEBATCH , delimiter ) ) ; return filesInfo ; } throw new SQLParser . Exception ( "Did not find valid delimiter for \"file -inlinebatch\" command." ) ; } // It is either a plain or a -batch file command. FileOption option = FileOption . PLAIN ; Matcher batchMatcher = DashBatchToken . matcher ( remainder ) ; if ( batchMatcher . lookingAt ( ) ) { option = FileOption . BATCH ; remainder = remainder . substring ( batchMatcher . end ( ) , remainder . length ( ) ) ; } // remove spaces before and after filenames remainder = remainder . trim ( ) ; // split filenames assuming they are separated by space ignoring spaces within quotes // tests for parsing in TestSqlCmdInterface.java List < String > filenames = new ArrayList <> ( ) ; Pattern regex = Pattern . compile ( "[^\\s\']+|'[^']*'" ) ; Matcher regexMatcher = regex . matcher ( remainder ) ; while ( regexMatcher . find ( ) ) { filenames . add ( regexMatcher . group ( ) ) ; } for ( String filename : filenames ) { Matcher filenameMatcher = FilenameToken . matcher ( filename ) ; // Use matches to match all input, not just beginning if ( filenameMatcher . matches ( ) ) { filename = filenameMatcher . group ( 1 ) ; // Trim whitespace from beginning and end of the file name. // User may have wanted quoted whitespace at the beginning or end // of the file name, but that seems very unlikely. filename = filename . trim ( ) ; if ( filename . startsWith ( "~" ) ) { filename = filename . replaceFirst ( "~" , System . getProperty ( "user.home" ) ) ; } filesInfo . add ( new FileInfo ( parentContext , option , filename ) ) ; } } // If no filename, or a filename of only spaces, then throw an error. if ( filesInfo . size ( ) == 0 ) { String msg = String . format ( "Did not find valid file name in \"file%s\" command." , option == FileOption . BATCH ? " -batch" : "" ) ; throw new SQLParser . Exception ( msg ) ; } return filesInfo ; }
Parse FILE statement for sqlcmd .