lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


	Hi Michael

> local stmt = conn:statement"SELECT a, b, c FROM t WHERE d > $1 AND e = $2"
> stmt:setInteger(1, 1234)
> stmt:setString(2, "abcd")
> local rs, count = stmt:selectResultSet()
	But the above code could be done in Lua don't you think?
If you are only creating SQL statements...

> Nothing revolutionary, but I find it much nicer than working with 
> cursors and doing a bunch of string concatenations to write queries. 
> Besides result set's you can also select individual results, individual 
> fields and key-value pair tables. I included a slightly more detailed 
> example with the source.
	I don't like to work with cursors either, neither checking every
statement if there was an error.  And of course assembling SQL statements
is boring.  But I've done all these jobs in Lua.  The result is something
like:

local dado = require"dado" -- my database abstraction
local db = dado.connect("database", "user", "pass", "postgres")

local cond = string.format ("d > %s and e = '%s'", 1234, "abcd")
for a, b, c in db:select("a,b,c", "t", cond) do
	-- do anything you want with the results
end

	In this example I have to be careful when assembling the SQL
statement, but I have some facilities for the common cases like:

print (table.fullconcat ({a = 1, b = 2, c = "a'bc"}, "=", " AND ", nil,
	string.quote))
a='1' AND c='a\'bc' AND b='2'

	What I'm really looking for is a uniform way to do Prepared
Statements in LuaSQL, which I think would really be a step further.
Have you thought about that?

	Regards,
		Tomás