Erlang/OTP Forums

Author Message

<  Erlyweb mailing list  ~  Dearth of documentation on CRUD from models / CRUD documenta

Guest
Posted: Tue Aug 12, 2008 12:24 pm Reply with quote
Guest
Would anyone please point me to a bunch of examples how those functions
in my models should/could/would be used?

I'd like to know for instance how to aggregate results from tables.

An example I'm working on:

I have 3 tables, entryT, entry_projectT, projectT.

I get a request for all entries pertaining to some project, so I get the
id from projectT.

<code>
...
ProjectId = project:find_first({name, '=', ProjectName})
...
</code>

Then I'd like to get all the entry_id(s) from entry_projectT. From that
entry_id(s) result from entry_projectT, I render all the entries in entryT.

<code>
...
, EntryProjects = entry_project:find_max_with(N, ...(?)...)
, EntryIds = [ entry_project:entry_id(EP) || EP <- EntryProjects ]
, Entries = [ entry:find_id(Id) || Id <- EntryIds ] %%% N queries to db
...
</code>

Firing off N queries is stupid. Denormalize? Maybe later. How do I
aggregate using crud?

I only use a handful of those functions because I simply have no idea
what they do. Is anyone secretly holding back documentation? If not
please share. I've been hacking some of my projects in erlyweb using
erlsql, which has more documentation about it.
It would be a shame if I were be forced to use erlsql, since I suspect
those crud thingies are better than this low-level sql hacking.

Things I know so far:

functions that match the name of a field are get/setters. The gets are
arity 1. The sets are arity 2. First parameter is for the record, Second
for the value to be changed.

max, sum, find, find_first etc. are all pretty easy to grok.

An few examples about each would be nice though.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post received from mailinglist
Guest
Posted: Tue Aug 12, 2008 6:27 pm Reply with quote
Guest
On Tue, Aug 12, 2008 at 5:24 AM, Buggaboo <buggasnoo@gmail.com> wrote:
>
> Would anyone please point me to a bunch of examples how those functions
> in my models should/could/would be used?
>
> I'd like to know for instance how to aggregate results from tables.
>
> An example I'm working on:
>
> I have 3 tables, entryT, entry_projectT, projectT.
>
> I get a request for all entries pertaining to some project, so I get the
> id from projectT.
>
> <code>
> ...
> ProjectId = project:find_first({name, '=', ProjectName})
> ...
> </code>
>
> Then I'd like to get all the entry_id(s) from entry_projectT. From that
> entry_id(s) result from entry_projectT, I render all the entries in entryT.
>
> <code>
> ...
> , EntryProjects = entry_project:find_max_with(N, ...(?)...)
> , EntryIds = [ entry_project:entry_id(EP) || EP <- EntryProjects ]
> , Entries = [ entry:find_id(Id) || Id <- EntryIds ] %%% N queries to db
> ...
> </code>
>
> Firing off N queries is stupid. Denormalize? Maybe later. How do I
> aggregate using crud?

ErlyDB is designed for simple single-table queries (insert, update,
select, delete). The exception is in many-to-many relations, which
performs a join against the relation table. For more complex stuff the
rule of thumb is to fall back on (Erl)SQL. To run your query
efficiently, you could try this:

EntryProjects = entry_project:find_max_with(N, ...(?)...),
Entries = entry:find({id, in, [entry_project:entry_id(EP) | Ep <-
EntryProjects]}).

To optimize even further you could try

entry:find({id, in, {select, [entry_id], from, entry_project, {limit, 5, 10}}}).

but to be honest I haven't tried using nested ErlSQL queries in ErlyDB
expressions so you'd be venturing into uncharted territory.


>
> I only use a handful of those functions because I simply have no idea
> what they do. Is anyone secretly holding back documentation? If not
> please share. I've been hacking some of my projects in erlyweb using
> erlsql, which has more documentation about it.
> It would be a shame if I were be forced to use erlsql, since I suspect
> those crud thingies are better than this low-level sql hacking.
>
> Things I know so far:
>
> functions that match the name of a field are get/setters. The gets are
> arity 1. The sets are arity 2. First parameter is for the record, Second
> for the value to be changed.
>
> max, sum, find, find_first etc. are all pretty easy to grok.
>
> An few examples about each would be nice though.

ErlyDB is a rather shallow layer over ErlSQL and if you really want to
know what it does you can look at erlydb_base.erl. Which functions are
you unsure about? Besides what you mentioned there are a couple of
other patterns: every function "base" e.g. "find" has 4 variaties:
find(), find(Where), find_with(Extras) and find(Where, Extras). Extras
are modifiers such as {limit, X} and {order_by, Foo}. 'Where' is a
where expression e.g. {id,'=',5}. Pretty much all the functions that
generate SELECT queries have these 4 varieties.

Yariv

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post received from mailinglist

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum