Skip to content

Commit 0eaf6d1

Browse files
authored
Add support for Oracle bind marker scheme using R2DBC
We now support Oracle's bind marker scheme that identifies dynamic parameters using names that are prefixed with a colon such as `:P0_myparam`. Closes gh-26680
1 parent 6e264f9 commit 0eaf6d1

File tree

2 files changed

+112
-2
lines changed

2 files changed

+112
-2
lines changed

spring-r2dbc/src/main/java/org/springframework/r2dbc/core/binding/BindMarkersFactoryResolver.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -118,10 +118,12 @@ static class BuiltInBindMarkersFactoryProvider implements BindMarkerFactoryProvi
118118

119119
static {
120120
BUILTIN.put("H2", BindMarkersFactory.indexed("$", 1));
121+
BUILTIN.put("MariaDB", BindMarkersFactory.anonymous("?"));
121122
BUILTIN.put("Microsoft SQL Server", BindMarkersFactory.named("@", "P", 32,
122123
BuiltInBindMarkersFactoryProvider::filterBindMarker));
123124
BUILTIN.put("MySQL", BindMarkersFactory.anonymous("?"));
124-
BUILTIN.put("MariaDB", BindMarkersFactory.anonymous("?"));
125+
BUILTIN.put("Oracle", BindMarkersFactory.named(":", "P", 32,
126+
BuiltInBindMarkersFactoryProvider::filterBindMarker));
125127
BUILTIN.put("PostgreSQL", BindMarkersFactory.indexed("$", 1));
126128
}
127129

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.r2dbc.core.binding;
18+
19+
import io.r2dbc.spi.Connection;
20+
import io.r2dbc.spi.ConnectionFactory;
21+
import io.r2dbc.spi.ConnectionFactoryMetadata;
22+
import org.junit.jupiter.api.Test;
23+
import org.reactivestreams.Publisher;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Unit tests for {@link BindMarkersFactoryResolver}.
29+
*
30+
* @author Mark Paluch
31+
*/
32+
class BindMarkersFactoryResolverUnitTests {
33+
34+
@Test
35+
void shouldReturnBindMarkersFactoryForH2() {
36+
37+
BindMarkers bindMarkers = BindMarkersFactoryResolver
38+
.resolve(new MockConnectionFactory("H2")).create();
39+
40+
assertThat(bindMarkers.next().getPlaceholder()).isEqualTo("$1");
41+
}
42+
43+
@Test
44+
void shouldReturnBindMarkersFactoryForMariaDB() {
45+
46+
BindMarkers bindMarkers = BindMarkersFactoryResolver
47+
.resolve(new MockConnectionFactory("MariaDB")).create();
48+
49+
assertThat(bindMarkers.next().getPlaceholder()).isEqualTo("?");
50+
}
51+
52+
@Test
53+
void shouldReturnBindMarkersFactoryForMicrosoftSQLServer() {
54+
55+
BindMarkers bindMarkers = BindMarkersFactoryResolver
56+
.resolve(new MockConnectionFactory("Microsoft SQL Server")).create();
57+
58+
assertThat(bindMarkers.next("foo").getPlaceholder()).isEqualTo("@P0_foo");
59+
}
60+
61+
@Test
62+
void shouldReturnBindMarkersFactoryForMySQL() {
63+
64+
BindMarkers bindMarkers = BindMarkersFactoryResolver
65+
.resolve(new MockConnectionFactory("MySQL")).create();
66+
67+
assertThat(bindMarkers.next().getPlaceholder()).isEqualTo("?");
68+
}
69+
70+
@Test
71+
void shouldReturnBindMarkersFactoryForOracle() {
72+
73+
BindMarkers bindMarkers = BindMarkersFactoryResolver
74+
.resolve(new MockConnectionFactory("Oracle Database")).create();
75+
76+
assertThat(bindMarkers.next("foo").getPlaceholder()).isEqualTo(":P0_foo");
77+
}
78+
79+
@Test
80+
void shouldReturnBindMarkersFactoryForPostgreSQL() {
81+
82+
BindMarkers bindMarkers = BindMarkersFactoryResolver
83+
.resolve(new MockConnectionFactory("PostgreSQL")).create();
84+
85+
assertThat(bindMarkers.next().getPlaceholder()).isEqualTo("$1");
86+
}
87+
88+
static class MockConnectionFactory implements ConnectionFactory {
89+
90+
private final String driverName;
91+
92+
MockConnectionFactory(String driverName) {
93+
this.driverName = driverName;
94+
}
95+
96+
@Override
97+
public Publisher<? extends Connection> create() {
98+
throw new UnsupportedOperationException();
99+
}
100+
101+
@Override
102+
public ConnectionFactoryMetadata getMetadata() {
103+
return () -> driverName;
104+
}
105+
106+
}
107+
108+
}

0 commit comments

Comments
 (0)