lunarmodules / luasql

LuaSQL is a simple interface from Lua to a DBMS.
http://lunarmodules.github.io/luasql
535 stars 192 forks source link

Reusable Prepare Statements on MySQL (plus basic support for postgres and sqlite3) #100

Open fcr-- opened 5 years ago

fcr-- commented 5 years ago

This pull request is based on the PR #99, however this one adds additional support for reusing prepared statements.

Example:

local driver = require 'luasql.mysql'.mysql()

local conn = assert(driver:connect('test'))
assert(conn:setautocommit(false))
assert(conn:execute 'create table if not exists numbers(n int)')
assert(conn:execute 'truncate table numbers')

local t = os.time()
local stmt = assert(conn:prepare('insert into numbers values(?)'..(',(?)'):rep(9)))
for i = 0, 999 do
  for j = i*1000 + 1, i*1000 + 1000, 10 do
    assert(stmt:execute(j, j+1, j+2, j+3, j+4, j+5, j+6, j+7, j+8, j+9))
  end
  print((i+1)*1000 .. ' rows inserted.')
end
assert(conn:commit())
print('inserted a million rows in about ' .. os.time() - t .. ' seconds')
blumf commented 5 years ago

Could you add some test cases to the relevant drivers? Not sure if it's worth putting the tests into the main test script or keeping them separate for now.

Got the ODBC working to match. Will be working on Firebird next.

fcr-- commented 5 years ago

I've added them as another extension, so it wouldn't interfere with the other drivers.