/
PluginDao.java
210 lines (178 loc) · 7.43 KB
/
PluginDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright 2014-2020 Groupon, Inc
* Copyright 2020-2020 Equinix, Inc
* Copyright 2014-2020 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
* 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 org.killbill.billing.plugin.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;
import javax.annotation.Nullable;
import javax.sql.DataSource;
import org.joda.time.DateTime;
import org.jooq.SQLDialect;
import org.jooq.conf.MappedSchema;
import org.jooq.conf.RenderMapping;
import org.jooq.conf.RenderNameCase;
import org.jooq.conf.Settings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
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';
protected static final String DEFAULT_SCHEMA_NAME = "killbill";
protected static final ObjectMapper objectMapper = new ObjectMapper();
protected static final XmlMapper XML_MAPPER = new XmlMapper();
protected final DataSource dataSource;
protected final SQLDialect dialect;
protected final Settings settings;
public PluginDao(final DataSource dataSource) throws SQLException {
this(dataSource, getSQLDialect(getDBEngine(dataSource)));
}
public PluginDao(final DataSource dataSource, final SQLDialect dialect) throws SQLException {
this.dataSource = dataSource;
this.dialect = dialect;
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)
.withRenderNameCase(RenderNameCase.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;
}
}
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) {
return bool ? TRUE : FALSE;
}
protected static LocalDateTime toLocalDateTime(@Nullable final DateTime dateTime) {
return dateTime == null ? null : LocalDateTime.ofInstant(Instant.ofEpochMilli(dateTime.getMillis()), ZoneId.of(dateTime.getZone().getID()));
}
protected static Timestamp toTimestamp(@Nullable final DateTime dateTime) {
return dateTime == null ? null : new Timestamp(dateTime.getMillis());
}
protected static Timestamp toTimestamp(@Nullable final Date date) {
return date == null ? null : new Timestamp(date.getTime());
}
protected String getProperty(final String key, final Map additionalData) {
return Strings.emptyToNull(additionalData == null || additionalData.get(key) == null ? null : String.valueOf(additionalData.get(key)));
}
protected String asString(final Map additionalData) throws SQLException {
if (additionalData == null || additionalData.isEmpty()) {
return null;
}
return asString((Object) additionalData);
}
protected String asString(final Object additionalData) throws SQLException {
try {
return objectMapper.writeValueAsString(additionalData);
} catch (final JsonProcessingException e) {
throw new SQLException(e);
}
}
public static SQLDialect getSQLDialect(final 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 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 DBEngine dbEngine;
if ("H2".equalsIgnoreCase(databaseProductName)) {
dbEngine = DBEngine.H2;
} else if ("MySQL".equalsIgnoreCase(databaseProductName)) {
dbEngine = DBEngine.MYSQL;
} else if ("PostgreSQL".equalsIgnoreCase(databaseProductName)) {
dbEngine = DBEngine.POSTGRESQL;
} else {
dbEngine = DBEngine.GENERIC;
}
return dbEngine;
}
public static Settings getSettings(final InputStream xml) throws IOException {
return XML_MAPPER.readValue(xml, Settings.class);
}
protected interface WithConnectionCallback<T> {
public T withConnection(final Connection conn) throws SQLException;
}
protected <T> T execute(final Connection conn, final WithConnectionCallback<T> callback) throws SQLException {
try {
return callback.withConnection(conn);
} finally {
conn.close();
}
}
// Duplicate the enum to avoid pulling in killbill-embeddeddb-common into plugins (lots of dependencies)
public enum DBEngine {
GENERIC,
MYSQL,
H2,
POSTGRESQL
}
}