You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ddlutils-dev@db.apache.org by to...@apache.org on 2006/05/15 02:26:06 UTC

svn commit: r406474 - /db/ddlutils/trunk/src/java/org/apache/ddlutils/util/SqlTokenizer.java

Author: tomdz
Date: Sun May 14 17:26:06 2006
New Revision: 406474

URL: http://svn.apache.org/viewcvs?rev=406474&view=rev
Log:
Fixed alteration test so that they now use the catalog and schema if specified
Enhanced alteration support for Oracle
Introduced new sql statement tokenizer that splits in row-mode

Added:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/util/SqlTokenizer.java

Added: db/ddlutils/trunk/src/java/org/apache/ddlutils/util/SqlTokenizer.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/util/SqlTokenizer.java?rev=406474&view=auto
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/util/SqlTokenizer.java (added)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/util/SqlTokenizer.java Sun May 14 17:26:06 2006
@@ -0,0 +1,82 @@
+package org.apache.ddlutils.util;
+
+
+/**
+ * A statement tokenizer for SQL strings that splits only at delimiters that
+ * are at the end of a line or the end of the SQL (row mode).  
+ */
+public class SqlTokenizer
+{
+    private String  _sql;
+    private int     _lastCharIdx;
+    private int     _lastDelimiterPos = -1;
+    private int     _nextDelimiterPos = -1;
+    private boolean _finished;
+
+    /**
+     * Creates a new sql tokenizer.
+     * 
+     * @param sql The sql text
+     */
+    public SqlTokenizer(String sql)
+    {
+        _sql         = sql;
+        _lastCharIdx = sql.length() - 1;
+    }
+
+    /**
+     * Determines whether there are more statements.
+     * 
+     * @return <code>true</code> if there are more statements
+     */
+    public boolean hasMoreStatements()
+    {
+        if (_finished)
+        {
+            return false;
+        }
+        else
+        {
+            if (_nextDelimiterPos <= _lastDelimiterPos)
+            {
+                _nextDelimiterPos = _sql.indexOf(';', _lastDelimiterPos + 1);
+                while ((_nextDelimiterPos >= 0) && (_nextDelimiterPos < _lastCharIdx))
+                {
+                    char nextChar = _sql.charAt(_nextDelimiterPos + 1);
+
+                    if ((nextChar == '\r') || (nextChar == '\n'))
+                    {
+                        break;
+                    }
+                    _nextDelimiterPos = _sql.indexOf(';', _nextDelimiterPos + 1);
+                }
+            }
+            return (_nextDelimiterPos >= 0) || (_lastDelimiterPos < _lastCharIdx);
+        }
+    }
+
+    /**
+     * Returns the next statement.
+     * 
+     * @return The statement
+     */
+    public String getNextStatement()
+    {
+        String result = null;
+
+        if (hasMoreStatements())
+        {
+            if (_nextDelimiterPos >= 0)
+            {
+                result            = _sql.substring(_lastDelimiterPos + 1, _nextDelimiterPos);
+                _lastDelimiterPos = _nextDelimiterPos;
+            }
+            else
+            {
+                result    = _sql.substring(_lastDelimiterPos + 1);
+                _finished = true;
+            }
+        }
+        return result;
+    }
+}