| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package net.wotonomy.access; |
| 19 |
|
|
| 20 |
|
import net.wotonomy.control.EOFetchSpecification; |
| 21 |
|
import net.wotonomy.control.EOQualifier; |
| 22 |
|
import net.wotonomy.foundation.NSArray; |
| 23 |
|
import net.wotonomy.foundation.NSDictionary; |
| 24 |
|
import net.wotonomy.foundation.NSMutableDictionary; |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public abstract class EOAdaptorChannel { |
| 33 |
|
|
| 34 |
|
protected EOAdaptorContext _context; |
| 35 |
|
|
| 36 |
|
public EOAdaptorChannel(EOAdaptorContext context) { |
| 37 |
0 |
super(); |
| 38 |
0 |
_context = context; |
| 39 |
0 |
} |
| 40 |
|
|
| 41 |
|
public EOAdaptorContext adaptorContext() { |
| 42 |
0 |
return _context; |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
public void addStoredProceduresNamed(NSArray names, EOModel model) { |
| 46 |
0 |
} |
| 47 |
|
|
| 48 |
|
public abstract NSArray attributesToFetch(); |
| 49 |
|
|
| 50 |
|
public abstract void cancelFetch(); |
| 51 |
|
|
| 52 |
|
public abstract void closeChannel(); |
| 53 |
|
|
| 54 |
|
public abstract NSArray describeResults(); |
| 55 |
|
|
| 56 |
|
public abstract int deleteRowsDescribedByQualifier(EOQualifier q, EOEntity entity); |
| 57 |
|
|
| 58 |
|
public abstract void evaluateExpression(EOSQLExpression sql); |
| 59 |
|
|
| 60 |
|
public abstract void executeStoredProcedure(EOStoredProcedure proc, NSDictionary values); |
| 61 |
|
|
| 62 |
|
public abstract NSMutableDictionary fetchRow(); |
| 63 |
|
|
| 64 |
|
public abstract void insertRow(NSDictionary row, EOEntity entity); |
| 65 |
|
|
| 66 |
|
public abstract boolean isFetchInProgress(); |
| 67 |
|
|
| 68 |
|
public abstract boolean isOpen(); |
| 69 |
|
|
| 70 |
|
public abstract void openChannel(); |
| 71 |
|
|
| 72 |
|
public abstract NSDictionary returnValuesForLastStoredProcedureInvocation(); |
| 73 |
|
|
| 74 |
|
public abstract void selectAttributes(NSArray atts, EOFetchSpecification fspec, boolean lock, EOEntity entity); |
| 75 |
|
|
| 76 |
|
public abstract void setAttributesToFetch(NSArray atts); |
| 77 |
|
|
| 78 |
|
public abstract int updateValuesInRowsDescribedByQualifier(NSDictionary row, EOQualifier q, EOEntity entity); |
| 79 |
|
|
| 80 |
|
public void deleteRowDescribedByQualifier(EOQualifier q, EOEntity entity) { |
| 81 |
0 |
adaptorContext().beginTransaction(); |
| 82 |
0 |
int count = deleteRowsDescribedByQualifier(q, entity); |
| 83 |
0 |
if (count != 1) { |
| 84 |
0 |
adaptorContext().rollbackTransaction(); |
| 85 |
0 |
throw new EOGeneralAdaptorException("Qualifier deleted " + count + " rows instead of exactly one."); |
| 86 |
|
} |
| 87 |
0 |
adaptorContext().commitTransaction(); |
| 88 |
0 |
} |
| 89 |
|
|
| 90 |
|
public EOModel describeModelWithTableNames(NSArray names) { |
| 91 |
0 |
return null; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
public NSArray describeStoredProcedureNames() { |
| 95 |
0 |
return NSArray.EmptyArray; |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
public NSArray describeTableNames() { |
| 99 |
0 |
return NSArray.EmptyArray; |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
public NSMutableDictionary dictionaryWithObjectsForAttributes(Object[] values, NSArray attributes) { |
| 103 |
0 |
Object[] keys = new Object[attributes.count()]; |
| 104 |
0 |
for (int i = 0; i < attributes.count(); i++) |
| 105 |
0 |
keys[i] = ((EOAttribute)attributes.objectAtIndex(i)).name(); |
| 106 |
0 |
return new NSMutableDictionary(values, keys); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
public void lockRowComparingAttributes(NSArray atts, EOEntity entity, EOQualifier q, NSDictionary snapshot) { |
| 110 |
0 |
EOFetchSpecification fspec = new EOFetchSpecification(entity.name(), q, null); |
| 111 |
0 |
adaptorContext().beginTransaction(); |
| 112 |
0 |
selectAttributes(atts, fspec, true, entity); |
| 113 |
0 |
if (isFetchInProgress()) { |
| 114 |
0 |
NSDictionary row = fetchRow(); |
| 115 |
0 |
if (row == null) { |
| 116 |
0 |
cancelFetch(); |
| 117 |
0 |
adaptorContext().rollbackTransaction(); |
| 118 |
0 |
throw new EOGeneralAdaptorException("Cannot obtain row to lock. Probably modified from the outside."); |
| 119 |
|
} |
| 120 |
0 |
if (isFetchInProgress()) { |
| 121 |
0 |
if (fetchRow() != null) { |
| 122 |
0 |
cancelFetch(); |
| 123 |
0 |
adaptorContext().rollbackTransaction(); |
| 124 |
0 |
throw new EOGeneralAdaptorException("Qualifier returns more than one row."); |
| 125 |
|
} |
| 126 |
|
} |
| 127 |
0 |
java.util.Enumeration enumeration = snapshot.keyEnumerator(); |
| 128 |
0 |
while (enumeration.hasMoreElements()) { |
| 129 |
0 |
Object key = enumeration.nextElement(); |
| 130 |
0 |
Object svalue = snapshot.objectForKey(key); |
| 131 |
0 |
Object rvalue = row.objectForKey(key); |
| 132 |
0 |
if (rvalue == null) { |
| 133 |
0 |
cancelFetch(); |
| 134 |
0 |
adaptorContext().rollbackTransaction(); |
| 135 |
0 |
throw new EOGeneralAdaptorException("Value for key " + key + " not found in locked row."); |
| 136 |
|
} |
| 137 |
0 |
if (!rvalue.equals(svalue)) { |
| 138 |
0 |
cancelFetch(); |
| 139 |
0 |
adaptorContext().rollbackTransaction(); |
| 140 |
0 |
throw new EOGeneralAdaptorException("Value for key " + key + " differes from snapshot."); |
| 141 |
|
} |
| 142 |
0 |
} |
| 143 |
0 |
adaptorContext().commitTransaction(); |
| 144 |
0 |
return; |
| 145 |
|
} |
| 146 |
0 |
adaptorContext().rollbackTransaction(); |
| 147 |
0 |
throw new EOGeneralAdaptorException("A fetch was never generated."); |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
public void performAdaptorOperation(EOAdaptorOperation operation) { |
| 151 |
0 |
int opcode = operation.adaptorOperator(); |
| 152 |
0 |
switch (opcode) { |
| 153 |
|
case EODatabaseOperation.AdaptorLockOperator: |
| 154 |
0 |
if (operation.entity() == null) |
| 155 |
0 |
throw new EOGeneralAdaptorException("A lock operation must have an entity assigned to it.", |
| 156 |
0 |
new NSDictionary(operation, "operation")); |
| 157 |
0 |
if (operation.qualifier() == null) |
| 158 |
0 |
throw new EOGeneralAdaptorException("A lock operation must have a qualifier assigned to it.", |
| 159 |
0 |
new NSDictionary(operation, "operation")); |
| 160 |
0 |
if (operation.qualifier() == null) |
| 161 |
0 |
throw new EOGeneralAdaptorException("A lock operation must have changedValues assigned to it.", |
| 162 |
0 |
new NSDictionary(operation, "operation")); |
| 163 |
0 |
lockRowComparingAttributes(operation.attributes(), operation.entity(), operation.qualifier(), operation.changedValues()); |
| 164 |
0 |
break; |
| 165 |
|
case EODatabaseOperation.AdaptorInsertOperator: |
| 166 |
0 |
if (operation.entity() == null) |
| 167 |
0 |
throw new EOGeneralAdaptorException("An insert operation must have an entity assigned to it.", |
| 168 |
0 |
new NSDictionary(operation, "operation")); |
| 169 |
0 |
if (operation.changedValues() == null) |
| 170 |
0 |
throw new EOGeneralAdaptorException("An insert operation must have changedValues assigned to it.", |
| 171 |
0 |
new NSDictionary(operation, "operation")); |
| 172 |
0 |
insertRow(operation.changedValues(), operation.entity()); |
| 173 |
0 |
break; |
| 174 |
|
case EODatabaseOperation.AdaptorUpdateOperator: |
| 175 |
0 |
if (operation.entity() == null) |
| 176 |
0 |
throw new EOGeneralAdaptorException("An update operation must have an entity assigned to it.", |
| 177 |
0 |
new NSDictionary(operation, "operation")); |
| 178 |
0 |
if (operation.changedValues() == null) |
| 179 |
0 |
throw new EOGeneralAdaptorException("An update operation must have changedValues assigned to it.", |
| 180 |
0 |
new NSDictionary(operation, "operation")); |
| 181 |
0 |
updateValuesInRowsDescribedByQualifier(operation.changedValues(), operation.qualifier(), operation.entity()); |
| 182 |
0 |
break; |
| 183 |
|
case EODatabaseOperation.AdaptorDeleteOperator: |
| 184 |
0 |
if (operation.entity() == null) |
| 185 |
0 |
throw new EOGeneralAdaptorException("A delete operation must have an entity assigned to it.", |
| 186 |
0 |
new NSDictionary(operation, "operation")); |
| 187 |
0 |
deleteRowsDescribedByQualifier(operation.qualifier(), operation.entity()); |
| 188 |
0 |
break; |
| 189 |
|
case EODatabaseOperation.AdaptorStoredProcedureOperator: |
| 190 |
0 |
if (operation.storedProcedure() == null) |
| 191 |
0 |
throw new EOGeneralAdaptorException("A stored procedure operation must have a stored procedure assigned to it.", |
| 192 |
0 |
new NSDictionary(operation, "operation")); |
| 193 |
0 |
executeStoredProcedure(operation.storedProcedure(), operation.changedValues()); |
| 194 |
0 |
break; |
| 195 |
|
default: |
| 196 |
0 |
throw new EOGeneralAdaptorException("I don't know how to perform an operation with code " + opcode, |
| 197 |
0 |
new NSDictionary(operation, "operation")); |
| 198 |
|
} |
| 199 |
0 |
} |
| 200 |
|
|
| 201 |
|
public void performAdaptorOperations(NSArray ops) { |
| 202 |
0 |
for (int i = 0; i < ops.count(); i++) { |
| 203 |
0 |
EOAdaptorOperation adop = (EOAdaptorOperation)ops.objectAtIndex(i); |
| 204 |
0 |
performAdaptorOperation(adop); |
| 205 |
|
} |
| 206 |
0 |
} |
| 207 |
|
|
| 208 |
|
public NSArray primaryKeysForNewRowsWithEntity(int count, EOEntity entity) { |
| 209 |
0 |
NSDictionary[] keys = new NSDictionary[count]; |
| 210 |
0 |
for (int i = 0; i < count; i++) |
| 211 |
0 |
keys[i] = NSDictionary.EmptyDictionary; |
| 212 |
0 |
return new NSArray(keys); |
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
public void updateValuesInRowDescribedByQualifier(NSDictionary row, EOQualifier q, EOEntity entity) { |
| 216 |
0 |
adaptorContext().beginTransaction(); |
| 217 |
0 |
int count = updateValuesInRowsDescribedByQualifier(row, q, entity); |
| 218 |
0 |
if (count != 1) { |
| 219 |
0 |
adaptorContext().rollbackTransaction(); |
| 220 |
0 |
throw new EOGeneralAdaptorException("The qualifier should describe exactly one row (updated " + count + " rows)"); |
| 221 |
|
} |
| 222 |
0 |
adaptorContext().commitTransaction(); |
| 223 |
0 |
} |
| 224 |
|
|
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|
| 239 |
|
|
| 240 |
|
|
| 241 |
|
|
| 242 |
|
|
| 243 |
|
|
| 244 |
|
|