PK generation
Ben Ketteridge <[email protected]> Mon, 26 Apr 2004 17:07:18 +0100
| Newsgroups | gmane.comp.web.webobjects.eof,gmane.comp.web.webobjects.devel |
|---|---|
| Message-ID | <[email protected]> |
I have a DB that does not provide Sequences in the same way as Oracle. However it does provide me with the functionality by where the PK field of EO_PK_TABLE is auto-incremented ON READ. Consequently I don't need to write to the table EXCEPT when I'm generating a primary key for the first time on a given table. I can't seem to persuade EOF to get primary keys from this EO_PK_TABLE either by the standard method (not doing anything, except allowing EOF to believe there is no sequence support), nor by overriding newPrimaryKeys in JDBCPlugin. Anyone any suggestions? My code is attached. Many TIA! -- Kind Regards Ben. Dr Ben Ketteridge [email protected] Team Leader, ProAct International, PO Box 100, Denbigh, UK. Tel: 01745 817161 ext. 322 _______________________________________________ EOF mailing list [email protected] http://www.omnigroup.com/mailman/listinfo/eof
CachePlugIn.java
(text/plain, 2.6 KB)
/*
* Created on 26-Apr-2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package com.webobjects.jdbcadaptor;
import com.webobjects.foundation.*;
import com.webobjects.eoaccess.*;
import com.webobjects.eocontrol.EOFetchSpecification;
import com.webobjects.eocontrol.EOQualifier;
/**
* @author Ben Ketteridge
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class CachePlugIn extends JDBCPlugIn {
/**
* @param arg0
*/
public CachePlugIn(JDBCAdaptor arg0) {
super(arg0);
}
public NSArray newPrimaryKeys(int count, EOEntity entity, JDBCChannel channel) {
if (count <= 0 || entity == null || entity.primaryKeyAttributeNames().count() > 1
|| channel == null || !channel.isOpen()) {
return new NSArray();
}
NSMutableArray ma = new NSMutableArray(count);
NSDictionary results = null;
Number pk = null;
EOSQLExpressionFactory expressionFactory = new EOSQLExpressionFactory(channel.adaptorContext().adaptor());
EOSQLExpression expression = (EOSQLExpression)expressionFactory.createExpression(entity);
expression.setStatement("select PK from "+primaryKeyTableName()+" where name = '"+entity.name()+"'");
EOAttribute pkCountAttribute = new EOAttribute();
pkCountAttribute.setName("PK");
pkCountAttribute.setColumnName("PK");
pkCountAttribute.setClassName("java.lang.Number");
pkCountAttribute.setValueType("i");
pkCountAttribute.setReadFormat("PK");
channel.setAttributesToFetch(new NSArray(pkCountAttribute));
for (int ii =0; ii < count; ii++) {
channel.evaluateExpression(expression);
results = channel.fetchRow();
if (results.count() > 0) {
pk = (Number)((NSKeyValueCoding)results.allValues().objectAtIndex(0)).valueForKey("PK");
}
else {
expression.setStatement("insert into "+primaryKeyTableName()+" (Name) values ('"+entity.name()+"')");
channel.evaluateExpression(expression);
pk = new Integer(1);
expression.setStatement("select PK from "+primaryKeyTableName()+" where name = '"+entity.name()+"'");
}
ma.addObject(new NSDictionary(pk, entity.primaryKeyAttributeNames().objectAtIndex(0)));
}
return ma;
}
}