Search in sources :

Example 56 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class JdbcPkGenerator method longPkFromDatabase.

/**
 * Performs primary key generation ignoring cache. Generates a range of
 * primary keys as specified by "pkCacheSize" bean property.
 * <p>
 * This method is called internally from "generatePkForDbEntity" and then
 * generated range of key values is saved in cache for performance.
 * Subclasses that implement different primary key generation solutions
 * should override this method, not "generatePkForDbEntity".
 * </p>
 *
 * @since 3.0
 */
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
    String select = "SELECT #result('NEXT_ID' 'long' 'NEXT_ID') FROM AUTO_PK_SUPPORT " + "WHERE TABLE_NAME = '" + entity.getName() + '\'';
    // run queries via DataNode to utilize its transactional behavior
    List<Query> queries = new ArrayList<>(2);
    queries.add(new SQLTemplate(entity, select));
    queries.add(new SQLTemplate(entity, pkUpdateString(entity.getName())));
    PkRetrieveProcessor observer = new PkRetrieveProcessor(entity.getName());
    node.performQueries(queries, observer);
    return observer.getId();
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) ArrayList(java.util.ArrayList)

Example 57 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class FrontBasePkGenerator method longPkFromDatabase.

/**
 * @since 3.0
 */
@Override
protected long longPkFromDatabase(DataNode node, DbEntity entity) throws Exception {
    String template = "SELECT #result('UNIQUE' 'long') FROM " + entity.getName();
    final long[] pkHolder = new long[1];
    SQLTemplate query = new SQLTemplate(entity, template);
    OperationObserver observer = new DoNothingOperationObserver() {

        @Override
        public void nextRows(Query query, List<?> dataRows) {
            if (dataRows.size() != 1) {
                throw new CayenneRuntimeException("Error fetching PK. Expected one row, got %d", dataRows.size());
            }
            DataRow row = (DataRow) dataRows.get(0);
            Number pk = (Number) row.get("UNIQUE");
            pkHolder[0] = pk.longValue();
        }
    };
    node.performQueries(Collections.singleton((Query) query), observer);
    return pkHolder[0];
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) Query(org.apache.cayenne.query.Query) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) OperationObserver(org.apache.cayenne.access.OperationObserver) DoNothingOperationObserver(org.apache.cayenne.access.util.DoNothingOperationObserver) ArrayList(java.util.ArrayList) List(java.util.List) DoNothingOperationObserver(org.apache.cayenne.access.util.DoNothingOperationObserver) DataRow(org.apache.cayenne.DataRow)

Example 58 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class CayenneContextSQLTemplateIT method testObjectRoot.

@Test
public void testObjectRoot() throws Exception {
    assertNull(Cayenne.objectForPK(context, ClientMtTable1.class, 1));
    context.performGenericQuery(new SQLTemplate(ClientMtTable1.class, "insert into MT_TABLE1 " + "(TABLE1_ID, GLOBAL_ATTRIBUTE1, SERVER_ATTRIBUTE1) " + "values (1, 'g1', 's1')"));
    assertNotNull(Cayenne.objectForPK(context, ClientMtTable1.class, 1));
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Test(org.junit.Test)

Example 59 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class EnumIT method testSQLTemplate.

@Test
public void testSQLTemplate() throws Exception {
    createDataSet();
    SQLTemplate q = new SQLTemplate(EnumEntity.class, "SELECT * FROM ENUM_ENTITY WHERE ENUM_ATTRIBUTE = 'one'");
    q.setColumnNamesCapitalization(CapsStrategy.UPPER);
    EnumEntity e = (EnumEntity) Cayenne.objectForQuery(context, q);
    assertNotNull(e);
    assertSame(Enum1.one, e.getEnumAttribute());
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) EnumEntity(org.apache.cayenne.testdo.enum_test.EnumEntity) Test(org.junit.Test)

Example 60 with SQLTemplate

use of org.apache.cayenne.query.SQLTemplate in project cayenne by apache.

the class JointPrefetchIT method testJointPrefetchDataTypes.

/**
 * Tests that joined entities can have non-standard type mappings.
 */
@Test
public void testJointPrefetchDataTypes() {
    // prepare... can't load from XML, as it doesn't yet support dates..
    SQLTemplate artistSQL = new SQLTemplate(Artist.class, "insert into ARTIST (ARTIST_ID, ARTIST_NAME, DATE_OF_BIRTH) " + "values (33001, 'a1', #bind($date 'DATE'))");
    artistSQL.setParams(Collections.singletonMap("date", new Date(System.currentTimeMillis())));
    SQLTemplate paintingSQL = new SQLTemplate(Painting.class, "INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ARTIST_ID, ESTIMATED_PRICE) " + "VALUES (33001, 'p1', 33001, 1000)");
    context.performNonSelectingQuery(artistSQL);
    context.performNonSelectingQuery(paintingSQL);
    // test
    SelectQuery<Painting> q = new SelectQuery<>(Painting.class);
    q.addPrefetch(Painting.TO_ARTIST.joint());
    ObjEntity artistE = context.getEntityResolver().getObjEntity("Artist");
    ObjAttribute dateOfBirth = artistE.getAttribute("dateOfBirth");
    assertEquals("java.util.Date", dateOfBirth.getType());
    dateOfBirth.setType("java.sql.Date");
    try {
        final List<Painting> objects = q.select(context);
        queryInterceptor.runWithQueriesBlocked(() -> {
            assertEquals(1, objects.size());
            for (Painting p : objects) {
                Artist a = p.getToArtist();
                assertNotNull(a);
                assertNotNull(a.getDateOfBirth());
                assertTrue(a.getDateOfBirth().getClass().getName(), Date.class.isAssignableFrom(a.getDateOfBirth().getClass()));
            }
        });
    } finally {
        dateOfBirth.setType("java.util.Date");
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) SelectQuery(org.apache.cayenne.query.SelectQuery) Artist(org.apache.cayenne.testdo.testmap.Artist) ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) Date(java.sql.Date) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

SQLTemplate (org.apache.cayenne.query.SQLTemplate)118 Test (org.junit.Test)108 Artist (org.apache.cayenne.testdo.testmap.Artist)27 DataRow (org.apache.cayenne.DataRow)18 HashMap (java.util.HashMap)16 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)14 Query (org.apache.cayenne.query.Query)14 SelectQuery (org.apache.cayenne.query.SelectQuery)11 Painting (org.apache.cayenne.testdo.testmap.Painting)11 List (java.util.List)8 Connection (java.sql.Connection)7 SQLAction (org.apache.cayenne.query.SQLAction)7 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)6 Map (java.util.Map)5 OperationObserver (org.apache.cayenne.access.OperationObserver)5 DataMap (org.apache.cayenne.map.DataMap)5 AbstractPerson (org.apache.cayenne.testdo.inheritance_people.AbstractPerson)5 PersonNotes (org.apache.cayenne.testdo.inheritance_people.PersonNotes)5 Date (java.sql.Date)4 FrontBaseAdapter (org.apache.cayenne.dba.frontbase.FrontBaseAdapter)4