@Test public void testSplit() { assertEquals( Arrays.asList( new String[0] ), sqlScriptParser.split( null ) ); assertEquals( Arrays.asList( new String[0] ), sqlScriptParser.split( "" ) ); assertEquals( Arrays.asList( new String[0] ), sqlScriptParser.split( " " ) ); assertEquals( Arrays.asList( "SELECT 1", "SELECT 2" ), sqlScriptParser.split( "SELECT 1;SELECT 2" ) ); assertEquals( Collections.singletonList( "SELECT '1;2'" ), sqlScriptParser.split( "SELECT '1;2'" ) ); assertEquals( Collections.singletonList( "SELECT \"1;2\"" ), sqlScriptParser.split( "SELECT \"1;2\"" ) ); assertEquals( Collections.singletonList( "SELECT -- 1;2" ), sqlScriptParser.split( "SELECT -- 1;2" ) ); assertEquals( Collections.singletonList( "SELECT " ), sqlScriptParser.split( "SELECT " ) ); assertEquals( Arrays.asList( "SELECT /1", "2" ), sqlScriptParser.split( "SELECT /1;2" ) ); assertEquals( Arrays.asList( "SELECT /1", "2" ), sqlScriptParser.split( "SELECT /1;;;;2" ) ); assertEquals( Collections.singletonList( "SELECT /1" ), sqlScriptParser.split( "SELECT /1;\n \n" ) ); assertEquals( Collections.singletonList( "SELECT \"hello\\\"world\" FROM dual" ), sqlScriptParser.split( "SELECT \"hello\\\"world\" FROM dual" ) ); assertEquals( Collections.singletonList( "CREATE TABLE test1 (col1 STRING) TBLPROPERTIES (\"prop1\" = \"my\\\"value\")" ), sqlScriptParser.split( "CREATE TABLE test1 (col1 STRING) TBLPROPERTIES (\"prop1\" = \"my\\\"value\");" ) ); assertEquals( Collections.singletonList( "CREATE TABLE test1 (col1 STRING) TBLPROPERTIES ('prop1' = 'my\\\"value')" ), sqlScriptParser.split( "CREATE TABLE test1 (col1 STRING) TBLPROPERTIES ('prop1' = 'my\\\"value');" ) ); assertEquals( Collections.singletonList( "SELECT \"test\\\";SELECT 1" ), sqlScriptParser.split( "SELECT \"test\\\";SELECT 1" ) ); assertEquals( Collections.singletonList( "SELECT 'test\\';SELECT 1" ), sqlScriptParser.split( "SELECT 'test\\';SELECT 1" ) ); assertEquals( Arrays.asList( "create table pdi13654 (col1 string) TBLPROPERTIES (\"quoteChar\"=\"\\\"\", \"escapeChar\"=\"\\\\\")", "SELECT 1" ), sqlScriptParser.split( "create table pdi13654 (col1 string) TBLPROPERTIES (\"quoteChar\"=\"\\\"\", \"escapeChar\"=\"\\\\\");SELECT 1" ) ); assertEquals( Collections.singletonList( "SELECT 1 from test where t='\\'||t=a" ), oracleSqlScriptParser.split( "SELECT 1 from test where t='\\'||t=a;" ) ); }
public List<String> split( String script ) { if ( script == null ) { return Collections.emptyList(); } List<String> result = new ArrayList<String>(); MODE mode = MODE.SQL; char currentStringChar = 0; int statementStart = 0; for ( int i = 0; i < script.length(); i++ ) { char ch = script.charAt( i ); char nextCh = i < script.length() - 1 ? script.charAt( i + 1 ) : 0; switch ( mode ) { case SQL: switch ( ch ) { case '/': if ( nextCh == '*' ) { mode = MODE.BLOCK_COMMENT; i++; } break; case '-': if ( nextCh == '-' ) { mode = MODE.LINE_COMMENT; i++; } break; case '\'': case '"': mode = MODE.STRING; currentStringChar = ch; break; case ';': String st = script.substring( statementStart, i ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } statementStart = i + 1; break; } break; case BLOCK_COMMENT: if ( ch == '*' ) { if ( nextCh == '/' ) { mode = MODE.SQL; i++; } } break; case LINE_COMMENT: if ( ch == '\n' || ch == '\r' ) { mode = MODE.SQL; } break; case STRING: if ( ch == '\\' && nextCh == '\\' ) { i++; } else if ( ch == '\\' && nextCh == currentStringChar && usingBackslashAsEscapeCharForQuotation ) { i++; } else if ( ch == currentStringChar ) { mode = MODE.SQL; } break; } } if ( statementStart < script.length() ) { String st = script.substring( statementStart ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } } return result; }
SqlScriptParser { public List<String> split( String script ) { if ( script == null ) { return Collections.emptyList(); } List<String> result = new ArrayList<String>(); MODE mode = MODE.SQL; char currentStringChar = 0; int statementStart = 0; for ( int i = 0; i < script.length(); i++ ) { char ch = script.charAt( i ); char nextCh = i < script.length() - 1 ? script.charAt( i + 1 ) : 0; switch ( mode ) { case SQL: switch ( ch ) { case '/': if ( nextCh == '*' ) { mode = MODE.BLOCK_COMMENT; i++; } break; case '-': if ( nextCh == '-' ) { mode = MODE.LINE_COMMENT; i++; } break; case '\'': case '"': mode = MODE.STRING; currentStringChar = ch; break; case ';': String st = script.substring( statementStart, i ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } statementStart = i + 1; break; } break; case BLOCK_COMMENT: if ( ch == '*' ) { if ( nextCh == '/' ) { mode = MODE.SQL; i++; } } break; case LINE_COMMENT: if ( ch == '\n' || ch == '\r' ) { mode = MODE.SQL; } break; case STRING: if ( ch == '\\' && nextCh == '\\' ) { i++; } else if ( ch == '\\' && nextCh == currentStringChar && usingBackslashAsEscapeCharForQuotation ) { i++; } else if ( ch == currentStringChar ) { mode = MODE.SQL; } break; } } if ( statementStart < script.length() ) { String st = script.substring( statementStart ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } } return result; } }
SqlScriptParser { public List<String> split( String script ) { if ( script == null ) { return Collections.emptyList(); } List<String> result = new ArrayList<String>(); MODE mode = MODE.SQL; char currentStringChar = 0; int statementStart = 0; for ( int i = 0; i < script.length(); i++ ) { char ch = script.charAt( i ); char nextCh = i < script.length() - 1 ? script.charAt( i + 1 ) : 0; switch ( mode ) { case SQL: switch ( ch ) { case '/': if ( nextCh == '*' ) { mode = MODE.BLOCK_COMMENT; i++; } break; case '-': if ( nextCh == '-' ) { mode = MODE.LINE_COMMENT; i++; } break; case '\'': case '"': mode = MODE.STRING; currentStringChar = ch; break; case ';': String st = script.substring( statementStart, i ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } statementStart = i + 1; break; } break; case BLOCK_COMMENT: if ( ch == '*' ) { if ( nextCh == '/' ) { mode = MODE.SQL; i++; } } break; case LINE_COMMENT: if ( ch == '\n' || ch == '\r' ) { mode = MODE.SQL; } break; case STRING: if ( ch == '\\' && nextCh == '\\' ) { i++; } else if ( ch == '\\' && nextCh == currentStringChar && usingBackslashAsEscapeCharForQuotation ) { i++; } else if ( ch == currentStringChar ) { mode = MODE.SQL; } break; } } if ( statementStart < script.length() ) { String st = script.substring( statementStart ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } } return result; } SqlScriptParser( boolean usingBackslashAsEscapeCharForQuotation ); }
SqlScriptParser { public List<String> split( String script ) { if ( script == null ) { return Collections.emptyList(); } List<String> result = new ArrayList<String>(); MODE mode = MODE.SQL; char currentStringChar = 0; int statementStart = 0; for ( int i = 0; i < script.length(); i++ ) { char ch = script.charAt( i ); char nextCh = i < script.length() - 1 ? script.charAt( i + 1 ) : 0; switch ( mode ) { case SQL: switch ( ch ) { case '/': if ( nextCh == '*' ) { mode = MODE.BLOCK_COMMENT; i++; } break; case '-': if ( nextCh == '-' ) { mode = MODE.LINE_COMMENT; i++; } break; case '\'': case '"': mode = MODE.STRING; currentStringChar = ch; break; case ';': String st = script.substring( statementStart, i ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } statementStart = i + 1; break; } break; case BLOCK_COMMENT: if ( ch == '*' ) { if ( nextCh == '/' ) { mode = MODE.SQL; i++; } } break; case LINE_COMMENT: if ( ch == '\n' || ch == '\r' ) { mode = MODE.SQL; } break; case STRING: if ( ch == '\\' && nextCh == '\\' ) { i++; } else if ( ch == '\\' && nextCh == currentStringChar && usingBackslashAsEscapeCharForQuotation ) { i++; } else if ( ch == currentStringChar ) { mode = MODE.SQL; } break; } } if ( statementStart < script.length() ) { String st = script.substring( statementStart ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } } return result; } SqlScriptParser( boolean usingBackslashAsEscapeCharForQuotation ); List<String> split( String script ); String removeComments( String script ); }
SqlScriptParser { public List<String> split( String script ) { if ( script == null ) { return Collections.emptyList(); } List<String> result = new ArrayList<String>(); MODE mode = MODE.SQL; char currentStringChar = 0; int statementStart = 0; for ( int i = 0; i < script.length(); i++ ) { char ch = script.charAt( i ); char nextCh = i < script.length() - 1 ? script.charAt( i + 1 ) : 0; switch ( mode ) { case SQL: switch ( ch ) { case '/': if ( nextCh == '*' ) { mode = MODE.BLOCK_COMMENT; i++; } break; case '-': if ( nextCh == '-' ) { mode = MODE.LINE_COMMENT; i++; } break; case '\'': case '"': mode = MODE.STRING; currentStringChar = ch; break; case ';': String st = script.substring( statementStart, i ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } statementStart = i + 1; break; } break; case BLOCK_COMMENT: if ( ch == '*' ) { if ( nextCh == '/' ) { mode = MODE.SQL; i++; } } break; case LINE_COMMENT: if ( ch == '\n' || ch == '\r' ) { mode = MODE.SQL; } break; case STRING: if ( ch == '\\' && nextCh == '\\' ) { i++; } else if ( ch == '\\' && nextCh == currentStringChar && usingBackslashAsEscapeCharForQuotation ) { i++; } else if ( ch == currentStringChar ) { mode = MODE.SQL; } break; } } if ( statementStart < script.length() ) { String st = script.substring( statementStart ); if ( StringUtils.isNotBlank( st ) ) { result.add( st ); } } return result; } SqlScriptParser( boolean usingBackslashAsEscapeCharForQuotation ); List<String> split( String script ); String removeComments( String script );  }