1 19 20 package org.apache.cayenne.access; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.apache.cayenne.CayenneException; 26 import org.apache.cayenne.CayenneRuntimeException; 27 import org.apache.cayenne.DataRow; 28 import org.apache.cayenne.ObjectId; 29 import org.apache.cayenne.map.DbAttribute; 30 import org.apache.cayenne.query.BatchQuery; 31 import org.apache.cayenne.query.InsertBatchQuery; 32 import org.apache.cayenne.query.Query; 33 import org.apache.cayenne.util.Util; 34 35 41 class DataDomainFlushObserver implements OperationObserver { 42 43 public void nextQueryException(Query query, Exception ex) { 44 throw new CayenneRuntimeException("Raising from query exception.", Util 45 .unwindException(ex)); 46 } 47 48 public void nextGlobalException(Exception ex) { 49 throw new CayenneRuntimeException( 50 "Raising from underlyingQueryEngine exception.", 51 Util.unwindException(ex)); 52 } 53 54 59 public void nextGeneratedDataRows(Query query, ResultIterator keysIterator) { 60 61 List keys; 63 try { 64 keys = keysIterator.dataRows(true); 65 } 66 catch (CayenneException ex) { 67 throw new CayenneRuntimeException("Error reading primary key", Util 68 .unwindException(ex)); 69 } 70 71 if (!(query instanceof InsertBatchQuery)) { 72 throw new CayenneRuntimeException( 73 "Generated keys only supported for InsertBatchQuery, instead got " 74 + query); 75 } 76 77 BatchQuery batch = (BatchQuery) query; 78 79 ObjectId id = batch.getObjectId(); 80 if (id == null || !id.isTemporary()) { 81 return; 83 } 84 85 if (keys.size() != 1) { 86 throw new CayenneRuntimeException( 87 "One and only one PK row is expected, instead got " + keys.size()); 88 } 89 90 DataRow key = (DataRow) keys.get(0); 91 92 if (key.size() == 0) { 94 throw new CayenneRuntimeException("Empty key generated."); 95 } 96 97 99 if (key.size() > 1) { 104 throw new CayenneRuntimeException( 105 "Only a single column autogenerated PK is supported. " 106 + "Generated key: " 107 + key); 108 } 109 110 Iterator it = batch.getDbEntity().getGeneratedAttributes().iterator(); 111 while (it.hasNext()) { 112 DbAttribute attribute = (DbAttribute) it.next(); 113 114 if (attribute.isPrimaryKey()) { 117 Object value = key.values().iterator().next(); 118 119 id.getReplacementIdMap().put(attribute.getName(), value); 122 break; 123 } 124 } 125 } 126 127 public void nextBatchCount(Query query, int[] resultCount) { 128 } 129 130 public void nextCount(Query query, int resultCount) { 131 } 132 133 public void nextDataRows(Query query, List dataRows) { 134 } 135 136 public void nextDataRows(Query q, ResultIterator it) { 137 throw new UnsupportedOperationException ( 138 "'nextDataRows(Query,ResultIterator)' is unsupported (and unexpected) on commit."); 139 } 140 141 public boolean isIteratedResult() { 142 return false; 143 } 144 } 145 | Popular Tags |