Search in sources :

Example 6 with DalClient

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

the class DalDirectClient method execute.

@Override
public void execute(DalCommand command, DalHints hints) throws SQLException {
    final DalClient client = this;
    ConnectionAction<?> action = new ConnectionAction<Object>() {

        @Override
        public Object execute() throws Exception {
            command.execute(client);
            return null;
        }
    };
    action.populate(command);
    doInTransaction(action, hints);
}
Also used : DalClient(com.ctrip.platform.dal.dao.DalClient)

Example 7 with DalClient

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

the class DalShardingHelperTest method testIsAlreadySharded.

@Test
public void testIsAlreadySharded() {
    DalClient client;
    try {
        // No shard enabled
        assertTrue(DalShardingHelper.isAlreadySharded("HA_Test_1", null, null));
    } catch (SQLException e) {
        e.printStackTrace();
    }
    //is in transaction
    client = DalClientFactory.getClient("dao_test_sqlsvr_tableShard");
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                assertTrue(DalShardingHelper.isAlreadySharded("dao_test_sqlsvr_tableShard", "dal_client_test", null));
                return false;
            }
        }, new DalHints().inShard("0"));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Test shard by DB
    try {
        assertFalse(DalShardingHelper.isAlreadySharded("dao_test_mod", null, new DalHints()));
        assertTrue(DalShardingHelper.isAlreadySharded("dao_test_mod", null, new DalHints().inShard("0")));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Test shard by table
    try {
        assertFalse(DalShardingHelper.isAlreadySharded("dao_test_sqlsvr_tableShard", "dal_client_test", new DalHints()));
        assertTrue(DalShardingHelper.isAlreadySharded("dao_test_sqlsvr_tableShard", "dal_client_test", new DalHints().inTableShard("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) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 8 with DalClient

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

the class DalShardingHelperTest method testShuffle.

@Test
public void testShuffle() {
    //;columns=id;mod=2
    final String logicDbName = "dao_test_mod";
    final List<Map<String, ?>> daoPojos = new ArrayList<>();
    DalClient client;
    String shardId;
    Map<String, Object> pojo = new HashMap<>();
    pojo.put("id", 0);
    daoPojos.add(pojo);
    pojo = new HashMap<>();
    pojo.put("id", 1);
    daoPojos.add(pojo);
    // Test by pojos
    try {
        shardId = null;
        Map<String, Map<Integer, Map<String, ?>>> shuffled = DalShardingHelper.shuffle(logicDbName, shardId, daoPojos);
        assertEquals(2, shuffled.size());
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Test preset shardid
    try {
        shardId = "0";
        Map<String, Map<Integer, Map<String, ?>>> shuffled = DalShardingHelper.shuffle(logicDbName, shardId, daoPojos);
        assertEquals(1, shuffled.size());
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // same shard
    client = DalClientFactory.getClient(logicDbName);
    try {
        assertFalse(DalTransactionManager.isInTransaction());
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.shuffle(logicDbName, "0", daoPojos);
                return false;
            }
        }, new DalHints().inShard("0"));
    } catch (SQLException e) {
        e.printStackTrace();
        fail();
    }
    // Detect in different shard with two shard ids
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

            public boolean execute(DalClient client) throws SQLException {
                DalShardingHelper.shuffle(logicDbName, null, daoPojos);
                return false;
            }
        }, new DalHints().inShard("0"));
        fail();
    } catch (SQLException e) {
    }
    // Detect in different shard with one shard ids
    client = DalClientFactory.getClient(logicDbName);
    try {
        client.execute(new DalCommand() {

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

Example 9 with DalClient

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

the class DalDirectClientTestStub method executeTestWithOneCommand.

/**
	 * Test execute command function
	 * @throws SQLException
	 */
@Test
public void executeTestWithOneCommand() throws SQLException {
    final DalHints hints = new DalHints();
    DalCommand command = new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    };
    client.execute(command, hints);
    List<ClientTestModel> models = this.queryModelsByIds(1);
    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) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 10 with DalClient

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

the class DalConcurrentSqlServerTestStub method testExecute.

@Test
public void testExecute() throws SQLException {
    List<DalCommand> commands = new ArrayList<DalCommand>();
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "INSERT INTO " + TABLE_NAME + "(quantity,type,address)" + " VALUES(10, 1, 'SH INFO')";
            int ret = client.update(sql, new StatementParameters(), new DalHints());
            if (ret > 0) {
                System.out.println("insert success.");
            } else {
                System.out.println("insert failed.");
            }
            return ret > 0;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE address = '" + "SH INFO'";
            int ret = client.update(sql, new StatementParameters(), new DalHints());
            if (ret > 0) {
                System.out.println("delete success");
            } else {
                System.out.println("delete failed");
            }
            return ret > 0;
        }
    });
    client.execute(commands, new DalHints());
    String sql = "SELECT count(1) from " + TABLE_NAME;
    Number count = (Number) client.query(sql, new StatementParameters(), new DalHints(), new DalScalarExtractor());
    Assert.assertEquals(INSERT_COUNT, count.intValue());
}
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) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalScalarExtractor(com.ctrip.platform.dal.dao.helper.DalScalarExtractor) Test(org.junit.Test)

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