I'm starting to see the idea behind the erlydb crud functions. They
serve as shorthand for common erlsql patterns. I shall take a good
long look at erlydb_base.erl

Thanks for the elucidation.

On Aug 12, 8:26 pm, "Yariv Sadan" <[EMAIL PROTECTED]> wrote:
> On Tue, Aug 12, 2008 at 5:24 AM, Buggaboo <[EMAIL PROTECTED]> 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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to