Java Code Examples for com.j256.ormlite.stmt.UpdateBuilder#updateColumnExpression()

The following examples show how to use com.j256.ormlite.stmt.UpdateBuilder#updateColumnExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbcBaseDaoImplTest.java    From ormlite-jdbc with ISC License 6 votes vote down vote up
@Test
public void testPrepareStatementUpdateValueExpression() throws Exception {
	Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
	Foo foo = new Foo();
	foo.val = 123213;
	assertEquals(1, fooDao.create(foo));

	QueryBuilder<Foo, Integer> stmtb = fooDao.queryBuilder();
	stmtb.where().eq(Foo.ID_FIELD_NAME, foo.id);
	List<Foo> results = fooDao.query(stmtb.prepare());
	assertEquals(1, results.size());

	UpdateBuilder<Foo, Integer> updateb = fooDao.updateBuilder();
	String stuff = "deopdjq";
	updateb.updateColumnValue(Foo.STUFF_FIELD_NAME, stuff);
	StringBuilder sb = new StringBuilder();
	updateb.escapeColumnName(sb, Foo.VAL_FIELD_NAME);
	sb.append("+ 1");
	updateb.updateColumnExpression(Foo.VAL_FIELD_NAME, sb.toString());
	assertEquals(1, fooDao.update(updateb.prepare()));

	results = fooDao.queryForAll();
	assertEquals(1, results.size());
	assertEquals(stuff, results.get(0).stuff);
	assertEquals(foo.val + 1, results.get(0).val);
}
 
Example 2
Source File: DataPersisterMain.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
/**
 * Read and write some example data.
 */
private void readWriteData() throws Exception {
	// create an instance of User
	String name = "Jim Coakley";
	Date birthDate = new Date();
	DateTime createDateTime = new DateTime().plusDays(10);
	User user = new User(name, birthDate, createDateTime);

	// persist the user object to the database
	userDao.create(user);

	// if we get the user from the database then we should
	User result = userDao.queryForId(user.getId());
	// our result birth-date should now be null because it is too early
	assertEquals(birthDate, result.getBirthDate());
	assertEquals(createDateTime, result.getCreateDateTime());

	// to simulate a 'zero-date' we update the database by hand
	UpdateBuilder<User, Integer> ub = userDao.updateBuilder();
	// set it to some silly value
	ub.updateColumnExpression(User.FIELD_BIRTH_DATE, "'0000-01-01'");
	assertEquals(1, ub.update());

	// now we pull back out the user to see if we get a null birth-date
	result = userDao.queryForId(user.getId());
	// our result birth-date should now be null because it is too early
	assertNull(result.getBirthDate());
}
 
Example 3
Source File: JdbcBaseDaoImplTest.java    From ormlite-jdbc with ISC License 5 votes vote down vote up
@Test
public void testPrepareStatementUpdateValueWhere() throws Exception {
	Dao<Foo, Integer> fooDao = createDao(Foo.class, true);
	Foo foo1 = new Foo();
	foo1.val = 78582351;
	assertEquals(1, fooDao.create(foo1));
	Foo foo2 = new Foo();
	String stuff = "eopqjdepodje";
	foo2.stuff = stuff;
	foo2.val = 123344131;
	assertEquals(1, fooDao.create(foo2));

	UpdateBuilder<Foo, Integer> updateb = fooDao.updateBuilder();
	String newStuff = "deopdjq";
	updateb.updateColumnValue(Foo.STUFF_FIELD_NAME, newStuff);
	StringBuilder sb = new StringBuilder();
	updateb.escapeColumnName(sb, Foo.VAL_FIELD_NAME);
	sb.append("+ 1");
	updateb.updateColumnExpression(Foo.VAL_FIELD_NAME, sb.toString());
	updateb.where().eq(Foo.ID_FIELD_NAME, foo2.id);
	assertEquals(1, fooDao.update(updateb.prepare()));

	List<Foo> results = fooDao.queryForAll();
	assertEquals(2, results.size());
	Foo foo = results.get(0);
	assertEquals(foo1.id, foo.id);
	assertEquals(foo1.val, foo.val);
	assertNull(foo.stuff);
	foo = results.get(1);
	assertEquals(foo2.id, foo.id);
	assertEquals(foo2.val + 1, foo.val);
	assertEquals(newStuff, foo.stuff);
}