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();
}
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];
}
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));
}
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());
}
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");
}
}
Aggregations