schemacrawler.schema.Column Java Examples

The following examples show how to use schemacrawler.schema.Column. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Enum.java    From avro-schema-generator with MIT License 6 votes vote down vote up
public Enum(Column column) {
    super("enum");
    this.name = column.getName();

    String allowedValues = column.getAttribute("COLUMN_TYPE").toString();
    this.symbols = allowedValues
            .replaceFirst("enum", "")
            .replaceFirst("ENUM", "")
            .replace(")", "")
            .replace("(", "")
            .split(",");

    for (int i = 0; i < symbols.length; i++) {
        symbols[i] = symbols[i].trim().replaceAll("'", "");
    }
}
 
Example #2
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected JdbcType getNodeJdbcType(Column column) {
    switch(column.getColumnDataType().getJavaSqlType().getJavaSqlType()) {
    case Types.VARCHAR: return JdbcType.VARCHAR;
    case Types.INTEGER: return JdbcType.INT;
    case Types.BIGINT: return JdbcType.BIGINT;
    case Types.CHAR: return JdbcType.CHAR;
    case Types.DATE: return JdbcType.DATE;
    case Types.TIMESTAMP: return JdbcType.TIMESTAMP;
    case Types.DECIMAL: return JdbcType.DECIMAL;
    case Types.NUMERIC: return JdbcType.DECIMAL;
    case Types.SMALLINT: return JdbcType.SMALLINT;
    case Types.BIT: return JdbcType.SMALLINT;
    case Types.BLOB: return JdbcType.BLOB;
    case Types.CLOB: return JdbcType.CLOB;
    case 2147483647: {
      JdbcType jt = getJdbcTypeForUnknownJavaSqlType(column);
      if (jt != null) {
        return jt;
      }
    }
    }
   throw new IllegalArgumentException("Unsupported column type " + column.getColumnDataType().getJavaSqlType() + " (" + column.getColumnDataType().getJavaSqlType().getJavaSqlType() + ")");
}
 
Example #3
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected NodeSpec toNodeSpec(EntitySpec entitySpec, Table table, Column column) {
    NodeSpec nodeSpec = new NodeSpec();
    nodeSpec.setName( getNodeName( column ));
    if (isPrimaryKey(table, column)) {
        nodeSpec.setPrimaryKey(true);
    }
    nodeSpec.setColumnName( column.getName());
    nodeSpec.setJdbcType( getNodeJdbcType( column ));
    nodeSpec.setJavaType( getJavaType( nodeSpec.getJdbcType() ));
    nodeSpec.setNullable( column.isNullable() ? Nullable.NULL : Nullable.NOT_NULL);
    if (nodeSpec.getJavaType() == JavaType.STRING) {
        nodeSpec.setLength( column.getSize() );
    }
    else if (nodeSpec.getJavaType() == JavaType.BIGDECIMAL) {
        nodeSpec.setPrecision( column.getSize() );
        nodeSpec.setScale( column.getDecimalDigits());
    }
    nodeSpec.setEntity(entitySpec);
    nodeSpecs.put(column, nodeSpec);
    return nodeSpec;
}
 
Example #4
Source File: AvroTypeUtilTest.java    From avro-schema-generator with MIT License 6 votes vote down vote up
@Test
public void testDecimalTypes() throws Exception {
    String[] dateTypes = new String[] { "decimal", "numeric" };

    for (String dateType : dateTypes) {
        Column column = column(dateType);
        when(column.getSize()).thenReturn(20);
        when(column.getDecimalDigits()).thenReturn(3);

        AvroType avroType = AvroTypeUtil.getAvroType(column, defaultConfig());
        assertThat(avroType.getType(), instanceOf(Decimal.class));
        assertThat(avroType.getType().getPrimitiveType(), is("string"));
        assertThat(((Decimal) avroType.getType()).getLogicalType(), is("decimal"));
        assertThat(((Decimal) avroType.getType()).getPrecision(), is(20));
        assertThat(((Decimal) avroType.getType()).getScale(), is(3));
        assertThat(((Decimal) avroType.getType()).getJavaClass(), is("java.math.BigDecimal"));
    }
}
 
Example #5
Source File: DaTableImpl.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public ImmutableList<DaColumn> getColumns() {
    return ListAdapter.adapt(table.getColumns()).collect(new Function<Column, DaColumn>() {
        @Override
        public DaColumn valueOf(Column object) {
            return (DaColumn) new DaColumnImpl(object, schemaStrategy);
        }
    }).toImmutable();
}
 
Example #6
Source File: SchemaCrawlerTest.java    From neo4j-rdbms-import with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws SQLException, SchemaCrawlerException, ClassNotFoundException {

        Driver driver = DriverManager.getDriver("jdbc:derby:memory:test;create=true");
        Connection connection = DriverManager.getConnection("jdbc:derby:memory:test;create=true", new Properties());
        Statement statement = connection.createStatement();
        // id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1)
        statement.execute("CREATE TABLE USERS (id INT NOT NULL, name varchar(20), constraint users_pk_id primary key(id))");
        statement.execute("CREATE TABLE FRIENDS (id1 INT, id2 INT, " +
                " constraint fk_users_id1 foreign key(id1) references users(id)," +
                " constraint fk_users_id2 foreign key(id2) references users(id)" +
                ")");

        final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
        options.setSchemaInfoLevel(SchemaInfoLevel.standard());

        final Catalog catalog = SchemaCrawlerUtility.getCatalog(connection, options);

        for (final Schema schema : catalog.getSchemas()) {
            System.out.println(schema);
            for (final Table table : catalog.getTables(schema)) {
                System.out.println("o--> " + table + " pk " + table.getPrimaryKey() + " fks " + table.getForeignKeys() + " type " + table.getTableType());
                for (final Column column : table.getColumns()) {
                    System.out.println("     o--> " + column + " pk: " + column.isPartOfPrimaryKey() + " fk: " + column.isPartOfForeignKey());
                }
            }
        }
    }
 
Example #7
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected JdbcType getJdbcTypeForUnknownJavaSqlType(Column column) {
  /*
   * unknown type
   */
  if (column.getColumnDataType().getName().contains("TIMESTAMP")) {
      return JdbcType.TIMESTAMP;
  }
  return null;
}
 
Example #8
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected boolean isPrimaryKey(Table table, Column column) {
  if (table.getPrimaryKey() != null) {
    for (IndexColumn col : table.getPrimaryKey().getColumns()) {
      if (col.getName().equalsIgnoreCase(column.getName())) {
        return true;
      }
    }
  } else {
    LOG.warn("No primary key constraints for {}", table.getName());
  }
  return false;
}
 
Example #9
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected EntitySpec toEntitySpec(Table table) {
    EntitySpec entitySpec = new EntitySpec();
    entitySpec.setTableName( table.getName());
    entitySpec.setClassName( generateClassName(table) );
    entitySpec.setAbstractEntity(false);
    entitySpec.setQueryClassName( generateQueryClassName(table) );
    entitySpec.setDtoClassName( generateDtoClassName(table) );

    for (Column column: table.getColumns()) {
        if (exclusionRules.excludeColumn( column )) {
          continue;
        }
        LOG.debug("Processing column {}", column.getName());
        NodeSpec nodeSpec = toNodeSpec(entitySpec, table, column);
        entitySpec.add( nodeSpec );

        //set PK contraints
        Collection<NodeSpec> key = new LinkedList<>();
        for (NodeSpec ns: entitySpec.getNodeSpecs()) {
            if (ns.isPrimaryKey()) {
                key.add(ns);
            }
        }
        if (!key.isEmpty()) {
            createPrimaryKeyConstraint(entitySpec, key);
        }

    }
    return entitySpec;
}
 
Example #10
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public FromDatabaseSchemaToSpecification(String namespace) {
  this(namespace, new ExclusionRules() {
    @Override
    public boolean excludeTable(Table tableName) {
      return false;
    }

    @Override
    public boolean excludeColumn(Column column) {
      return false;
    }
  });
}
 
Example #11
Source File: DaIndexImpl.java    From obevo with Apache License 2.0 5 votes vote down vote up
private DaIndexImpl(DependantObject<Table> index, List<? extends Column> columns, SchemaStrategy schemaStrategy, boolean unique, DaIndexType indexType) {
    this.index = Validate.notNull(index);
    Validate.notNull(schemaStrategy);
    this.columns = ListAdapter.adapt(columns)
            .collect((Function<Column, DaColumn>) object -> new DaColumnImpl(object, schemaStrategy))
            .toImmutable();
    this.schemaStrategy = Validate.notNull(schemaStrategy);
    this.unique = unique;
    this.indexType = indexType;
}
 
