Ejemplo n.º 1
0
  @Override
  public String ddlCreateIndex(TableMapping mapping, TableMapping.Index index) {
    final String name = index.name;

    if (index.unique) {
      return null;
      // Prepend Primary Key.
      //            final LinkedHashSet<String> columns = new LinkedHashSet<>();
      //
      // columns.addAll(Lists.newArrayList(columnNames(mapping.getPrimaryKeyProperties())));
      //            columns.addAll(Lists.newArrayList(index.columnNames));
      //            return create()
      //                .alterTable(mapping.getTableName())
      //                .add(DSL.constraint(name).unique(columns.toArray(new
      // String[columns.size()]))).getSQL();
    }

    return create()
        .createIndex(name)
        .on(mapping.getTableName(), index.columnNames)
        .getSQL(ParamType.INLINED);
  }
Ejemplo n.º 2
0
  /**
   * @param mapping
   * @return
   */
  @Override
  public String ddlCreateTable(TableMapping mapping) {
    final CreateTableAsStep step = create().createTable(mapping.getTableName());
    CreateTableColumnStep createColumnStep = null;
    for (TableMapping.Property property : mapping.getProperties()) {
      createColumnStep = step.column(property.getColumnName(), property.fieldDataType());
    }

    if (!mapping.tableAnnotation.columnStore()) {
      createColumnStep.constraints(
          DSL.constraint("pk_" + mapping.getTableName())
              .primaryKey(columnNames(mapping.getPrimaryKeyProperties())));

      final List<TableMapping.Index> indexes = mapping.getIndexes();
      if (indexes != null) {
        for (TableMapping.Index index : indexes) {
          if (index.unique) {
            final LinkedHashSet<String> columns = new LinkedHashSet<>();
            columns.addAll(Lists.newArrayList(columnNames(mapping.getPrimaryKeyProperties())));
            columns.addAll(Lists.newArrayList(index.columnNames));

            createColumnStep.constraint(
                DSL.constraint(index.name).unique(columns.toArray(new String[columns.size()])));
          }
        }
      }
    }

    String sql = createColumnStep.getSQL(ParamType.INLINED);

    // Handle reference tables.
    if (mapping.tableAnnotation.reference()) {
      if (sql.startsWith("create table") || sql.startsWith("CREATE TABLE")) {
        sql = sql.substring(12);
        sql = "CREATE REFERENCE TABLE" + sql;
      }
    }

    sql = sql.trim();

    if (sql.endsWith(";")) {
      sql = sql.substring(0, sql.length() - 1);
    }

    if (sql.endsWith(")")) {
      sql = sql.substring(0, sql.length() - 1);
    }

    if (mapping.tableAnnotation.columnStore()) {
      final List<TableMapping.Property> primaryKey = mapping.getPrimaryKeyProperties();

      sql +=
          ", KEY ("
              + Joiner.on(",").join(columnNames(primaryKey))
              + ") USING CLUSTERED COLUMNSTORE";
    } else {
      final List<TableMapping.Property> primaryKey = mapping.getPrimaryKeyProperties();

      // Add index on "id" column if it isn't the primary key.
      if (primaryKey.size() == 1 && !primaryKey.get(0).columnName.equals(AbstractEntity.ID)) {
        sql += ", KEY ix_" + mapping.getTableName() + "_id (`id`)";
      }
    }

    if (!mapping.tableAnnotation.reference()) {
      final List<TableMapping.Property> shardKey = mapping.getShardKeyProperties();
      if (shardKey != null && !shardKey.isEmpty()) {
        sql = sql + ", SHARD KEY (" + Joiner.on(",").join(columnNames(shardKey)) + ")";
      }
    }

    sql += ")";

    return sql;
  }