Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: oracle insert sql use sysdate error. #2685

Merged
merged 23 commits into from Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f7ce922
fix: oracle insert sql use sysdate error.
jsbxyyx May 13, 2020
d2b21f9
fix ci
jsbxyyx May 14, 2020
6a895ec
fix reviews.
jsbxyyx May 14, 2020
efcd2f9
fix test.
jsbxyyx May 14, 2020
7db5d59
fix review.
jsbxyyx May 15, 2020
4350195
Merge branch 'develop' of https://github.com/seata/seata into fix_sys…
jsbxyyx May 21, 2020
b424c4e
fix tests.
jsbxyyx May 22, 2020
8745a49
Merge branch 'develop' into fix_sysdate
l81893521 May 26, 2020
1d085be
Merge branch 'develop' into fix_sysdate
jsbxyyx May 30, 2020
d20fd22
fix conflict
jsbxyyx Jun 5, 2020
2f5c34b
Merge branch 'fix_sysdate' of https://github.com/jsbxyyx/seata into f…
jsbxyyx Jun 5, 2020
1fa488a
Merge branch 'develop' into fix_sysdate
jsbxyyx Jun 9, 2020
25b56f6
Merge branch 'develop' into fix_sysdate
jsbxyyx Jun 19, 2020
deffbc6
Merge branch 'develop' into fix_sysdate
funky-eyes Jun 27, 2020
bc95299
Merge branch 'develop' into fix_sysdate
funky-eyes Jun 29, 2020
5f5abcd
Merge branch 'develop' into fix_sysdate
jsbxyyx Jun 30, 2020
1028c09
Merge branch 'develop' into fix_sysdate
funky-eyes Jun 30, 2020
1eb8a06
Merge branch 'develop' into fix_sysdate
slievrly Jun 30, 2020
01d0270
Merge branch 'develop' of https://github.com/seata/seata into fix_sys…
jsbxyyx Jul 3, 2020
6d2b2a3
Merge branch 'develop' of https://github.com/seata/seata into fix_sys…
jsbxyyx Jul 3, 2020
3cd2c1e
fix test case
jsbxyyx Jul 7, 2020
79fda43
Merge branch 'develop' into fix_sysdate
slievrly Jul 8, 2020
1a1d3cd
Merge branch 'develop' into fix_sysdate
slievrly Jul 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -144,7 +144,7 @@ protected Map<String,List<Object>> parsePkValuesFromStatement() {
if (statementProxy instanceof PreparedStatementProxy) {
PreparedStatementProxy preparedStatementProxy = (PreparedStatementProxy) statementProxy;

List<List<Object>> insertRows = recognizer.getInsertRows();
List<List<Object>> insertRows = recognizer.getInsertRows(pkIndexMap.values());
if (insertRows != null && !insertRows.isEmpty()) {
Map<Integer,ArrayList<Object>> parameters = preparedStatementProxy.getParameters();
final int rowSize = insertRows.size();
Expand Down Expand Up @@ -195,7 +195,7 @@ protected Map<String,List<Object>> parsePkValuesFromStatement() {
}
} else {
ps = false;
List<List<Object>> insertRows = recognizer.getInsertRows();
List<List<Object>> insertRows = recognizer.getInsertRows(pkIndexMap.values());
for (List<Object> row : insertRows) {
for (String pkKey:pkIndexMap.keySet()) {
int pkIndex = pkIndexMap.get(pkKey);
Expand Down
Expand Up @@ -58,6 +58,9 @@ public class BatchInsertExecutorTest {

private MySQLInsertExecutor insertExecutor;

private final int pkIndex = 1;
private HashMap pkIndexMap;

@BeforeEach
public void init() {
ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
Expand All @@ -71,7 +74,11 @@ public void init() {
tableMeta = mock(TableMeta.class);
insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));

doReturn(new HashMap<String,Integer>(){{put("id",1);}}).when(insertExecutor).getPkIndex();
pkIndexMap = new HashMap() {{
put(ID_COLUMN, pkIndex);
}};

doReturn(pkIndexMap).when(insertExecutor).getPkIndex();
}

@Test
Expand Down Expand Up @@ -199,7 +206,7 @@ private void mockParameters_with_null_and_insertRows_with_placeholder_null() {
List<List<Object>> insertRows = new ArrayList<>();
insertRows.add(Arrays.asList("?", "?", "?", "userStatus1"));
insertRows.add(Arrays.asList("?", Null.get(), "?", "userStatus2"));
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(insertRows);
}

private void mockParameters_with_number_and_insertRows_with_placeholde_null() {
Expand All @@ -224,7 +231,7 @@ private void mockParameters_with_number_and_insertRows_with_placeholde_null() {
List<List<Object>> insertRows = new ArrayList<>();
insertRows.add(Arrays.asList("?", "?", "?", "userStatus1"));
insertRows.add(Arrays.asList("?", Null.get(), "?", "userStatus2"));
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(insertRows);
}

private List<String> mockInsertColumns() {
Expand All @@ -239,7 +246,7 @@ private List<String> mockInsertColumns() {

private void mockParameters() {
int PK_INDEX = 1;
Map<Integer,ArrayList<Object>> paramters = new HashMap<>(4);
Map<Integer,ArrayList<Object>> paramters = new HashMap<>();
ArrayList arrayList0 = new ArrayList<>();
arrayList0.add("userId1");
arrayList0.add("userId2");
Expand Down Expand Up @@ -274,7 +281,7 @@ private void mockParameters() {
insertRows.add(Arrays.asList("?", "?", "?", "?"));

when(statementProxy.getParameters()).thenReturn(paramters);
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(insertRows);
when(statementProxy.getParamsByIndex(PK_INDEX)).thenReturn(paramters.get(PK_INDEX + 1));
}

Expand Down Expand Up @@ -354,7 +361,7 @@ private void mockParametersAllRefOfMysql() {
insertRows.add(Arrays.asList("?", "?", "?", "?"));
insertRows.add(Arrays.asList("?", "?", "?", "?"));
when(statementProxy.getParameters()).thenReturn(paramters);
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(insertRows);
when(statementProxy.getParameters()).thenReturn(paramters);
}

Expand Down Expand Up @@ -398,7 +405,7 @@ private void mockParametersWithPkRefOfMysql() {
insertRows.add(Arrays.asList("?", "?", "4", "44"));
insertRows.add(Arrays.asList("?", "?", "5", "55"));
when(statementProxy.getParameters()).thenReturn(paramters);
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(insertRows);
when(statementProxy.getParameters()).thenReturn(paramters);
}

Expand Down Expand Up @@ -442,7 +449,7 @@ private void mockParametersWithPkUnRefOfMysql() {
insertRows.add(Arrays.asList("?", 100000004, "?", "4"));
insertRows.add(Arrays.asList("?", 100000005, "?", "5"));
when(statementProxy.getParameters()).thenReturn(paramters);
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(insertRows);
}


Expand Down Expand Up @@ -481,8 +488,8 @@ private void mockParametersWithAllRefOfJDBC() {
List<List<Object>> insertRows = new ArrayList<>();
insertRows.add(Arrays.asList("?", "?", "?", "?"));
when(statementProxy.getParameters()).thenReturn(paramters);
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(statementProxy.getParamsByIndex(PK_INDEX)).thenReturn(paramters.get(PK_INDEX + 1));
doReturn(insertRows).when(sqlInsertRecognizer).getInsertRows(pkIndexMap.values());
}


Expand All @@ -507,7 +514,7 @@ private void mockParametersWithPkRefOfJDBC() {
List<List<Object>> insertRows = new ArrayList<>();
insertRows.add(Arrays.asList("?", "?", "userName1", "userStatus1"));
when(statementProxy.getParameters()).thenReturn(paramters);
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(insertRows);
when(statementProxy.getParamsByIndex(PK_INDEX)).thenReturn(paramters.get(PK_INDEX + 1));
}

Expand All @@ -524,7 +531,7 @@ private void mockParametersWithPkUnRefOfJDBC(int pkId) {
List<List<Object>> insertRows = new ArrayList<>();
insertRows.add(Arrays.asList("?", pkId, "?", "userStatus"));
when(statementProxy.getParameters()).thenReturn(paramters);
when(sqlInsertRecognizer.getInsertRows()).thenReturn(insertRows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(insertRows);
}

}
Expand Up @@ -15,15 +15,6 @@
*/
package io.seata.rm.datasource.exec;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.seata.common.exception.ShouldNeverHappenException;
import io.seata.rm.datasource.ConnectionProxy;
import io.seata.rm.datasource.PreparedStatementProxy;
Expand All @@ -44,6 +35,15 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand All @@ -69,6 +69,9 @@ public class MySQLInsertExecutorTest {

private MySQLInsertExecutor insertExecutor;

private final int pkIndex = 0;
private HashMap<String,Integer> pkIndexMap;

@BeforeEach
public void init() {
ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
Expand All @@ -81,6 +84,12 @@ public void init() {
sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
tableMeta = mock(TableMeta.class);
insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));

pkIndexMap = new HashMap<String,Integer>(){
{
put(ID_COLUMN, pkIndex);
}
};
}

@Test
Expand Down Expand Up @@ -165,9 +174,9 @@ public void testGetPkValuesByColumn() throws SQLException {
when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[]{ID_COLUMN}));
List<Object> pkValues = new ArrayList<>();
pkValues.add(PK_VALUE);
doReturn(new HashMap<String,Integer>(){{put(ID_COLUMN,0);}}).when(insertExecutor).getPkIndex();
doReturn(pkIndexMap).when(insertExecutor).getPkIndex();
Map<String,List<Object>> pkValuesList = insertExecutor.getPkValuesByColumn();
Assertions.assertEquals(pkValuesList.get(ID_COLUMN), pkValues);
Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValues);
}

@Test
Expand Down Expand Up @@ -196,11 +205,11 @@ public void testGetPkValuesByColumn_PkValue_Null() throws SQLException {
pkValuesAuto.add(PK_VALUE);
//mock getPkValuesByAuto
doReturn(new HashMap<String,List<Object>>(){{put(ID_COLUMN,pkValuesAuto);}}).when(insertExecutor).getPkValuesByAuto();
doReturn(new HashMap<String,Integer>(){{put(ID_COLUMN,0);}}).when(insertExecutor).getPkIndex();
doReturn(pkIndexMap).when(insertExecutor).getPkIndex();
Map<String,List<Object>> pkValuesList = insertExecutor.getPkValuesByColumn();
//pk value = Null so getPkValuesByAuto
verify(insertExecutor).getPkValuesByAuto();
Assertions.assertEquals(pkValuesList.get(ID_COLUMN), pkValuesAuto);
Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValuesAuto);
}


Expand Down Expand Up @@ -287,7 +296,7 @@ public void testGetPkValuesByAuto_GeneratedKeys_HasResult() throws SQLException
List<Object> pkValues = new ArrayList<>();
pkValues.add(PK_VALUE);
Map<String,List<Object>> pkValuesList = insertExecutor.getPkValuesByAuto();
Assertions.assertEquals(pkValuesList.get(ID_COLUMN), pkValues);
Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValues);
}

@Test
Expand All @@ -308,7 +317,7 @@ public void testGetPkValuesByAuto_ExecuteQuery_HasResult() throws SQLException {
List<Object> pkValues = new ArrayList<>();
pkValues.add(PK_VALUE);
Map<String,List<Object>> pkValuesList = insertExecutor.getPkValuesByAuto();
Assertions.assertEquals(pkValuesList.get(ID_COLUMN), pkValues);
Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValues);
}

@Test
Expand Down Expand Up @@ -351,7 +360,7 @@ public void test_checkPkValuesForMultiPk()
//method is not support at all
pkValues1.clear();
pkValues2.clear();
pkValues1.add(new SqlMethodExpr());
pkValues1.add(SqlMethodExpr.get());
pkValues2.add(2);
Assertions.assertFalse(insertExecutor.checkPkValuesForMultiPk(pkValues));

Expand Down Expand Up @@ -380,12 +389,12 @@ public void test_checkPkValues() {
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));

pkValues = new ArrayList<>();
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));

