Skip to content

Commit 200effd

Browse files
committed
[#1614] Format existing code
1 parent ff0f8a0 commit 200effd

File tree

2 files changed

+152
-137
lines changed

2 files changed

+152
-137
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@ public static Parameters instance(Dialect dialect) {
4040
if ( dialect instanceof DialectDelegateWrapper ) {
4141
dialect = ( (DialectDelegateWrapper) dialect ).getWrappedDialect();
4242
}
43-
if (dialect instanceof PostgreSQLDialect || dialect instanceof CockroachDialect ) return PostgresParameters.INSTANCE;
44-
if (dialect instanceof SQLServerDialect) return SQLServerParameters.INSTANCE;
43+
if ( dialect instanceof PostgreSQLDialect || dialect instanceof CockroachDialect ) {
44+
return PostgresParameters.INSTANCE;
45+
}
46+
if ( dialect instanceof SQLServerDialect ) {
47+
return SQLServerParameters.INSTANCE;
48+
}
4549
return NO_PARSING;
4650
}
4751

4852
public static boolean isProcessingNotRequired(String sql) {
4953
return sql == null
5054
// There aren't any parameters
51-
|| sql.indexOf('?') == -1;
55+
|| sql.indexOf( '?' ) == -1;
5256
}
5357

5458
public abstract String process(String sql);

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

Lines changed: 145 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -9,139 +9,150 @@
99

1010
public class SQLServerParameters extends Parameters {
1111

12-
public static final SQLServerParameters INSTANCE = new SQLServerParameters();
13-
14-
private SQLServerParameters() {
15-
}
16-
17-
@Override
18-
public String process(String sql) {
19-
if (isProcessingNotRequired(sql)) {
20-
return sql;
21-
}
22-
return new Parser(sql).result();
23-
}
24-
25-
@Override
26-
public String process(String sql, int parameterCount) {
27-
if (isProcessingNotRequired(sql)) {
28-
return sql;
29-
}
30-
return new Parser(sql, parameterCount).result();
31-
}
32-
33-
/* Offset and Fetch gets applied just before the execution of the query but because we know
34-
* how the string looks like for Sql Server, it's faster to replace the last bit instead
35-
* of processing the whole query
36-
*/
37-
@Override
38-
public String processLimit(String sql, Object[] parameterArray, boolean hasOffset) {
39-
if ( isProcessingNotRequired( sql ) ) {
40-
return sql;
41-
}
42-
43-
// Replace 'offset ? fetch next ? rows only' with the @P style parameters for Sql Server
44-
int index = hasOffset ? parameterArray.length - 1 : parameterArray.length;
45-
int pos = sql.indexOf( " offset " );
46-
if ( pos > -1 ) {
47-
// The dialect doesn't use a parameter if the offset is 0
48-
String offsetQueryString = sql.contains( " offset 0 " )
49-
? " offset 0"
50-
: " offset @P" + index++;
51-
String sqlProcessed = sql.substring( 0, pos ) + offsetQueryString + " rows";
52-
if ( sql.contains( " fetch next ?" ) ) {
53-
sqlProcessed += " fetch next @P" + index + " rows only ";
54-
}
55-
return sqlProcessed;
56-
}
57-
// Different Hibernate ORM versions may use different lowercase/uppercase letters
58-
if ( sql.toLowerCase( Locale.ROOT ).startsWith( "select top(?)" ) ) {
59-
// 13 is the length of the string "select top(?)"
60-
String sqlProcessed = "select top(@P" + index + ")" + sql.substring( 13 );
61-
shiftValues( parameterArray );
62-
return sqlProcessed;
63-
}
64-
65-
// No order by, no filter
66-
// See SQLServer2005LimitHandler#processSql
67-
int paginationFilterPos = sql.lastIndexOf( "__row__ >= ? and __row__ < ?" );
68-
if ( paginationFilterPos > -1 ) {
69-
sql = sql.substring( 0, paginationFilterPos );
70-
sql += "__row__ >= @P" + index++ + " and __row__ < @P" + index++;
71-
}
72-
return sql;
73-
}
74-
75-
/**
76-
* Left shift all the values in the array (moving the first value to the end)
77-
*/
78-
private void shiftValues(Object[] parameterArray) {
79-
Object temp = parameterArray[0];
80-
System.arraycopy( parameterArray, 1, parameterArray, 0, parameterArray.length - 1 );
81-
parameterArray[parameterArray.length - 1] = temp;
82-
}
83-
84-
private static class Parser {
85-
86-
private boolean inString;
87-
private boolean inQuoted;
88-
private boolean inSqlComment;
89-
private boolean inCComment;
90-
private boolean escaped;
91-
private int count = 0;
92-
private final StringBuilder result;
93-
private int previous;
94-
95-
private Parser(String sql) {
96-
this(sql, 10);
97-
}
98-
99-
private Parser(String sql, int parameterCount) {
100-
result = new StringBuilder(sql.length() + parameterCount);
101-
sql.codePoints().forEach(this::append);
102-
}
103-
104-
private String result() {
105-
return result.toString();
106-
}
107-
108-
private void append(int codePoint) {
109-
if (escaped) {
110-
escaped = false;
111-
} else {
112-
switch (codePoint) {
113-
case '\\':
114-
escaped = true;
115-
break;
116-
case '"':
117-
if (!inString && !inSqlComment && !inCComment) inQuoted = !inQuoted;
118-
break;
119-
case '\'':
120-
if (!inQuoted && !inSqlComment && !inCComment) inString = !inString;
121-
break;
122-
case '-':
123-
if (!inQuoted && !inString && !inCComment && previous == '-') inSqlComment = true;
124-
break;
125-
case '\n':
126-
inSqlComment = false;
127-
break;
128-
case '*':
129-
if (!inQuoted && !inString && !inSqlComment && previous == '/') inCComment = true;
130-
break;
131-
case '/':
132-
if (previous == '*') inCComment = false;
133-
break;
134-
//TODO: $$-quoted strings
135-
case '?':
136-
if (!inQuoted && !inString) {
137-
result.append("@P").append(++count);
138-
previous = '?';
139-
return;
12+
public static final SQLServerParameters INSTANCE = new SQLServerParameters();
13+
14+
private SQLServerParameters() {
15+
}
16+
17+
@Override
18+
public String process(String sql) {
19+
if ( isProcessingNotRequired( sql ) ) {
20+
return sql;
21+
}
22+
return new Parser( sql ).result();
23+
}
24+
25+
@Override
26+
public String process(String sql, int parameterCount) {
27+
if ( isProcessingNotRequired( sql ) ) {
28+
return sql;
29+
}
30+
return new Parser( sql, parameterCount ).result();
31+
}
32+
33+
/* Offset and Fetch gets applied just before the execution of the query but because we know
34+
* how the string looks like for Sql Server, it's faster to replace the last bit instead
35+
* of processing the whole query
36+
*/
37+
@Override
38+
public String processLimit(String sql, Object[] parameterArray, boolean hasOffset) {
39+
if ( isProcessingNotRequired( sql ) ) {
40+
return sql;
41+
}
42+
43+
// Replace 'offset ? fetch next ? rows only' with the @P style parameters for Sql Server
44+
int index = hasOffset ? parameterArray.length - 1 : parameterArray.length;
45+
int pos = sql.indexOf( " offset " );
46+
if ( pos > -1 ) {
47+
// The dialect doesn't use a parameter if the offset is 0
48+
String offsetQueryString = sql.contains( " offset 0 " )
49+
? " offset 0"
50+
: " offset @P" + index++;
51+
String sqlProcessed = sql.substring( 0, pos ) + offsetQueryString + " rows";
52+
if ( sql.contains( " fetch next ?" ) ) {
53+
sqlProcessed += " fetch next @P" + index + " rows only ";
54+
}
55+
return sqlProcessed;
56+
}
57+
// Different Hibernate ORM versions may use different lowercase/uppercase letters
58+
if ( sql.toLowerCase( Locale.ROOT ).startsWith( "select top(?)" ) ) {
59+
// 13 is the length of the string "select top(?)"
60+
String sqlProcessed = "select top(@P" + index + ")" + sql.substring( 13 );
61+
shiftValues( parameterArray );
62+
return sqlProcessed;
63+
}
64+
65+
// No order by, no filter
66+
// See SQLServer2005LimitHandler#processSql
67+
int paginationFilterPos = sql.lastIndexOf( "__row__ >= ? and __row__ < ?" );
68+
if ( paginationFilterPos > -1 ) {
69+
sql = sql.substring( 0, paginationFilterPos );
70+
sql += "__row__ >= @P" + index++ + " and __row__ < @P" + index++;
71+
}
72+
return sql;
73+
}
74+
75+
/**
76+
* Left shift all the values in the array (moving the first value to the end)
77+
*/
78+
private void shiftValues(Object[] parameterArray) {
79+
Object temp = parameterArray[0];
80+
System.arraycopy( parameterArray, 1, parameterArray, 0, parameterArray.length - 1 );
81+
parameterArray[parameterArray.length - 1] = temp;
82+
}
83+
84+
private static class Parser {
85+
86+
private boolean inString;
87+
private boolean inQuoted;
88+
private boolean inSqlComment;
89+
private boolean inCComment;
90+
private boolean escaped;
91+
private int count = 0;
92+
private final StringBuilder result;
93+
private int previous;
94+
95+
private Parser(String sql) {
96+
this( sql, 10 );
97+
}
98+
99+
private Parser(String sql, int parameterCount) {
100+
result = new StringBuilder( sql.length() + parameterCount );
101+
sql.codePoints().forEach( this::append );
102+
}
103+
104+
private String result() {
105+
return result.toString();
106+
}
107+
108+
private void append(int codePoint) {
109+
if ( escaped ) {
110+
escaped = false;
111+
}
112+
else {
113+
switch ( codePoint ) {
114+
case '\\':
115+
escaped = true;
116+
break;
117+
case '"':
118+
if ( !inString && !inSqlComment && !inCComment ) {
119+
inQuoted = !inQuoted;
140120
}
141-
}
142-
}
143-
previous = codePoint;
144-
result.appendCodePoint(codePoint);
145-
}
146-
}
121+
break;
122+
case '\'':
123+
if ( !inQuoted && !inSqlComment && !inCComment ) {
124+
inString = !inString;
125+
}
126+
break;
127+
case '-':
128+
if ( !inQuoted && !inString && !inCComment && previous == '-' ) {
129+
inSqlComment = true;
130+
}
131+
break;
132+
case '\n':
133+
inSqlComment = false;
134+
break;
135+
case '*':
136+
if ( !inQuoted && !inString && !inSqlComment && previous == '/' ) {
137+
inCComment = true;
138+
}
139+
break;
140+
case '/':
141+
if ( previous == '*' ) {
142+
inCComment = false;
143+
}
144+
break;
145+
//TODO: $$-quoted strings
146+
case '?':
147+
if ( !inQuoted && !inString ) {
148+
result.append( "@P" ).append( ++count );
149+
previous = '?';
150+
return;
151+
}
152+
}
153+
}
154+
previous = codePoint;
155+
result.appendCodePoint( codePoint );
156+
}
157+
}
147158
}

0 commit comments

Comments
 (0)