-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sharding table rule and test (#6)
- Loading branch information
Showing
10 changed files
with
428 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
integration-tests/src/main/java/io/quarkiverse/shardingsphere/jdbc/it/Account.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.quarkiverse.shardingsphere.jdbc.it; | ||
|
||
public class Account { | ||
private int account_id; | ||
private int user_id; | ||
private String status; | ||
|
||
public int getAccount_id() { | ||
return account_id; | ||
} | ||
|
||
public void setAccount_id(int account_id) { | ||
this.account_id = account_id; | ||
} | ||
|
||
public int getUser_id() { | ||
return user_id; | ||
} | ||
|
||
public void setUser_id(int user_id) { | ||
this.user_id = user_id; | ||
} | ||
|
||
public String getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(String status) { | ||
this.status = status; | ||
} | ||
} |
99 changes: 79 additions & 20 deletions
99
...tests/src/main/java/io/quarkiverse/shardingsphere/jdbc/it/ShardingsphereJdbcResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,91 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.quarkiverse.shardingsphere.jdbc.it; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.event.Observes; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
import javax.sql.DataSource; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import io.quarkus.runtime.ShutdownEvent; | ||
import io.quarkus.runtime.StartupEvent; | ||
|
||
@Path("/shardingsphere-jdbc") | ||
@ApplicationScoped | ||
public class ShardingsphereJdbcResource { | ||
// add some rest methods here | ||
@Inject | ||
DataSource dataSource; | ||
|
||
@Named("ds") | ||
DataSource h2_ds; | ||
|
||
void onStart(@Observes StartupEvent ev) throws Exception { | ||
createAccountTable(); | ||
} | ||
|
||
void onStop(@Observes ShutdownEvent ev) throws Exception { | ||
dropAccountTable(); | ||
} | ||
|
||
private void createAccountTable() throws SQLException { | ||
String sql = "CREATE TABLE t_account (account_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (account_id))"; | ||
try (Connection connection = dataSource.getConnection(); | ||
Statement statement = connection.createStatement()) { | ||
statement.executeUpdate(sql); | ||
} | ||
} | ||
|
||
private void dropAccountTable() throws SQLException { | ||
String sql = "DROP TABLE t_account"; | ||
try (Connection connection = dataSource.getConnection(); | ||
Statement statement = connection.createStatement()) { | ||
statement.executeUpdate(sql); | ||
} | ||
} | ||
|
||
@POST | ||
@Path("/account") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Integer createAccount(Account account) throws Exception { | ||
int result = 0; | ||
|
||
String sql = "INSERT INTO t_account (account_id, user_id, status) VALUES (?, ?, ?)"; | ||
try (Connection connection = dataSource.getConnection(); | ||
PreparedStatement statement = connection.prepareStatement(sql)) { | ||
statement.setInt(1, account.getAccount_id()); | ||
statement.setInt(2, account.getUser_id()); | ||
statement.setString(3, account.getStatus()); | ||
result = statement.executeUpdate(); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@GET | ||
public String hello() { | ||
return "Hello shardingsphere-jdbc"; | ||
@Path("/account/{tbl}") | ||
public Integer count(@PathParam("tbl") String table) throws Exception { | ||
String sql = "SELECT COUNT(*) from " + table; | ||
try (Connection connection = h2_ds.getConnection(); | ||
Statement statement = connection.createStatement()) { | ||
ResultSet result = statement.executeQuery(sql); | ||
if (result.next()) { | ||
return result.getInt(1); | ||
} else { | ||
return -1; | ||
} | ||
} | ||
} | ||
} | ||
} |
40 changes: 33 additions & 7 deletions
40
integration-tests/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,36 @@ | ||
# H2 DataSource | ||
quarkus.datasource.ds1.db-kind=h2 | ||
quarkus.datasource.ds1.username=sa | ||
quarkus.datasource.ds1.jdbc.url=jdbc:h2:tcp://localhost/ds1 | ||
quarkus.datasource.ds.db-kind=h2 | ||
quarkus.datasource.ds.username=sa | ||
quarkus.datasource.ds.jdbc.url=jdbc:h2:mem:ds | ||
|
||
quarkus.datasource.ds2.db-kind=h2 | ||
quarkus.datasource.ds2.username=sa | ||
quarkus.datasource.ds2.jdbc.url=jdbc:h2:tcp://localhost/ds2 | ||
# ShardingSphere sharding tables | ||
quarkus.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds.t_order_$->{0..1} | ||
quarkus.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id | ||
quarkus.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=t-order-inline | ||
quarkus.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id | ||
quarkus.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake | ||
|
||
# Sharding Sphere | ||
quarkus.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes=ds.t_order_item_$->{0..1} | ||
quarkus.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-column=order_id | ||
quarkus.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-algorithm-name=t-order-item-inline | ||
quarkus.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column=order_item_id | ||
quarkus.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake | ||
|
||
quarkus.shardingsphere.rules.sharding.tables.t_account.actual-data-nodes=ds.t_account_$->{0..1} | ||
quarkus.shardingsphere.rules.sharding.tables.t_account.table-strategy.standard.sharding-algorithm-name=t-account-inline | ||
quarkus.shardingsphere.rules.sharding.tables.t_account.key-generate-strategy.column=account_id | ||
quarkus.shardingsphere.rules.sharding.tables.t_account.key-generate-strategy.key-generator-name=snowflake | ||
|
||
quarkus.shardingsphere.rules.sharding.default-sharding-column=account_id | ||
quarkus.shardingsphere.rules.sharding.binding-tables=t_order,t_order_item | ||
quarkus.shardingsphere.rules.sharding.broadcast-tables=t_address | ||
|
||
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-order-inline.type=INLINE | ||
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-order-inline.props.algorithm-expression=t_order_$->{order_id % 2} | ||
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-inline.type=INLINE | ||
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-order-item-inline.props.algorithm-expression=t_order_item_$->{order_id % 2} | ||
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-account-inline.type=INLINE | ||
quarkus.shardingsphere.rules.sharding.sharding-algorithms.t-account-inline.props.algorithm-expression=t_account_$->{account_id % 2} | ||
|
||
quarkus.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE | ||
quarkus.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.