Skip to content

Commit f83053e

Browse files
committed
DATAJDBC-278 - Add MySQL dialect.
1 parent 5c5eb3e commit f83053e

File tree

4 files changed

+223
-1
lines changed

4 files changed

+223
-1
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/Dialect.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import org.springframework.data.relational.core.sql.render.SelectRenderContext;
1919

2020
/**
21-
* Represents a dialect that is implemented by a particular database.
21+
* Represents a dialect that is implemented by a particular database. Please note that not all features are supported by
22+
* all vendors. Dialects typically express this with feature flags. Methods for unsupported functionality may throw
23+
* {@link UnsupportedOperationException}.
2224
*
2325
* @author Mark Paluch
2426
* @author Jens Schauder
@@ -42,5 +44,10 @@ default ArrayColumns getArraySupport() {
4244
return ArrayColumns.Unsupported.INSTANCE;
4345
}
4446

47+
/**
48+
* Obtain the {@link SelectRenderContext}.
49+
*
50+
* @return the {@link SelectRenderContext}.
51+
*/
4552
SelectRenderContext getSelectContext();
4653
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2019 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+
* http://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+
package org.springframework.data.relational.core.dialect;
17+
18+
/**
19+
* An SQL dialect for MySQL.
20+
*
21+
* @author Mark Paluch
22+
* @since 1.1
23+
*/
24+
public class MySqlDialect extends AbstractDialect {
25+
26+
/**
27+
* Singleton instance.
28+
*/
29+
public static final MySqlDialect INSTANCE = new MySqlDialect();
30+
31+
private static final LimitClause LIMIT_CLAUSE = new LimitClause() {
32+
33+
/*
34+
* (non-Javadoc)
35+
* @see org.springframework.data.relational.core.dialect.LimitClause#getLimit(long)
36+
*/
37+
@Override
38+
public String getLimit(long limit) {
39+
return "LIMIT " + limit;
40+
}
41+
42+
/*
43+
* (non-Javadoc)
44+
* @see org.springframework.data.relational.core.dialect.LimitClause#getOffset(long)
45+
*/
46+
@Override
47+
public String getOffset(long offset) {
48+
throw new UnsupportedOperationException("MySQL does not support OFFSET without LIMIT");
49+
}
50+
51+
/*
52+
* (non-Javadoc)
53+
* @see org.springframework.data.relational.core.dialect.LimitClause#getClause(long, long)
54+
*/
55+
@Override
56+
public String getLimitOffset(long limit, long offset) {
57+
58+
// LIMIT {[offset,] row_count}
59+
return String.format("LIMIT %d, %d", offset, limit);
60+
}
61+
62+
/*
63+
* (non-Javadoc)
64+
* @see org.springframework.data.relational.core.dialect.LimitClause#getClausePosition()
65+
*/
66+
@Override
67+
public Position getClausePosition() {
68+
return Position.AFTER_ORDER_BY;
69+
}
70+
};
71+
72+
/*
73+
* (non-Javadoc)
74+
* @see org.springframework.data.relational.core.dialect.Dialect#limit()
75+
*/
76+
@Override
77+
public LimitClause limit() {
78+
return LIMIT_CLAUSE;
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2019 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+
* http://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+
package org.springframework.data.relational.core.dialect;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import org.springframework.data.relational.core.sql.Select;
24+
import org.springframework.data.relational.core.sql.StatementBuilder;
25+
import org.springframework.data.relational.core.sql.Table;
26+
import org.springframework.data.relational.core.sql.render.NamingStrategies;
27+
import org.springframework.data.relational.core.sql.render.SqlRenderer;
28+
29+
/**
30+
* Tests for {@link MySqlDialect}-specific rendering.
31+
*
32+
* @author Mark Paluch
33+
*/
34+
public class MySqlDialectRenderingTests {
35+
36+
private final RenderContextFactory factory = new RenderContextFactory(MySqlDialect.INSTANCE);
37+
38+
@Before
39+
public void before() {
40+
factory.setNamingStrategy(NamingStrategies.asIs());
41+
}
42+
43+
@Test // DATAJDBC-278
44+
public void shouldRenderSelectWithLimit() {
45+
46+
Table table = Table.create("foo");
47+
Select select = StatementBuilder.select(table.asterisk()).from(table).limit(10).build();
48+
49+
String sql = SqlRenderer.create(factory.createRenderContext()).render(select);
50+
51+
assertThat(sql).isEqualTo("SELECT foo.* FROM foo LIMIT 10");
52+
}
53+
54+
@Test // DATAJDBC-278
55+
public void shouldRenderSelectWithOffset() {
56+
57+
Table table = Table.create("foo");
58+
Select select = StatementBuilder.select(table.asterisk()).from(table).offset(10).build();
59+
60+
assertThatThrownBy(() -> SqlRenderer.create(factory.createRenderContext()).render(select))
61+
.isInstanceOf(UnsupportedOperationException.class);
62+
}
63+
64+
@Test // DATAJDBC-278
65+
public void shouldRenderSelectWithLimitOffset() {
66+
67+
Table table = Table.create("foo");
68+
Select select = StatementBuilder.select(table.asterisk()).from(table).limit(10).offset(20).build();
69+
70+
String sql = SqlRenderer.create(factory.createRenderContext()).render(select);
71+
72+
assertThat(sql).isEqualTo("SELECT foo.* FROM foo LIMIT 20, 10");
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2019 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+
* http://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+
package org.springframework.data.relational.core.dialect;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.Test;
21+
22+
/**
23+
* Unit tests for {@link MySqlDialect}.
24+
*
25+
* @author Mark Paluch
26+
*/
27+
public class MySqlDialectUnitTests {
28+
29+
@Test // DATAJDBC-278
30+
public void shouldNotSupportArrays() {
31+
32+
ArrayColumns arrayColumns = MySqlDialect.INSTANCE.getArraySupport();
33+
34+
assertThat(arrayColumns.isSupported()).isFalse();
35+
}
36+
37+
@Test // DATAJDBC-278
38+
public void shouldRenderLimit() {
39+
40+
LimitClause limit = MySqlDialect.INSTANCE.limit();
41+
42+
assertThat(limit.getClausePosition()).isEqualTo(LimitClause.Position.AFTER_ORDER_BY);
43+
assertThat(limit.getLimit(10)).isEqualTo("LIMIT 10");
44+
}
45+
46+
@Test // DATAJDBC-278
47+
public void shouldRenderOffset() {
48+
49+
LimitClause limit = MySqlDialect.INSTANCE.limit();
50+
51+
assertThatThrownBy(() -> limit.getOffset(10)).isInstanceOf(UnsupportedOperationException.class);
52+
}
53+
54+
@Test // DATAJDBC-278
55+
public void shouldRenderLimitOffset() {
56+
57+
LimitClause limit = MySqlDialect.INSTANCE.limit();
58+
59+
assertThat(limit.getLimitOffset(20, 10)).isEqualTo("LIMIT 10, 20");
60+
}
61+
}

0 commit comments

Comments
 (0)