Example #1
0
  public static void main(String[] args) {
    int nbArg = args.length;

    // input read
    if (args.length > 0) {
      int indice = 0;
      while (indice < nbArg) {
        if (args[indice].equals("-in")) {
          if ((indice + 1) < nbArg) {
            indice++;
            scriptName = args[indice];
          }
        } else if (args[indice].equals("-out")) {
          if ((indice + 1) < nbArg) {
            indice++;
            outputName = args[indice];
          }
        } else if (args[indice].equals("-install")) {
          if ((indice + 1) < nbArg) {
            indice++;
            install = Boolean.valueOf(args[indice]);
          }
        }
        indice++;
      }
    }
    // input argument read
    LogUtils.info("--------------------------------------");
    LogUtils.info("This program prepares the script DB creation");
    LogUtils.info("Arguments :");
    LogUtils.info(" -in : " + scriptName);
    LogUtils.info(" -out : " + outputName);
    LogUtils.info("--------------------------------------");

    // open files
    try {
      out = new PrintStream(new FileOutputStream(outputName));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    try {
      in = new BufferedReader(new FileReader(scriptName));
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    // read input file, format it and write in output file
    initOutput();
    readWrite();

    // end
    try {
      in.close();
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Example #2
0
  private static void readWrite() {
    // XXX Auto-generated method stub
    try {
      String tableName = "";
      String line = (String) in.readLine();
      while (line != null) {

        // 1. to uppercase
        String outLine = line.toUpperCase();

        if (install) {
          // find table name
          if (outLine.startsWith("CREATE TABLE ")) {
            String endOfLine = outLine.substring(13, outLine.length());
            int endTableName = endOfLine.indexOf(" (");
            if (endTableName != -1) tableName = endOfLine.substring(0, endTableName);
            // LogUtils.info("TableName : "+ tableName + " install? "
            // + isInstallTable(tableName));
          }
        }

        // 2. LONG BINARY - > LONGBLOB
        int i = outLine.indexOf("LONG VARBINARY");
        if (i != -1) {
          LogUtils.info("LONG VARBINARY found");
          outLine = outLine.replaceAll("LONG VARBINARY", "LONGBLOB");
        }

        int t = outLine.indexOf("TEXT(65535)");
        while (t != -1) {
          LogUtils.info("TEXT(65535) found");
          String source = "TEXT(65535)";
          int size = source.length();
          outLine =
              outLine.substring(0, t) + "LONGTEXT" + outLine.substring(t + size, outLine.length());
          // outLine = outLine.replaceAll("DATETIME(19)", "DATETIME");
          // LogUtils.info(outLine);
          t = outLine.indexOf("TEXT(65535)");
        }

        // 2. LONG BINARY - > LONGBLOB
        int d = outLine.indexOf("DATETIME(19)");
        while (d != -1) {
          LogUtils.info("DATETIME found");
          String source = "DATETIME(19)";
          int size = source.length();
          outLine =
              outLine.substring(0, d) + "DATETIME" + outLine.substring(d + size, outLine.length());
          // outLine = outLine.replaceAll("DATETIME(19)", "DATETIME");
          // LogUtils.info(outLine);
          d = outLine.indexOf("DATETIME(19)");
        }

        int p = outLine.indexOf("PRIMARY KEY PRIMARY");
        if (p != -1) {
          LogUtils.info("PRIMARY KEY PRIMARY found");
          String source = "PRIMARY KEY PRIMARY";
          int size = source.length();
          outLine =
              outLine.substring(0, p)
                  + "PRIMARY KEY "
                  + outLine.substring(p + size, outLine.length());
          // outLine = outLine.replaceAll("DATETIME(19)", "DATETIME");
          // LogUtils.info(outLine);
        }

        int lb = outLine.indexOf("LONGBLOB(2147483647)");
        while (lb != -1) {
          LogUtils.info("LONGBLOB(2147483647) found");
          String source = "LONGBLOB(2147483647)";
          int size = source.length();
          outLine =
              outLine.substring(0, lb)
                  + "LONGBLOB"
                  + outLine.substring(lb + size, outLine.length());
          // outLine = outLine.replaceAll("DATETIME(19)", "DATETIME");
          // LogUtils.info(outLine);
          lb = outLine.indexOf("LONGBLOB(2147483647)");
        }

        int db = outLine.indexOf("DOUBLE(22)");
        while (db != -1) {
          LogUtils.info("DOUBLE(22) found");
          String source = "DOUBLE(22)";
          int size = source.length();
          outLine =
              outLine.substring(0, db) + "DOUBLE" + outLine.substring(db + size, outLine.length());
          // outLine = outLine.replaceAll("DATETIME(19)", "DATETIME");
          // LogUtils.info(outLine);
          db = outLine.indexOf("DOUBLE(22)");
        }

        // CONSTRAINT PRIMARY
        // 3. CONSTRAINT PRIMARY ->
        int ic = outLine.indexOf("CONSTRAINT PRIMARY");
        if (ic != -1) {
          LogUtils.info("CONSTRAINT PRIMARY found");
          outLine = outLine.replaceAll("CONSTRAINT PRIMARY", " ");
        }

        if (!install) out.println(outLine);
        else {
          if (isInstallTable(tableName)) {
            if (tableName.toUpperCase().equals("USER0")) {
              String decDbCode = "DBCODE VARCHAR(255)";
              int ivar = outLine.indexOf(decDbCode);
              if (ivar != -1) {
                LogUtils.info("USER0 found");
                String res = outLine.substring(0, ivar + decDbCode.length());
                res += " DEFAULT '1'";
                res += outLine.substring(ivar + decDbCode.length(), outLine.length());
                outLine = res;
              }
            }
            out.println(outLine);
          }
        }
        line = (String) in.readLine();
      }
    } catch (IOException e) {
      LogUtils.severe(null, e);
    }
  }