Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 85e1183

Browse files
committed
[#929] Add H2Database
1 parent bfe4110 commit 85e1183

File tree

1 file changed

+144
-0
lines changed
  • hibernate-reactive-core/src/test/java/org/hibernate/reactive/containers

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.containers;
7+
8+
import java.io.Serializable;
9+
import java.math.BigDecimal;
10+
import java.math.BigInteger;
11+
import java.net.URL;
12+
import java.sql.Time;
13+
import java.sql.Timestamp;
14+
import java.time.Duration;
15+
import java.time.Instant;
16+
import java.time.LocalDate;
17+
import java.time.LocalDateTime;
18+
import java.time.LocalTime;
19+
import java.util.Date;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.TimeZone;
23+
import java.util.UUID;
24+
25+
import org.hibernate.type.NumericBooleanType;
26+
import org.hibernate.type.TextType;
27+
import org.hibernate.type.TrueFalseType;
28+
import org.hibernate.type.YesNoType;
29+
import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor;
30+
31+
public class H2Database implements TestableDatabase {
32+
public static H2Database INSTANCE = new H2Database();
33+
34+
private static Map<Class<?>, String> expectedDBTypeForClass = new HashMap<>();
35+
36+
static {
37+
{
38+
expectedDBTypeForClass.put( boolean.class, "BOOLEAN" );
39+
expectedDBTypeForClass.put( Boolean.class, "BOOLEAN" );
40+
expectedDBTypeForClass.put( NumericBooleanType.class, "INTEGER" );
41+
expectedDBTypeForClass.put( TrueFalseType.class, "CHARACTER" );
42+
expectedDBTypeForClass.put( YesNoType.class, "CHARACTER" );
43+
expectedDBTypeForClass.put( int.class, "INTEGER" );
44+
expectedDBTypeForClass.put( Integer.class, "INTEGER" );
45+
expectedDBTypeForClass.put( long.class, "BIGINT" );
46+
expectedDBTypeForClass.put( Long.class, "BIGINT" );
47+
expectedDBTypeForClass.put( float.class, "DOUBLE PRECISION" );
48+
expectedDBTypeForClass.put( Float.class, "DOUBLE PRECISION" );
49+
expectedDBTypeForClass.put( double.class, "DOUBLE PRECISION" );
50+
expectedDBTypeForClass.put( Double.class, "DOUBLE PRECISION" );
51+
expectedDBTypeForClass.put( byte.class, "TINYINT" );
52+
expectedDBTypeForClass.put( Byte.class, "TINYINT" );
53+
expectedDBTypeForClass.put( PrimitiveByteArrayTypeDescriptor.class, "BINARY VARYING" );
54+
expectedDBTypeForClass.put( URL.class, "CHARACTER VARYING" );
55+
expectedDBTypeForClass.put( TimeZone.class, "CHARACTER VARYING" );
56+
expectedDBTypeForClass.put( Date.class, "DATE" );
57+
expectedDBTypeForClass.put( Timestamp.class, "TIMESTAMP" );
58+
expectedDBTypeForClass.put( Time.class, "TIME" );
59+
expectedDBTypeForClass.put( LocalDate.class, "DATE" );
60+
expectedDBTypeForClass.put( LocalTime.class, "time" );
61+
expectedDBTypeForClass.put( LocalDateTime.class, "TIMESTAMP" );
62+
expectedDBTypeForClass.put( BigInteger.class, "NUMERIC" );
63+
expectedDBTypeForClass.put( BigDecimal.class, "NUMERIC" );
64+
expectedDBTypeForClass.put( Serializable.class, "BINARY VARYING" );
65+
expectedDBTypeForClass.put( UUID.class, "binary" );
66+
expectedDBTypeForClass.put( Instant.class, "datetime" );
67+
expectedDBTypeForClass.put( Duration.class, "bigint" );
68+
expectedDBTypeForClass.put( Character.class, "VARCHAR_IGNORECASE" );
69+
expectedDBTypeForClass.put( char.class, "VARCHAR_IGNORECASE" );
70+
expectedDBTypeForClass.put( TextType.class, "text" );
71+
expectedDBTypeForClass.put( String.class, "CHARACTER VARYING" );
72+
}
73+
}
74+
75+
@Override
76+
public String getJdbcUrl() {
77+
return "jdbc:" + getUri();
78+
}
79+
80+
@Override
81+
public String getUri() {
82+
return "h2:~/test;DATABASE_TO_UPPER=FALSE";
83+
}
84+
85+
86+
@Override
87+
public String getScheme() {
88+
return "h2:";
89+
}
90+
91+
@Override
92+
public String getNativeDatatypeQuery(String tableName, String columnName) {
93+
return "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS COLS " +
94+
"WHERE COLS.TABLE_NAME = '" + tableName + "'" +
95+
"AND COLS.COLUMN_NAME= '" + columnName + "'";
96+
}
97+
98+
@Override
99+
public String getExpectedNativeDatatype(Class<?> dataType) {
100+
return expectedDBTypeForClass.get( dataType );
101+
}
102+
103+
@Override
104+
public String createJdbcUrl(String host, int port, String database, Map<String, String> params) {
105+
// Primary mode for H2 is embedded which uses the URL format: "jdbc:h2:~/test"
106+
// H2 can also be configured as a remote server.
107+
// EXAMPLE 1: jdbc:h2:tcp://localhost/D:/myproject/data/project-name
108+
// EXAMPLE 2: jdbc:h2:tcp://localhost/~/test
109+
// EXAMpLE 3: jdbc:h2:tcp://localhost:9081/~/test
110+
final StringBuilder paramsBuilder = new StringBuilder();
111+
if ( params != null && !params.isEmpty() ) {
112+
params.forEach( (key, value) -> {
113+
paramsBuilder.append( jdbcParamDelimiter() );
114+
paramsBuilder.append( key );
115+
paramsBuilder.append( "=" );
116+
paramsBuilder.append( value );
117+
} );
118+
}
119+
String url = "jdbc:" + getScheme() + "//" + host;
120+
if ( port > -1 ) {
121+
url += ":" + port;
122+
}
123+
if ( paramsBuilder.length() > 0 ) {
124+
url += jdbcStartQuery() + paramsBuilder.substring( 1 );
125+
}
126+
if ( database != null ) {
127+
return url + ";database=" + database;
128+
}
129+
return url;
130+
}
131+
132+
@Override
133+
public String jdbcStartQuery() {
134+
return ";";
135+
}
136+
137+
@Override
138+
public String jdbcParamDelimiter() {
139+
return ";";
140+
}
141+
142+
private H2Database() {
143+
}
144+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /