Raw query

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Raw query

Amedeo Mantica
Hi all,

Ho to execute a simple raw query with cayenne ?

let say: SELECT COUNT(1) FROM MY_ENTITY;

Thank you
Amedeo
Reply | Threaded
Open this post in threaded view
|

Re: Raw query

Nikita Timofeev
Hi Amedeo,

For raw queries you can use SQLTemplate, like this:

SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
FROM MY_ENTITY");
query.setFetchingDataRows(true);
List<DataRow> rows = (List<DataRow>) context.performQuery(query);

You can also use aggregate functions directly with Cayenne API (since
version 4.0.M5):

long count = ObjectSelect.query(MyEntity.class).selectCount(context);

Hope this helps!

On Sat, Apr 29, 2017 at 4:49 PM, Amedeo Mantica <[hidden email]> wrote:
> Hi all,
>
> Ho to execute a simple raw query with cayenne ?
>
> let say: SELECT COUNT(1) FROM MY_ENTITY;
>
> Thank you
> Amedeo



--
Best regards,
Nikita Timofeev
Reply | Threaded
Open this post in threaded view
|

Re: Raw query

Amedeo Mantica
thank you so much !

Amedeo

> On 29 Apr 2017, at 16:22, Nikita Timofeev <[hidden email]> wrote:
>
> Hi Amedeo,
>
> For raw queries you can use SQLTemplate, like this:
>
> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
> FROM MY_ENTITY");
> query.setFetchingDataRows(true);
> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
>
> You can also use aggregate functions directly with Cayenne API (since
> version 4.0.M5):
>
> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
>
> Hope this helps!
>
> On Sat, Apr 29, 2017 at 4:49 PM, Amedeo Mantica <[hidden email]> wrote:
>> Hi all,
>>
>> Ho to execute a simple raw query with cayenne ?
>>
>> let say: SELECT COUNT(1) FROM MY_ENTITY;
>>
>> Thank you
>> Amedeo
>
>
>
> --
> Best regards,
> Nikita Timofeev

Reply | Threaded
Open this post in threaded view
|

Re: Raw query

Andrus Adamchik
In reply to this post by Nikita Timofeev
> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
> FROM MY_ENTITY");
> query.setFetchingDataRows(true);
> List<DataRow> rows = (List<DataRow>) context.performQuery(query);

Or you can use SQLSelect as a type-safe and more user-friendly flavor doing the same thing:

  List<DataRow> rows = SQLSelect.dataRowQuery("SELECT COUNT(1) FROM MY_ENTITY").select(context);

> You can also use aggregate functions directly with Cayenne API (since
> version 4.0.M5):
>
> long count = ObjectSelect.query(MyEntity.class).selectCount(context);

Yep, this one is the best option.

Andrus


> On Apr 29, 2017, at 5:22 PM, Nikita Timofeev <[hidden email]> wrote:
>
> Hi Amedeo,
>
> For raw queries you can use SQLTemplate, like this:
>
> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
> FROM MY_ENTITY");
> query.setFetchingDataRows(true);
> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
>
> You can also use aggregate functions directly with Cayenne API (since
> version 4.0.M5):
>
> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
>
> Hope this helps!
>
> On Sat, Apr 29, 2017 at 4:49 PM, Amedeo Mantica <[hidden email]> wrote:
>> Hi all,
>>
>> Ho to execute a simple raw query with cayenne ?
>>
>> let say: SELECT COUNT(1) FROM MY_ENTITY;
>>
>> Thank you
>> Amedeo
>
>
>
> --
> Best regards,
> Nikita Timofeev

Reply | Threaded
Open this post in threaded view
|

Re: Raw query

Amedeo Mantica
Cool.
However I'm using the 3.1.2 yet
Amedeo

> On 30 Apr 2017, at 10:21, Andrus Adamchik <[hidden email]> wrote:
>
>> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
>> FROM MY_ENTITY");
>> query.setFetchingDataRows(true);
>> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
>
> Or you can use SQLSelect as a type-safe and more user-friendly flavor doing the same thing:
>
>  List<DataRow> rows = SQLSelect.dataRowQuery("SELECT COUNT(1) FROM MY_ENTITY").select(context);
>
>> You can also use aggregate functions directly with Cayenne API (since
>> version 4.0.M5):
>>
>> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
>
> Yep, this one is the best option.
>
> Andrus
>
>
>> On Apr 29, 2017, at 5:22 PM, Nikita Timofeev <[hidden email]> wrote:
>>
>> Hi Amedeo,
>>
>> For raw queries you can use SQLTemplate, like this:
>>
>> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
>> FROM MY_ENTITY");
>> query.setFetchingDataRows(true);
>> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
>>
>> You can also use aggregate functions directly with Cayenne API (since
>> version 4.0.M5):
>>
>> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
>>
>> Hope this helps!
>>
>> On Sat, Apr 29, 2017 at 4:49 PM, Amedeo Mantica <[hidden email]> wrote:
>>> Hi all,
>>>
>>> Ho to execute a simple raw query with cayenne ?
>>>
>>> let say: SELECT COUNT(1) FROM MY_ENTITY;
>>>
>>> Thank you
>>> Amedeo
>>
>>
>>
>> --
>> Best regards,
>> Nikita Timofeev
>

Reply | Threaded
Open this post in threaded view
|

Re: Raw query

Andrus Adamchik
SQLTemplate it is then.

> On Apr 30, 2017, at 9:02 PM, Amedeo Mantica <[hidden email]> wrote:
>
> Cool.
> However I'm using the 3.1.2 yet
> Amedeo
>
>> On 30 Apr 2017, at 10:21, Andrus Adamchik <[hidden email]> wrote:
>>
>>> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
>>> FROM MY_ENTITY");
>>> query.setFetchingDataRows(true);
>>> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
>>
>> Or you can use SQLSelect as a type-safe and more user-friendly flavor doing the same thing:
>>
>> List<DataRow> rows = SQLSelect.dataRowQuery("SELECT COUNT(1) FROM MY_ENTITY").select(context);
>>
>>> You can also use aggregate functions directly with Cayenne API (since
>>> version 4.0.M5):
>>>
>>> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
>>
>> Yep, this one is the best option.
>>
>> Andrus
>>
>>
>>> On Apr 29, 2017, at 5:22 PM, Nikita Timofeev <[hidden email]> wrote:
>>>
>>> Hi Amedeo,
>>>
>>> For raw queries you can use SQLTemplate, like this:
>>>
>>> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
>>> FROM MY_ENTITY");
>>> query.setFetchingDataRows(true);
>>> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
>>>
>>> You can also use aggregate functions directly with Cayenne API (since
>>> version 4.0.M5):
>>>
>>> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
>>>
>>> Hope this helps!
>>>
>>> On Sat, Apr 29, 2017 at 4:49 PM, Amedeo Mantica <[hidden email]> wrote:
>>>> Hi all,
>>>>
>>>> Ho to execute a simple raw query with cayenne ?
>>>>
>>>> let say: SELECT COUNT(1) FROM MY_ENTITY;
>>>>
>>>> Thank you
>>>> Amedeo
>>>
>>>
>>>
>>> --
>>> Best regards,
>>> Nikita Timofeev
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Raw query

Michael Gentry
Hi Amedeo,

Another option for 3.1 is to use my AggregateUtils:

https://github.com/mrg/cbe/tree/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/utilities

(You'd need both of the files there.)

With it you get count, min, max, avg, and sum functions.

For your example, this is how you would do a count:

SelectQuery query = new SelectQuery(MyEntity.class);
long count = AggregateUtils.count(dataContext, query);

An example of all 5 aggregate functions in use can be found here:

https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/Aggregates.java

mrg


On Mon, May 1, 2017 at 3:55 AM, Andrus Adamchik <[hidden email]>
wrote:

> SQLTemplate it is then.
>
> > On Apr 30, 2017, at 9:02 PM, Amedeo Mantica <[hidden email]>
> wrote:
> >
> > Cool.
> > However I'm using the 3.1.2 yet
> > Amedeo
> >
> >> On 30 Apr 2017, at 10:21, Andrus Adamchik <[hidden email]>
> wrote:
> >>
> >>> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
> >>> FROM MY_ENTITY");
> >>> query.setFetchingDataRows(true);
> >>> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
> >>
> >> Or you can use SQLSelect as a type-safe and more user-friendly flavor
> doing the same thing:
> >>
> >> List<DataRow> rows = SQLSelect.dataRowQuery("SELECT COUNT(1) FROM
> MY_ENTITY").select(context);
> >>
> >>> You can also use aggregate functions directly with Cayenne API (since
> >>> version 4.0.M5):
> >>>
> >>> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
> >>
> >> Yep, this one is the best option.
> >>
> >> Andrus
> >>
> >>
> >>> On Apr 29, 2017, at 5:22 PM, Nikita Timofeev <
> [hidden email]> wrote:
> >>>
> >>> Hi Amedeo,
> >>>
> >>> For raw queries you can use SQLTemplate, like this:
> >>>
> >>> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
> >>> FROM MY_ENTITY");
> >>> query.setFetchingDataRows(true);
> >>> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
> >>>
> >>> You can also use aggregate functions directly with Cayenne API (since
> >>> version 4.0.M5):
> >>>
> >>> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
> >>>
> >>> Hope this helps!
> >>>
> >>> On Sat, Apr 29, 2017 at 4:49 PM, Amedeo Mantica <[hidden email]>
> wrote:
> >>>> Hi all,
> >>>>
> >>>> Ho to execute a simple raw query with cayenne ?
> >>>>
> >>>> let say: SELECT COUNT(1) FROM MY_ENTITY;
> >>>>
> >>>> Thank you
> >>>> Amedeo
> >>>
> >>>
> >>>
> >>> --
> >>> Best regards,
> >>> Nikita Timofeev
> >>
> >
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Raw query

Amedeo Mantica
cool, thank you

> On 1 May 2017, at 13:52, Michael Gentry <[hidden email]> wrote:
>
> Hi Amedeo,
>
> Another option for 3.1 is to use my AggregateUtils:
>
> https://github.com/mrg/cbe/tree/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/utilities
>
> (You'd need both of the files there.)
>
> With it you get count, min, max, avg, and sum functions.
>
> For your example, this is how you would do a count:
>
> SelectQuery query = new SelectQuery(MyEntity.class);
> long count = AggregateUtils.count(dataContext, query);
>
> An example of all 5 aggregate functions in use can be found here:
>
> https://github.com/mrg/cbe/blob/master/FetchingObjects/Aggregates/src/main/java/cbe/fetching/Aggregates.java
>
> mrg
>
>
> On Mon, May 1, 2017 at 3:55 AM, Andrus Adamchik <[hidden email]>
> wrote:
>
>> SQLTemplate it is then.
>>
>>> On Apr 30, 2017, at 9:02 PM, Amedeo Mantica <[hidden email]>
>> wrote:
>>>
>>> Cool.
>>> However I'm using the 3.1.2 yet
>>> Amedeo
>>>
>>>> On 30 Apr 2017, at 10:21, Andrus Adamchik <[hidden email]>
>> wrote:
>>>>
>>>>> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
>>>>> FROM MY_ENTITY");
>>>>> query.setFetchingDataRows(true);
>>>>> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
>>>>
>>>> Or you can use SQLSelect as a type-safe and more user-friendly flavor
>> doing the same thing:
>>>>
>>>> List<DataRow> rows = SQLSelect.dataRowQuery("SELECT COUNT(1) FROM
>> MY_ENTITY").select(context);
>>>>
>>>>> You can also use aggregate functions directly with Cayenne API (since
>>>>> version 4.0.M5):
>>>>>
>>>>> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
>>>>
>>>> Yep, this one is the best option.
>>>>
>>>> Andrus
>>>>
>>>>
>>>>> On Apr 29, 2017, at 5:22 PM, Nikita Timofeev <
>> [hidden email]> wrote:
>>>>>
>>>>> Hi Amedeo,
>>>>>
>>>>> For raw queries you can use SQLTemplate, like this:
>>>>>
>>>>> SQLTemplate query = new SQLTemplate(MyEntity.class, "SELECT COUNT(1)
>>>>> FROM MY_ENTITY");
>>>>> query.setFetchingDataRows(true);
>>>>> List<DataRow> rows = (List<DataRow>) context.performQuery(query);
>>>>>
>>>>> You can also use aggregate functions directly with Cayenne API (since
>>>>> version 4.0.M5):
>>>>>
>>>>> long count = ObjectSelect.query(MyEntity.class).selectCount(context);
>>>>>
>>>>> Hope this helps!
>>>>>
>>>>> On Sat, Apr 29, 2017 at 4:49 PM, Amedeo Mantica <[hidden email]>
>> wrote:
>>>>>> Hi all,
>>>>>>
>>>>>> Ho to execute a simple raw query with cayenne ?
>>>>>>
>>>>>> let say: SELECT COUNT(1) FROM MY_ENTITY;
>>>>>>
>>>>>> Thank you
>>>>>> Amedeo
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Best regards,
>>>>> Nikita Timofeev
>>>>
>>>
>>
>>