update to database 1.0.0, xbib net 3.0.1, oracle jdbc 21.7.0.0

This commit is contained in:
Jörg Prante 2022-11-01 18:51:21 +01:00
parent 6e1835daea
commit f8fa71892a
11 changed files with 35 additions and 533 deletions

View file

@ -1,5 +1,5 @@
group = org.xbib
name = net-http
version = 3.0.0
version = 3.0.1
org.gradle.warning.mode = ALL

View file

@ -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
}

View file

@ -5,6 +5,8 @@ publishing {
publications {
mavenJava(MavenPublication) {
from components.java
artifact sourcesJar
artifact javadocJar
pom {
name = project.name
description = rootProject.ext.description

View file

@ -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;
}

View file

@ -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<String, Object> 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<String, Object> params = new HashMap<>(table.getParams());
String statement = table.getStatement() + " " + table.getWhereClause();
return countRows(statement, params);
}
@Override
public long countRows(String statement, Map<String, Object> 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<String, Object> params) {
return getLimitedRows(statement, params, 1, fetchSize, timeoutSeconds);
}
@Override
public Table getUnlimitedRows(String statement, Map<String, Object> params) {
return getLimitedRows(statement, params, 0, fetchSize, timeoutSeconds);
}
@Override
public Table getLimitedRows(String statement, Map<String, Object> 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<Object> columnNames = new ArrayList<>();
List<Object> 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<String, Object> params) {
SqlInsert sql = db.toInsert(statement);
insertParams(sql, params);
sql.insert(1);
}
@Override
public void insert(String statement, List<Map<String, Object>> params) {
SqlInsert sqlInsert = db.toInsert(statement);
for (Map<String, Object> param : params) {
insertParams(sqlInsert, param);
sqlInsert.batch();
}
sqlInsert.insertBatch();
}
@Override
public void update(String statement, Map<String, Object> params) {
SqlUpdate sql = db.toUpdate(statement);
updateParams(sql, params);
sql.update();
}
@Override
public void update(String statement, List<Map<String, Object>> params) {
SqlUpdate sqlUpdate = db.toUpdate(statement);
for (Map<String, Object> param : params) {
updateParams(sqlUpdate, param);
sqlUpdate.update();
}
}
@Override
public void upsert(String insertStatement, String updateStatement, Map<String, Object> 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<String, Object> params) {
SqlUpdate sql = db.toDelete(statement);
updateParams(sql, params);
sql.update();
}
@Override
public void delete(String statement, List<Map<String, Object>> params) {
SqlUpdate sqlUpdate = db.toDelete(statement);
for (Map<String, Object> param : params) {
updateParams(sqlUpdate, param);
sqlUpdate.update();
}
}
private void selectParams(SqlSelect sql, Map<String, Object> 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<String, Object> 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<String, Object> 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<Object> getRow(Rows rows, List<Object> classNames) {
List<Object> 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;
}
}

View file

@ -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<String, Object> params);
Table getSingleRow(String statement, Map<String, Object> params);
Table getUnlimitedRows(String statement, Map<String, Object> params);
Table getLimitedRows(String statement, Map<String, Object> params,
int limit, int fetchSize, int timeoutSeconds);
void insert(String statement, Map<String, Object> params);
void insert(String statement, List<Map<String, Object>> params);
void update(String statement, Map<String, Object> params);
void update(String statement, List<Map<String, Object>> params);
void upsert(String insertStatement, String updateStatement, Map<String, Object> params);
void delete(String statement, Map<String, Object> params);
void delete(String statement, List<Map<String, Object>> params);
void close() throws IOException;
}

View file

@ -68,6 +68,7 @@ public class DatabaseApplicationModule extends BaseApplicationModule {
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

View file

@ -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<List<Object>> implements List<List<Object>> {
private String statement;
private Map<String, Object> params;
private String search;
private LocalDateTime from;
private LocalDateTime to;
private Integer offset;
private Integer size;
private List<String> where;
private String whereClause;
private String groupByClause;
private final List<Map.Entry<String, Boolean>> sort = new ArrayList<>();
private long total;
public void setStatement(String statement) {
this.statement = statement;
}
public String getStatement() {
return statement;
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
public Map<String, Object> 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<String> where) {
this.where = where;
}
public List<String> 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<String> getColumnNames() {
return get(0).stream().map(Object::toString).collect(Collectors.toList());
}
public int getColumn(String columnName) {
return get(0).indexOf(columnName);
}
public List<String> 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<Object> getRow(int i) {
return get(i + 2);
}
public Map<String, Object> getRowAsMap(int i) {
TinyMap.Builder<String, Object> map = TinyMap.builder();
List<Object> 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> T getValue(int row, int col) {
return (T) getObject(row, col);
}
@SuppressWarnings("unchecked")
public <T> 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;
}
}

View file

@ -1,6 +1,9 @@
package org.xbib.net.http.server;
import org.xbib.net.Handler;
import java.io.IOException;
public interface HttpHandler extends Handler<HttpServerContext> {
@FunctionalInterface
public interface HttpHandler {
void handle(HttpServerContext httpServerContext) throws IOException;
}

View file

@ -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<HttpRequestBuilder, HttpResponseBuilder> {
public interface HttpServerContext {
HttpRequestBuilder request();
HttpResponseBuilder response();
void setResolverResult(HttpRouteResolver.Result<HttpService> result);

View file

@ -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')