db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r406474 - /db/ddlutils/trunk/src/java/org/apache/ddlutils/util/SqlTokenizer.java
Date Mon, 15 May 2006 00:26:06 GMT
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;
+    }
+}



Mime
View raw message