Search in sources :

Example 1 with DoNothingOperationObserver

use of org.apache.cayenne.access.util.DoNothingOperationObserver 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)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 DataRow (org.apache.cayenne.DataRow)1 OperationObserver (org.apache.cayenne.access.OperationObserver)1 DoNothingOperationObserver (org.apache.cayenne.access.util.DoNothingOperationObserver)1 Query (org.apache.cayenne.query.Query)1 SQLTemplate (org.apache.cayenne.query.SQLTemplate)1