Search in sources :

Example 51 with ClientTestModel

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

the class DalDirectClientTestStub method queryModelsByIds.

/**
 * Get the models all in dal_client_test by specified IDs
 *
 * @param ids
 * @return The list of ClientTestModel
 */
private List<ClientTestModel> queryModelsByIds(int... ids) {
    List<ClientTestModel> models = new ArrayList<ClientTestModel>();
    String querySql = "";
    if (null != ids && ids.length > 0) {
        Integer[] idds = new Integer[ids.length];
        for (int i = 0; i < idds.length; i++) {
            idds[i] = ids[i];
        }
        querySql = "SELECT * FROM %s WHERE id in(%s)";
        String inClause = StringUtils.join(idds, ",");
        querySql = String.format(querySql, TABLE_NAME, inClause);
    } else {
        querySql = "SELECT * FROM " + TABLE_NAME;
    }
    StatementParameters parameters = new StatementParameters();
    DalHints hints = new DalHints();
    try {
        models = client.query(querySql, parameters, hints, new DalRowMapperExtractor<ClientTestModel>(mapper));
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return models;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalRowMapperExtractor(com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)

Example 52 with ClientTestModel

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

the class DalDirectClientTestStub method callTestForSpWithOutParameter.

/**
 * Test the call function with out parameters
 * @throws SQLException
 */
@Test
public void callTestForSpWithOutParameter() throws SQLException {
    String callSql = diff.category == DatabaseCategory.SqlServer ? "{call " + SP_WITH_OUT_PARAM + "(?,?)}" : "call " + SP_WITH_OUT_PARAM + "(?,?)";
    StatementParameters parameters = new StatementParameters();
    parameters.set("v_id", Types.INTEGER, 1);
    parameters.registerOut("count", Types.INTEGER);
    DalHints hints = new DalHints();
    Map<String, ?> res = client.call(callSql, parameters, hints);
    Assert.assertTrue(null != res);
    Assert.assertEquals(1, res.size());
    Assert.assertEquals(2, ((Number) res.get("count")).intValue());
    List<ClientTestModel> models = this.queryModelsByIds(1);
    Assert.assertEquals(0, models.size());
    List<ClientTestModel> models_d = this.queryModelsByIds();
    Assert.assertEquals(2, models_d.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 53 with ClientTestModel

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

the class DalDirectClientTestStub method batchCallTestWithParametersForSpWithoutOutParameter.

/**
 * Test batch call with parameters but has no ResultParameters
 * @throws SQLException
 */
@Test
public void batchCallTestWithParametersForSpWithoutOutParameter() throws SQLException {
    String callSql = "{call " + SP_WITHOUT_OUT_PARAM + "(?,?,?,?)}";
    StatementParameters[] parametersList = new StatementParameters[7];
    DalHints hints = new DalHints();
    for (int i = 0; i < 7; i++) {
        StatementParameters parameters = new StatementParameters();
        parameters.set("v_id", Types.INTEGER, null);
        parameters.set("v_quantity", Types.INTEGER, 10 + i);
        parameters.set("v_type", Types.SMALLINT, 3);
        parameters.set("v_address", Types.VARCHAR, "SZ INFO" + "_" + i);
        parametersList[i] = parameters;
    }
    int[] res = client.batchCall(callSql, parametersList, hints);
    Assert.assertEquals(7, res.length);
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(10, models.size());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) Test(org.junit.Test)

Example 54 with ClientTestModel

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

the class DalDirectClientSqlServerTest method queryModelsByIds.

/**
 * Get the models all in dal_client_test by specified IDs
 *
 * @param ids
 * @return The list of ClientTestModel
 */
private List<ClientTestModel> queryModelsByIds(int... ids) {
    List<ClientTestModel> models = new ArrayList<ClientTestModel>();
    String querySql = "";
    if (null != ids && ids.length > 0) {
        Integer[] idds = new Integer[ids.length];
        for (int i = 0; i < idds.length; i++) {
            idds[i] = ids[i];
        }
        querySql = "SELECT * FROM %s WHERE id in(%s)";
        String inClause = StringUtils.join(idds, ",");
        querySql = String.format(querySql, TABLE_NAME, inClause);
    } else {
        querySql = "SELECT * FROM " + TABLE_NAME;
    }
    StatementParameters parameters = new StatementParameters();
    DalHints hints = new DalHints();
    try {
        models = client.query(querySql, parameters, hints, new DalRowMapperExtractor<ClientTestModel>(mapper));
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return models;
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalRowMapperExtractor(com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)

Example 55 with ClientTestModel

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

the class DalDirectClientSqlServerTest method execTestForSpWithInOutParameters.

@Test
public void execTestForSpWithInOutParameters() throws SQLException {
    String callSql = "exec " + SP_WITH_IN_OUT_PARAM + " @v_id=?, @v_quantity=?, @v_type=?, @v_address=?";
    // set parameter by name
    StatementParameters parameters = new StatementParameters();
    parameters.set("v_id", Types.INTEGER, 1);
    parameters.set("v_quantity", Types.INTEGER, 10);
    parameters.set("v_type", Types.SMALLINT, 3);
    parameters.registerInOut("v_address", Types.VARCHAR, "SZ INFO");
    DalHints hints = new DalHints();
    Map<String, ?> res = client.call(callSql, parameters, hints);
    Assert.assertTrue(null != res);
    Assert.assertEquals(1, res.size());
    Assert.assertTrue(res.containsKey("v_address"));
    Assert.assertEquals("output", res.get("v_address"));
    List<ClientTestModel> models = this.queryModelsByIds(1);
    Assert.assertEquals(1, models.size());
    Assert.assertEquals("SZ INFO", models.get(0).getAddress());
    // set parameter by index
    StatementParameters parameters2 = new StatementParameters();
    int index = 1;
    parameters2.set(index++, Types.INTEGER, 2);
    parameters2.set(index++, Types.INTEGER, 10);
    parameters2.set(index++, Types.SMALLINT, 3);
    parameters2.registerInOut(index++, Types.VARCHAR, "SZ INFO");
    res = client.call(callSql, parameters2, hints);
    Assert.assertTrue(null != res);
    Assert.assertEquals(1, res.size());
    Assert.assertEquals("output", parameters.get(3).getValue());
    // set parameter with disordered index
    StatementParameters parameters3 = new StatementParameters();
    index = 1;
    parameters3.registerInOut(index++, Types.VARCHAR, "SZ INFO");
    parameters3.set(index++, Types.INTEGER, 3);
    parameters3.set(index++, Types.INTEGER, 10);
    parameters3.set(index++, Types.SMALLINT, 3);
    try {
        client.call(callSql, parameters3, hints);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("was not declared as an OUTPUT parameter"));
    }
    // set parameter by disordered name
    StatementParameters parameters4 = new StatementParameters();
    parameters4.registerInOut("v_address", Types.VARCHAR, "SZ INFO");
    parameters4.set("v_id", Types.INTEGER, 3);
    parameters4.set("v_quantity", Types.INTEGER, 10);
    parameters4.set("v_type", Types.SMALLINT, 3);
    try {
        res = client.call(callSql, parameters4, hints);
    } catch (Exception e) {
        Assert.fail();
    }
    Assert.assertTrue(null != res);
    Assert.assertEquals(1, res.size());
    Assert.assertTrue(res.containsKey("v_address"));
    Assert.assertEquals("output", res.get("v_address"));
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) ClientTestModel(com.ctrip.platform.dal.dao.unitbase.ClientTestModel) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) SQLException(java.sql.SQLException) Test(org.junit.Test)

Aggregations

ClientTestModel (com.ctrip.platform.dal.dao.unitbase.ClientTestModel)57 Test (org.junit.Test)55 DalHints (com.ctrip.platform.dal.dao.DalHints)29 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)27 ArrayList (java.util.ArrayList)13 SQLException (java.sql.SQLException)9 SelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.SelectSqlBuilder)5 DalClient (com.ctrip.platform.dal.dao.DalClient)3 DalCommand (com.ctrip.platform.dal.dao.DalCommand)3 ClientTestDalRowMapper (com.ctrip.platform.dal.dao.unitbase.ClientTestDalRowMapper)3 DalRowMapperExtractor (com.ctrip.platform.dal.dao.helper.DalRowMapperExtractor)2 DalCustomRowMapper (com.ctrip.platform.dal.dao.helper.DalCustomRowMapper)1 FreeSelectSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.FreeSelectSqlBuilder)1 ResultSet (java.sql.ResultSet)1 Timestamp (java.sql.Timestamp)1 List (java.util.List)1 Map (java.util.Map)1