Exemple #1
0
  public String addTableBody(Table table) {
    String name = SQLStringVisitor.escapeSinglePart(table.getName());
    append(name);

    if (table.getColumns() != null) {
      append(SPACE);
      append(LPAREN);
      boolean first = true;
      for (Column c : table.getColumns()) {
        if (first) {
          first = false;
        } else {
          append(COMMA);
        }
        visit(table, c);
      }
      buildContraints(table);
      append(NEWLINE);
      append(RPAREN);
    }

    // options
    String options = buildTableOptions(table);
    if (!options.isEmpty()) {
      append(SPACE).append(OPTIONS).append(SPACE).append(LPAREN).append(options).append(RPAREN);
    }
    return name;
  }
Exemple #2
0
  private void buildContraints(Table table) {
    addConstraints(table.getAccessPatterns(), "AP", ACCESSPATTERN); // $NON-NLS-1$

    KeyRecord pk = table.getPrimaryKey();
    if (pk != null) {
      addConstraint("PK", PRIMARY_KEY, pk, true); // $NON-NLS-1$
    }

    addConstraints(table.getUniqueKeys(), UNIQUE, UNIQUE);
    addConstraints(table.getIndexes(), INDEX, INDEX);
    addConstraints(table.getFunctionBasedIndexes(), INDEX, INDEX);

    for (int i = 0; i < table.getForeignKeys().size(); i++) {
      ForeignKey key = table.getForeignKeys().get(i);
      addConstraint("FK" + i, FOREIGN_KEY, key, false); // $NON-NLS-1$
      append(SPACE).append(REFERENCES);
      if (key.getReferenceKey() != null) {
        if (key.getReferenceKey().getParent().getParent().equals(key.getParent().getParent())) {
          append(SPACE).append(new GroupSymbol(key.getReferenceKey().getParent().getName()));
        } else {
          append(SPACE).append(new GroupSymbol(key.getReferenceKey().getParent().getFullName()));
        }
      } else if (key.getReferenceTableName() != null) {
        append(SPACE).append(new GroupSymbol(key.getReferenceTableName()));
      }
      append(SPACE);
      addNames(key.getReferenceColumns());
      appendOptions(key);
    }
  }
Exemple #3
0
  private void visit(Table table, Column column) {
    append(NEWLINE).append(TAB);
    if (table.getTableType() == Table.Type.TemporaryTable
        && column.isAutoIncremented()
        && column.getNullType() == NullType.No_Nulls
        && column.getJavaType() == DataTypeManager.DefaultDataClasses.INTEGER) {
      append(SQLStringVisitor.escapeSinglePart(column.getName()));
      append(SPACE);
      append(SERIAL);
    } else {
      appendColumn(column, true, true);

      if (column.isAutoIncremented()) {
        append(SPACE).append(AUTO_INCREMENT);
      }
    }

    appendDefault(column);

    // options
    appendColumnOptions(column);
  }
Exemple #4
0
  private String buildTableOptions(Table table) {
    StringBuilder options = new StringBuilder();
    addCommonOptions(options, table);

    if (table.isMaterialized()) {
      addOption(options, MATERIALIZED, table.isMaterialized());
      if (table.getMaterializedTable() != null) {
        addOption(options, MATERIALIZED_TABLE, table.getMaterializedTable().getName());
      }
    }
    if (table.supportsUpdate()) {
      addOption(options, UPDATABLE, table.supportsUpdate());
    }
    if (table.getCardinality() != -1) {
      if (table.getCardinality() != table.getCardinalityAsFloat()) {
        addOption(options, CARDINALITY, (long) table.getCardinalityAsFloat());
      } else {
        addOption(options, CARDINALITY, table.getCardinality());
      }
    }
    if (!table.getProperties().isEmpty()) {
      for (String key : table.getProperties().keySet()) {
        addOption(options, key, table.getProperty(key, false));
      }
    }
    return options.toString();
  }
Exemple #5
0
  private void visit(Table table) {
    if (this.filter != null && !filter.matcher(table.getName()).matches()) {
      return;
    }

    append(CREATE).append(SPACE);
    if (table.isPhysical()) {
      append(FOREIGN_TABLE);
    } else {
      if (table.getTableType() == Table.Type.TemporaryTable) {
        append(GLOBAL).append(SPACE).append(TEMPORARY).append(SPACE).append(TABLE);
      } else {
        append(VIEW);
      }
    }
    append(SPACE);
    String name = addTableBody(table);

    if (table.getTableType() != Table.Type.TemporaryTable) {
      if (table.isVirtual()) {
        append(NEWLINE)
            .append(SQLConstants.Reserved.AS)
            .append(NEWLINE)
            .append(table.getSelectTransformation());
      }
      append(SQLConstants.Tokens.SEMICOLON);

      if (table.isInsertPlanEnabled()) {
        buildTrigger(name, null, INSERT, table.getInsertPlan());
      }

      if (table.isUpdatePlanEnabled()) {
        buildTrigger(name, null, UPDATE, table.getUpdatePlan());
      }

      if (table.isDeletePlanEnabled()) {
        buildTrigger(name, null, DELETE, table.getDeletePlan());
      }

      for (Trigger tr : table.getTriggers().values()) {
        buildTrigger(name, tr.getName(), tr.getEvent().name(), tr.getPlan());
      }
    } else {
      append(SQLConstants.Tokens.SEMICOLON);
    }
  }