Search in sources :

Example 26 with TableMeta

use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.

the class MysqlTableMetaCacheTest method refreshTest_0.

@Test
public void refreshTest_0() throws SQLException {
    MockDriver mockDriver = new MockDriver(columnMetas, indexMetas);
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl("jdbc:mock:xxx");
    druidDataSource.setDriver(mockDriver);
    DataSourceProxy dataSourceProxy = new DataSourceProxy(druidDataSource);
    TableMeta tableMeta = getTableMetaCache().getTableMeta(dataSourceProxy.getPlainConnection(), "t1", dataSourceProxy.getResourceId());
    // change the columns meta
    columnMetas = new Object[][] { new Object[] { "", "", "mt1", "id", Types.INTEGER, "INTEGER", 64, 0, 10, 1, "", "", 0, 0, 64, 1, "NO", "YES" }, new Object[] { "", "", "mt1", "name1", Types.VARCHAR, "VARCHAR", 65, 0, 10, 0, "", "", 0, 0, 64, 2, "YES", "NO" }, new Object[] { "", "", "mt1", "name2", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 3, "YES", "NO" }, new Object[] { "", "", "mt1", "name3", Types.VARCHAR, "VARCHAR", 64, 0, 10, 0, "", "", 0, 0, 64, 4, "YES", "NO" } };
    mockDriver.setMockColumnsMetasReturnValue(columnMetas);
    getTableMetaCache().refresh(dataSourceProxy.getPlainConnection(), dataSourceProxy.getResourceId());
}
Also used : MockDriver(io.seata.rm.datasource.mock.MockDriver) DataSourceProxy(io.seata.rm.datasource.DataSourceProxy) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) DruidDataSource(com.alibaba.druid.pool.DruidDataSource) Test(org.junit.jupiter.api.Test)

Example 27 with TableMeta

use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.

the class DataCompareUtilsTest method isRecordsEquals.

@Test
public void isRecordsEquals() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "pk" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
    TableRecords beforeImage = new TableRecords();
    beforeImage.setTableName("table_name");
    beforeImage.setTableMeta(tableMeta);
    List<Row> rows = new ArrayList<>();
    Row row = new Row();
    Field field01 = addField(row, "pk", 1, "12345");
    Field field02 = addField(row, "age", 1, "18");
    rows.add(row);
    beforeImage.setRows(rows);
    Assertions.assertFalse(DataCompareUtils.isRecordsEquals(beforeImage, null).getResult());
    Assertions.assertFalse(DataCompareUtils.isRecordsEquals(null, beforeImage).getResult());
    TableRecords afterImage = new TableRecords();
    // wrong table name
    afterImage.setTableName("table_name1");
    afterImage.setTableMeta(tableMeta);
    Assertions.assertFalse(DataCompareUtils.isRecordsEquals(beforeImage, afterImage).getResult());
    afterImage.setTableName("table_name");
    Assertions.assertFalse(DataCompareUtils.isRecordsEquals(beforeImage, afterImage).getResult());
    List<Row> rows2 = new ArrayList<>();
    Row row2 = new Row();
    Field field11 = addField(row2, "pk", 1, "12345");
    Field field12 = addField(row2, "age", 1, "18");
    rows2.add(row2);
    afterImage.setRows(rows2);
    Assertions.assertTrue(DataCompareUtils.isRecordsEquals(beforeImage, afterImage).getResult());
    field11.setValue("23456");
    Assertions.assertFalse(DataCompareUtils.isRecordsEquals(beforeImage, afterImage).getResult());
    field11.setValue("12345");
    field12.setName("sex");
    Assertions.assertFalse(DataCompareUtils.isRecordsEquals(beforeImage, afterImage).getResult());
    field12.setName("age");
    field12.setValue("19");
    Assertions.assertFalse(DataCompareUtils.isRecordsEquals(beforeImage, afterImage).getResult());
    field12.setName("18");
    Field field3 = new Field("pk", 1, "12346");
    Row row3 = new Row();
    row3.add(field3);
    rows2.add(row3);
    Assertions.assertFalse(DataCompareUtils.isRecordsEquals(beforeImage, afterImage).getResult());
    beforeImage.setRows(new ArrayList<>());
    afterImage.setRows(new ArrayList<>());
    Assertions.assertTrue(DataCompareUtils.isRecordsEquals(beforeImage, afterImage).getResult());
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(io.seata.rm.datasource.sql.struct.Field) ArrayList(java.util.ArrayList) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) Test(org.junit.jupiter.api.Test)

