use of com.ctrip.platform.dal.dao.DalCommand 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.DalCommand 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());
}
use of com.ctrip.platform.dal.dao.DalCommand in project dal by ctripcorp.
the class DalDirectClientTestStub method executeTestWithMultipleCommandsNotAllSuccessed.
/**
* Test execute multiple commands and not all successes.
* If one failed, the after commands will be dropped.
* @throws SQLException
*/
@Test
public void executeTestWithMultipleCommandsNotAllSuccessed() 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 false;
}
});
commands.add(new DalCommand() {
@Override
public boolean execute(DalClient client) throws SQLException {
String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 100";
StatementParameters parameters = new StatementParameters();
return client.update(sql, parameters, hints) == 1;
}
});
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();
return client.update(sql, parameters, hints) == 1;
}
});
client.execute(commands, hints);
List<ClientTestModel> models = this.queryModelsByIds();
Assert.assertEquals(2, models.size());
}
use of com.ctrip.platform.dal.dao.DalCommand in project dal by ctripcorp.
the class DalConcurrentMysqlTest 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());
}
use of com.ctrip.platform.dal.dao.DalCommand in project dal by ctripcorp.
the class DalDirectClient method execute.
@Override
public void execute(final List<DalCommand> commands, final DalHints hints) throws SQLException {
final DalClient client = this;
ConnectionAction<?> action = new ConnectionAction<Object>() {
@Override
public Object execute() throws Exception {
for (DalCommand cmd : commands) {
if (!cmd.execute(client))
break;
}
return null;
}
};
action.populate(commands);
doInTransaction(action, hints);
}
Aggregations