Example #12
Source File: EnumTest.java    From avro-schema-generator with MIT License 5 votes vote down vote up
@Test
public void testEnumCreation() throws Exception {
    Column column = mock(Column.class);
    when(column.getName()).thenReturn("someEnum");
    when(column.getAttribute("COLUMN_TYPE")).thenReturn("ENUM('value1', 'value2')");

    Enum anEnum = new Enum(column);

    assertThat(anEnum.getName(), is("someEnum"));
    assertThat(anEnum.getSymbols()[0], is("value1"));
    assertThat(anEnum.getSymbols()[1], is("value2"));
}
 
Example #13
Source File: AvroTypeUtilTest.java    From avro-schema-generator with MIT License 5 votes vote down vote up
@Test
public void testUserDefinedType() throws Exception {
    Column column = column("test");
    when(column.getType().isUserDefined()).thenReturn(true);

    AvroType avroType = AvroTypeUtil.getAvroType(column, defaultConfig());
    assertThat(avroType.getType(), instanceOf(Primitive.class));
    assertThat(avroType.getType().getPrimitiveType(), is("string"));
}
 
Example #14
Source File: EnumFormatterTest.java    From avro-schema-generator with MIT License 5 votes vote down vote up
@Test
public void testEnumFormatting() throws Exception {
    Column column = mock(Column.class);
    when(column.getName()).thenReturn("someEnum");
    when(column.getAttribute("COLUMN_TYPE")).thenReturn("ENUM('value1', 'value2')");

    Enum anEnum = new Enum(column);

    assertThat(anEnum.getName(), is("someEnum"));
    assertThat(anEnum.getSymbols()[0], is("value1"));
    assertThat(anEnum.getSymbols()[1], is("value2"));

    String json = new EnumFormatter().toJson(anEnum, FormatterConfig.builder().build());
    assertThat(json, is("{ \"type\": \"enum\", \"name\": \"someEnum\", \"symbols\": [\"value1\", \"value2\"] }"));
}
 
Example #15
Source File: AvroSchema.java    From avro-schema-generator with MIT License 5 votes vote down vote up
public AvroSchema(Table table, AvroConfig avroConfig) {
    this.name = avroConfig.getSchemaNameMapper().apply(table.getName());
    this.namespace = avroConfig.getNamespace();
    this.fields = new ArrayList<>(table.getColumns().size());

    for (Column column : table.getColumns()) {
        this.fields.add(new AvroField(column, avroConfig));
    }

    avroConfig.getAvroSchemaPostProcessor().accept(this, table);
}
 
Example #16
Source File: AvroTypeUtil.java    From avro-schema-generator with MIT License 5 votes vote down vote up
/**
 * Maps db Columns to AvroTypes.
 * Mapping logic can be tweaked a bit using AvroConfig.
 */
static AvroType getAvroType(Column column, AvroConfig config) {
    boolean nullable = column.isNullable() || config.isNullableTrueByDefault();
    String type = column.getType().getName().toLowerCase();
    Class typeClz = column.getType().getTypeMappedClass();

    if (type.equalsIgnoreCase("enum")) {
        if (config.representEnumsAsStrings()) {
            return new AvroType(new Primitive("string"), nullable);
        } else {
            return new AvroType(new Enum(column), nullable);
        }
    }
    else if (column.getType().isUserDefined()) {
        return new AvroType(new Primitive("string"), nullable);
    }
    else if (asList("decimal", "numeric").contains(type)) {
        return new AvroType(new Decimal(column, config), nullable);
    }
    else if (asList("timestamp", "datetime", "date", "time").contains(type)) {
        return new AvroType(new Date(column, config), nullable);
    } else if (typeClz == java.sql.Array.class) {
        return new AvroType(new Array(new Primitive(getPrimitiveType(type.substring(1), config))), nullable);
    }

    return new AvroType(new Primitive(getPrimitiveType(type, config)), nullable);
}
 
Example #17
Source File: AvroField.java    From avro-schema-generator with MIT License 5 votes vote down vote up
public AvroField(Column column, AvroConfig avroConfig) {
    String columnName = column.getName()
        .replaceAll("`", "")
        .replaceAll("\"", "")
        .replaceAll("'", "");

    name = avroConfig.getFieldNameMapper().apply(columnName);
    type = AvroTypeUtil.getAvroType(column, avroConfig);

    if (avroConfig.isAllFieldsDefaultNull()) {
        defaultValue = null;
    } else if (column.getDefaultValue() != null) {
        defaultValue = column.getDefaultValue().contains("NULL") ? null : defaultValue;
    }
}
 
Example #18
Source File: Date.java    From avro-schema-generator with MIT License 4 votes vote down vote up
public Date(Column column, AvroConfig config) {
    super("long");
    this.javaClass = config.getDateTypeClass().getCanonicalName();
}
 
Example #19
Source File: DaColumnImpl.java    From obevo with Apache License 2.0 4 votes vote down vote up
public DaColumnImpl(Column column, SchemaStrategy schemaStrategy) {
    this.column = Validate.notNull(column);
    this.schemaStrategy = Validate.notNull(schemaStrategy);
}
 
Example #20
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * process the foreign key relations and constraints.
 */
private void secondPass(Catalog catalog) {
    for (Schema schema: catalog.getSchemas()) {
        LOG.debug(schema.toString());
        for (Table table: catalog.getTables(schema)) {
            for (ForeignKey fk: table.getForeignKeys()) {
                for (ForeignKeyColumnReference fkRef: fk.getColumnReferences()) {
                    Column fkCol = fkRef.getForeignKeyColumn();
                    Column pkCol = fkRef.getPrimaryKeyColumn();
                    Table pkTable = pkCol.getParent();

                    EntitySpec pkEspec = entitySpecs.get(pkTable);
                    EntitySpec fkEspec = entitySpecs.get(fkCol.getParent());
                    NodeSpec fkNSpec = nodeSpecs.get(fkCol);
                    if (pkEspec == null || fkEspec == null || fkNSpec == null) {
                      continue;
                    }

                    if (!processedFks.add(new ProcessedFk(fkNSpec, pkEspec))) {
                        continue;
                    }

                    /*
                     *  Do the N:1 natuarl foreign key relation *
                     */
                    fkNSpec.setName( genRealtionNodeName(fkNSpec) );
                    //java type is null, as the relation defines the type.
                    fkNSpec.setJavaType(null);
                    RelationSpec rspec = new RelationSpec();
                    rspec.setEntitySpec(pkEspec);
                    rspec.setJoinType(JoinTypeSpec.LEFT_OUTER_JOIN);
                    rspec.setType(RelationType.REFERS);
                    fkNSpec.setRelation(rspec);

                    LOG.debug("Added FK relation from {}.{} to {}", fkEspec.getClassName(), fkNSpec.getName(), pkEspec.getClassName());

                    createForeignKeyConstraint(fkEspec, fkNSpec, rspec);

                    /*
                     * do the opposite 1:N relation
                     */
                    //create the nodespec as there is no dbcolumn whch created a node for us
                    LOG.debug("Creating tomany node for N relation from {} to {}", pkEspec.getClassName(), fkEspec.getClassName());
                    NodeSpec toManyNodeSpec = new NodeSpec();
                    String nodeName = genRealtionNodeName(fkEspec, true);
                    while (pkEspec.getNodeSpec(nodeName) != null) {
                        nodeName = incrementNodeName(nodeName);
                    }
                    toManyNodeSpec.setName( nodeName );
                    toManyNodeSpec.setEntity(pkEspec);

                    rspec = new RelationSpec();
                    rspec.setBackReference(fkNSpec);
                    rspec.setEntitySpec(fkEspec);
                    rspec.setJoinType(JoinTypeSpec.LEFT_OUTER_JOIN);
                    rspec.setType(RelationType.REFERS);
                    toManyNodeSpec.setRelation(rspec);
                    pkEspec.add(toManyNodeSpec);
                }
            }
        }
    }
}
 
Example #21
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected static String getNodeName(Column column) {
  return toCamelCase(stripBadChars( column.getName() ));
}
 
Example #22
Source File: Decimal.java    From avro-schema-generator with MIT License 4 votes vote down vote up
public Decimal(Column column, AvroConfig config) {
    super("string");
    this.precision = column.getSize();
    this.scale = column.getDecimalDigits();
    this.javaClass = config.getDecimalTypeClass().getCanonicalName();
}
 
Example #23
Source File: FromDatabaseSchemaToSpecification.java    From barleydb with GNU Lesser General Public License v3.0 votes vote down vote up
boolean excludeColumn(Column column);