home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-05-08 | 5.2 KB | 197 lines |
- /*
- * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
- *
- * This SOURCE CODE FILE, which has been provided by Borland as part
- * of a Borland product for use ONLY by licensed users of the product,
- * includes CONFIDENTIAL and PROPRIETARY information of Borland.
- *
- * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
- * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
- * THE PRODUCT.
- *
- * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
- * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
- * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
- * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
- * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
- * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
- * CODE FILE.
- */
-
- //Title: Stored Procedure Sample
- //Version: 2.0
- //Copyright: Copyright (c) 1998
- //Company: Borland Int'l
- //Description: Class can break an sql string up into sql statements.
- // Feel free to copy/modify.
- //Special case: Oracle doesn't accept \r characters, and require a ';' after
- // a function/procedure definition, but cannot handle a ';' after
- // a table definition.
-
-
- package borland.samples.tutorial.dataset.storedprocedure;
-
- public class StatementTokenizer {
-
- public StatementTokenizer(String stream, boolean acceptCR, boolean keepSemiColonForBeginEnd) {
- this.keepSemiColonForBeginEnd = keepSemiColonForBeginEnd;
- buffer = new StringBuffer(stream);
- if (!acceptCR)
- removeCR(stream);
- index = 0;
- last = stream.length();
- }
-
- private void removeCR(String stream) {
- int index = stream.indexOf((int)'\r');
- while (index >= 0) {
- buffer.setCharAt(index,' ');
- index = stream.indexOf((int)'\r',index+1);
- }
- }
-
- private void skipComment() {
- char ch = buffer.charAt(index);
- if (ch == '/') {
- index++;
- while (index < last) {
- ch = buffer.charAt(index++);
- if (ch == '\r' || ch == '\n')
- break;
- }
- } else if (ch == '*') {
- index++;
- int stop = last - 1;
- while (index < stop) {
- if (buffer.charAt(index++) == '*' && buffer.charAt(index) == '/')
- break;
- }
- if (index < stop)
- index++;
- }
- }
-
- private void skipSpacing(boolean allowComment) {
- while (index < last) {
- char ch = buffer.charAt(index++);
- switch (ch) {
- case '\r':
- case ' ':
- case '\t':
- case '\n':
- continue;
-
- case '/':
- if (allowComment) {
- if (index < last)
- skipComment();
- continue;
- }
-
- default:
- index--;
- return;
- }
- }
- }
-
- private boolean isWhiteSpace(char ch) {
- switch (ch) {
- case ' ':
- case '\t':
- case '\r':
- case '\n':
- return true;
- default:
- return false;
- }
- }
-
- private boolean skipIdentifier(String identifier) {
- char ch;
- if (index > 0) {
- ch = buffer.charAt(index-1);
- if (!isWhiteSpace(ch))
- return false;
- }
- int end = index + identifier.length();
- if (end > last)
- return false;
-
- String part = new String(buffer).substring(index,end);
- if (!identifier.equalsIgnoreCase(part))
- return false;
-
- if (end < last) {
- ch = buffer.charAt(end);
- if (ch != ';' && !isWhiteSpace(ch))
- return false;
- }
-
- index = end-1;
- return true;
- }
-
- public String nextToken() {
- skipSpacing(true);
- int start = index;
- int begin_level = 0;
- boolean is_creating = false;
- boolean was_creating = false;
-
- while (index < last) {
- char ch = buffer.charAt(index);
- if (ch == ';' && begin_level == 0)
- break;
- switch (ch) {
- case 'b':
- case 'B':
- if (skipIdentifier("begin")) {
- if (is_creating)
- is_creating = false;
- else
- begin_level++;
- }
- break;
-
- case 'c':
- case 'C':
- if (skipIdentifier("create")) {
- index++;
- skipSpacing(false);
- if (skipIdentifier("procedure") || skipIdentifier("function")) {
- begin_level++;
- index++;
- is_creating = true;
- was_creating = true;
- }
- index--;
- }
- break;
-
- case 'e':
- case 'E':
- if (skipIdentifier("end"))
- begin_level--;
- break;
-
- default:
- }
- index++;
- }
- if (start == last)
- return null;
- String result;
- int end = keepSemiColonForBeginEnd && was_creating ? index + 1 : index;
- result = new String(buffer).substring(start,end);
- if (index < last)
- index++;
- return result;
- }
-
- private StringBuffer buffer;
- private int index;
- private int last;
- private boolean keepSemiColonForBeginEnd;
- }
-