
br.com.objectos.db.Metadata Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.db;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.stream.Stream;
/**
* @author [email protected] (Marcio Endo)
*/
public class Metadata {
private final Dialect dialect;
private final DatabaseMetaData db;
private final String catalogName;
private final String schemaName;
Metadata(Dialect dialect, DatabaseMetaData db, String catalogName, String schemaName) {
this.dialect = dialect;
this.db = db;
this.catalogName = catalogName;
this.schemaName = schemaName;
}
public void close() throws SqlException {
try {
db.getConnection().close();
} catch (SQLException e) {
throw SqlException.wrap(e);
}
}
public Stream columnStream(String tableName) {
return columnStream(tableName, null);
}
public Stream columnStream(String tableName, String columnName) {
try {
return stream(db.getColumns(catalogName, schemaName, tableName, columnName));
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
public Stream foreignKeyStream(String tableName) {
try {
return stream(db.getImportedKeys(catalogName, schemaName, tableName));
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
public Metadata metadata(String tableCat, String tableSchem) {
return new Metadata(dialect, db, tableCat, tableSchem);
}
public Stream primaryKeyStream(String tableName) {
try {
return stream(db.getPrimaryKeys(catalogName, schemaName, tableName));
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
public String schemaName() {
return dialect.schemaName(catalogName, schemaName);
}
public Stream tableStream(String schemaName) {
try {
return stream(dialect.tables(db, schemaName));
} catch (SQLException e) {
throw SqlRuntimeException.wrap(e);
}
}
private Stream stream(ResultSet resultSet) {
return Result.of(resultSet).stream();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy