Logo Search packages:      
Sourcecode: henplus version File versions  Download package

void henplus::SQLStatementSeparator::parsePartialInput (  )  [inline, private]

parse partial input and set state to POTENTIAL_END_FOUND if we either reached end-of-line or a semicolon.

Definition at line 187 of file SQLStatementSeparator.java.

Referenced by hasNext().

                                      {
      int pos = 0;
      char current;
      byte oldstate = -1;

      // local variables: faster access.
      byte state = _currentState.getState();
      boolean lastEoline = _currentState.hasNewlineSeen();

      final StringBuffer input  = _currentState.getInputBuffer();
      final StringBuffer parsed = _currentState.getCommandBuffer();

      if (state == NEW_STATEMENT) {
          parsed.setLength(0);
          /* skip leading whitespaces of next statement .. */
          while (pos < input.length()
               && Character.isWhitespace (input.charAt(pos))) {
                _currentState.setNewlineSeen( input.charAt(pos) == '\n' );
            ++pos;
          }
          input.delete(0, pos);
          pos = 0;
      }
      
      if (input.length() == 0) {
          state = POTENTIAL_END_FOUND;
      }

        //System.err.println("Startstate: " + state + "; LEOL: " + lastEoline);

      while (state != POTENTIAL_END_FOUND && pos < input.length()) {
          boolean vetoAppend = false;
          boolean reIterate;
          current = input.charAt(pos);
          if (current == '\r') {
            current = '\n'; // canonicalize.
          }

            if (current == '\n') {
                _currentState.setNewlineSeen( true );
            }

          //System.out.print ("Pos: " + pos + "\t");
          do {
            reIterate = false;
            switch (state) {
            case NEW_STATEMENT:
                //case START: START == STATEMENT.
            case STATEMENT :
                if (current == '\n') {
                  state = POTENTIAL_END_FOUND;
                  _currentState.setNewlineSeen(true);
                }

                    /*
                     * special handling of the 'first-two-semicolons-after
                     * a-newline-comment'.
                     */
                else if (_removeComments && lastEoline && current== ';' ) {
                  state = FIRST_SEMICOLON_ON_LINE_SEEN;
                }
                else if (!lastEoline && current==';') {
                  _currentState.setNewlineSeen(false);
                  state = POTENTIAL_END_FOUND;
                }
                else if (_removeComments && current == '/') {
                        state = START_COMMENT;
                    }

                    /*
                     * only if '#' this is the first character, make it
                     * a comment..
                     */
                else if (_removeComments && lastEoline && current == '#') {
                        state = ENDLINE_COMMENT;
                    }
                else if (current == '"')  state = STRING;
                else if (current == '\'') state = SQLSTRING;
                else if (current == '-')  state = START_ANSI;
                else if (current == '\\') state = STATEMENT_QUOTE;
                break;
            case STATEMENT_QUOTE:
                state = STATEMENT;
                break;
            case FIRST_SEMICOLON_ON_LINE_SEEN:
                if (current == ';') state = ENDLINE_COMMENT;
                else {
                  state = POTENTIAL_END_FOUND;
                  current = ';';
                  /*
                   * we've read too much. Reset position.
                   */
                  --pos;
                }
                break;
            case START_COMMENT:
                if (current == '*')         state = COMMENT;
                /*
                 * Endline comment in the style '// comment' is not a
                 * good idea, since many JDBC-urls contain the '//' as
                 * part of the URL .. and this should _not_ be regarded as
                 * commend of course.
                 */
                //else if (current == '/')    state = ENDLINE_COMMENT;
                else { 
                  parsed.append ('/'); 
                  state = STATEMENT; 
                  reIterate = true;
                }
                break;
            case COMMENT:
                if (current == '*') state = PRE_END_COMMENT;
                break;
            case PRE_END_COMMENT:
                if (current == '/')      state = STATEMENT;
                else if (current == '*') state = PRE_END_COMMENT;
                else state = COMMENT;
                break;
            case START_ANSI:
                if (current == '-')        state = ENDLINE_COMMENT;
                else { 
                  parsed.append('-'); 
                  state = STATEMENT; 
                  reIterate = true;
                }
                break;
            case ENDLINE_COMMENT:
                if (current == '\n')      state = POTENTIAL_END_FOUND;
                break;
            case STRING:     
                if (current == '\\') state = STRING_QUOTE;
                else if (current == '"') state = STATEMENT;
                break;
            case SQLSTRING:
                if (current == '\\') state = SQLSTRING_QUOTE;
                if (current == '\'') state = STATEMENT;
                break;
            case STRING_QUOTE:
                if (current == '\"') parsed.append("\"");
                vetoAppend = (current == '\n');
                state = STRING;
                break;
            case SQLSTRING_QUOTE:
                if (current == '\'') parsed.append("'");
                vetoAppend = (current == '\n');
                state = SQLSTRING;
                break;
            }
          }
          while (reIterate);

          /* append to parsed; ignore comments */
          if (!vetoAppend
            && ((state == STATEMENT && oldstate != PRE_END_COMMENT)
                || state == NEW_STATEMENT
                || state == STATEMENT_QUOTE
                || state == STRING
                || state == SQLSTRING
                || state == POTENTIAL_END_FOUND)) {
            parsed.append(current);
          }
          
          oldstate = state;
          pos++;
          /*
           * we maintain the state of 'just seen newline' as long
           * as we only skip whitespaces..
           */
          lastEoline &= Character.isWhitespace(current);
      }
      // we reached: POTENTIAL_END_FOUND. Store the rest, that
      // has not been parsed in the input-buffer.
      input.delete(0, pos);
      _currentState.setState(state);
    }


Generated by  Doxygen 1.6.0   Back to index