-
Notifications
You must be signed in to change notification settings - Fork 26
/
H2Data.java
414 lines (395 loc) · 14.3 KB
/
H2Data.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**
* Copyright (c) 2012-2016, jcabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.dynamo.mock;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import com.jcabi.dynamo.AttributeUpdates;
import com.jcabi.dynamo.Attributes;
import com.jcabi.dynamo.Conditions;
import com.jcabi.jdbc.JdbcSession;
import com.jcabi.jdbc.ListOutcome;
import com.jcabi.jdbc.Outcome;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.codec.binary.Base32;
import org.h2.Driver;
/**
* Mock data in H2 database.
*
* @author Yegor Bugayenko (yegor@tpc2.com)
* @version $Id$
* @since 0.10
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
@Immutable
@ToString
@Loggable(Loggable.DEBUG)
@EqualsAndHashCode(of = "jdbc")
@SuppressWarnings({ "PMD.TooManyMethods", "PMD.ExcessiveImports" })
public final class H2Data implements MkData {
/**
* Fetcher of rows.
*/
private static final Outcome<Iterable<Attributes>> OUTCOME =
// @checkstyle AnonInnerLengthCheck (50 lines)
new Outcome<Iterable<Attributes>>() {
@Override
public Iterable<Attributes> handle(final ResultSet rset,
final Statement stmt) throws SQLException {
final Collection<Attributes> items =
new LinkedList<Attributes>();
while (rset.next()) {
items.add(this.fetch(rset));
}
return items;
}
/**
* Convert result set to Attributes.
* @param rset Result set
* @return Attribs
* @throws SQLException If fails
*/
private Attributes fetch(final ResultSet rset) throws SQLException {
final ResultSetMetaData meta = rset.getMetaData();
Attributes attrs = new Attributes();
for (int idx = 0; idx < meta.getColumnCount(); ++idx) {
final String text = rset.getString(idx + 1);
AttributeValue value = new AttributeValue().withS(text);
if (text.matches("[0-9]+")) {
value = value.withN(text);
}
attrs = attrs.with(
meta.getColumnName(idx + 1).toLowerCase(Locale.ENGLISH),
value
);
}
return attrs;
}
};
/**
* Where clause.
*/
private static final Function<String, String> WHERE =
new Function<String, String>() {
@Override
public String apply(final String key) {
return String.format("%s = ?", key);
}
};
/**
* Create primary key.
*/
private static final Function<String, String> CREATE_KEY =
new Function<String, String>() {
@Override
public String apply(final String key) {
return String.format("%s VARCHAR PRIMARY KEY", key);
}
};
/**
* Create attr.
*/
private static final Function<String, String> CREATE_ATTR =
new Function<String, String>() {
@Override
public String apply(final String key) {
return String.format("%s CLOB", key);
}
};
/**
* WHERE clauses are joined with this.
*/
private static final String AND = " AND ";
/**
* JDBC URL.
*/
private final transient String jdbc;
/**
* Public ctor.
* @throws IOException If fails
*/
public H2Data() throws IOException {
this(File.createTempFile("jcabi-dynamo-", ".h2"));
}
/**
* Public ctor.
* @param file Where to keep the database
*/
public H2Data(final File file) {
this.jdbc = String.format(
"jdbc:h2:file:%s",
file.getAbsolutePath()
);
}
@Override
public Iterable<String> keys(final String table) throws IOException {
try {
return Iterables.transform(
new JdbcSession(this.connection())
// @checkstyle LineLength (1 line)
.sql("SELECT SQL FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE TABLE_NAME = ?")
.set(H2Data.encodeTableName(table))
.select(
new ListOutcome<String>(
new ListOutcome.Mapping<String>() {
@Override
public String map(final ResultSet rset)
throws SQLException {
return rset.getString(1);
}
}
)
),
new Function<String, String>() {
@Override
public String apply(final String input) {
final Matcher matcher = Pattern.compile(
"PRIMARY KEY\\((.*)\\)"
).matcher(input);
if (!matcher.find()) {
throw new IllegalStateException(
String.format(
"something is wrong here: \"%s\"", input
)
);
}
return matcher.group(1).toLowerCase(Locale.ENGLISH);
}
}
);
} catch (final SQLException ex) {
throw new IOException(ex);
}
}
@Override
public Iterable<Attributes> iterate(final String table,
final Conditions conds) throws IOException {
try {
final StringBuilder sql = new StringBuilder("SELECT * FROM ")
.append(H2Data.encodeTableName(table));
if (!conds.isEmpty()) {
sql.append(" WHERE ");
Joiner.on(H2Data.AND).appendTo(
sql,
Iterables.transform(conds.keySet(), H2Data.WHERE)
);
}
JdbcSession session = new JdbcSession(this.connection())
.sql(sql.toString());
for (final Condition cond : conds.values()) {
if (cond.getAttributeValueList().size() != 1) {
throw new UnsupportedOperationException(
"at the moment only one value of condition is supported"
);
}
if (!cond.getComparisonOperator()
.equals(ComparisonOperator.EQ.toString())) {
throw new UnsupportedOperationException(
String.format(
"at the moment only EQ operator is supported: %s",
cond.getComparisonOperator()
)
);
}
final AttributeValue val = cond.getAttributeValueList().get(0);
session = session.set(H2Data.value(val));
}
return session.select(H2Data.OUTCOME);
} catch (final SQLException ex) {
throw new IOException(ex);
}
}
@Override
public void put(final String table, final Attributes attrs)
throws IOException {
try {
JdbcSession session = new JdbcSession(this.connection());
for (final AttributeValue value : attrs.values()) {
session = session.set(H2Data.value(value));
}
session.sql(
String.format(
"INSERT INTO %s (%s) VALUES (%s)",
H2Data.encodeTableName(table),
Joiner.on(',').join(attrs.keySet()),
Joiner.on(',').join(Collections.nCopies(attrs.size(), "?"))
)
);
session.execute();
} catch (final SQLException ex) {
throw new IOException(ex);
}
}
@Override
public void update(final String table, final Attributes keys,
final AttributeUpdates attrs)
throws IOException {
try {
JdbcSession session = new JdbcSession(this.connection());
for (final AttributeValueUpdate value : attrs.values()) {
session = session.set(H2Data.value(value.getValue()));
}
for (final AttributeValue value : keys.values()) {
session = session.set(H2Data.value(value));
}
session.sql(
String.format(
"UPDATE %s SET %s WHERE %s",
H2Data.encodeTableName(table),
Joiner.on(',').join(
Iterables.transform(attrs.keySet(), H2Data.WHERE)
),
Joiner.on(H2Data.AND).join(
Iterables.transform(keys.keySet(), H2Data.WHERE)
)
)
);
session.execute();
} catch (final SQLException ex) {
throw new IOException(ex);
}
}
@Override
public void delete(final String table, final Attributes keys)
throws IOException {
try {
JdbcSession session = new JdbcSession(this.connection());
for (final AttributeValue value : keys.values()) {
session = session.set(H2Data.value(value));
}
session.sql(
String.format(
"DELETE FROM %s WHERE %s",
H2Data.encodeTableName(table),
Joiner.on(H2Data.AND).join(
Iterables.transform(keys.keySet(), H2Data.WHERE)
)
)
);
session.execute();
} catch (final SQLException ex) {
throw new IOException(ex);
}
}
/**
* With this table, that has given primary keys.
* @param table Table name
* @param keys Primary keys
* @param attrs Attributes
* @return New data, modified
* @throws IOException If fails
*/
public H2Data with(final String table, final String[] keys,
final String... attrs) throws IOException {
if (keys.length == 0) {
throw new IllegalArgumentException(
String.format("empty list of keys for %s table", table)
);
}
final StringBuilder sql = new StringBuilder("CREATE TABLE ")
.append(H2Data.encodeTableName(table)).append(" (");
Joiner.on(',').appendTo(
sql,
Iterables.transform(Arrays.asList(keys), H2Data.CREATE_KEY)
);
if (attrs.length > 0) {
sql.append(',');
Joiner.on(',').appendTo(
sql,
Iterables.transform(Arrays.asList(attrs), H2Data.CREATE_ATTR)
);
}
sql.append(')');
try {
new JdbcSession(this.connection()).sql(sql.toString()).execute();
} catch (final SQLException ex) {
throw new IOException(ex);
}
return this;
}
/**
* Make data source.
* @return Data source for JDBC
* @throws SQLException If fails
*/
private Connection connection() throws SQLException {
return new Driver().connect(this.jdbc, new Properties());
}
/**
* Get value from attribute.
* @param attr Attribute value
* @return Text format
*/
private static String value(final AttributeValue attr) {
String val = attr.getS();
if (val == null) {
val = attr.getN();
}
if (val == null) {
throw new IllegalArgumentException(
"we support only N and S at the moment"
);
}
return val;
}
/**
* Base32-encodes table name for use with H2.
* @param table Table name to encode
* @return Base-32-encoded table name
*/
private static String encodeTableName(final String table) {
return Joiner.on("").join(
"_",
new Base32(true, (byte) '_').encodeAsString(table.getBytes())
);
}
}