All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.drus.jpa.unit.sql.dbunit.SqlScript Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
package eu.drus.jpa.unit.sql.dbunit;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.StringTokenizer;

public class SqlScript implements Iterable {

    private static final String COMMENT_PATTERN = "(?:--.*)|(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)|(?:#.*)";

    private static class SqlScriptIterator implements Iterator {

        private final StringTokenizer st;
        private String cached;
        private boolean hasNextCached;
        private boolean hasNext;

        private SqlScriptIterator(final String script) {
            st = new StringTokenizer(script.replaceAll(COMMENT_PATTERN, ""), ";");
        }

        @Override
        public boolean hasNext() {
            return hasNextCached ? hasNext : findNextMatch();
        }

        @Override
        public String next() {
            if (hasNext()) {
                hasNextCached = false;
                return cached;
            } else {
                throw new NoSuchElementException();
            }
        }

        private boolean findNextMatch() {
            boolean match = false;
            while (!match && st.hasMoreElements()) {
                cached = st.nextToken().trim();
                match = !cached.isEmpty();
            }
            hasNextCached = true;
            hasNext = match;
            return match;
        }

    }

    private String script;

    public SqlScript(final String script) {
        this.script = script;
    }

    @Override
    public Iterator iterator() {
        return new SqlScriptIterator(script);
    }

    @Override
    public Spliterator spliterator() {
        throw new UnsupportedOperationException();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy