示例#1
0
  /**
   * Reads an table from the database and returns the parsed object.
   *
   * <p>Note that this method does not parse everything: default values, enum types, primary and
   * foreign keys are not parsed.
   *
   * @param conn the JDBC {@link Connection}
   * @param template the query template
   * @param tableName the table name
   * @return the parsed table
   * @throws SQLException if any sql error occur
   */
  public static Table createTable(Connection conn, QueryTemplate template, String tableName)
      throws SQLException {
    Statement st = conn.createStatement();
    try {
      st.execute(
          new StringBuilder(template.getSelect())
              .append("*")
              .append(template.getFrom())
              .append(template.quoteIdentifier(tableName))
              .toString());
      final ResultSet rs = st.getResultSet();
      ResultSetMetaData metaData = rs.getMetaData();

      final Map<String, Column> columns = CollectionFactory.newMap();
      for (int i = 1; i <= metaData.getColumnCount(); i++) {
        columns.put(
            metaData.getColumnName(i),
            new Column(
                metaData.getColumnName(i),
                getColumnType(metaData.getColumnType(i)),
                false,
                metaData.getColumnDisplaySize(i),
                false,
                null));
      }
      return new Table(metaData.getTableName(1), columns, null, null);
    } finally {
      st.close();
    }
  }
示例#2
0
 /**
  * Creates the foreign key list
  *
  * @param tablePath the table object
  * @param columns the columns
  * @return the foreign key list
  */
 private static List<ForeignKey> createFKs(
     RelationalPath<?> tablePath, Map<String, Column> columns) {
   final List<ForeignKey> fks = CollectionFactory.newList();
   for (final com.mysema.query.sql.ForeignKey<?> fk : tablePath.getForeignKeys()) {
     StringBuilder name = new StringBuilder();
     final List<Column> cols = CollectionFactory.newList();
     for (final Path<?> path : fk.getLocalColumns()) {
       String colName = path.getMetadata().getExpression().toString();
       cols.add(columns.get(colName));
       name.append(colName).append("-");
     }
     name.setLength(name.length() - 1);
     fks.add(new ForeignKey(name.toString(), cols));
   }
   return fks;
 }
示例#3
0
  /**
   * @param tablePath the query entity
   * @param path the path
   * @return the column object
   */
  private static Column createColumn(RelationalPath<?> tablePath, Path<?> path) {
    final String columnName = path.getMetadata().getExpression().toString();
    final ColumnType columnType = getColumnType(path.getType());
    final Field field = ClassUtils.getFieldWithValue(tablePath, path);

    // settings
    int columnSize = -1;
    boolean columnNullable = false;
    boolean hasDefaultValue = false;
    String defaultValue = null;

    if (field != null) {
      final ColumnSize size = field.getAnnotation(ColumnSize.class);
      if (size != null) {
        columnSize = size.value();
      }

      final ColumnAutoIncrement autoInc = field.getAnnotation(ColumnAutoIncrement.class);
      if (autoInc != null) {
        return new Column(columnName, columnSize, true);
      }

      final ColumnNullable nullable = field.getAnnotation(ColumnNullable.class);
      if (nullable == null) {
        columnNullable = false;
      } else {
        columnNullable = true;
      }

      final ColumnDefault def = field.getAnnotation(ColumnDefault.class);
      if (def == null && nullable != null) {
        hasDefaultValue = true;
        defaultValue = null;
      } else if (def != null) {
        hasDefaultValue = true;
        defaultValue = def.value();
      }
    }

    if (columnType == ColumnType.ENUM) {
      @SuppressWarnings("unchecked")
      Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) path.getType();
      Enum<?>[] enums = enumClass.getEnumConstants();
      final List<String> enumValues = CollectionFactory.newList();
      for (final Enum<?> e : enums) {
        enumValues.add(e.name());
      }

      return new Column(columnName, columnNullable, enumValues, hasDefaultValue, defaultValue);
    }

    return new Column(
        columnName, columnType, columnNullable, columnSize, hasDefaultValue, defaultValue);
  }
示例#4
0
  /**
   * Creates an {@link Table} object from an {@link RelationalPath}
   *
   * @param tablePath the table path
   * @return the {@link Table} object
   */
  public static Table createTable(RelationalPath<?> tablePath) {
    final Map<String, Column> columns = CollectionFactory.newMap();
    for (final Path<?> path : tablePath.getColumns()) {
      final Column col = createColumn(tablePath, path);
      columns.put(col.getName(), col);
    }

    return new Table(
        tablePath.getTableName(),
        columns,
        createPK(tablePath, columns),
        createFKs(tablePath, columns));
  }