Exemplo n.º 1
0
  public String generateForeignKeyAddStatement(ITable table) {
    try {
      List<IColumn> columnList = table.getColumnList();

      List<IColumn> foreignKeys = new ArrayList<IColumn>();
      for (IColumn column : columnList) {
        if (column.isForeignKey()) {
          foreignKeys.add(column);
        }
      }

      if (foreignKeys.size() > 0) {
        StringBuffer sb = new StringBuffer();
        sb.append("ALTER TABLE "); // $NON-NLS-1$
        sb.append(table.getQualifiedName());
        sb.append("\n"); // $NON-NLS-1$
        sb.append("  ADD FOREIGN KEY "); // $NON-NLS-1$
        for (IColumn foreignKey : foreignKeys) {
          sb.append(" ("); // $NON-NLS-1$
          sb.append(quoteIdentifier(table.getDatabaseInfo(), foreignKey.getName()));
          sb.append(")\n"); // $NON-NLS-1$
          sb.append("     REFERENCES "); // $NON-NLS-1$
          List<IForeignKeyReference> relationshipList = foreignKey.getForeignKeyReferenceList();

          for (int i = 0; i < relationshipList.size(); i++) {
            IForeignKeyReference relationship = relationshipList.get(i);
            if (i == 0) {
              sb.append(relationship.getPrimaryKeyTable() + " ("); // $NON-NLS-1$
            }
            sb.append(quoteIdentifier(table.getDatabaseInfo(), relationship.getPrimaryKeyColumn()));
            if ((relationshipList.size() > 1) && (i < relationshipList.size() - 1)) {
              sb.append(", "); // $NON-NLS-1$
            }
            if (i == relationshipList.size() - 1) {
              sb.append(")"); // $NON-NLS-1$
            }
          }
        }
        sb.append(";\n"); // $NON-NLS-1$
        return sb.toString().trim();
      }
    } catch (SQLException exc) {
      exc.printStackTrace();
    }
    return null;
  }
Exemplo n.º 2
0
  public String generateTableCreateStatement(ITable table, boolean generateForeignKey) {
    try {
      List<IColumn> columnList = table.getColumnList();
      StringBuffer tableCreateBuffer = new StringBuffer();
      tableCreateBuffer.append("CREATE TABLE "); // $NON-NLS-1$
      tableCreateBuffer.append(table.getQualifiedName());
      tableCreateBuffer.append(" (\n"); // $NON-NLS-1$
      List<IColumn> primaryKeys = new ArrayList<IColumn>();
      List<IColumn> foreignKeys = new ArrayList<IColumn>();
      for (IColumn column : columnList) {
        tableCreateBuffer.append(
            "   " //$NON-NLS-1$
                + quoteIdentifier(table.getDatabaseInfo(), column.getName())
                + " "); //$NON-NLS-1$
        tableCreateBuffer.append(column.getType().getName());
        if (column.getSize() > 0) {
          tableCreateBuffer.append("(" + column.getSize() + ")"); // $NON-NLS-1$ //$NON-NLS-2$
        }
        if (!column.isNullAllowed()) {
          tableCreateBuffer.append(" NOT NULL "); // $NON-NLS-1$
        }

        if ((column.getDefaultValue() != null) && !column.getDefaultValue().trim().equals("")) {
          tableCreateBuffer.append(" DEFAULT " + column.getDefaultValue().trim()); // $NON-NLS-1$
        }

        if (isAutoIncrementSupported(column)) {
          if (column.isAutoIncrement()) {
            tableCreateBuffer.append(" AUTO_INCREMENT "); // $NON-NLS-1$
          }
        }
        // else if (isDefaultSupported(column)){
        //
        // }
        if (column.isPrimaryKey()) {
          primaryKeys.add(column);
        }
        if (column.isForeignKey()) {
          foreignKeys.add(column);
        }
        if (columnList.indexOf(column) < (columnList.size() - 1)) {
          tableCreateBuffer.append(",\n"); // $NON-NLS-1$
        }
      }
      if (primaryKeys.size() > 0) {
        tableCreateBuffer.append(",\n   PRIMARY KEY ("); // $NON-NLS-1$
        for (IColumn primaryKey : primaryKeys) {
          tableCreateBuffer.append(quoteIdentifier(table.getDatabaseInfo(), primaryKey.getName()));
          if (primaryKeys.indexOf(primaryKey) < (primaryKeys.size() - 1)) {
            tableCreateBuffer.append(","); // $NON-NLS-1$
          } else {
            tableCreateBuffer.append(")\n"); // $NON-NLS-1$
          }
        }
      }
      if (generateForeignKey) {
        if (foreignKeys.size() > 0) {
          tableCreateBuffer.append(",\n   FOREIGN KEY "); // $NON-NLS-1$
          for (IColumn foreignKey : foreignKeys) {
            tableCreateBuffer.append(" ("); // $NON-NLS-1$
            tableCreateBuffer.append(
                quoteIdentifier(table.getDatabaseInfo(), foreignKey.getName()));
            tableCreateBuffer.append(") "); // $NON-NLS-1$
            tableCreateBuffer.append(" REFRENCES "); // $NON-NLS-1$
            List<IForeignKeyReference> relationshipList = foreignKey.getForeignKeyReferenceList();

            for (int i = 0; i < relationshipList.size(); i++) {
              IForeignKeyReference relationship = relationshipList.get(i);
              if (i == 0) {
                tableCreateBuffer.append(relationship.getPrimaryKeyTable() + "("); // $NON-NLS-1$
              }
              tableCreateBuffer.append(
                  quoteIdentifier(table.getDatabaseInfo(), relationship.getPrimaryKeyColumn()));
              if ((relationshipList.size() > 1) && (i < relationshipList.size() - 1)) {
                tableCreateBuffer.append(", "); // $NON-NLS-1$
              }
              if (i == relationshipList.size() - 1) {
                tableCreateBuffer.append(")"); // $NON-NLS-1$
              }
            }
          }
        }
      }
      tableCreateBuffer.append("\n);\n"); // $NON-NLS-1$
      return tableCreateBuffer.toString().trim();
    } catch (SQLException exc) {
      exc.printStackTrace();
      // ErrorManager.showException(exc);
    }
    return null;
  }