Re: PK generation
Re: PK generation
- Subject: Re: PK generation
- From: Jonathan Rochkind <email@hidden>
- Date: Mon, 26 Apr 2004 11:29:41 -0500
I have used newPrimaryKeys in a custom JBCPlugIn sub-class to
customize pk generation behavior. It's worked fine for me. I'm
pretty confident you should be able to customize pk generation
behavior however you want using this method.
I haven't looked in detail at your code, but: Are you sure the
JDBCPlugIn class you think is being used is actually being used? If
not, then it doesn't matter what your code is, you've got to figure
out why not. (Not specified in the model properly?). If your custom
JDBCPlugIn class is being used, and your method is being called (all
of this can be easily confirmed with a debugger), then you've got to
figure out why your code is not doing what you want it to do (again,
a debugger would be valuable here).
--Jonathan
At 5:07 PM +0100 4/26/04, Ben Ketteridge wrote:
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@hidden
Team Leader,
ProAct International,
PO Box 100, Denbigh, UK.
Tel: 01745 817161 ext. 322
/*
* 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;
}
}
_______________________________________________
EOF mailing list
email@hidden
http://www.omnigroup.com/mailman/listinfo/eof
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.