use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.
the class MySQLInsertExecutorTest method testGetPkValuesByAuto_ExecuteQuery_HasResult.
@Test
public void testGetPkValuesByAuto_ExecuteQuery_HasResult() throws SQLException {
doReturn(tableMeta).when(insertExecutor).getTableMeta();
ColumnMeta columnMeta = mock(ColumnMeta.class);
Map<String, ColumnMeta> columnMetaMap = new HashMap<>();
columnMetaMap.put(ID_COLUMN, columnMeta);
when(columnMeta.isAutoincrement()).thenReturn(true);
when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap);
PreparedStatement preparedStatement = mock(PreparedStatement.class);
when(statementProxy.getTargetStatement()).thenReturn(preparedStatement);
when(statementProxy.getGeneratedKeys()).thenThrow(new SQLException("", MySQLInsertExecutor.ERR_SQL_STATE));
ResultSet resultSet = mock(ResultSet.class);
when(preparedStatement.executeQuery(anyString())).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true).thenReturn(false);
when(resultSet.getObject(1)).thenReturn(PK_VALUE);
List<Object> pkValues = new ArrayList<>();
pkValues.add(PK_VALUE);
Map<String, List<Object>> pkValuesList = insertExecutor.getPkValuesByAuto();
Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValues);
}
use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.
the class MySQLInsertExecutorTest method testGetPkValuesByAuto_SQLException_WarnLog.
@Test
public void testGetPkValuesByAuto_SQLException_WarnLog() throws SQLException {
doReturn(tableMeta).when(insertExecutor).getTableMeta();
ColumnMeta columnMeta = mock(ColumnMeta.class);
Map<String, ColumnMeta> columnMetaMap = new HashMap<>();
columnMetaMap.put(ID_COLUMN, columnMeta);
when(columnMeta.isAutoincrement()).thenReturn(true);
when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap);
PreparedStatement preparedStatement = mock(PreparedStatement.class);
when(statementProxy.getTargetStatement()).thenReturn(preparedStatement);
SQLException e = new SQLException("test warn log", MySQLInsertExecutor.ERR_SQL_STATE, 1);
when(statementProxy.getGeneratedKeys()).thenThrow(e);
ResultSet genKeys = mock(ResultSet.class);
when(statementProxy.getTargetStatement().executeQuery("SELECT LAST_INSERT_ID()")).thenReturn(genKeys);
Map<String, List<Object>> pkValueMap = insertExecutor.getPkValuesByAuto();
Assertions.assertTrue(pkValueMap.get(ID_COLUMN).isEmpty());
}
use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.
the class BaseH2Test method mockTableMeta.
protected static TableMeta mockTableMeta() {
TableMeta tableMeta = Mockito.mock(TableMeta.class);
Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "ID" }));
Mockito.when(tableMeta.getEscapePkNameList("h2")).thenReturn(Arrays.asList(new String[] { "ID" }));
Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
ColumnMeta meta0 = Mockito.mock(ColumnMeta.class);
Mockito.when(meta0.getDataType()).thenReturn(Types.INTEGER);
Mockito.when(meta0.getColumnName()).thenReturn("ID");
Mockito.when(tableMeta.getColumnMeta("ID")).thenReturn(meta0);
ColumnMeta meta1 = Mockito.mock(ColumnMeta.class);
Mockito.when(meta1.getDataType()).thenReturn(Types.VARCHAR);
Mockito.when(meta1.getColumnName()).thenReturn("NAME");
Mockito.when(tableMeta.getColumnMeta("NAME")).thenReturn(meta1);
return tableMeta;
}
use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.
the class MockResultSet method mockResultSetMetaData.
public void mockResultSetMetaData(Object[][] mockColumnsMetasReturnValue) {
for (Object[] meta : mockColumnsMetasReturnValue) {
ColumnMeta columnMeta = new ColumnMeta();
columnMeta.setTableName(meta[2].toString());
columnMeta.setColumnName(meta[3].toString());
this.columnMetas.add(columnMeta);
}
}
use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.
the class OracleTableMetaCache method resultSetMetaToSchema.
private TableMeta resultSetMetaToSchema(DatabaseMetaData dbmd, String tableName) throws SQLException {
TableMeta tm = new TableMeta();
tm.setTableName(tableName);
String[] schemaTable = tableName.split("\\.");
String schemaName = schemaTable.length > 1 ? schemaTable[0] : dbmd.getUserName();
tableName = schemaTable.length > 1 ? schemaTable[1] : tableName;
if (schemaName.contains("\"")) {
schemaName = schemaName.replace("\"", "");
} else {
schemaName = schemaName.toUpperCase();
}
if (tableName.contains("\"")) {
tableName = tableName.replace("\"", "");
} else {
tableName = tableName.toUpperCase();
}
try (ResultSet rsColumns = dbmd.getColumns("", schemaName, tableName, "%");
ResultSet rsIndex = dbmd.getIndexInfo(null, schemaName, tableName, false, true);
ResultSet rsPrimary = dbmd.getPrimaryKeys(null, schemaName, tableName)) {
while (rsColumns.next()) {
ColumnMeta col = new ColumnMeta();
col.setTableCat(rsColumns.getString("TABLE_CAT"));
col.setTableSchemaName(rsColumns.getString("TABLE_SCHEM"));
col.setTableName(rsColumns.getString("TABLE_NAME"));
col.setColumnName(rsColumns.getString("COLUMN_NAME"));
col.setDataType(rsColumns.getInt("DATA_TYPE"));
col.setDataTypeName(rsColumns.getString("TYPE_NAME"));
col.setColumnSize(rsColumns.getInt("COLUMN_SIZE"));
col.setDecimalDigits(rsColumns.getInt("DECIMAL_DIGITS"));
col.setNumPrecRadix(rsColumns.getInt("NUM_PREC_RADIX"));
col.setNullAble(rsColumns.getInt("NULLABLE"));
col.setRemarks(rsColumns.getString("REMARKS"));
col.setColumnDef(rsColumns.getString("COLUMN_DEF"));
col.setSqlDataType(rsColumns.getInt("SQL_DATA_TYPE"));
col.setSqlDatetimeSub(rsColumns.getInt("SQL_DATETIME_SUB"));
col.setCharOctetLength(rsColumns.getInt("CHAR_OCTET_LENGTH"));
col.setOrdinalPosition(rsColumns.getInt("ORDINAL_POSITION"));
col.setIsNullAble(rsColumns.getString("IS_NULLABLE"));
tm.getAllColumns().put(col.getColumnName(), col);
}
while (rsIndex.next()) {
String indexName = rsIndex.getString("INDEX_NAME");
if (StringUtils.isNullOrEmpty(indexName)) {
continue;
}
String colName = rsIndex.getString("COLUMN_NAME");
ColumnMeta col = tm.getAllColumns().get(colName);
if (tm.getAllIndexes().containsKey(indexName)) {
IndexMeta index = tm.getAllIndexes().get(indexName);
index.getValues().add(col);
} else {
IndexMeta index = new IndexMeta();
index.setIndexName(indexName);
index.setNonUnique(rsIndex.getBoolean("NON_UNIQUE"));
index.setIndexQualifier(rsIndex.getString("INDEX_QUALIFIER"));
index.setIndexName(rsIndex.getString("INDEX_NAME"));
index.setType(rsIndex.getShort("TYPE"));
index.setOrdinalPosition(rsIndex.getShort("ORDINAL_POSITION"));
index.setAscOrDesc(rsIndex.getString("ASC_OR_DESC"));
index.setCardinality(rsIndex.getInt("CARDINALITY"));
index.getValues().add(col);
if (!index.isNonUnique()) {
index.setIndextype(IndexType.UNIQUE);
} else {
index.setIndextype(IndexType.NORMAL);
}
tm.getAllIndexes().put(indexName, index);
}
}
while (rsPrimary.next()) {
String pkIndexName = rsPrimary.getString("PK_NAME");
if (tm.getAllIndexes().containsKey(pkIndexName)) {
IndexMeta index = tm.getAllIndexes().get(pkIndexName);
index.setIndextype(IndexType.PRIMARY);
}
}
if (tm.getAllIndexes().isEmpty()) {
throw new ShouldNeverHappenException(String.format("Could not found any index in the table: %s", tableName));
}
}
return tm;
}
Aggregations