cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Florin T.PATRASCU" <f...@rogers.com>
Subject Re: count(*) with parameter
Date Sun, 18 Apr 2010 16:59:16 GMT
or something like this:
 ...
 String sql = "select count(*) as total_clicks from user_clicks " +
              " where user_id = #bind( $userId, 'VARCHAR')";

 SQLTemplate sqlTemplate = new SQLTemplate( UserClick.class, sql);
 sqlTemplate.setParameters(params); //params defined elsewhere 
 sqlTemplate.setFetchingDataRows(true);
 
 List res = BaseContext.getThreadObjectContext().performQuery( sqlTemplate);
 return ((Integer) ((Map) res.get(0)).get("total_clicks")).intValue();
}

it works :)
-florin


On 18-Apr-10, at 12:43 , Joe Baldwin wrote:

> Arnaud,
> 
> If I understand you, I think you want to do this in the code and not the modeler?
> 
> Here is a simple code fragment for a like-ignore-case qualifier:
> 
> 	String substring = "Martin";
> 	ObjectContext oc = BaseContext.getThreadObjectContext();
> 	Expression exp = ExpressionFactory.likeIgnoreCaseExp(Product.NAME_PROPERTY, ("%" + substring
+ "%"));
> 	SelectQuery query = new SelectQuery(Product.class, exp);
> 	List list = oc.performQuery(query);
> 	System.out.println("\ncount: " + list.size());
> 
> This is pretty efficient and to my knowledge does not trigger any DataObject faults.
 There are a few other ways to do this depending on your design.  The ExpresionFactory has
a lot of powerful methods to help you out.
> 
> Joe
> 
> 
> 
> 
> On Apr 18, 2010, at 12:15 PM, Arnaud Garcia wrote:
> 
>> Hello,
>> 
>> I am not sure how to create a select count(*) in the modeler when I need to
>> pass a parameter ?
>> I downloaded the petstore example, and it seems that I have to create a "Raw
>> SQL" Query, and directly write my select count(*)...
>> 
>> =>but how to I had my parameter in my SQL ?
>> 
>> ... then I suppose I can do ctxt.performQuery("myCount",params,true) ?
>> 
>> thanks for help for this beginner question...
>> 
>> Arnaud
> 


Mime
View raw message