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.
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