From f8fa71892a55c70ce02a47e8666ffc93d961bac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Tue, 1 Nov 2022 18:51:21 +0100 Subject: [PATCH] update to database 1.0.0, xbib net 3.0.1, oracle jdbc 21.7.0.0 --- gradle.properties | 2 +- gradle/compile/java.gradle | 14 + gradle/publish/maven.gradle | 2 + .../src/main/java/module-info.java | 2 + .../application/database/BaseDatabase.java | 283 ------------------ .../server/application/database/Database.java | 37 --- .../database/DatabaseApplicationModule.java | 3 +- .../server/application/database/Table.java | 203 ------------- .../org/xbib/net/http/server/HttpHandler.java | 7 +- .../net/http/server/HttpServerContext.java | 7 +- settings.gradle | 8 +- 11 files changed, 35 insertions(+), 533 deletions(-) delete mode 100644 net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/BaseDatabase.java delete mode 100644 net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/Database.java delete mode 100644 net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/Table.java diff --git a/gradle.properties b/gradle.properties index a49ba24..87df7b1 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = net-http -version = 3.0.0 +version = 3.0.1 org.gradle.warning.mode = ALL diff --git a/gradle/compile/java.gradle b/gradle/compile/java.gradle index 9cd3f3b..b011802 100644 --- a/gradle/compile/java.gradle +++ b/gradle/compile/java.gradle @@ -28,3 +28,17 @@ tasks.withType(JavaCompile) { javadoc { options.addStringOption('Xdoclint:none', '-quiet') } + +task sourcesJar(type: Jar, dependsOn: classes) { + classifier 'sources' + from sourceSets.main.allSource +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier 'javadoc' + from javadoc.destinationDir +} + +artifacts { + archives sourcesJar, javadocJar +} diff --git a/gradle/publish/maven.gradle b/gradle/publish/maven.gradle index 3786359..4ada9c0 100644 --- a/gradle/publish/maven.gradle +++ b/gradle/publish/maven.gradle @@ -5,6 +5,8 @@ publishing { publications { mavenJava(MavenPublication) { from components.java + artifact sourcesJar + artifact javadocJar pom { name = project.name description = rootProject.ext.description diff --git a/net-http-server-application-database/src/main/java/module-info.java b/net-http-server-application-database/src/main/java/module-info.java index 8f5f36f..9971f71 100644 --- a/net-http-server-application-database/src/main/java/module-info.java +++ b/net-http-server-application-database/src/main/java/module-info.java @@ -6,7 +6,9 @@ module org.xbib.net.http.server.application.database { requires org.xbib.net.http; requires org.xbib.net.http.server; requires org.xbib.jdbc.query; + requires org.xbib.jdbc.connection.pool; requires org.xbib.datastructures.tiny; requires java.logging; + requires java.sql; provides ApplicationModule with DatabaseApplicationModule; } diff --git a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/BaseDatabase.java b/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/BaseDatabase.java deleted file mode 100644 index a34ee32..0000000 --- a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/BaseDatabase.java +++ /dev/null @@ -1,283 +0,0 @@ -package org.xbib.net.http.server.application.database; - -import org.xbib.jdbc.query.DatabaseException; -import org.xbib.jdbc.query.Rows; -import org.xbib.jdbc.query.SqlInsert; -import org.xbib.jdbc.query.SqlSelect; -import org.xbib.jdbc.query.SqlUpdate; - -import java.math.BigDecimal; -import java.sql.ResultSetMetaData; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; - -public abstract class BaseDatabase implements Database { - - private static final Logger logger = Logger.getLogger(BaseDatabase.class.getName()); - - private final org.xbib.jdbc.query.Database db; - - private final int fetchSize; - - private final int timeoutSeconds; - - public BaseDatabase(org.xbib.jdbc.query.Database db, int fetchSize, int timeoutSeconds) throws Exception { - this.db = db; - this.fetchSize = fetchSize; - this.timeoutSeconds = timeoutSeconds; - } - - @Override - public Table getPagedRows(Table table) { - if (table == null) { - return table; - } - Map params = table.getParams() != null ? - new HashMap<>(table.getParams()) : new HashMap<>(); - if (table.getOffset() != null && table.getSize() != null) { - params.put("offset", table.getOffset()); - params.put("limit", table.getSize()); - } - String where = table.getWhereClause() != null ? table.getWhereClause() : ""; - String groupby = table.getGroupByClause() != null ? table.getGroupByClause() : ""; - String orderby = !table.getSort().isEmpty() ? "order by " + table.getSort() : ""; - String statement = table.getStatement() + " " + where + " " + groupby + " " + orderby; - if (table.getOffset() != null && table.getSize() != null) { - statement = statement + " offset :offset rows fetch next :limit rows only"; - } - return getUnlimitedRows(statement, params); - } - - @Override - public long getRowsCount(Table table) { - Map params = new HashMap<>(table.getParams()); - String statement = table.getStatement() + " " + table.getWhereClause(); - return countRows(statement, params); - } - - @Override - public long countRows(String statement, Map params) { - String countStatament = "select count(*) as \"cnt\" from (" + statement + ")"; - Table table = getSingleRow(countStatament, params); - if (!table.isEmpty()) { - BigDecimal bigDecimal = table.getValue(0,"cnt"); - return bigDecimal.longValue(); - } else { - return -1L; - } - } - - @Override - public Table getSingleRow(String statement, Map params) { - return getLimitedRows(statement, params, 1, fetchSize, timeoutSeconds); - } - - @Override - public Table getUnlimitedRows(String statement, Map params) { - return getLimitedRows(statement, params, 0, fetchSize, timeoutSeconds); - } - - @Override - public Table getLimitedRows(String statement, Map params, - int limit, int fetchSize, int timeoutSeconds) { - SqlSelect sql = db.toSelect(statement).fetchSize(fetchSize).withTimeoutSeconds(timeoutSeconds); - selectParams(sql, params); - Table table = new Table(); - sql.query(rows -> { - ResultSetMetaData md = rows.getMetadata(); - List columnNames = new ArrayList<>(); - List classNames = new ArrayList<>(); - for (int i = 1; i <= md.getColumnCount(); i++) { - columnNames.add(md.getColumnName(i).toLowerCase(Locale.ROOT)); - classNames.add(md.getColumnClassName(i)); - } - table.add(columnNames); - table.add(classNames); - int i = 0; - while (rows.next() && (limit <= 0 || i++ < limit)) { - table.add(getRow(rows, classNames)); - } - table.setTotal(rows.rowCount()); - return true; - }); - return table; - } - - @Override - public void insert(String statement, Map params) { - SqlInsert sql = db.toInsert(statement); - insertParams(sql, params); - sql.insert(1); - } - - @Override - public void insert(String statement, List> params) { - SqlInsert sqlInsert = db.toInsert(statement); - for (Map param : params) { - insertParams(sqlInsert, param); - sqlInsert.batch(); - } - sqlInsert.insertBatch(); - } - - @Override - public void update(String statement, Map params) { - SqlUpdate sql = db.toUpdate(statement); - updateParams(sql, params); - sql.update(); - } - - @Override - public void update(String statement, List> params) { - SqlUpdate sqlUpdate = db.toUpdate(statement); - for (Map param : params) { - updateParams(sqlUpdate, param); - sqlUpdate.update(); - } - } - - @Override - public void upsert(String insertStatement, String updateStatement, Map params) { - // try insert then update if error - try { - SqlInsert sql = db.toInsert(insertStatement); - insertParams(sql, params); - sql.insert(1); - } catch (Exception e) { - logger.log(Level.WARNING, e.getMessage(), e); - SqlUpdate sql = db.toUpdate(updateStatement); - updateParams(sql, params); - sql.update(); - } - } - - @Override - public void delete(String statement, Map params) { - SqlUpdate sql = db.toDelete(statement); - updateParams(sql, params); - sql.update(); - } - - @Override - public void delete(String statement, List> params) { - SqlUpdate sqlUpdate = db.toDelete(statement); - for (Map param : params) { - updateParams(sqlUpdate, param); - sqlUpdate.update(); - } - } - - private void selectParams(SqlSelect sql, Map params) { - if (params == null) { - return; - } - params.forEach((k, v) -> { - if (v instanceof String) { - sql.argString(k, (String) v); - } else if (v instanceof Integer) { - sql.argInteger(k, (Integer) v); - } else if (v instanceof Long) { - sql.argLong(k, (Long) v); - } else if (v instanceof Boolean) { - sql.argBoolean(k, (Boolean) v); - } else if (v instanceof LocalDate) { - sql.argLocalDate(k, (LocalDate) v); - } else if (v instanceof LocalDateTime) { - sql.argDate(k, (LocalDateTime) v); - } else { - throw new DatabaseException("unknown type for param: " + (v != null ? v.getClass() : "null")); - } - }); - } - - private void insertParams(SqlInsert sql, Map params) { - if (params == null) { - return; - } - params.forEach((k, v) -> { - if (v instanceof String) { - sql.argString(k, (String) v); - } else if (v instanceof Integer) { - sql.argInteger(k, (Integer) v); - } else if (v instanceof Long) { - sql.argLong(k, (Long) v); - } else if (v instanceof Boolean) { - sql.argBoolean(k, (Boolean) v); - } else if (v instanceof LocalDate) { - sql.argLocalDate(k, (LocalDate) v); - } else if (v instanceof LocalDateTime) { - sql.argDate(k, (LocalDateTime) v); - } else { - throw new DatabaseException("unknown type for param: " + (v != null ? v.getClass() : "null")); - } - }); - } - - private void updateParams(SqlUpdate sql, Map params) { - if (params == null) { - return; - } - params.forEach((k, v) -> { - if (v instanceof String) { - sql.argString(k, (String) v); - } else if (v instanceof Integer) { - sql.argInteger(k, (Integer) v); - } else if (v instanceof Long) { - sql.argLong(k, (Long) v); - } else if (v instanceof Boolean) { - sql.argBoolean(k, (Boolean) v); - } else if (v instanceof LocalDate) { - sql.argLocalDate(k, (LocalDate) v); - } else if (v instanceof LocalDateTime) { - sql.argDate(k, (LocalDateTime) v); - } else { - throw new DatabaseException("unknown type for param: " + (v != null ? v.getClass() : "null")); - } - }); - } - - private List getRow(Rows rows, List classNames) { - List row = new ArrayList<>(); - for (int i = 0; i < classNames.size(); i++) { - String className = classNames.get(i).toString(); - switch (className) { - case "java.lang.String": - row.add(rows.getStringOrEmpty(i + 1)); - break; - case "java.lang.Integer": - row.add(rows.getIntegerOrNull(i + 1)); - break; - case "java.lang.Long": - row.add(rows.getLongOrNull(i + 1)); - break; - case "java.lang.Boolean": - row.add(rows.getBooleanOrFalse(i + 1)); - break; - case "java.sql.Clob": - case "oracle.jdbc.OracleClob": - row.add(rows.getClobStringOrEmpty(i + 1)); - break; - case "java.sql.Date": - row.add(rows.getLocalDateOrNull(i + 1)); - break; - case "java.sql.Timestamp": - case "oracle.sql.TIMESTAMP": - row.add(rows.getLocalDateTimeOrNull(i + 1)); - break; - case "java.math.BigDecimal": - row.add(rows.getBigDecimalOrNull(i + 1)); - break; - default: - throw new DatabaseException("unexpected column class name: " + className); - } - } - return row; - } -} diff --git a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/Database.java b/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/Database.java deleted file mode 100644 index 3363e13..0000000 --- a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/Database.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.xbib.net.http.server.application.database; - -import java.io.IOException; -import java.util.List; -import java.util.Map; - -public interface Database extends AutoCloseable { - - Table getPagedRows(Table table); - - long getRowsCount(Table table); - - long countRows(String statement, Map params); - - Table getSingleRow(String statement, Map params); - - Table getUnlimitedRows(String statement, Map params); - - Table getLimitedRows(String statement, Map params, - int limit, int fetchSize, int timeoutSeconds); - - void insert(String statement, Map params); - - void insert(String statement, List> params); - - void update(String statement, Map params); - - void update(String statement, List> params); - - void upsert(String insertStatement, String updateStatement, Map params); - - void delete(String statement, Map params); - - void delete(String statement, List> params); - - void close() throws IOException; -} diff --git a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/DatabaseApplicationModule.java b/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/DatabaseApplicationModule.java index 1285b0f..ce3de09 100644 --- a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/DatabaseApplicationModule.java +++ b/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/DatabaseApplicationModule.java @@ -62,12 +62,13 @@ public class DatabaseApplicationModule extends BaseApplicationModule { } }); if (!properties.containsKey("url")) { - throw new IllegalArgumentException(" no database.url in system properties given"); + throw new IllegalArgumentException("no database.url in system properties given"); } if (!properties.containsKey("user")) { logger.log(Level.WARNING, "no database.user in system properties given"); } PoolConfig config = new PoolConfig(properties); + config.setUrl(properties.getProperty("url")); config.setPoolName("net-http-database"); config.setMaximumPoolSize(getAsInt(properties, "poolsize", 4)); config.setMaxLifetime(getAsLong(properties, "maxlifetime", 600L * 1000L)); // 10 minutes diff --git a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/Table.java b/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/Table.java deleted file mode 100644 index 2f9a9b7..0000000 --- a/net-http-server-application-database/src/main/java/org/xbib/net/http/server/application/database/Table.java +++ /dev/null @@ -1,203 +0,0 @@ -package org.xbib.net.http.server.application.database; - -import org.xbib.datastructures.tiny.TinyMap; - -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -@SuppressWarnings("serial") -public class Table extends ArrayList> implements List> { - - private String statement; - - private Map params; - - private String search; - - private LocalDateTime from; - - private LocalDateTime to; - - private Integer offset; - - private Integer size; - - private List where; - - private String whereClause; - - private String groupByClause; - - private final List> sort = new ArrayList<>(); - - private long total; - - public void setStatement(String statement) { - this.statement = statement; - } - - public String getStatement() { - return statement; - } - - public void setParams(Map params) { - this.params = params; - } - - public Map getParams() { - return params; - } - - public void setSearch(String search) { - this.search = search; - } - - public String getSearch() { - return search; - } - - public void setFromDate(LocalDateTime from) { - this.from = from; - } - - public LocalDateTime getFromDate() { - return from; - } - - public void setToDate(LocalDateTime to) { - this.to = to; - } - - public LocalDateTime getToDate() { - return to; - } - - public void setOffset(Integer offset) { - this.offset = offset; - } - - public Integer getOffset() { - return offset; - } - - public void setSize(Integer size) { - this.size = size; - } - - public Integer getSize() { - return size; - } - - public void setWhere(List where) { - this.where = where; - } - - public List getWhere() { - return where; - } - - public void setWhereClause(String whereClause) { - this.whereClause = whereClause; - } - - public String getWhereClause() { - return whereClause; - } - - public void setGroupByClause(String groupByClause) { - this.groupByClause = groupByClause; - } - - public String getGroupByClause() { - return groupByClause; - } - - public void addSort(String sort) { - addSort(sort, true); - } - - public void addSort(String sort, Boolean asc) { - this.sort.add(Map.entry(sort, asc)); - } - - public String getSort() { - return sort.stream().map(e -> "\"" + e.getKey() + "\"" + " " + (e.getValue() ? "asc" : "desc")) - .collect(Collectors.joining(",")); - } - - public List getColumnNames() { - return get(0).stream().map(Object::toString).collect(Collectors.toList()); - } - - public int getColumn(String columnName) { - return get(0).indexOf(columnName); - } - - public List getColumnClassNames() { - return get(1).stream().map(Object::toString).collect(Collectors.toList()); - } - - public String getColumnName(int i) { - return (String) get(0).get(i); - } - - public String getColumnClassName(int i) { - return (String) get(1).get(i); - } - - public int getRowCount() { - return size() - 2; - } - - public int getColumnCount() { - return get(0).size(); - } - - public List getRow(int i) { - return get(i + 2); - } - - public Map getRowAsMap(int i) { - TinyMap.Builder map = TinyMap.builder(); - List row = getRow(i); - for (int c = 0; c < getColumnCount(); c++) { - map.put(getColumnName(c), row.get(c)); - } - return map.build(); - } - - public Object getObject(int row, int col) { - return get(row + 2).get(col); - } - - public Object getObject(int row, String columnName) { - int col = getColumn(columnName); - return col >= 0 ? get(row + 2).get(col) : null; - } - - @SuppressWarnings("unchecked") - public T getValue(int row, int col) { - return (T) getObject(row, col); - } - - @SuppressWarnings("unchecked") - public T getValue(int row, String columnName) { - return (T) getObject(row, columnName); - } - - public void setTotal(long total) { - this.total = total; - } - - public long getTotal() { - return total; - } - - @Override - public boolean isEmpty() { - return super.isEmpty() || size() <= 2; - } -} diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/HttpHandler.java b/net-http-server/src/main/java/org/xbib/net/http/server/HttpHandler.java index 09c33ab..4ef73f2 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/HttpHandler.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/HttpHandler.java @@ -1,6 +1,9 @@ package org.xbib.net.http.server; -import org.xbib.net.Handler; +import java.io.IOException; -public interface HttpHandler extends Handler { +@FunctionalInterface +public interface HttpHandler { + + void handle(HttpServerContext httpServerContext) throws IOException; } diff --git a/net-http-server/src/main/java/org/xbib/net/http/server/HttpServerContext.java b/net-http-server/src/main/java/org/xbib/net/http/server/HttpServerContext.java index e1a066d..2304b5c 100644 --- a/net-http-server/src/main/java/org/xbib/net/http/server/HttpServerContext.java +++ b/net-http-server/src/main/java/org/xbib/net/http/server/HttpServerContext.java @@ -1,7 +1,6 @@ package org.xbib.net.http.server; import org.xbib.net.Attributes; -import org.xbib.net.Context; import org.xbib.net.URL; import org.xbib.net.buffer.DataBuffer; import org.xbib.net.http.server.route.HttpRouteResolver; @@ -13,7 +12,11 @@ import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.nio.file.Path; -public interface HttpServerContext extends Context { +public interface HttpServerContext { + + HttpRequestBuilder request(); + + HttpResponseBuilder response(); void setResolverResult(HttpRouteResolver.Result result); diff --git a/settings.gradle b/settings.gradle index b05dde7..51aeb04 100644 --- a/settings.gradle +++ b/settings.gradle @@ -3,7 +3,7 @@ dependencyResolutionManagement { libs { version('gradle', '7.5.1') version('junit', '5.9.1') - version('net', '3.0.0') + version('net', '3.0.1') version('netty', '4.1.84.Final') version('netty-tcnative', '2.0.54.Final') version('datastructures', '1.0.1') @@ -37,10 +37,10 @@ dependencyResolutionManagement { library('datastructures-json-tiny', 'org.xbib', 'datastructures-json-tiny').versionRef('datastructures') library('datastructures-yaml-tiny', 'org.xbib', 'datastructures-yaml-tiny').versionRef('datastructures') library('groovy-templates', 'org.apache.groovy', 'groovy-templates').versionRef('groovy') - library('jdbc-query', 'org.xbib', 'jdbc-query').version('0.0.5') - library('jdbc-connection-pool', 'org.xbib', 'jdbc-connection-pool').version('0.0.5') + library('jdbc-query', 'org.xbib', 'jdbc-query').version('1.0.0') + library('jdbc-connection-pool', 'org.xbib', 'jdbc-connection-pool').version('1.0.0') library('event', 'org.xbib', 'event').version('0.0.1') - library('oracle', 'com.oracle.database.jdbc', 'ojdbc11').version('21.4.0.0') + library('oracle', 'com.oracle.database.jdbc', 'ojdbc11').version('21.7.0.0') library('webjars-bootstrap', 'org.webjars.bower', 'bootstrap').version('3.4.1') library('webjars-jquery', 'org.webjars.bower', 'jquery').version('3.5.1') library('webjars-fontawesome', 'org.webjars', 'font-awesome').version('5.14.0')