update to gradle 7.4.2, add metadata column length API

This commit is contained in:
Jörg Prante 2022-06-01 18:39:14 +02:00
parent 81a0bd8bc9
commit aec2061f68
14 changed files with 97 additions and 30 deletions

View file

@ -4,7 +4,7 @@ plugins {
} }
wrapper { wrapper {
gradleVersion = "${project.property('gradle.wrapper.version')}" gradleVersion = libs.versions.gradle.get()
distributionType = Wrapper.DistributionType.ALL distributionType = Wrapper.DistributionType.ALL
} }

View file

@ -1,11 +1,6 @@
group = org.xbib group = org.xbib
name = database name = database
version = 0.0.1 version = 0.0.2
org.gradle.warning.mode = ALL org.gradle.warning.mode = ALL
gradle.wrapper.version = 7.3.2
h2.version = 1.4.200
mockito.version = 3.3.3
testcontainers.version = 1.16.2
derby.version = 10.15.2.0
oracle-client.version = 21.4.0.0

View file

@ -1,12 +1,8 @@
def junitVersion = project.hasProperty('junit.version')?project.property('junit.version'):'5.6.2'
def hamcrestVersion = project.hasProperty('hamcrest.version')?project.property('hamcrest.version'):'2.2'
dependencies { dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:${junitVersion}" testImplementation libs.junit.jupiter.api
testImplementation "org.junit.jupiter:junit-jupiter-params:${junitVersion}" testImplementation libs.junit.jupiter.params
testImplementation "org.hamcrest:hamcrest-library:${hamcrestVersion}" testImplementation libs.hamcrest
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitVersion}" testRuntimeOnly libs.junit.jupiter.engine
} }
test { test {

Binary file not shown.

View file

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.2-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View file

@ -1,4 +1,4 @@
dependencies { dependencies {
testImplementation "com.h2database:h2:${project.property('h2.version')}" testImplementation libs.h2
testImplementation "org.mockito:mockito-core:${project.property('mockito.version')}" testImplementation libs.mockito.core
} }

View file

@ -1,7 +1,7 @@
dependencies { dependencies {
api project(":jdbc-connection-pool") api project(":jdbc-connection-pool")
testImplementation "org.apache.derby:derby:${project.property('derby.version')}" testImplementation libs.derby
testImplementation "org.testcontainers:testcontainers:${project.property('testcontainers.version')}" testImplementation libs.testcontainers
testImplementation "org.testcontainers:junit-jupiter:${project.property('testcontainers.version')}" testImplementation libs.testcontainers.junit.jupiter
testImplementation "org.testcontainers:oracle-xe:${project.property('testcontainers.version')}" testImplementation libs.testcontainers.oracle.xe
} }

View file

@ -2,6 +2,7 @@ package org.xbib.jdbc.query;
import java.sql.Connection; import java.sql.Connection;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Map;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
@ -176,6 +177,13 @@ public interface Database extends Supplier<Database> {
*/ */
boolean tableExists( String tableName, String schemaName); boolean tableExists( String tableName, String schemaName);
/**
* Convenience method to get all column sizes from a table.
* @param tableName the table to be checked
* @return a map of columns names with size
*/
Map<String, Integer> getColumnSizesOfTable(String tableName);
/** /**
* Return the DB table name in the normalized form in which it is stored. * Return the DB table name in the normalized form in which it is stored.
* Databases like Oracle, Derby, HSQL store their tables in upper case. * Databases like Oracle, Derby, HSQL store their tables in upper case.

View file

@ -4,10 +4,15 @@ import java.lang.reflect.Method;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -200,16 +205,13 @@ public class DatabaseImpl implements Database {
return tableExists(tableName, schemaName); return tableExists(tableName, schemaName);
} }
@Override @Override
public boolean tableExists(String tableName, String schemaName) throws DatabaseException { public boolean tableExists(String tableName, String schemaName) throws DatabaseException {
if (tableName != null) { if (tableName != null && connection != null) {
try { try {
DatabaseMetaData metadata = connection.getMetaData(); DatabaseMetaData metadata = connection.getMetaData();
String normalizedTable = normalizeTableName(tableName); String normalizedTable = normalizeTableName(tableName);
ResultSet resultSet = ResultSet resultSet = metadata.getTables(connection.getCatalog(), schemaName, normalizedTable, new String[]{"TABLE", "VIEW"});
metadata.getTables(connection.getCatalog(), schemaName, normalizedTable, new String[]{"TABLE", "VIEW"});
while (resultSet.next()) { while (resultSet.next()) {
if (normalizedTable.equals(resultSet.getString("TABLE_NAME"))) { if (normalizedTable.equals(resultSet.getString("TABLE_NAME"))) {
return true; return true;
@ -221,10 +223,42 @@ public class DatabaseImpl implements Database {
exc); exc);
} }
} }
return false; return false;
} }
@Override
public Map<String, Integer> getColumnSizesOfTable(String tableName) {
Map<String, Integer> map = new HashMap<>();
if (tableName != null && connection != null) {
try {
DatabaseMetaData metaData = connection.getMetaData();
String normalizedTable = normalizeTableName(tableName);
ResultSet rs = metaData.getColumns(null, null, normalizedTable, "%");
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while (rs.next()) {
String name = "";
Integer size = 0;
for (int i = 1; i <= cols; i++) {
String label = rsmd.getColumnName(i);
Object v = rs.getObject(i);
if (label.equals("COLUMN_NAME")) {
name = (String) v;
}
if (label.equals("COLUMN_SIZE")) {
size = (Integer) v;
}
}
map.put(name, size);
}
rs.close();
} catch (SQLException exc) {
throw new DatabaseException("Unable to look up table " + tableName + " : " + exc.getMessage(), exc);
}
}
return map;
}
@Override @Override
public String normalizeTableName(String tableName) { public String normalizeTableName(String tableName) {
if (tableName == null) { if (tableName == null) {

View file

@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* Exercise database functionality with a real HyperSQL database. * Exercise database functionality with a real HyperSQL database.
*/ */
@Disabled
public class HsqldbTest extends CommonTest { public class HsqldbTest extends CommonTest {
private static final Logger logger = Logger.getLogger(HsqldbTest.class.getName()); private static final Logger logger = Logger.getLogger(HsqldbTest.class.getName());

View file

@ -13,6 +13,7 @@ import java.util.Properties;
/** /**
* Exercise Database functionality with a real Oracle database. * Exercise Database functionality with a real Oracle database.
*/ */
@Disabled
public class OracleTest extends CommonTest { public class OracleTest extends CommonTest {
@Container @Container

View file

@ -1,6 +1,7 @@
package org.xbib.jdbc.query.test; package org.xbib.jdbc.query.test;
import java.io.IOException; import java.io.IOException;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.jdbc.query.DatabaseProvider; import org.xbib.jdbc.query.DatabaseProvider;
import org.xbib.jdbc.query.OptionsOverride; import org.xbib.jdbc.query.OptionsOverride;
@ -14,6 +15,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
/** /**
* Exercise Database functionality with a real PostgreSQL database. * Exercise Database functionality with a real PostgreSQL database.
*/ */
@Disabled
public class PostgreSqlTest extends CommonTest { public class PostgreSqlTest extends CommonTest {
@Override @Override

View file

@ -11,8 +11,9 @@ import org.xbib.jdbc.query.Schema;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
/** /**
* Exercise Database functionality with a real Oracle database. * Exercise Database functionality with a real SQL server database.
*/ */
@Disabled
public class SqlServerTest extends CommonTest { public class SqlServerTest extends CommonTest {
@Override @Override
protected DatabaseProvider createDatabaseProvider(OptionsOverride options) throws Exception { protected DatabaseProvider createDatabaseProvider(OptionsOverride options) throws Exception {

View file

@ -1,2 +1,31 @@
dependencyResolutionManagement {
versionCatalogs {
libs {
version('gradle', '7.4.2')
version('junit', '5.8.2')
version('testcontainers', '1.16.2')
library('junit-jupiter-api', 'org.junit.jupiter', 'junit-jupiter-api').versionRef('junit')
library('junit-jupiter-params', 'org.junit.jupiter', 'junit-jupiter-params').versionRef('junit')
library('junit-jupiter-engine', 'org.junit.jupiter', 'junit-jupiter-engine').versionRef('junit')
library('hamcrest', 'org.hamcrest:hamcrest-library:2.2')
library('junit4', 'junit:junit:4.13.2')
library('derby', 'org.apache.derby', 'derby').version('10.15.2.0')
library('testcontainers', 'org.testcontainers', 'testcontainers').versionRef('testcontainers')
library('testcontainers-junit-jupiter', 'org.testcontainers', 'junit-jupiter').versionRef('testcontainers')
library('testcontainers-oracle-xe', 'org.testcontainers', 'oracle-xe').versionRef('testcontainers')
library('h2', 'com.h2database', 'h2').version('1.4.200')
library('mockito-core', 'org.mockito', 'mockito-core').version('3.3.3')
}
}
}
/*
gradle.wrapper.version = 7.4.2
h2.version = 1.4.200
mockito.version = 3.3.3
testcontainers.version = 1.16.2
derby.version = 10.15.2.0
oracle-client.version = 21.4.0.0
*/
include 'jdbc-connection-pool' include 'jdbc-connection-pool'
include 'jdbc-query' include 'jdbc-query'