17
17
18
18
package com .dangdang .ddframe .rdb .integrate .tbl .pstatement ;
19
19
20
- import static org .hamcrest .CoreMatchers .is ;
21
- import static org .junit .Assert .assertThat ;
22
-
23
- import java .sql .Connection ;
24
- import java .sql .PreparedStatement ;
25
- import java .sql .SQLException ;
26
-
27
20
import com .dangdang .ddframe .rdb .integrate .tbl .AbstractShardingTablesOnlyDBUnitTest ;
21
+ import com .dangdang .ddframe .rdb .sharding .jdbc .ShardingDataSource ;
28
22
import com .dangdang .ddframe .rdb .sharding .parser .result .router .SQLStatementType ;
29
23
import org .dbunit .DatabaseUnitException ;
30
24
import org .junit .Before ;
31
25
import org .junit .Test ;
32
26
33
- import com .dangdang .ddframe .rdb .sharding .jdbc .ShardingDataSource ;
27
+ import java .sql .Connection ;
28
+ import java .sql .PreparedStatement ;
29
+ import java .sql .SQLException ;
30
+
31
+ import static org .hamcrest .CoreMatchers .is ;
32
+ import static org .junit .Assert .assertThat ;
34
33
35
34
public final class ShardingTablesOnlyForPStatementWithDMLTest extends AbstractShardingTablesOnlyDBUnitTest {
36
35
@@ -44,9 +43,9 @@ public void init() throws SQLException {
44
43
@ Test
45
44
public void assertInsert () throws SQLException , DatabaseUnitException {
46
45
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)" ;
47
- for ( int i = 1 ; i <= 10 ; i ++) {
48
- try ( Connection connection = shardingDataSource . getConnection ( )) {
49
- PreparedStatement preparedStatement = connection . prepareStatement ( sql );
46
+ try ( Connection connection = shardingDataSource . getConnection ();
47
+ PreparedStatement preparedStatement = connection . prepareStatement ( sql )) {
48
+ for ( int i = 1 ; i <= 10 ; i ++) {
50
49
preparedStatement .setInt (1 , i );
51
50
preparedStatement .setInt (2 , i );
52
51
preparedStatement .setString (3 , "insert" );
@@ -59,9 +58,9 @@ public void assertInsert() throws SQLException, DatabaseUnitException {
59
58
@ Test
60
59
public void assertInsertWithAllPlaceholders () throws SQLException , DatabaseUnitException {
61
60
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)" ;
62
- for ( int i = 1 ; i <= 10 ; i ++) {
63
- try ( Connection connection = shardingDataSource . getConnection ( )) {
64
- PreparedStatement preparedStatement = connection . prepareStatement ( sql );
61
+ try ( Connection connection = shardingDataSource . getConnection ();
62
+ PreparedStatement preparedStatement = connection . prepareStatement ( sql )) {
63
+ for ( int i = 1 ; i <= 10 ; i ++) {
65
64
preparedStatement .setInt (1 , i );
66
65
preparedStatement .setInt (2 , i );
67
66
preparedStatement .setString (3 , "insert" );
@@ -75,8 +74,8 @@ public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitE
75
74
public void assertInsertWithoutPlaceholder () throws SQLException , DatabaseUnitException {
76
75
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, 'insert')" ;
77
76
for (int i = 1 ; i <= 10 ; i ++) {
78
- try (Connection connection = shardingDataSource .getConnection ()) {
79
- PreparedStatement preparedStatement = connection .prepareStatement (String .format (sql , i , i ));
77
+ try (Connection connection = shardingDataSource .getConnection ();
78
+ PreparedStatement preparedStatement = connection .prepareStatement (String .format (sql , i , i ))) {
80
79
preparedStatement .executeUpdate ();
81
80
}
82
81
}
@@ -87,8 +86,8 @@ public void assertInsertWithoutPlaceholder() throws SQLException, DatabaseUnitEx
87
86
public void assertInsertWithPlaceholdersForShardingKeys () throws SQLException , DatabaseUnitException {
88
87
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, ?)" ;
89
88
for (int i = 1 ; i <= 10 ; i ++) {
90
- try (Connection connection = shardingDataSource .getConnection ()) {
91
- PreparedStatement preparedStatement = connection .prepareStatement (String .format (sql , i , i ));
89
+ try (Connection connection = shardingDataSource .getConnection ();
90
+ PreparedStatement preparedStatement = connection .prepareStatement (String .format (sql , i , i ))) {
92
91
preparedStatement .setString (1 , "insert" );
93
92
preparedStatement .executeUpdate ();
94
93
}
@@ -100,8 +99,8 @@ public void assertInsertWithPlaceholdersForShardingKeys() throws SQLException, D
100
99
public void assertInsertWithPlaceholdersForNotShardingKeys () throws SQLException , DatabaseUnitException {
101
100
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, ?)" ;
102
101
for (int i = 1 ; i <= 10 ; i ++) {
103
- try (Connection connection = shardingDataSource .getConnection ()) {
104
- PreparedStatement preparedStatement = connection .prepareStatement (String .format (sql , i , i ));
102
+ try (Connection connection = shardingDataSource .getConnection ();
103
+ PreparedStatement preparedStatement = connection .prepareStatement (String .format (sql , i , i ))) {
105
104
preparedStatement .setString (1 , "insert" );
106
105
preparedStatement .executeUpdate ();
107
106
}
@@ -113,10 +112,10 @@ public void assertInsertWithPlaceholdersForNotShardingKeys() throws SQLException
113
112
public void assertUpdateWithoutAlias () throws SQLException , DatabaseUnitException {
114
113
ShardingDataSource shardingDataSource = getShardingDataSource ();
115
114
String sql = "UPDATE `t_order` SET `status` = ? WHERE `order_id` = ? AND `user_id` = ?" ;
116
- for ( int i = 10 ; i < 12 ; i ++) {
117
- for ( int j = 0 ; j < 10 ; j ++ ) {
118
- try ( Connection connection = shardingDataSource . getConnection () ) {
119
- PreparedStatement preparedStatement = connection . prepareStatement ( sql );
115
+ try ( Connection connection = shardingDataSource . getConnection ();
116
+ PreparedStatement preparedStatement = connection . prepareStatement ( sql ) ) {
117
+ for ( int i = 10 ; i < 12 ; i ++ ) {
118
+ for ( int j = 0 ; j < 10 ; j ++) {
120
119
preparedStatement .setString (1 , "updated" );
121
120
preparedStatement .setInt (2 , i * 100 + j );
122
121
preparedStatement .setInt (3 , i );
@@ -131,10 +130,10 @@ public void assertUpdateWithoutAlias() throws SQLException, DatabaseUnitExceptio
131
130
public void assertUpdateWithAlias () throws SQLException , DatabaseUnitException {
132
131
ShardingDataSource shardingDataSource = getShardingDataSource ();
133
132
String sql = "UPDATE `t_order` as o SET o.`status` = ? WHERE o.`order_id` = ? AND o.`user_id` = ?" ;
134
- for ( int i = 10 ; i < 12 ; i ++) {
135
- for ( int j = 0 ; j < 10 ; j ++ ) {
136
- try ( Connection connection = shardingDataSource . getConnection () ) {
137
- PreparedStatement preparedStatement = connection . prepareStatement ( sql );
133
+ try ( Connection connection = shardingDataSource . getConnection ();
134
+ PreparedStatement preparedStatement = connection . prepareStatement ( sql ) ) {
135
+ for ( int i = 10 ; i < 12 ; i ++ ) {
136
+ for ( int j = 0 ; j < 10 ; j ++) {
138
137
preparedStatement .setString (1 , "updated" );
139
138
preparedStatement .setInt (2 , i * 100 + j );
140
139
preparedStatement .setInt (3 , i );
@@ -149,8 +148,8 @@ public void assertUpdateWithAlias() throws SQLException, DatabaseUnitException {
149
148
public void assertUpdateWithoutShardingValue () throws SQLException , DatabaseUnitException {
150
149
ShardingDataSource shardingDataSource = getShardingDataSource ();
151
150
String sql = "UPDATE `t_order` SET `status` = ? WHERE `status` = ?" ;
152
- try (Connection connection = shardingDataSource .getConnection ()) {
153
- PreparedStatement preparedStatement = connection .prepareStatement (sql );
151
+ try (Connection connection = shardingDataSource .getConnection ();
152
+ PreparedStatement preparedStatement = connection .prepareStatement (sql )) {
154
153
preparedStatement .setString (1 , "updated" );
155
154
preparedStatement .setString (2 , "init" );
156
155
assertThat (preparedStatement .executeUpdate (), is (20 ));
@@ -162,10 +161,10 @@ public void assertUpdateWithoutShardingValue() throws SQLException, DatabaseUnit
162
161
public void assertDeleteWithoutAlias () throws SQLException , DatabaseUnitException {
163
162
ShardingDataSource shardingDataSource = getShardingDataSource ();
164
163
String sql = "DELETE `t_order` WHERE `order_id` = ? AND `user_id` = ? AND `status` = ?" ;
165
- for ( int i = 10 ; i < 12 ; i ++) {
166
- for ( int j = 0 ; j < 10 ; j ++ ) {
167
- try ( Connection connection = shardingDataSource . getConnection () ) {
168
- PreparedStatement preparedStatement = connection . prepareStatement ( sql );
164
+ try ( Connection connection = shardingDataSource . getConnection ();
165
+ PreparedStatement preparedStatement = connection . prepareStatement ( sql ) ) {
166
+ for ( int i = 10 ; i < 12 ; i ++ ) {
167
+ for ( int j = 0 ; j < 10 ; j ++) {
169
168
preparedStatement .setInt (1 , i * 100 + j );
170
169
preparedStatement .setInt (2 , i );
171
170
preparedStatement .setString (3 , "init" );
@@ -180,8 +179,8 @@ public void assertDeleteWithoutAlias() throws SQLException, DatabaseUnitExceptio
180
179
public void assertDeleteWithoutShardingValue () throws SQLException , DatabaseUnitException {
181
180
ShardingDataSource shardingDataSource = getShardingDataSource ();
182
181
String sql = "DELETE `t_order` WHERE `status` = ?" ;
183
- try (Connection connection = shardingDataSource .getConnection ()) {
184
- PreparedStatement preparedStatement = connection .prepareStatement (sql );
182
+ try (Connection connection = shardingDataSource .getConnection ();
183
+ PreparedStatement preparedStatement = connection .prepareStatement (sql )) {
185
184
preparedStatement .setString (1 , "init" );
186
185
assertThat (preparedStatement .executeUpdate (), is (20 ));
187
186
}
0 commit comments