예제 #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 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));
  }