Git Product home page Git Product logo

lua-odbc's Introduction

ODBC library for Lua 5.1/5.2

Licence Build Status Coverage Status

Supports

  • prepared query
  • bind parameters (input/output)
  • bind column
  • async mode for statements
  • catalog functions
  • luasql compatable module
  • C API to create Lua object based on existed ODBC handle

Known issues

  • assert method rise error as object. This works fine on Lua > 5.1 and on LuaJIT but on Lua 5.1 it results message as lua.exe: (error object is not a string). But it allows wrap code in pcall function and handle errors as object(with access to SQLCODE and SQLSTATE). To change this behavior set -DLODBC_ASSERT_TOSTRING flag in compile time.

Usage

Insert multiple rows

odbc = require "odbc"
dbassert = odbc.assert

cnn = odbc.connect('EmptyDB')
cnn:setautocommit(false)
stmt = cnn:prepare("insert into test_table(f1, f2) values(?, ?)")
f1 = stmt:vbind_param_ulong(1)
for i = 1, 5 do
  f1:set(i)          -- just set data
  stmt:bindnum(2, i) -- each time call SQLBindParameter
  dbassert(stmt:execute())
end
dbassert( cnn:commit() )

Select resultset

odbc = require "odbc"
dbassert = odbc.assert

cnn = odbc.connect('EmptyDB')

stmt = cnn:execute('select f1, f2 from test_table order by 1')
stmt:foreach(function(f1, f2)
  if f1 == 2 then return true end
end)
assert(stmt:closed()) -- foreach close cursor 
assert(not stmt:destroyed()) -- statement valid

stmt:execute('select f1, f2 from test_table order by 1')
f1, f2 = stmt:fetch()

Input/Output parameters

odbc = require "odbc"
dbassert = odbc.assert
cnn = odbc.connect('EmptyDB')

stmt = dbassert(cnn:prepare("{?= call dba.fn_test(?)}"))
ret  = stmt:vbind_param_ulong(1, ret, odbc.PARAM_OUTPUT)
val  = stmt:vbind_param_char(2, "hello") -- default odbc.PARAM_INPUT
dbassert(stmt:execute())
print(ret:get())

stmt:reset()
stmt:vbind_col(1, ret)
dbassert(stmt:execute('select 321'))
stmt:vfetch()
print(ret:get())

Use async mode

local odbc = require "odbc"
local dbassert = odbc.assert

cnn = odbc.connect('EmptyDB')

stmt = cnn:prepare('select f1, f2 from test_table order by 1')
local f1 = stmt:vbind_col_ulong(1)
local f2 = stmt:vbind_col_ulong(2)

stmt:setasyncmode(true)

local COUNTER = 0

function async(stmt, f, ...)
  while true do
    local ok, err = f(stmt, ...)
    if ok ~= 'timeout' then return ok, err end
    COUNTER = COUNTER + 1
  end
end

dbassert(async(stmt, stmt.execute))
while(async(stmt, stmt.vfetch))do
  print(f1:get(),f2:get())
end

print("execute counter:", COUNTER)

Use C API

static void luaX_call_method(lua_State *L, const char *name, int nargs, int nresults){
  int obj_index = -nargs - 1;
  lua_getfield(L, obj_index, name);
  lua_insert(L, obj_index - 1);
  return lua_call(L, nargs + 1, nresults);
}

static int odbc_first_row_impl(lua_State *L){
  SQLHSTMT *pstmt = (SQLHSTMT *)lua_touserdata(L, lua_upvalueindex(1));

  lua_settop(L, 1); // sql

  // wrap existed handle to lua object
  lodbc_statement(L, *pstmt, 0);      // sql, stmt
  lua_insert(L, 1); lua_insert(L, 1); // stmt, sql

  luaX_call_method(L, "execute", 1, 2); // [nil/stmt], [err/nil]

  // here we should check error and ether execute return recordset

  lua_pop(L, 1); //stmt
  luaX_call_method(L, "fetch", 0, LUA_MULTRET);

  return lua_gettop(L);
}

