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);
}
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();
}
}
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) {
}
}
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());
}
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());
}
Aggregations