pkValues = new ArrayList<>();
pkValues.add(new SqlMethodExpr());
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
pkValues.add(SqlMethodExpr.get());
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, true));

pkValues = new ArrayList<>();
Expand Down Expand Up @@ -426,12 +435,12 @@ public void test_checkPkValues() {
Assertions.assertTrue(insertExecutor.checkPkValuesForSinglePk(pkValues, false));

pkValues = new ArrayList<>();
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));

pkValues = new ArrayList<>();
pkValues.add(new SqlMethodExpr());
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));

pkValues = new ArrayList<>();
Expand Down Expand Up @@ -465,12 +474,12 @@ public void test_checkPkValues() {

pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));

pkValues = new ArrayList<>();
pkValues.add(1);
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));

pkValues = new ArrayList<>();
Expand All @@ -495,12 +504,12 @@ public void test_checkPkValues() {

pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));

pkValues = new ArrayList<>();
pkValues.add(Null.get());
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));


Expand All @@ -525,22 +534,22 @@ public void test_checkPkValues() {
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));

pkValues = new ArrayList<>();
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));

pkValues = new ArrayList<>();
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
pkValues.add(new SqlSequenceExpr());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));

pkValues = new ArrayList<>();
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));

pkValues = new ArrayList<>();
pkValues.add(new SqlMethodExpr());
pkValues.add(SqlMethodExpr.get());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));

Expand All @@ -550,6 +559,7 @@ public void test_checkPkValues() {
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, true));

pkValues = new ArrayList<>();
pkValues.add(SqlMethodExpr.get());
pkValues.add(new SqlSequenceExpr());
pkValues.add(SqlDefaultExpr.get());
Assertions.assertFalse(insertExecutor.checkPkValuesForSinglePk(pkValues, false));
Expand Down Expand Up @@ -613,6 +623,6 @@ private void mockParametersOfOnePk() {
private void mockInsertRows() {
List<List<Object>> rows = new ArrayList<>();
rows.add(Arrays.asList("?", "?", "?", "?"));
when(sqlInsertRecognizer.getInsertRows()).thenReturn(rows);
when(sqlInsertRecognizer.getInsertRows(pkIndexMap.values())).thenReturn(rows);
}
}