Permalink
Browse files

dao: initial support for various database engines

See #19.

Signed-off-by: Pierre-Alexandre Meyer <pierre@mouraf.org>
  • Loading branch information...
pierre committed Feb 14, 2018
1 parent f2267e1 commit 5f2e988be20cc548246bf282ca562c7d5551122e
View
@@ -55,6 +55,46 @@ jobs:
- store_artifacts:
path: ~/junit
test-mysql:
<<: *defaults
docker:
- image: killbill/kbbuild:0.1.0
steps:
- checkout
- restore_cache:
key: v1-dependencies-{{ .Branch }}-{{ checksum "pom.xml" }}
- run: mvn clean install -Pmysql
- run:
name: Save test results
command: |
mkdir -p ~/junit/
find . -type f -regex ".*/target/surefire-reports/.*xml" -exec cp {} ~/junit/ \;
when: always
- store_test_results:
path: ~/junit
- store_artifacts:
path: ~/junit
test-postgresql:
<<: *defaults
docker:
- image: killbill/kbbuild:0.1.0
steps:
- checkout
- restore_cache:
key: v1-dependencies-{{ .Branch }}-{{ checksum "pom.xml" }}
- run: mvn clean install -Ppostgresql
- run:
name: Save test results
command: |
mkdir -p ~/junit/
find . -type f -regex ".*/target/surefire-reports/.*xml" -exec cp {} ~/junit/ \;
when: always
- store_test_results:
path: ~/junit
- store_artifacts:
path: ~/junit
workflows:
version: 2
build-and-test:
@@ -63,3 +103,9 @@ workflows:
- test-h2:
requires:
- build
- test-mysql:
requires:
- build
- test-postgresql:
requires:
- build
View
32 pom.xml
@@ -20,7 +20,7 @@
<parent>
<artifactId>killbill-oss-parent</artifactId>
<groupId>org.kill-bill.billing</groupId>
<version>0.140.46</version>
<version>0.140.53</version>
</parent>
<groupId>org.kill-bill.billing.plugin.java</groupId>
<artifactId>killbill-base-plugin</artifactId>
@@ -84,6 +84,21 @@
<groupId>com.ning</groupId>
<artifactId>async-http-client</artifactId>
</dependency>
<dependency>
<groupId>io.airlift</groupId>
<artifactId>command</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.airlift</groupId>
<artifactId>testing-postgresql-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.airlift</groupId>
<artifactId>units</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
@@ -134,6 +149,11 @@
<groupId>org.kill-bill.billing</groupId>
<artifactId>killbill-platform-osgi-bundles-lib-slf4j-osgi</artifactId>
</dependency>
<dependency>
<groupId>org.kill-bill.billing</groupId>
<artifactId>killbill-platform-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.kill-bill.billing.plugin</groupId>
<artifactId>killbill-plugin-api-control</artifactId>
@@ -158,10 +178,18 @@
<groupId>org.kill-bill.commons</groupId>
<artifactId>killbill-clock</artifactId>
</dependency>
<dependency>
<groupId>org.kill-bill.commons</groupId>
<artifactId>killbill-embeddeddb-common</artifactId>
</dependency>
<dependency>
<groupId>org.kill-bill.commons</groupId>
<artifactId>killbill-embeddeddb-mysql</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.kill-bill.commons</groupId>
<artifactId>killbill-embeddeddb-postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@@ -1,6 +1,6 @@
/*
* Copyright 2014 Groupon, Inc
* Copyright 2014 The Billing Project, LLC
* Copyright 2014-2018 Groupon, Inc
* Copyright 2014-2018 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
@@ -30,14 +30,20 @@
import org.jooq.SQLDialect;
import org.jooq.conf.MappedSchema;
import org.jooq.conf.RenderMapping;
import org.jooq.conf.RenderNameStyle;
import org.jooq.conf.Settings;
import org.killbill.commons.embeddeddb.EmbeddedDB;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
public class PluginDao {
private static final Logger logger = LoggerFactory.getLogger(PluginDao.class);
public static final byte TRUE = (byte) '1';
public static final byte FALSE = (byte) '0';
@@ -50,24 +56,48 @@
protected final Settings settings;
public PluginDao(final DataSource dataSource) throws SQLException {
this(dataSource, SQLDialect.MYSQL);
this(dataSource, getSQLDialect(getDBEngine(dataSource)));
}
public PluginDao(final DataSource dataSource, final SQLDialect dialect) throws SQLException {
this.dataSource = dataSource;
this.dialect = dialect;
final String schema;
Connection connection = null;
try {
connection = dataSource.getConnection();
schema = connection.getCatalog();
} finally {
if (connection != null) {
connection.close();
}
switch (dialect) {
case H2:
final String schema;
Connection connection = null;
try {
connection = dataSource.getConnection();
schema = connection.getSchema();
} finally {
if (connection != null) {
connection.close();
}
}
final MappedSchema mappedSchema = new MappedSchema().withInput(DEFAULT_SCHEMA_NAME) // As defined by the jOOQ codegen
.withOutput(schema);
final RenderMapping renderMapping = new RenderMapping().withSchemata(mappedSchema);
this.settings = new Settings().withRenderMapping(renderMapping)
.withRenderNameStyle(RenderNameStyle.UPPER);
break;
default:
// For MySQL, schema doesn't matter.
// For PostgreSQL, assume schema is part of the JDBC url
this.settings = new Settings().withRenderSchema(false);
break;
}
this.settings = new Settings().withRenderMapping(new RenderMapping().withSchemata(new MappedSchema().withInput(DEFAULT_SCHEMA_NAME).withOutput(schema)));
}
public PluginDao(final DataSource dataSource, final Settings settings) throws SQLException {
this(dataSource, getSQLDialect(getDBEngine(dataSource)), settings);
}
public PluginDao(final DataSource dataSource, final SQLDialect dialect, final Settings settings) {
this.dataSource = dataSource;
this.dialect = dialect;
this.settings = settings;
}
protected static byte fromBoolean(final Boolean bool) {
@@ -101,6 +131,47 @@ protected String asString(final Object additionalData) throws SQLException {
}
}
public static SQLDialect getSQLDialect(final EmbeddedDB.DBEngine dbEngine) {
switch (dbEngine) {
case H2:
return SQLDialect.H2;
case MYSQL:
return SQLDialect.MARIADB;
case POSTGRESQL:
return SQLDialect.POSTGRES;
case GENERIC:
logger.warn("Generic DBEngine detected, falling back to SQLDialect.MYSQL");
return SQLDialect.MYSQL;
default:
throw new IllegalArgumentException("Unsupported DB engine: " + dbEngine);
}
}
public static EmbeddedDB.DBEngine getDBEngine(final DataSource dataSource) throws SQLException {
Connection connection = null;
String databaseProductName;
try {
connection = dataSource.getConnection();
databaseProductName = connection.getMetaData().getDatabaseProductName();
} finally {
if (connection != null) {
connection.close();
}
}
final EmbeddedDB.DBEngine dbEngine;
if ("H2".equalsIgnoreCase(databaseProductName)) {
dbEngine = EmbeddedDB.DBEngine.H2;
} else if ("MySQL".equalsIgnoreCase(databaseProductName)) {
dbEngine = EmbeddedDB.DBEngine.MYSQL;
} else if ("PostgreSQL".equalsIgnoreCase(databaseProductName)) {
dbEngine = EmbeddedDB.DBEngine.POSTGRESQL;
} else {
dbEngine = EmbeddedDB.DBEngine.GENERIC;
}
return dbEngine;
}
protected interface WithConnectionCallback<T> {
public T withConnection(final Connection conn) throws SQLException;
Oops, something went wrong.

0 comments on commit 5f2e988

Please sign in to comment.