On Thu, Jan 3, 2013 at 5:32 AM, Daniel Gruno <rum...@cord.dk> wrote:

> -------------------------
> Connecting to a database:
> -------------------------
> function handler(r)
>     local db, error = r:dbopen("mod_dbd") -- Open a mod_dbd connection
>

Shouldn't this be a method on the server representation, not the request
representation?


>     if error then ... end
>     -- or...
>     local db, error = r:dbopen("mysql",
> "server=localhost,user=root,database=somedb")
>     -- essentially the same as mod_dbd's connection string.
>     do_stuff()
>     db:close() -- close the db handle (can also be done by GC)
>

Hmm, if db here represents a handle, it should prolly be paired with
acquire not open.


>     local still_running = db:active() -- returns false, since we closed
>                                       -- the connection.
> end
>
> ---------
> Querying:
> ---------
> -- Run a command and get the no. of rows affected:
> local affected, err = db:do(r, "DELETE FROM `table` WHERE 1")
> if err then
>     print("DB error: " .. err)
> else
>     print("Deleted " .. affected .. " rows!")
> end
>
> -- Run a query and get the rows returned:
> local rows, err = db:query(r, "SELECT `name` FROM `table` WHERE 1")
>

Check your errors :-)

Also, be careful what you return, you don't want to the API to force you to
realize all results from a query eagerly.


> if rows then
>     r:puts("We got " .. #rows " .. " results!")
>     for k, row in pairs(rows) do
>         print("Name: " .. row[1] .. "<br/>")
>     end
> else
>     r:puts("DB error: " .. err)
> end
>
> -- Run a prepared statement and inject values into it:
> local rows, err = db:inject(r, "SELECT `name` FROM `tbl` WHERE `id` =
> %u", 1234)
>


Hmm, I would expect an API like

local pstmt, err = h:prepare("...")
... = pstmt:execute("hello", 7")
-- or ... = pstmt:query("hello", 7")

or such style api. "Injecting" into implicit prepared statement is a
strange api.


> if rows then
>     ....
> else
>     ....
> end
>
>
> --------------
> Miscellaneous:
> --------------
>
> -- escaping strings for use in db queries:
> local escaped = db:escape(r, [[foo"bar'&|baz]])
>
>
> So, any comments, suggestions, remarks, objections and so on is very
> much welcomed. If there are no big objections to implementing this
> feature, I will consider it as lazy consensus and commit the bindings to
> trunk sometime soon along with updated documentation.
>
> With regards,
> Daniel.
>
> PS: I _have_ checked and double checked the code properly this time, so
> it conforms to the style requirements and works with maintainer mode. I
> know I usually get something wrong, but this time I think it's as close
> to perfect as it can get :) (but then again, I always write something
> bad, so apologies in advance if you find a bug)
>

Reply via email to