Example 28 with TableMeta

use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.

the class DataCompareUtilsTest method isRowsEquals.

@Test
public void isRowsEquals() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "pk" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
    List<Row> rows = new ArrayList<>();
    Field field = new Field("pk", 1, "12345");
    Row row = new Row();
    row.add(field);
    rows.add(row);
    Assertions.assertFalse(DataCompareUtils.isRowsEquals(tableMeta, rows, null).getResult());
    Assertions.assertFalse(DataCompareUtils.isRowsEquals(tableMeta, null, rows).getResult());
    List<Row> rows2 = new ArrayList<>();
    Field field2 = new Field("pk", 1, "12345");
    Row row2 = new Row();
    row2.add(field2);
    rows2.add(row2);
    Assertions.assertTrue(DataCompareUtils.isRowsEquals(tableMeta, rows, rows2).getResult());
    field.setValue("23456");
    Assertions.assertFalse(DataCompareUtils.isRowsEquals(tableMeta, rows, rows2).getResult());
    field.setValue("12345");
    Field field3 = new Field("pk", 1, "12346");
    Row row3 = new Row();
    row3.add(field3);
    rows2.add(row3);
    Assertions.assertFalse(DataCompareUtils.isRowsEquals(tableMeta, rows, rows2).getResult());
}
Also used : Field(io.seata.rm.datasource.sql.struct.Field) ArrayList(java.util.ArrayList) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) Test(org.junit.jupiter.api.Test)

Example 29 with TableMeta

use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.

the class AbstractTableMetaCache method refresh.

@Override
public void refresh(final Connection connection, String resourceId) {
    ConcurrentMap<String, TableMeta> tableMetaMap = TABLE_META_CACHE.asMap();
    for (Map.Entry<String, TableMeta> entry : tableMetaMap.entrySet()) {
        String key = getCacheKey(connection, entry.getValue().getTableName(), resourceId);
        if (entry.getKey().equals(key)) {
            try {
                TableMeta tableMeta = fetchSchema(connection, entry.getValue().getTableName());
                if (!tableMeta.equals(entry.getValue())) {
                    TABLE_META_CACHE.put(entry.getKey(), tableMeta);
                    LOGGER.info("table meta change was found, update table meta cache automatically.");
                }
            } catch (SQLException e) {
                LOGGER.error("get table meta error:{}", e.getMessage(), e);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 30 with TableMeta

use of io.seata.rm.datasource.sql.struct.TableMeta 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;
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) ResultSet(java.sql.ResultSet) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) IndexMeta(io.seata.rm.datasource.sql.struct.IndexMeta) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta)

Aggregations

TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)38 ArrayList (java.util.ArrayList)15 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)14 Test (org.junit.jupiter.api.Test)12 Row (io.seata.rm.datasource.sql.struct.Row)11 Field (io.seata.rm.datasource.sql.struct.Field)8 ResultSet (java.sql.ResultSet)7 DataSourceProxy (io.seata.rm.datasource.DataSourceProxy)6 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)6 DruidDataSource (com.alibaba.druid.pool.DruidDataSource)5 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)5 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)5 MockDriver (io.seata.rm.datasource.mock.MockDriver)5 SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)5 PreparedStatement (java.sql.PreparedStatement)5 List (java.util.List)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)4 ColumnMeta (io.seata.rm.datasource.sql.struct.ColumnMeta)4 MySQLInsertExecutor (io.seata.rm.datasource.exec.mysql.MySQLInsertExecutor)3