public static List<String> splitSql(final String sql) {
    final List<String> commands = new ArrayList<>();
    StringBuilder current = new StringBuilder();
    int index = 0;
    while (index < sql.length()) {
      if (sql.charAt(index) == SINGLE_QUOTE) {
        final int closingToken = sql.indexOf(SINGLE_QUOTE, index + 1);
        validateToken(String.valueOf(SINGLE_QUOTE), closingToken);
        current.append(sql, index, closingToken + 1);
        index = closingToken + 1;
      } else if (index < sql.length() - 1 && sql.startsWith(SHORT_COMMENT_OPENER, index)) {
        index = sql.indexOf(SHORT_COMMENT_CLOSER, index + 1) + 1;
        validateToken(SHORT_COMMENT_CLOSER, index - 1);
      } else if (index < sql.length() - 1 && sql.startsWith(LONG_COMMENT_OPENER, index)) {
        index = sql.indexOf(LONG_COMMENT_CLOSER, index + 1) + 2;
        validateToken(LONG_COMMENT_CLOSER, index - 2);
      } else if (sql.charAt(index) == SEMICOLON) {
        current.append(';');
        commands.add(current.toString());
        current = new StringBuilder();
        index++;
      } else {
        current.append(sql.charAt(index));
        index++;
      }
    }

    if (!current.toString().trim().isEmpty()) {
      throw new MigrationException(String.format(
              "Unmatched command at end of file; missing semicolon: '%s'", current
      ));
    }

    return commands;
  }
@Test
  public void shouldSplitCommandsWithComments() {
    // When:
    List<String> commands = CommandParser.splitSql("-- Before\n"
        + "CREATE STREAM valid_purchases AS\n"
        + "  SELECT *\n"
        + "  FROM purchases\n"
        + "  WHERE cost > 0.00 AND quantity > 0;\n"
        + "-- After\n"
        + "CREATE OR REPLACE STREAM valid_purchases AS\n"
        + "  SELECT *\n"
        + "  FROM purchases\n"
        + "  WHERE quantity > 0;"
        + "/* Let's insert some values now! -- it's fun! */\n"
        + "INSERT INTO purchases VALUES ('c''ow', -90);\n"
        + "INSERT INTO purchases VALUES ('/*she*/ep',     80);\n"
        + "INSERT INTO purchases VALUES ('pol/*ar;;be--ar*/;', 200000);");

    // Then:
    assertThat(commands.size(), is(5));
    assertThat(commands.get(0), is("CREATE STREAM valid_purchases AS\n  SELECT *\n  FROM purchases\n  WHERE cost > 0.00 AND quantity > 0;"));
    assertThat(commands.get(1), is("\nCREATE OR REPLACE STREAM valid_purchases AS\n  SELECT *\n  FROM purchases\n  WHERE quantity > 0;"));
    assertThat(commands.get(2), is("\nINSERT INTO purchases VALUES ('c''ow', -90);"));
    assertThat(commands.get(3), is("\nINSERT INTO purchases VALUES ('/*she*/ep',     80);"));
    assertThat(commands.get(4), is("\nINSERT INTO purchases VALUES ('pol/*ar;;be--ar*/;', 200000);"));
  }