Re: EOF & pgSQL join syntax
Re: EOF & pgSQL join syntax
- Subject: Re: EOF & pgSQL join syntax
- From: Ben Ketteridge <email@hidden>
- Date: Fri, 30 Apr 2004 10:00:10 +0100
Chuck Hill wrote:
> I've never done this. As near as I can figure it...
>
> EOSQLExpression has this method:
>
> public String assembleJoinClause(String leftName,
> String rightName,
> int semantic)
> This method is invoked from addJoinClause to return a JOIN clause. The
> clause is of the form:
> leftName operator rightName
> where operator is "=" for an inner join, "*=" for a left-outer join, and
> "=*" for a right-outer join.
>
> Therein lies the "root of evil".
>
> For our purposes we are interested in the subclass
> com.webobjects.jdbcadaptor.JDBCExpression. You will need to subclass that,
> and re-implement assembleJoinClause to do what you want. Who knows, you
> might need to attend to some other methods as well. If there is a
> PostgreSQL plugin, then it probably already has this class.
>
> Then our task becomes, "How do I get EOF to use this expression class?".
> Here is where Art's point comes into play.
> com.webobjects.jdbcadaptor.JDBCPlugIn has the method
> public Class defaultExpressionClass()
> which you can use to the return the clase of your customized JDBCExpression
> sub-class.
>
> I *think* that should do it, but there are a few other methods that you
> might want to look over if things don't work. Not trivial, but totally
> doable.
>
Attached is the Cache plugin class I recently wrote. It deals with (amongst
other things) the issue of the outer JOIN syntax manipulation. Apparently the
default implementation in the WO JDBCPlugIn uses the =*/*= the 'wrong' way
round. :)
The other main issue this plugin addresses is the read-only primary key
generation (pseudo-sequences) that I've managed to achieve by getting Cache to
impersonate the EO_PK_TABLE.PK column with an increment-on-read field.
One gotcha is, however, that the PlugIn class must be in the
com.webobjects.jdbcadaptor package. If anyone knows how to get the WO
makefiles to build the plugin into a normal framework & its jar file, I'd be
very happy to hear. Currently I have to use Eclipse to build the project with
this class in it, and then manually add it to the project jar file.
HTH
--
Kind Regards
Ben.
Dr Ben Ketteridge
email@hidden
Team Leader,
ProAct International,
PO Box 100, Denbigh, UK.
Tel: 01745 817161 ext. 322
/* $Revision: 1.1 $
* Created on 26-Apr-2004
*/
package com.webobjects.jdbcadaptor;
import com.webobjects.foundation.*;
import com.webobjects.eoaccess.*;
/**
* @author Ben Ketteridge
*/
public class CachePlugIn extends JDBCPlugIn {
/**
* @param arg0
*/
public CachePlugIn(JDBCAdaptor arg0) {
super(arg0);
}
public Class defaultExpressionClass() {
return CacheExpression.class;
}
public String databaseProductName() {
return "Cache";
}
public String defaultDriverName() {
return "com.intersys.jdbc.CacheDriver";
}
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();
}
NSArray attributes = _pkAttributes();
NSMutableArray ma = new NSMutableArray(count);
NSArray results = null;
Number pk = null;
String selectString = "select "+PK_SELECT_ATTRIBUTE_NAME+" from "+primaryKeyTableName()+" where name = '"+entity.name()+"'";
EOSQLExpression selectExpression = (EOSQLExpression)expressionFactory().expressionForString(selectString);
String insertString = "insert into "+primaryKeyTableName()+" (name) values ('"+entity.name()+"')";
EOSQLExpression insertExpression = null;
for (int ii =0; ii < count; ii++) {
results = channel._fetchRowsForSQLExpressionAndAttributes(selectExpression, attributes);
if (results != null && results.count() > 0) {
pk = (Number)((NSKeyValueCoding)results.lastObject()).valueForKey(PK_SELECT_ATTRIBUTE_NAME);
pk = new Integer(pk.intValue() + 1);
}
else {
if (insertExpression == null) {
insertExpression = (EOSQLExpression)expressionFactory().expressionForString(insertString);
}
results = channel._fetchRowsForSQLExpressionAndAttributes(insertExpression, new NSArray());
pk = new Integer(1);
}
ma.addObject(new NSDictionary(pk, entity.primaryKeyAttributeNames().objectAtIndex(0)));
}
return ma;
}
private static final String PK_SELECT_ATTRIBUTE_NAME = "PK";
private NSArray _pkAttributes() {
EOAttribute pkSelectAttribute = new EOAttribute();
pkSelectAttribute.setName(PK_SELECT_ATTRIBUTE_NAME);
pkSelectAttribute.setColumnName(pkSelectAttribute.name());
pkSelectAttribute.setClassName("java.lang.Number");
pkSelectAttribute.setValueType("i");
pkSelectAttribute.setReadFormat(pkSelectAttribute.name());
return new NSArray(pkSelectAttribute);
}
// just for outer join syntax
public static class CacheExpression extends JDBCExpression {
public CacheExpression(EOEntity entity) {
super(entity);
}
public String assembleJoinClause(String leftName,
String rightName,
int semantic) {
String operator = "=";
switch (semantic) {
case EORelationship.LeftOuterJoin:
operator += "*";
break;
case EORelationship.RightOuterJoin:
operator = "*" + operator;
}
return leftName + " " + operator + " " + rightName;
}
}
}
_______________________________________________
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.