Re: Fetching record counts.
Re: Fetching record counts.
- Subject: Re: Fetching record counts.
- From: "Ruenagel, Frank" <email@hidden>
- Date: Wed, 2 Feb 2005 10:58:40 +0100
Yeah! I have done this before. Works fine. :-)
/** * Returns the number of records of a fetchspec
*
* @param fsx -- die Fetchspecification
* @param ec -- the EC
* @return
*/
public Long countRows(EOFetchSpecification fsx,
EOEditingContext ec)
{
EOFetchSpecification myfs = (EOFetchSpecification)
fsx.clone();
EOEntity myEnitty = EOUtilities.entityNamed(ec,
myfs.entityName() ) ;
EODatabaseContext dbc = EOUtilities.
databaseContextForModelNamed( ec,
myEnitty.model().name()) ;
NSMutableArray myergebnis = new NSMutableArray();
dbc.lock();
try
{
EOAdaptorChannel eodbc =
dbc.availableChannel().adaptorChannel() ; //this could thoretically be null?
if (!eodbc.isOpen()) {eodbc.openChannel(); }
myfs.setSortOrderings(null); // count(1) does not
like order by clauses...
EOSQLExpression mysqlE =
EOAdaptor.adaptorWithModel(myEnitty.model()).expressionFactory().expressionF
orEntity(myEnitty);
NSMutableArray myColumnsM = new NSMutableArray();
myColumnsM.addObject(myEnitty.attributesToFetch().objectAtIndex(0)); // Take
any attribute as Dummy, we will replace it
mysqlE.prepareSelectExpressionWithAttributes(myColumnsM , false, myfs) ;
// create SQL-Statement-String
String myStatement = mysqlE.statement();
//this.app().logger.debug("Initial statement:" +
myStatement );
int myIndex = myStatement.indexOf(" FROM "); //
find the magic word
myStatement = myStatement.substring(myIndex,
myStatement.length() ); // skip everything before "FROM"
myStatement = "SELECT COUNT(1) " + myStatement;
mysqlE.setStatement(myStatement);
//this.app().logger.debug("Vor evaluate:" +
myStatement );
eodbc.evaluateExpression(mysqlE) ;
eodbc.setAttributesToFetch(null); // we don't need
any data
NSMutableDictionary myrow= eodbc.fetchRow(); //
fetch the only single row
eodbc.cancelFetch();
return new
Long(myrow.allValues().objectAtIndex(0).toString());
}
catch (Exception e)
{
System.out.println("Counter sucks!");
e.printStackTrace();
return new Long("0");
}
finally
{
dbc.unlock();
}
}
> -----Original Message-----
> From: Anjo Krank [mailto:email@hidden]
> Sent: Tuesday, February 01, 2005 9:57 PM
> To: Chuck Hill
> Cc: 'email@hidden' Development
> Subject: Re: Fetching record counts.
>
>
>
> Am 01.02.2005 um 20:10 schrieb Chuck Hill:
>
> > Here are some other counting implementation that I have
> gleaned from
> > the list. Perhaps they are of interest?
>
> Instead of adding an attribute to the model, it'd be better
> to get the
> expression factory, create the statement with the FS, remove
> the stuff
> until the " FROM " and add a "SELECT count(*) " in it's stead. Then
> execute it and fetch the row count. Threadsave and still
> model-compliant. Adding the attribute at runtime just can't be made
> thread-save (locking the OSC won't help, because eg. D2W just
> accesses
> model data and doesn't fetch)
>
> Cheers, Anjo
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Webobjects-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden