Пример #1
0
  public String generateDatabaseName() { // prompts user for database name
    String dbNameDefault = "MySQLDB";
    // String databaseName = "";

    do {
      databaseName =
          (String)
              JOptionPane.showInputDialog(
                  null,
                  "Enter the database name:",
                  "Database Name",
                  JOptionPane.PLAIN_MESSAGE,
                  null,
                  null,
                  dbNameDefault);
      if (databaseName == null) {
        DatabaseConvertGUI.setReadSuccess(false);
        return "";
      }
      if (databaseName.equals("")) {
        JOptionPane.showMessageDialog(null, "You must select a name for your database.");
      }
    } while (databaseName.equals(""));
    return databaseName;
  }
Пример #2
0
 public void createDDL() {
   DatabaseConvertGUI.setReadSuccess(true);
   databaseName = generateDatabaseName();
   sb.append("CREATE DATABASE " + databaseName + ";\r\n");
   sb.append("USE " + databaseName + ";\r\n");
   for (int boundCount = 0;
       boundCount <= maxBound;
       boundCount++) { // process tables in order from least dependent (least number of bound
                       // tables) to most dependent
     for (int tableCount = 0;
         tableCount < numBoundTables.length;
         tableCount++) { // step through list of tables
       if (numBoundTables[tableCount] == boundCount) { //
         sb.append("CREATE TABLE " + tables[tableCount].getName() + " (\r\n");
         int[] nativeFields = tables[tableCount].getNativeFieldsArray();
         int[] relatedFields = tables[tableCount].getRelatedFieldsArray();
         boolean[] primaryKey = new boolean[nativeFields.length];
         int numPrimaryKey = 0;
         int numForeignKey = 0;
         for (int nativeFieldCount = 0;
             nativeFieldCount < nativeFields.length;
             nativeFieldCount++) { // print out the fields
           DatabaseField currentField = getField(nativeFields[nativeFieldCount]);
           sb.append(
               "\t" + currentField.getName() + " " + strDataType[currentField.getDataType()]);
           if (currentField.getDataType() == 0) { // varchar
             sb.append(
                 "("
                     + currentField.getVarcharValue()
                     + ")"); // append varchar length in () if data type is varchar
           }
           if (currentField.getDisallowNull()) {
             sb.append(" NOT NULL");
           }
           if (!currentField.getDefaultValue().equals("")) {
             if (currentField.getDataType() == 1) { // boolean data type
               sb.append(" DEFAULT " + convertStrBooleanToInt(currentField.getDefaultValue()));
             } else { // any other data type
               sb.append(" DEFAULT " + currentField.getDefaultValue());
             }
           }
           if (currentField.getIsPrimaryKey()) {
             primaryKey[nativeFieldCount] = true;
             numPrimaryKey++;
           } else {
             primaryKey[nativeFieldCount] = false;
           }
           if (currentField.getFieldBound() != 0) {
             numForeignKey++;
           }
           sb.append(",\r\n"); // end of field
         }
         if (numPrimaryKey > 0) { // table has primary key(s)
           sb.append("CONSTRAINT " + tables[tableCount].getName() + "_PK PRIMARY KEY (");
           for (int i = 0; i < primaryKey.length; i++) {
             if (primaryKey[i]) {
               sb.append(getField(nativeFields[i]).getName());
               numPrimaryKey--;
               if (numPrimaryKey > 0) {
                 sb.append(", ");
               }
             }
           }
           sb.append(")");
           if (numForeignKey > 0) {
             sb.append(",");
           }
           sb.append("\r\n");
         }
         if (numForeignKey > 0) { // table has foreign keys
           int currentFK = 1;
           for (int i = 0; i < relatedFields.length; i++) {
             if (relatedFields[i] != 0) {
               sb.append(
                   "CONSTRAINT "
                       + tables[tableCount].getName()
                       + "_FK"
                       + currentFK
                       + " FOREIGN KEY("
                       + getField(nativeFields[i]).getName()
                       + ") REFERENCES "
                       + getTable(getField(nativeFields[i]).getTableBound()).getName()
                       + "("
                       + getField(relatedFields[i]).getName()
                       + ")");
               if (currentFK < numForeignKey) {
                 sb.append(",\r\n");
               }
               currentFK++;
             }
           }
           sb.append("\r\n");
         }
         sb.append(");\r\n\r\n"); // end of table
       }
     }
   }
 }