static int odbc_first_row(lua_State *L){
  int res;
  { // this is may be cpp scope (we can use RAII)
    // get existed handle from somewhere(e.g. prepared pool)
    SQLHSTMT hstmt = ... 

    lua_pushlightuserdata(L, &hstmt);
    lua_pushcclosure(L, odbc_first_row_impl, 1);
    lua_insert(L, 1);

    res = lua_pcall(L, lua_gettop(L)-1, LUA_MULTRET, 0);

    // cleanup
  }
  if(res) return lua_error(L);
  return lua_gettop(L);
}

And from Lua we can call this function just like ... = odbc.first_row("select ...")

odbc.dba module

Implementaion of dba API

Bitdeli Badge

lua-odbc's People

Contributors

bitdeli-chef avatar moteus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lua-odbc's Issues

ODBC Connect Fails Under CentOS 7 Using MDBTools

Attempting to port a working piece of lua code from Windows 10 to CentOs 732-Bit Linux. Attempt to connect to Access DB File fails using ODBC module for MDBTools v0.7. Same code works on Lua Interpreter installed on Windows 10 32-bit ODBC using Windows ODBC. Sample of Error follows:

[tonyd@morspirit appl]$ lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio

odbc = require "odbc"
dbassert = odbc.assert
cnn = odbc.connect('Pwds')
lua: src/lcnn.c:1115: cnn_supportsTransactions: Assertion `ret == 1' failed.
Aborted (core dumped)

List of Modules Installed on CentOs 7 PC:

MDBTools v0.7 (Built from Source)
unixODBC (Built from Source)
Lua Rocks Module: odbc
0.3.0-1 (installed) - /usr/lib/luarocks/rocks

Lua RPM Modules:

lua-5.1.4-14.el7.i686
lua-devel-5.1.4-14.el7.i686
luarocks-2.1.2-1.el6.noarch (No el7 Version of luarocks module available)

Database can be queried using the unixODBC isql command line tool. Sample session:

[tonyd@morspirit ~]$ isql Pwds
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> select Location from Pwds
+-----------------------------------------+
| Location |
+-----------------------------------------+
| Personal |
| Personal |
| Personal |
| Personal |
| Personal |
| Personal |
| Personal |
...
+-----------------------------------------+
SQLRowCount returns 59
59 rows fetched
SQL>

Is there anything that I provide in additional to help isolate the issue?

Thanks;

TD

Value safety

This might qualify perhaps more as a question but I will ask it here. When binding a variable into prepared query does that value goes through any safety clean-up? Especially important when we use char variables. If not is there some method available to escape textual values?

Invalid string or buffer length connecting to Mysql using lua sql odbc

Trying to connect to MySQL using luasql ODBC. Had to install the drivers:

$ odbcinst -q -d
[MySQL ODBC 8.0 Unicode Driver]
[MySQL ODBC 8.0 ANSI Driver]
But when I try and connect I get:

env:connect("DRIVER='MySQL ODBC 8.0 Unicode Driver'")
nil [unixODBC][Driver Manager]Invalid string or buffer length

Tried different quoting, enclosing the driver name in {}, changing the driver name to something shorter, but no joy.

Looking LuaSQL

Looking LuaSQL

Hi, friends.

I am trying to get info about LuaSQL, but the links seem broken.

In http://www.promixis.com/lua/luasql/index.html I found the following two:

LuaForge
http://luaforge.net/project/showfiles.php?group_id=12

LuaBinaries
http://luaforge.net/projects/luabinaries/

It seems that LuaForge got into GitHub (( https://github.com/luaforge )), but I donot see LuaSQL there

In http://keplerproject.github.io/luasql/ I got redirected to an URL based in LuaRocks, but without the DLLs.

In " History - LuaSQL 2.3.5 [23/Feb/2017] " (( http://keplerproject.github.io/luasql/history.html )) says that:

Added some bug corrections and tested with Lua versions 5.1, 5.2 and 5.3 (only Postgres, MySQL and SQLite were tested by the time of the launch).

I want to test with FireBird and SQLite3.

The forum Kepler developers, called "Kepler's mailing list" is not active, this was what Fabio says me.

Can you say me where can I find the LuaSQL DLLs, avoiding LuaRocks or with instructions that runs without this?

Moteus: Is odbc.luasql different from luasql.odbc? Can you say me where can I find it?

Thanks.

Can't install on Debian Squeeze 64 bits

I can't install on Debian Squeeze:

uname -a
Linux node1 2.6.32-5-amd64 #1 SMP Wed Apr 9 19:24:34 UTC 2014 x86_64 GNU/Linux

I installed unixodbc-dev (2.2.14p2-1).

luarocks install odbc
Installing http://luarocks.org/repositories/rocks/odbc-0.2.1-1.src.rock...
Using http://luarocks.org/repositories/rocks/odbc-0.2.1-1.src.rock... switching to 'build' mode
Archive:  /tmp/luarocks_luarocks-rock-odbc-0.2.1-1-603/odbc-0.2.1-1.src.rock
  inflating: odbc-0.2.1-1.rockspec
 extracting: v0.2.1.zip
Archive:  v0.2.1.zip
79d38405eb307f76862f6f916ff4fe7c9d1e7906
   creating: lua-odbc-0.2.1/
  inflating: lua-odbc-0.2.1/.gitignore
  inflating: lua-odbc-0.2.1/.travis.yml
   creating: lua-odbc-0.2.1/.travis/
  inflating: lua-odbc-0.2.1/.travis/setup_lua.sh
  inflating: lua-odbc-0.2.1/CMakeLists.txt
  inflating: lua-odbc-0.2.1/LICENCE.txt
  inflating: lua-odbc-0.2.1/README.md
   creating: lua-odbc-0.2.1/cmake/
  inflating: lua-odbc-0.2.1/cmake/FindLua.cmake
  inflating: lua-odbc-0.2.1/cmake/FindODBC.cmake
  inflating: lua-odbc-0.2.1/cmake/dist.cmake
  inflating: lua-odbc-0.2.1/cmake/lua.cmake
  inflating: lua-odbc-0.2.1/dist.info
   creating: lua-odbc-0.2.1/include/
  inflating: lua-odbc-0.2.1/include/luaodbc.h
  inflating: lua-odbc-0.2.1/lakeconfig.lua
  inflating: lua-odbc-0.2.1/lakefile
   creating: lua-odbc-0.2.1/lua/
  inflating: lua-odbc-0.2.1/lua/odbc.lua
   creating: lua-odbc-0.2.1/lua/odbc/
  inflating: lua-odbc-0.2.1/lua/odbc/dba.lua
   creating: lua-odbc-0.2.1/lua/odbc/dba/
  inflating: lua-odbc-0.2.1/lua/odbc/dba/utils.lua
  inflating: lua-odbc-0.2.1/lua/odbc/luasql.lua
  inflating: lua-odbc-0.2.1/lua/odbc/proxy.lua
   creating: lua-odbc-0.2.1/msvc/
  inflating: lua-odbc-0.2.1/msvc/luaodbc.sln
  inflating: lua-odbc-0.2.1/msvc/luaodbc.vcproj
   creating: lua-odbc-0.2.1/rockspec/
  inflating: lua-odbc-0.2.1/rockspec/odbc-0.1.0-1.rockspec
  inflating: lua-odbc-0.2.1/rockspec/odbc-0.1.0-2.rockspec
  inflating: lua-odbc-0.2.1/rockspec/odbc-0.1.0-3.rockspec
  inflating: lua-odbc-0.2.1/rockspec/odbc-0.2.0-1.rockspec
  inflating: lua-odbc-0.2.1/rockspec/odbc-0.2.1-1.rockspec
  inflating: lua-odbc-0.2.1/rockspec/odbc-scm-0.rockspec
   creating: lua-odbc-0.2.1/src/
  inflating: lua-odbc-0.2.1/src/driverinfo.c
  inflating: lua-odbc-0.2.1/src/driverinfo.h
  inflating: lua-odbc-0.2.1/src/l52util.c
  inflating: lua-odbc-0.2.1/src/l52util.h
  inflating: lua-odbc-0.2.1/src/lcnn.c
  inflating: lua-odbc-0.2.1/src/lcnn.h
  inflating: lua-odbc-0.2.1/src/lenv.c
  inflating: lua-odbc-0.2.1/src/lenv.h
  inflating: lua-odbc-0.2.1/src/lerr.c
  inflating: lua-odbc-0.2.1/src/lerr.h
  inflating: lua-odbc-0.2.1/src/libopt.c
  inflating: lua-odbc-0.2.1/src/libopt.h
  inflating: lua-odbc-0.2.1/src/lodbc.c
  inflating: lua-odbc-0.2.1/src/lodbc.h
  inflating: lua-odbc-0.2.1/src/lstmt.c
  inflating: lua-odbc-0.2.1/src/lstmt.h
  inflating: lua-odbc-0.2.1/src/lval.c
  inflating: lua-odbc-0.2.1/src/lval.h
  inflating: lua-odbc-0.2.1/src/parlist.c
  inflating: lua-odbc-0.2.1/src/parlist.h
  inflating: lua-odbc-0.2.1/src/utils.c
  inflating: lua-odbc-0.2.1/src/utils.h
   creating: lua-odbc-0.2.1/test/
  inflating: lua-odbc-0.2.1/test/00_test_odbc.lua
  inflating: lua-odbc-0.2.1/test/01_test_odbc_env.lua
  inflating: lua-odbc-0.2.1/test/02_test_odbc_cnn.lua
  inflating: lua-odbc-0.2.1/test/03_test_odbc_cnn_info.lua
  inflating: lua-odbc-0.2.1/test/04_test_odbc_bind.lua
  inflating: lua-odbc-0.2.1/test/05_test_odbc_foreach.lua
  inflating: lua-odbc-0.2.1/test/06_test_odbc_more.lua
  inflating: lua-odbc-0.2.1/test/07_test_odbc_val.lua
  inflating: lua-odbc-0.2.1/test/08_test_odbc_stmt.lua
  inflating: lua-odbc-0.2.1/test/09_test_odbc_cnn_handle.lua
  inflating: lua-odbc-0.2.1/test/config.lua
   creating: lua-odbc-0.2.1/test/dba/
  inflating: lua-odbc-0.2.1/test/dba/.luacov
  inflating: lua-odbc-0.2.1/test/dba/test.lua
   creating: lua-odbc-0.2.1/test/luasql/
  inflating: lua-odbc-0.2.1/test/luasql/odbc.lua
  inflating: lua-odbc-0.2.1/test/luasql/test-luasql.lua
  inflating: lua-odbc-0.2.1/test/mytest.odbc.ini
  inflating: lua-odbc-0.2.1/test/perf_test.lua
  inflating: lua-odbc-0.2.1/test/test.lua
  inflating: lua-odbc-0.2.1/test/tools.lua
gcc -O2 -fPIC -I/usr/include/lua5.1 -c src/l52util.c -o src/l52util.o -DUNIXODBC -DLUAODBC_EXPORTS -DLODBC_ERROR_AS_OBJECT -DLODBC_MIN_PAR_BUFSIZE=64 -I./include -I/usr/include
gcc -O2 -fPIC -I/usr/include/lua5.1 -c src/lcnn.c -o src/lcnn.o -DUNIXODBC -DLUAODBC_EXPORTS -DLODBC_ERROR_AS_OBJECT -DLODBC_MIN_PAR_BUFSIZE=64 -I./include -I/usr/include
In file included from src/lcnn.c:4:
src/lenv.h:11: error: redefinition of typedef ‘lodbc_env’
src/lcnn.h:6: note: previous declaration of ‘lodbc_env’ was here
In file included from src/lcnn.c:5:
src/lstmt.h:7: error: redefinition of typedef ‘lodbc_cnn’
src/lcnn.h:38: note: previous declaration of ‘lodbc_cnn’ was here
Updating manifest for /usr/local/lib/luarocks/rocks

odbc 0.2.1-1 is now built and installed in /usr/local/ (license: MIT/X11)

Unclear way of specifying parameters

So if I have everything configured in odbc.ini (including username and password) just by specifyng DSN name I can connect.

local odbc = require "odbc"
local cnn = odbc.connect("MyDSN")

I also figured out that second and third parameter are username and password

local odbc = require "odbc"
local cnn = odbc.connect("MyDSN", "myuser", "mypwd")

Is there a way to specify any specific driver ODBC setting in the code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.