Search in sources :

Example 1 with UpdateSqlBuilder

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

the class BaseDalTabelDaoShardByTableTest method testUpdatePlain.

/**
	 * Test plain update with SQL
	 * @throws SQLException
	 */
@Test
public void testUpdatePlain() throws SQLException {
    String sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
    StatementParameters parameters = new StatementParameters();
    DalHints hints = new DalHints();
    int res;
    try {
        res = dao.update(sql, parameters, hints);
        fail();
    } catch (Exception e) {
    }
    // By tabelShard
    UpdateSqlBuilder usb = new UpdateSqlBuilder(TABLE_NAME, dao.getDatabaseCategory());
    usb.update("address", "CTRIP", Types.VARCHAR);
    usb.equal("id", "1", Types.INTEGER);
    res = dao.update(usb, new DalHints().inTableShard(0));
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().inTableShard(0)).getAddress());
    // By tableShardValue
    assertEquals(2, getCount(1));
    res = dao.update(usb, new DalHints().setTableShardValue(1));
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setTableShardValue(1)).getAddress());
    // By shardColValue
    assertEquals(3, getCount(2));
    res = dao.update(usb, new DalHints().setShardColValue("index", 2));
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setShardColValue("index", 2)).getAddress());
    // By shardColValue
    assertEquals(4, getCount(3));
    res = dao.update(usb, new DalHints().setShardColValue("tableIndex", 3));
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setShardColValue("tableIndex", 3)).getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) UpdateSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.UpdateSqlBuilder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 2 with UpdateSqlBuilder

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

the class BaseDalTabelDaoShardByTableTest method testUpdatePlainAsyncCallback.

@Test
public void testUpdatePlainAsyncCallback() throws SQLException {
    try {
        DalClientFactory.initClientFactory();
    } catch (Exception e1) {
        fail();
    }
    String sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
    StatementParameters parameters = new StatementParameters();
    DalHints hints;
    int res;
    try {
        hints = asyncHints();
        res = dao.update(sql, parameters, hints);
        res = assertInt(res, hints);
        fail();
    } catch (Exception e) {
    }
    // By tabelShard
    UpdateSqlBuilder usb = new UpdateSqlBuilder(TABLE_NAME, dao.getDatabaseCategory());
    usb.update("address", "CTRIP", Types.VARCHAR);
    usb.equal("id", "1", Types.INTEGER);
    hints = asyncHints();
    res = dao.update(usb, hints.inTableShard(0));
    res = assertInt(res, hints);
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().inTableShard(0)).getAddress());
    // By tableShardValue
    assertEquals(2, getCount(1));
    hints = intHints();
    res = dao.update(usb, hints.setTableShardValue(1));
    res = assertInt(res, hints);
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setTableShardValue(1)).getAddress());
    // By shardColValue
    assertEquals(3, getCount(2));
    hints = asyncHints();
    res = dao.update(usb, hints.setShardColValue("index", 2));
    res = assertInt(res, hints);
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setShardColValue("index", 2)).getAddress());
    // By shardColValue
    assertEquals(4, getCount(3));
    hints = intHints();
    res = dao.update(usb, hints.setShardColValue("tableIndex", 3));
    res = assertInt(res, hints);
    assertEquals("CTRIP", dao.queryByPk(1, new DalHints().setShardColValue("tableIndex", 3)).getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) UpdateSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.UpdateSqlBuilder) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 3 with UpdateSqlBuilder

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

the class BaseDalTableDaoShardByDbTableTest method testUpdatePlain.

/**
	 * Test plain update with SQL
	 * @throws SQLException
	 */
