Search in sources :

Example 6 with DalCommand

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());
}
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 7 with DalCommand

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

Example 8 with DalCommand

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());
}
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 9 with DalCommand

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

Example 10 with DalCommand

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);
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalClient(com.ctrip.platform.dal.dao.DalClient)

Aggregations

DalClient (com.ctrip.platform.dal.dao.DalClient)11 DalCommand (com.ctrip.platform.dal.dao.DalCommand)11 DalHints (com.ctrip.platform.dal.dao.DalHints)10 SQLException (java.sql.SQLException)9 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)7 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)6 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)3 DalScalarExtractor (com.ctrip.platform.dal.dao.helper.DalScalarExtractor)2 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 ResultSet (java.sql.ResultSet)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1