com.zergiu.tvman.init.SQLFileImporter.java Source code

Java tutorial

Introduction

Here is the source code for com.zergiu.tvman.init.SQLFileImporter.java

Source

/*
 * Copyright (c) Sergiu Giurgiu 2011.
 *
 * This file is part of TVMan.
 *
 * TVMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TVMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TVMan.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.zergiu.tvman.init;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.StrBuilder;

/**
 * Reads a sql file into sql statements. It considers a statement as being on as many lines as needed,
 * but MUST end with a semicolon.
 * Comments can be started with a "--" or a  "//" . It only accepts single line comments.
 * The comments can be within a statement if that statement spans multiple lines, and the comment will end that
 * line.   
 * @author sergiu
 *
 */
public class SQLFileImporter {

    private InputStream inputStream;

    public SQLFileImporter(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Iterator<String> getSQLLines() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        List<String> statements = new LinkedList<>();
        String line = null;
        StrBuilder statementBuilder = new StrBuilder();
        while ((line = reader.readLine()) != null) {
            line = trimLine(line);
            if (StringUtils.isEmpty(line)) {
                continue;
            }
            buildStatement(statements, line, statementBuilder);
        }
        return statements.iterator();
    }

    /**
     * @param statements
     * @param line
     * @param statementBuilder
     */
    private void buildStatement(List<String> statements, String line, StrBuilder statementBuilder) {
        int indexOfSemicolon = StringUtils.indexOf(line, ";");
        if (indexOfSemicolon >= 0) {
            statementBuilder.append(line.substring(0, indexOfSemicolon));//add contents of the line up to ;
            statements.add(statementBuilder.toString()); //add the statement to the list
            statementBuilder.clear();//clear whatever we had before
            statementBuilder.append(line.substring(indexOfSemicolon + 1));//add whatever is left on that line after ;
        } else {
            //we're not done yet
            //always prepend a space before adding a new line
            statementBuilder.append(" " + line);
        }
    }

    /**
     * @param line
     * @return
     */
    private String trimLine(String line) {
        line = line.trim();
        int index = StringUtils.indexOfAny(line, "--", "//");
        if (index >= 0) {
            line = StringUtils.left(line, index);
        }
        return line;
    }
}