Skip to content

Commit 1e42cc4

Browse files
committed
[#1614] Refactor the code
Group together all the duplicated code
1 parent c516952 commit 1e42cc4

File tree

4 files changed

+106
-382
lines changed

4 files changed

+106
-382
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/OracleParameters.java

Lines changed: 1 addition & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -10,111 +10,6 @@ public class OracleParameters extends Parameters {
1010
public static final OracleParameters INSTANCE = new OracleParameters();
1111

1212
private OracleParameters() {
13-
}
14-
15-
public String process(String sql) {
16-
if ( isProcessingNotRequired( sql ) ) {
17-
return sql;
18-
}
19-
return new OracleParameters.Parser( sql ).result();
20-
}
21-
22-
/**
23-
* Limit and offset gets applied just before the execution of the query but because we know
24-
* how the string looks like for Oracle, it's faster to replace the last bit instead
25-
* of processing the whole query
26-
*/
27-
public String processLimit(String sql, Object[] parameterArray, boolean hasOffset) {
28-
if ( isProcessingNotRequired( sql ) ) {
29-
return sql;
30-
}
31-
32-
throw new UnsupportedOperationException();
33-
}
34-
35-
/**
36-
* Replace all JDBC-style {@code ?} parameters with Oracle-style
37-
* {@code :n} parameters in the given SQL string.
38-
*/
39-
public String process(String sql, int parameterCount) {
40-
if ( isProcessingNotRequired( sql ) ) {
41-
return sql;
42-
}
43-
return new Parser( sql, parameterCount ).result();
44-
}
45-
46-
private static class Parser {
47-
48-
private boolean inString;
49-
private boolean inQuoted;
50-
private boolean inSqlComment;
51-
private boolean inCComment;
52-
private boolean escaped;
53-
private int count = 0;
54-
private StringBuilder result;
55-
private int previous;
56-
57-
private Parser(String sql) {
58-
this( sql, 10 );
59-
}
60-
61-
private Parser(String sql, int parameterCount) {
62-
result = new StringBuilder( sql.length() + parameterCount );
63-
sql.codePoints().forEach( this::append );
64-
}
65-
66-
private String result() {
67-
return result.toString();
68-
}
69-
70-
private void append(int codePoint) {
71-
if ( escaped ) {
72-
escaped = false;
73-
}
74-
else {
75-
switch ( codePoint ) {
76-
case '\\':
77-
escaped = true;
78-
return;
79-
case '"':
80-
if ( !inString && !inSqlComment && !inCComment ) {
81-
inQuoted = !inQuoted;
82-
}
83-
break;
84-
case '\'':
85-
if ( !inQuoted && !inSqlComment && !inCComment ) {
86-
inString = !inString;
87-
}
88-
break;
89-
case '-':
90-
if ( !inQuoted && !inString && !inCComment && previous == '-' ) {
91-
inSqlComment = true;
92-
}
93-
break;
94-
case '\n':
95-
inSqlComment = false;
96-
break;
97-
case '*':
98-
if ( !inQuoted && !inString && !inSqlComment && previous == '/' ) {
99-
inCComment = true;
100-
}
101-
break;
102-
case '/':
103-
if ( previous == '*' ) {
104-
inCComment = false;
105-
}
106-
break;
107-
//TODO: $$-quoted strings
108-
case '?':
109-
if ( !inQuoted && !inString ) {
110-
result.append( ':' ).append( ++count );
111-
previous = '?';
112-
return;
113-
}
114-
}
115-
}
116-
previous = codePoint;
117-
result.appendCodePoint( codePoint );
118-
}
13+
super( ":" );
11914
}
12015
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/Parameters.java

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
package org.hibernate.reactive.pool.impl;
77

8+
import java.util.function.IntConsumer;
9+
810
import org.hibernate.dialect.CockroachDialect;
911
import org.hibernate.dialect.Dialect;
1012
import org.hibernate.dialect.DialectDelegateWrapper;
@@ -19,7 +21,9 @@
1921
*/
2022
public abstract class Parameters {
2123

22-
private static final Parameters NO_PARSING = new Parameters() {
24+
private final String paramPrefix;
25+
26+
private static final Parameters NO_PARSING = new Parameters( null ) {
2327
@Override
2428
public String process(String sql) {
2529
return sql;
@@ -29,13 +33,12 @@ public String process(String sql) {
2933
public String process(String sql, int parameterCount) {
3034
return sql;
3135
}
32-
33-
@Override
34-
public String processLimit(String sql, Object[] parameterArray, boolean hasOffset) {
35-
return sql;
36-
}
3736
};
3837

38+
protected Parameters(String paramPrefix) {
39+
this.paramPrefix = paramPrefix;
40+
}
41+
3942
public static Parameters instance(Dialect dialect) {
4043
if ( dialect instanceof DialectDelegateWrapper ) {
4144
dialect = ( (DialectDelegateWrapper) dialect ).getWrappedDialect();
@@ -55,9 +58,100 @@ public static boolean isProcessingNotRequired(String sql) {
5558
|| sql.indexOf( '?' ) == -1;
5659
}
5760

58-
public abstract String process(String sql);
61+
public String process(String sql) {
62+
if ( isProcessingNotRequired( sql ) ) {
63+
return sql;
64+
}
65+
return new Parser( sql, paramPrefix ).result();
66+
}
67+
68+
/**
69+
* Replace all JDBC-style {@code ?} parameters with Postgres-style
70+
* {@code $n} parameters in the given SQL string.
71+
*/
72+
public String process(String sql, int parameterCount) {
73+
if ( isProcessingNotRequired( sql ) ) {
74+
return sql;
75+
}
76+
return new Parser( sql, parameterCount, paramPrefix ).result();
77+
}
78+
79+
private static class Parser {
5980

60-
public abstract String process(String sql, int parameterCount);
81+
private boolean inString;
82+
private boolean inQuoted;
83+
private boolean inSqlComment;
84+
private boolean inCComment;
85+
private boolean escaped;
86+
private int count = 0;
87+
private StringBuilder result;
88+
private int previous;
6189

62-
public abstract String processLimit(String sql, Object[] parameterArray, boolean hasOffset);
90+
private Parser(String sql, String paramPrefix) {
91+
this( sql, 10, paramPrefix );
92+
}
93+
94+
private Parser(String sql, int parameterCount, final String paramPrefix) {
95+
result = new StringBuilder( sql.length() + parameterCount );
96+
// We aren't using lambdas or method reference because of a bug in the JVM:
97+
// https://bugs.openjdk.java.net/browse/JDK-8161588
98+
// Please, don't change this unless you've tested it with Quarkus
99+
sql.codePoints().forEach( new IntConsumer() {
100+
@Override
101+
public void accept(int codePoint) {
102+
if ( escaped ) {
103+
escaped = false;
104+
}
105+
else {
106+
switch ( codePoint ) {
107+
case '\\':
108+
escaped = true;
109+
return;
110+
case '"':
111+
if ( !inString && !inSqlComment && !inCComment ) {
112+
inQuoted = !inQuoted;
113+
}
114+
break;
115+
case '\'':
116+
if ( !inQuoted && !inSqlComment && !inCComment ) {
117+
inString = !inString;
118+
}
119+
break;
120+
case '-':
121+
if ( !inQuoted && !inString && !inCComment && previous == '-' ) {
122+
inSqlComment = true;
123+
}
124+
break;
125+
case '\n':
126+
inSqlComment = false;
127+
break;
128+
case '*':
129+
if ( !inQuoted && !inString && !inSqlComment && previous == '/' ) {
130+
inCComment = true;
131+
}
132+
break;
133+
case '/':
134+
if ( previous == '*' ) {
135+
inCComment = false;
136+
}
137+
break;
138+
//TODO: $$-quoted strings
139+
case '?':
140+
if ( !inQuoted && !inString ) {
141+
result.append( paramPrefix ).append( ++count );
142+
previous = '?';
143+
return;
144+
}
145+
}
146+
}
147+
previous = codePoint;
148+
result.appendCodePoint( codePoint );
149+
}
150+
} );
151+
}
152+
153+
public String result() {
154+
return result.toString();
155+
}
156+
}
63157
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/pool/impl/PostgresParameters.java

Lines changed: 1 addition & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -5,133 +5,11 @@
55
*/
66
package org.hibernate.reactive.pool.impl;
77

8-
import java.util.function.IntConsumer;
9-
108
public class PostgresParameters extends Parameters {
119

1210
public static final PostgresParameters INSTANCE = new PostgresParameters();
1311

1412
private PostgresParameters() {
15-
}
16-
17-
public String process(String sql) {
18-
if ( isProcessingNotRequired( sql ) ) {
19-
return sql;
20-
}
21-
return new Parser( sql ).result();
22-
}
23-
24-
/**
25-
* Limit and offset gets applied just before the execution of the query but because we know
26-
* how the string looks like for Postgres, it's faster to replace the last bit instead
27-
* of processing the whole query
28-
*/
29-
public String processLimit(String sql, Object[] parameterArray, boolean hasOffset) {
30-
if ( isProcessingNotRequired( sql ) ) {
31-
return sql;
32-
}
33-
34-
// Replace 'limit ? offset ?' with the $ style parameters for PostgreSQL
35-
int index = hasOffset ? parameterArray.length - 1 : parameterArray.length;
36-
int pos = sql.indexOf( " limit ?" );
37-
if ( pos > -1 ) {
38-
String sqlProcessed = sql.substring( 0, pos ) + " limit $" + index++;
39-
if ( hasOffset ) {
40-
sqlProcessed += " offset $" + index;
41-
}
42-
return sqlProcessed;
43-
}
44-
45-
return sql;
46-
}
47-
48-
/**
49-
* Replace all JDBC-style {@code ?} parameters with Postgres-style
50-
* {@code $n} parameters in the given SQL string.
51-
*/
52-
public String process(String sql, int parameterCount) {
53-
if ( isProcessingNotRequired( sql ) ) {
54-
return sql;
55-
}
56-
return new Parser( sql, parameterCount ).result();
57-
}
58-
59-
private static class Parser {
60-
61-
private boolean inString;
62-
private boolean inQuoted;
63-
private boolean inSqlComment;
64-
private boolean inCComment;
65-
private boolean escaped;
66-
private int count = 0;
67-
private StringBuilder result;
68-
private int previous;
69-
70-
private Parser(String sql) {
71-
this( sql, 10 );
72-
}
73-
74-
private Parser(String sql, int parameterCount) {
75-
result = new StringBuilder( sql.length() + parameterCount );
76-
// We aren't using lambdas or method reference because of a bug in the JVM:
77-
// https://bugs.openjdk.java.net/browse/JDK-8161588
78-
// Please, don't change this unless you've tested it with Quarkus
79-
sql.codePoints().forEach( new IntConsumer() {
80-
@Override
81-
public void accept(int codePoint) {
82-
if ( escaped ) {
83-
escaped = false;
84-
}
85-
else {
86-
switch ( codePoint ) {
87-
case '\\':
88-
escaped = true;
89-
return;
90-
case '"':
91-
if ( !inString && !inSqlComment && !inCComment ) {
92-
inQuoted = !inQuoted;
93-
}
94-
break;
95-
case '\'':
96-
if ( !inQuoted && !inSqlComment && !inCComment ) {
97-
inString = !inString;
98-
}
99-
break;
100-
case '-':
101-
if ( !inQuoted && !inString && !inCComment && previous == '-' ) {
102-
inSqlComment = true;
103-
}
104-
break;
105-
case '\n':
106-
inSqlComment = false;
107-
break;
108-
case '*':
109-
if ( !inQuoted && !inString && !inSqlComment && previous == '/' ) {
110-
inCComment = true;
111-
}
112-
break;
113-
case '/':
114-
if ( previous == '*' ) {
115-
inCComment = false;
116-
}
117-
break;
118-
//TODO: $$-quoted strings
119-
case '?':
120-
if ( !inQuoted && !inString ) {
121-
result.append( '$' ).append( ++count );
122-
previous = '?';
123-
return;
124-
}
125-
}
126-
}
127-
previous = codePoint;
128-
result.appendCodePoint( codePoint );
129-
}
130-
} );
131-
}
132-
133-
private String result() {
134-
return result.toString();
135-
}
13+
super( "$" );
13614
}
13715
}

0 commit comments

Comments
 (0)