Hi:
I am using tdk 1.1a10.
This question begins when I want write a select count(abc.id) from abc;
using Peer.
Of cause I can use BasePeer + raw sql clauses to do it. But if I want do it
with AbcPeer. I met some problem.
I have this codes:
crit.addSelectColumn("count(" + AbcPeer.Id + ")");
AbcPeer.doSelect(crit);
***Problem 1: the generated sql:*********
Mysql gives out an error: I check the generated sql, it is:
select count(abc.id), id, name, address from abc;
This is not a correct clause.
I check the AbcPeer.doSelect function:
_______________________________________________________________
public static Vector doSelect( Criteria criteria, String className,
DBConnection dbCon) throws Exception
{
addSelectColumns ( criteria );
// BasePeer returns a Vector of Value (Village) arrays. The array
// order follows the order columns were placed in the Select clause.
Vector rows = null;
if (dbCon == null)
{
rows = BasePeer.doSelect(criteria);
}
else
{
rows = BasePeer.doSelect(criteria, dbCon);
}
Vector results = new Vector();
// populate the object(s)
for ( int i=0; i<rows.size(); i++ )
{
Record row = (Record)rows.elementAt(i);
results.add (row2Object (row,1,Class.forName (className)));
}
return results;
}
_______________________________________________________________
The line: addSelectColumns ( criteria ); will bring all the
columns into select clause. This cause the problem.
I think it should be
if(criteria.getSelectColumns().size() == 0)
addSelectColumns ( criteria );
***********Problem 2: the return vector**********************
the line: results.add (row2Object (row,1,Class.forName
(className)));
And the row2Object function is just for a select (*) from abc clause. Other
sql clause can not use that function.
For this problem I think there should be 2 solution:
1. add another doRawSelect() function which will only return the vector
returned from BasePeer.doSelect().
User should normalize the result by themselvs. This solution brings flexible
for users.
2. the user do not use AbcPeer directly. They first write a class inherated
from AbcPeer. And override the row2Object funtion to something like:
if(cls.getName().equals("xxxxx")
.......
if(cls.getName().equals("yyyyy")
.......
This solution do not need to touch turbine code. But not convenient for
uses.
3. add a getCount() function will get the count for user. This should used
with solution 1. Something like:
int static getCount()
crit.addSelectColumn("count(" + AbcPeer.Id + ")");
Vector row = AbcPeer.doRawSelect(crit);
int count = row.getValue(offset+0).asInt()
return count;
}
This is most convenient solution. But this solution only solve the count(*)
problem. May need max, min .....
Please give me advise which will be the best solution. After get your feed
back, I will try to make a change in TDK om templates, and submit the
change back to turbine group.
Regards& Thanks
fanyun
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?: [EMAIL PROTECTED]