use of com.querydsl.sql.dml.SQLInsertClause in project querydsl by querydsl.
the class CustomTypesTest method insert_query_update.
@Test
public void insert_query_update() {
QPerson person = QPerson.person;
// insert
SQLInsertClause insert = new SQLInsertClause(connection, configuration, person);
insert.set(person.id, 10);
insert.set(person.firstname, "Bob");
insert.set(person.gender, Gender.MALE);
assertEquals(1L, insert.execute());
// query
SQLQuery<?> query = new SQLQuery<Void>(connection, configuration);
assertEquals(Gender.MALE, query.from(person).where(person.id.eq(10)).select(person.gender).fetchOne());
// update
SQLUpdateClause update = new SQLUpdateClause(connection, configuration, person);
update.set(person.gender, Gender.FEMALE);
update.set(person.firstname, "Jane");
update.where(person.id.eq(10));
update.execute();
// query
query = new SQLQuery<Void>(connection, configuration);
assertEquals(Gender.FEMALE, query.from(person).where(person.id.eq(10)).select(person.gender).fetchOne());
}
use of com.querydsl.sql.dml.SQLInsertClause in project querydsl by querydsl.
the class ProductDaoImpl method save.
@Override
public Product save(Product p) {
Long id = p.getId();
if (id == null) {
id = populate(queryFactory.insert(product), p).executeWithKey(product.id);
p.setId(id);
} else {
populate(queryFactory.update(product), p).where(product.id.eq(id)).execute();
// delete l10n rows
queryFactory.delete(productL10n).where(productL10n.productId.eq(id)).execute();
}
SQLInsertClause insert = queryFactory.insert(productL10n);
for (ProductL10n l10n : p.getLocalizations()) {
insert.set(productL10n.productId, id).set(productL10n.description, l10n.getDescription()).set(productL10n.lang, l10n.getLang()).set(productL10n.name, l10n.getName()).addBatch();
}
insert.execute();
return p;
}
use of com.querydsl.sql.dml.SQLInsertClause in project querydsl by querydsl.
the class GeneratedKeysMySQLTest method test.
@Test
public void test() throws SQLException {
stmt.execute("drop table if exists GENERATED_KEYS");
stmt.execute("create table GENERATED_KEYS(" + "ID int AUTO_INCREMENT PRIMARY KEY, " + "NAME varchar(30))");
QGeneratedKeysEntity entity = new QGeneratedKeysEntity("entity");
SQLInsertClause insertClause = new SQLInsertClause(conn, new H2Templates(), entity);
ResultSet rs = insertClause.set(entity.name, "Hello").executeWithKeys();
ResultSetMetaData md = rs.getMetaData();
System.out.println(md.getColumnName(1));
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
insertClause = new SQLInsertClause(conn, new H2Templates(), entity);
rs = insertClause.set(entity.name, "World").executeWithKeys();
assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertFalse(rs.next());
}
use of com.querydsl.sql.dml.SQLInsertClause in project querydsl by querydsl.
the class MySQLQueryFactoryTest method insertOnDuplicateKeyUpdate.
@Test
public void insertOnDuplicateKeyUpdate() {
SQLInsertClause clause = queryFactory.insertOnDuplicateKeyUpdate(QSurvey.survey, "c = c+1");
assertEquals("insert into SURVEY\nvalues () on duplicate key update c = c+1", clause.toString());
}
use of com.querydsl.sql.dml.SQLInsertClause in project querydsl by querydsl.
the class MySQLQueryFactoryTest method insertOnDuplicateKeyUpdate_values.
@Test
public void insertOnDuplicateKeyUpdate_values() {
SQLInsertClause clause = queryFactory.insertOnDuplicateKeyUpdate(QSurvey.survey, SQLExpressions.set(QSurvey.survey.name, QSurvey.survey.name));
assertEquals("insert into SURVEY\n" + "values () on duplicate key update SURVEY.NAME = values(SURVEY.NAME)", clause.toString());
}
Aggregations