Java SQL Table createHSQLTables(LineNumberReader reader, Statement statement)

Here you can find the source of createHSQLTables(LineNumberReader reader, Statement statement)

Description

create HSQL Tables

License

Open Source License

Declaration

private static void createHSQLTables(LineNumberReader reader, Statement statement)
            throws IOException, SQLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.Statement;
import java.sql.SQLException;
import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Main {
    private static Pattern pattern = Pattern.compile("\\s*(\\w+)\\s+(\\w+)\\s+.*");

    private static void createHSQLTables(LineNumberReader reader, Statement statement)
            throws IOException, SQLException {
        String line;//from w w  w  . j a  va 2  s. co  m
        int idx = 0;
        String addPkLine = null;
        String createTableLine = null;
        StringBuffer stringBuffer = new StringBuffer(1000);

        while (null != (line = reader.readLine())) {
            if (line.startsWith("alter table ")) {
                // do nothing
            } else {

                if (line.startsWith("create table ")) {
                    createTableLine = (line + "\n");
                    stringBuffer.append(createTableLine);
                    addPkLine = null;
                }
                /*
                            else if (line.startsWith("drop")) {
                    
                stringBuffer.append(line+";\n");
                try {
                statement.execute(stringBuffer.toString());
                } catch (Exception e) {
                logger.error("Error running SQL-statement: "+ line,e);
                }
                    
                stringBuffer = new StringBuffer(1000);
                    
                            }
                */
                else if (line.startsWith("drop") || line.startsWith("alter") || line.startsWith(")")
                        || line.startsWith("create sequence")) {
                    /*
                                    if (line.indexOf("reportchange")!=-1)
                                    {
                    stringBuffer = new StringBuffer(1000);
                    continue;
                                    }
                    */
                    if (addPkLine != null) {
                        //                            stringBuffer.append(",\n"+addPkLine+"\n");
                        addPkLine = null;
                    }
                    stringBuffer.append(line + ";\n");

                    statement.execute(stringBuffer.toString());
                    stringBuffer = new StringBuffer(1000);
                } else if (line.indexOf("generated by default as identity (start with 1),") != -1) {
                    Matcher matcher = pattern.matcher(line);
                    matcher.matches();

                    stringBuffer.append("\t" + matcher.group(1) + " " + matcher.group(2) + ",\n");
                    addPkLine = "   primary key (" + matcher.group(1) + ")";
                } else {
                    stringBuffer.append(line + "\n");
                }
            }
            idx++;
        }
    }
}

Related

  1. constructObject(Class theClass, Connection db, int objectId, String tableName, String uniqueField)
  2. count(Connection conn, String table)
  3. countTables(Connection connection)
  4. createBagValuesTables(Connection con)
  5. createFreqTable()
  6. createSourceTable(Connection con)
  7. createTable(Connection dbCon, String tableName, String fields)
  8. createTable(Statement statement, String spaceName, String minSeeders, String capacity, String replicationCount)
  9. createTable(String createTblSql, Connection conn)