private void testUpdatePlain(int shardId, DalHints oldhints) throws SQLException {
    String sql = "UPDATE " + TABLE_NAME + " SET address = 'CTRIP' WHERE id = 1";
    StatementParameters parameters = new StatementParameters();
    DalHints hints;
    int res;
    try {
        hints = copy(oldhints);
        res = dao.update(sql, parameters, hints);
        res = assertInt(res, hints);
        Assert.fail();
    } catch (Exception e) {
    }
    // By tabelShard
    UpdateSqlBuilder usb = new UpdateSqlBuilder(TABLE_NAME, dao.getDatabaseCategory());
    usb.update("address", "CTRIP", Types.VARCHAR);
    usb.equal("id", "1", Types.INTEGER);
    //		sql = "UPDATE " + TABLE_NAME
    //				+ " SET address = 'CTRIP' WHERE id = 1";
    hints = copy(oldhints);
    res = dao.update(usb, hints.inTableShard(0));
    res = assertInt(res, hints);
    assertResEquals(1, res);
    Assert.assertEquals("CTRIP", queryByPk(hints.inTableShard(0)).getAddress());
    // By tableShardValue
    Assert.assertEquals(2, getCount(shardId, 1));
    hints = copy(oldhints);
    res = dao.update(usb, hints.setTableShardValue(1));
    res = assertInt(res, hints);
    assertResEquals(1, res);
    Assert.assertEquals("CTRIP", queryByPk(hints.setTableShardValue(1)).getAddress());
    // By shardColValue
    Assert.assertEquals(3, getCount(shardId, 2));
    hints = copy(oldhints);
    res = dao.update(usb, hints.setShardColValue("table", 2));
    res = assertInt(res, hints);
    assertResEquals(1, res);
    Assert.assertEquals("CTRIP", queryByPk(hints.setShardColValue("table", 2)).getAddress());
    // By shardColValue
    Assert.assertEquals(4, getCount(shardId, 3));
    hints = copy(oldhints);
    res = dao.update(usb, hints.setShardColValue("tableIndex", 3));
    res = assertInt(res, hints);
    assertResEquals(1, res);
    Assert.assertEquals("CTRIP", queryByPk(hints.setShardColValue("tableIndex", 3)).getAddress());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) UpdateSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.UpdateSqlBuilder) SQLException(java.sql.SQLException)

Example 4 with UpdateSqlBuilder

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

the class UpdateSqlBuilderTest method test1.

@Test
public void test1() throws SQLException {
    List<String> in = new ArrayList<String>();
    in.add("12");
    in.add("12");
    UpdateSqlBuilder builder = new UpdateSqlBuilder("Person", DatabaseCategory.MySql);
    builder.update("name", "value", Types.VARCHAR);
    builder.update("age", 52, Types.INTEGER);
    builder.update("addr", "china", Types.VARCHAR);
    builder.equal("age", 123, Types.INTEGER);
    builder.and().in("b", in, Types.INTEGER);
    builder.and().like("b", "in", Types.INTEGER);
    builder.and().betweenNullable("c", 20, 100, Types.INTEGER);
    builder.and().betweenNullable("d", null, "paramValue2", Types.VARCHAR);
    builder.and().isNull("e");
    String sql = builder.build();
    String expect_sql = "UPDATE `Person` SET `name` = ?, `age` = ?, `addr` = ? " + "WHERE `age` = ? AND `b` in ( ?, ? ) AND `b` LIKE ? " + "AND `c` BETWEEN ? AND ? AND `e` IS NULL";
    Assert.assertEquals(expect_sql, sql);
    builder.buildParameters();
    Assert.assertEquals(10, builder.getStatementParameterIndex());
    Assert.assertEquals(9, builder.buildParameters().size());
    Assert.assertEquals(3, builder.buildParameters().get(2).getIndex());
    Assert.assertEquals("addr", builder.buildParameters().get(2).getName());
    Assert.assertEquals(Types.VARCHAR, builder.buildParameters().get(2).getSqlType());
    Assert.assertEquals(9, builder.buildParameters().get(8).getIndex());
    Assert.assertEquals("c", builder.buildParameters().get(8).getName());
    Assert.assertEquals(Types.INTEGER, builder.buildParameters().get(8).getSqlType());
}
Also used : ArrayList(java.util.ArrayList) UpdateSqlBuilder(com.ctrip.platform.dal.dao.sqlbuilder.UpdateSqlBuilder) Test(org.junit.Test)

Aggregations

UpdateSqlBuilder (com.ctrip.platform.dal.dao.sqlbuilder.UpdateSqlBuilder)4 DalHints (com.ctrip.platform.dal.dao.DalHints)3 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)3 SQLException (java.sql.SQLException)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)1