Search in sources :

Example 1 with SQLInsertClause

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());
}
Also used : SQLUpdateClause(com.querydsl.sql.dml.SQLUpdateClause) SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) Test(org.junit.Test)

Example 2 with SQLInsertClause

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;
}
Also used : SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) ProductL10n(com.querydsl.example.dto.ProductL10n)

Example 3 with SQLInsertClause

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());
}
Also used : H2Templates(com.querydsl.sql.H2Templates) SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) QGeneratedKeysEntity(com.querydsl.sql.QGeneratedKeysEntity) Test(org.junit.Test)

Example 4 with SQLInsertClause

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());
}
Also used : SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) Test(org.junit.Test)

Example 5 with SQLInsertClause

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());
}
Also used : SQLInsertClause(com.querydsl.sql.dml.SQLInsertClause) Test(org.junit.Test)

Aggregations

SQLInsertClause (com.querydsl.sql.dml.SQLInsertClause)52 Test (org.junit.Test)25 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)13 ExcludeIn (com.querydsl.core.testutil.ExcludeIn)6 ColumnMetadata (com.querydsl.sql.ColumnMetadata)4 MAuditDelta (com.evolveum.midpoint.repo.sql.audit.beans.MAuditDelta)3 IncludeIn (com.querydsl.core.testutil.IncludeIn)3 AuditReferenceValue (com.evolveum.midpoint.audit.api.AuditReferenceValue)2 MAuditEventRecord (com.evolveum.midpoint.repo.sql.audit.beans.MAuditEventRecord)2 PolyStringType (com.evolveum.prism.xml.ns._public.types_3.PolyStringType)2 H2Templates (com.querydsl.sql.H2Templates)2 QGeneratedKeysEntity (com.querydsl.sql.QGeneratedKeysEntity)2 CanonicalItemPath (com.evolveum.midpoint.prism.path.CanonicalItemPath)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 SystemException (com.evolveum.midpoint.util.exception.SystemException)1 AuditEventRecordCustomColumnPropertyType (com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordCustomColumnPropertyType)1 AuditEventRecordReferenceType (com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordReferenceType)1 AuditEventRecordReferenceValueType (com.evolveum.midpoint.xml.ns._public.common.audit_3.AuditEventRecordReferenceValueType)1 ObjectDeltaOperationType (com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectDeltaOperationType)1