Search in sources :

Example 1 with DalClient

use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.

the class DalShardingHelperTest method testDetectDistributedTransactionStringDalHintsListOfMapOfStringQ.

//@Test
public void testDetectDistributedTransactionStringDalHintsListOfMapOfStringQ() {
    //;columns=id;mod=2
    final String logicDbName = "dao_test_mod";
    final List<Map<String, ?>> daoPojos = new ArrayList<>();
    DalClient client;
    String shardId;
    Set<String> shardIds = new HashSet<>();
    // Not in transaction
    try {
        shardIds.add("1");
        shardIds.add("0");
        DalShardingHelper.detectDistributedTransaction(shardIds);
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Shard ids is null
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(null);
                return false;
            }
        }, new DalHints().inShard("0"));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // More than 1 shards
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                Set<String> shardIds = new HashSet<>();
                shardIds.add("1");
                shardIds.add("0");
                DalShardingHelper.detectDistributedTransaction(shardIds);
                return false;
            }
        }, new DalHints().inShard("0"));
        fail();
    } catch (SQLException e) {
    }
    // Not same shard
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                Set<String> shardIds = new HashSet<>();
                shardIds.add("1");
                DalShardingHelper.detectDistributedTransaction(shardIds);
                return false;
            }
        }, new DalHints().inShard("0"));
        fail();
    } catch (SQLException e) {
    }
    // Not same shard
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                Set<String> shardIds = new HashSet<>();
                shardIds.add("0");
                DalShardingHelper.detectDistributedTransaction(shardIds);
                return false;
            }
        }, new DalHints().inShard("0"));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) Set(java.util.Set) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with DalClient

use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.

the class DalShardingHelperTest method testDetectDistributedTransactionSetOfString.

@Test
public void testDetectDistributedTransactionSetOfString() {
    DalClient client;
    // Not a shard enabled DB
    try {
        DalShardingHelper.detectDistributedTransaction("HA_Test_1", null, null);
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    //;columns=id;mod=2
    final String logicDbName = "dao_test_mod";
    // Not in transaction
    try {
        DalShardingHelper.detectDistributedTransaction(logicDbName, null, null);
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Shard is not same
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(logicDbName, new DalHints().inShard(0), null);
                return false;
            }
        }, new DalHints().inShard(1));
        fail();
    } catch (SQLException e) {
    }
    // Shard is same
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(logicDbName, new DalHints().inShard(1), null);
                return false;
            }
        }, new DalHints().inShard(1));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // can not decide and not same
    final List<Map<String, ?>> daoPojos = new ArrayList<>();
    Map<String, Object> pojo = new HashMap<>();
    pojo.put("id", 0);
    daoPojos.add(pojo);
    pojo = new HashMap<>();
    pojo.put("id", 1);
    daoPojos.add(pojo);
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(logicDbName, new DalHints(), daoPojos);
                return false;
            }
        }, new DalHints().inShard(1));
        fail();
    } catch (SQLException e) {
    }
    // can not decide and same
    daoPojos.clear();
    pojo = new HashMap<>();
    pojo.put("id", 1);
    daoPojos.add(pojo);
    pojo = new HashMap<>();
    pojo.put("id", 1);
    daoPojos.add(pojo);
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.detectDistributedTransaction(logicDbName, new DalHints(), daoPojos);
                return false;
            }
        }, new DalHints().inShard(1));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with DalClient

use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.

the class DalColumnMapRowMapperTest method testDalColumnMapRowMapperEmpty.

@Test
public void testDalColumnMapRowMapperEmpty() {
    try {
        StatementParameters parameters = new StatementParameters();
        DalClient client = DalClientFactory.getClient(DATABASE_NAME);
        List<Map<String, Object>> result1 = client.query(sqlNoResult, parameters, new DalHints(), new DalRowMapperExtractor<Map<String, Object>>(new DalColumnMapRowMapper()));
        assertEquals(0, result1.size());
        DalQueryDao dao = new DalQueryDao(DATABASE_NAME);
        List<Map<String, Object>> result = dao.query(sqlNoResult, parameters, hints, new DalColumnMapRowMapper());
        assertEquals(0, result.size());
    } catch (Exception e) {
        fail();
    }
}
Also used : DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalColumnMapRowMapper(com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper) DalQueryDao(com.ctrip.platform.dal.dao.DalQueryDao) Map(java.util.Map) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 4 with DalClient

use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.

the class DalDirectClientTestStub method executeTestWithMultipleCommandsAllSuccessed.

/**
	 * Test execute multiple commands and all successes.
	 * @throws SQLException
	 */
@Test
public void executeTestWithMultipleCommandsAllSuccessed() throws SQLException {
    final DalHints hints = new DalHints();
    List<DalCommand> commands = new ArrayList<DalCommand>();
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return true;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 2";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return true;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 3";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return true;
        }
    });
    client.execute(commands, hints);
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(0, models.size());
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalHints(com.ctrip.platform.dal.dao.DalHints) DalClient(com.ctrip.platform.dal.dao.DalClient) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with DalClient

use of com.ctrip.platform.dal.dao.DalClient in project dal by ctripcorp.

the class MarkdownAndUpIntergration method testQuery.

private void testQuery(String logicName) throws SQLException {
    DalClient client = DalClientFactory.getClient(logicName);
    client.query("SELECT 1", new StatementParameters(), new DalHints(), new DalResultSetExtractor<Integer>() {

        @Override
        public Integer extract(ResultSet rs) throws SQLException {
            return null;
        }
    });
}
Also used : DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ResultSet(java.sql.ResultSet)

Aggregations

DalClient (com.ctrip.platform.dal.dao.DalClient)15 DalHints (com.ctrip.platform.dal.dao.DalHints)13 SQLException (java.sql.SQLException)12 DalCommand (com.ctrip.platform.dal.dao.DalCommand)11 Test (org.junit.Test)11 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)9 ArrayList (java.util.ArrayList)7 Map (java.util.Map)4 DalScalarExtractor (com.ctrip.platform.dal.dao.helper.DalScalarExtractor)3 HashMap (java.util.HashMap)3 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)3 ResultSet (java.sql.ResultSet)2 DalQueryDao (com.ctrip.platform.dal.dao.DalQueryDao)1 ConnectionAction (com.ctrip.platform.dal.dao.client.ConnectionAction)1 DalTransactionListener (com.ctrip.platform.dal.dao.client.DalTransactionListener)1 DalTransactionManager (com.ctrip.platform.dal.dao.client.DalTransactionManager)1 DalColumnMapRowMapper (com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1