Skip to content

jinahya/database-metadata-bind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

803 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

database-metadata-bind

Java CI with Maven Quality Gate Status Maven Central javadoc

A library for binding results of methods defined in DatabaseMetaData.

Coordinates

See Maven Central for available versions.

<dependency>
  <groupId>io.github.jinahya</groupId>
  <artifactId>database-metadata-bind</artifactId>
</dependency>

Usages

All 26 methods in DatabaseMetaData that return ResultSet are bound to type-safe Java classes.

Basic Usage

try (var connection = dataSource.getConnection()) {
    var context = Context.newInstance(connection.getMetaData());

    // Get all catalogs
    List<Catalog> catalogs = context.getCatalogs();

    // Get all tables (null = don't filter)
    List<Table> tables = context.getTables(null, null, "%", null);

    // Get columns for a specific table
    List<Column> columns = context.getColumns("my_catalog", "my_schema", "my_table", "%");
}

Working with Results

// Tables have typed accessors
for (Table table : tables) {
    String catalog = table.getTableCat();    // may be null
    String schema = table.getTableSchem();   // may be null
    String name = table.getTableName();
    String type = table.getTableType();      // "TABLE", "VIEW", etc.
}

// Get primary keys for a table
List<PrimaryKey> pks = context.getPrimaryKeys(
    table.getTableCat(),
    table.getTableSchem(),
    table.getTableName()
);

// Get foreign keys pointing to this table
List<ExportedKey> exportedKeys = context.getExportedKeys(
    table.getTableCat(),
    table.getTableSchem(),
    table.getTableName()
);

Catalog/Schema Null Handling

JDBC uses null to mean "not applicable" in results and "don't filter" in parameters. This aligns naturally:

// Get a table (catalog/schema may be null depending on database)
Table table = tables.get(0);

// Pass values directly — null means "don't filter by this"
List<Column> columns = context.getColumns(
    table.getTableCat(),     // null → don't filter by catalog
    table.getTableSchem(),   // null → don't filter by schema
    table.getTableName(),
    "%"
);

How to contribute?

A lot of classes/methods defined in this module need to be tested with various kinds of real databases.

Add your JDBC driver as a test-scoped dependency.

<dependency>
  ...
  <scope>test</scope>
</dependency>

Run the ExternalIT class with url, user, and password parameter.

$ mvn \
  -Pfailsafe \
  -Dit.test=ExternalIT \
  -Durl='<your-jdbc-url>' \
  -Duser='<your-own-user>' \
  -Dpassword='<your-own-password>' \
  clean test-compile failsafe:integration-test

Links

Docker

MariaDB

MySQL

PostgreSQL

SQL Server

About

A library for binding information from java.sql.DatabaseMetadata

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages