{"id":15374220,"url":"https://github.com/moteus/lua-odbc","last_synced_at":"2025-04-15T12:31:46.021Z","repository":{"id":5175144,"uuid":"6348907","full_name":"moteus/lua-odbc","owner":"moteus","description":"ODBC Library for lua","archived":false,"fork":false,"pushed_at":"2017-08-03T10:21:17.000Z","size":7111,"stargazers_count":19,"open_issues_count":0,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T21:12:20.830Z","etag":null,"topics":["lua","lua-binding","odbc"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/moteus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-10-23T08:10:59.000Z","updated_at":"2024-06-27T16:03:16.000Z","dependencies_parsed_at":"2022-08-30T12:30:49.090Z","dependency_job_id":null,"html_url":"https://github.com/moteus/lua-odbc","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moteus%2Flua-odbc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moteus%2Flua-odbc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moteus%2Flua-odbc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moteus%2Flua-odbc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moteus","download_url":"https://codeload.github.com/moteus/lua-odbc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249072282,"owners_count":21208156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","lua-binding","odbc"],"created_at":"2024-10-01T13:57:49.308Z","updated_at":"2025-04-15T12:31:45.711Z","avatar_url":"https://github.com/moteus.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ODBC library for Lua 5.1/5.2 #\r\n\r\n[![Licence](http://img.shields.io/badge/Licence-MIT-brightgreen.svg)](LICENCE.txt)\r\n[![Build Status](https://travis-ci.org/moteus/lua-odbc.png?branch=master)](https://travis-ci.org/moteus/lua-odbc)\r\n[![Coverage Status](https://coveralls.io/repos/moteus/lua-odbc/badge.png?branch=master)](https://coveralls.io/r/moteus/lua-odbc?branch=master)\r\n\r\n## Supports ##\r\n- prepared query\r\n- bind parameters (input/output)\r\n- bind column\r\n- async mode for statements\r\n- catalog functions\r\n- luasql compatable module\r\n- C API to create Lua object based on existed ODBC handle\r\n\r\n## Known issues ##\r\n- assert method rise error as object.\r\nThis works fine on Lua \u003e 5.1 and on LuaJIT but on Lua 5.1 it results message as\r\n`lua.exe: (error object is not a string)`. But it allows wrap code\r\nin `pcall` function and handle errors as object(with access to SQLCODE and SQLSTATE).\r\nTo change this behavior set `-DLODBC_ASSERT_TOSTRING` flag in compile time.\r\n\r\n## Usage ##\r\n\r\nInsert multiple rows\r\n```lua\r\nodbc = require \"odbc\"\r\ndbassert = odbc.assert\r\n\r\ncnn = odbc.connect('EmptyDB')\r\ncnn:setautocommit(false)\r\nstmt = cnn:prepare(\"insert into test_table(f1, f2) values(?, ?)\")\r\nf1 = stmt:vbind_param_ulong(1)\r\nfor i = 1, 5 do\r\n  f1:set(i)          -- just set data\r\n  stmt:bindnum(2, i) -- each time call SQLBindParameter\r\n  dbassert(stmt:execute())\r\nend\r\ndbassert( cnn:commit() )\r\n```\r\n\r\nSelect resultset\r\n```lua\r\nodbc = require \"odbc\"\r\ndbassert = odbc.assert\r\n\r\ncnn = odbc.connect('EmptyDB')\r\n\r\nstmt = cnn:execute('select f1, f2 from test_table order by 1')\r\nstmt:foreach(function(f1, f2)\r\n  if f1 == 2 then return true end\r\nend)\r\nassert(stmt:closed()) -- foreach close cursor \r\nassert(not stmt:destroyed()) -- statement valid\r\n\r\nstmt:execute('select f1, f2 from test_table order by 1')\r\nf1, f2 = stmt:fetch()\r\n```\r\n\r\nInput/Output parameters\r\n```lua\r\nodbc = require \"odbc\"\r\ndbassert = odbc.assert\r\ncnn = odbc.connect('EmptyDB')\r\n\r\nstmt = dbassert(cnn:prepare(\"{?= call dba.fn_test(?)}\"))\r\nret  = stmt:vbind_param_ulong(1, ret, odbc.PARAM_OUTPUT)\r\nval  = stmt:vbind_param_char(2, \"hello\") -- default odbc.PARAM_INPUT\r\ndbassert(stmt:execute())\r\nprint(ret:get())\r\n\r\nstmt:reset()\r\nstmt:vbind_col(1, ret)\r\ndbassert(stmt:execute('select 321'))\r\nstmt:vfetch()\r\nprint(ret:get())\r\n```\r\n\r\nUse async mode\r\n```lua\r\nlocal odbc = require \"odbc\"\r\nlocal dbassert = odbc.assert\r\n\r\ncnn = odbc.connect('EmptyDB')\r\n\r\nstmt = cnn:prepare('select f1, f2 from test_table order by 1')\r\nlocal f1 = stmt:vbind_col_ulong(1)\r\nlocal f2 = stmt:vbind_col_ulong(2)\r\n\r\nstmt:setasyncmode(true)\r\n\r\nlocal COUNTER = 0\r\n\r\nfunction async(stmt, f, ...)\r\n  while true do\r\n    local ok, err = f(stmt, ...)\r\n    if ok ~= 'timeout' then return ok, err end\r\n    COUNTER = COUNTER + 1\r\n  end\r\nend\r\n\r\ndbassert(async(stmt, stmt.execute))\r\nwhile(async(stmt, stmt.vfetch))do\r\n  print(f1:get(),f2:get())\r\nend\r\n\r\nprint(\"execute counter:\", COUNTER)\r\n```\r\n\r\nUse C API\r\n```C\r\nstatic void luaX_call_method(lua_State *L, const char *name, int nargs, int nresults){\r\n  int obj_index = -nargs - 1;\r\n  lua_getfield(L, obj_index, name);\r\n  lua_insert(L, obj_index - 1);\r\n  return lua_call(L, nargs + 1, nresults);\r\n}\r\n\r\nstatic int odbc_first_row_impl(lua_State *L){\r\n  SQLHSTMT *pstmt = (SQLHSTMT *)lua_touserdata(L, lua_upvalueindex(1));\r\n\r\n  lua_settop(L, 1); // sql\r\n\r\n  // wrap existed handle to lua object\r\n  lodbc_statement(L, *pstmt, 0);      // sql, stmt\r\n  lua_insert(L, 1); lua_insert(L, 1); // stmt, sql\r\n\r\n  luaX_call_method(L, \"execute\", 1, 2); // [nil/stmt], [err/nil]\r\n\r\n  // here we should check error and ether execute return recordset\r\n\r\n  lua_pop(L, 1); //stmt\r\n  luaX_call_method(L, \"fetch\", 0, LUA_MULTRET);\r\n\r\n  return lua_gettop(L);\r\n}\r\n\r\nstatic int odbc_first_row(lua_State *L){\r\n  int res;\r\n  { // this is may be cpp scope (we can use RAII)\r\n    // get existed handle from somewhere(e.g. prepared pool)\r\n    SQLHSTMT hstmt = ... \r\n\r\n    lua_pushlightuserdata(L, \u0026hstmt);\r\n    lua_pushcclosure(L, odbc_first_row_impl, 1);\r\n    lua_insert(L, 1);\r\n\r\n    res = lua_pcall(L, lua_gettop(L)-1, LUA_MULTRET, 0);\r\n\r\n    // cleanup\r\n  }\r\n  if(res) return lua_error(L);\r\n  return lua_gettop(L);\r\n}\r\n```\r\nAnd from Lua we can call this function just like `... = odbc.first_row(\"select ...\")`\r\n\r\n## odbc.dba module ##\r\nImplementaion of [dba](http://moteus.github.io/dba/index.html) API\r\n\r\n\r\n[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/moteus/lua-odbc/trend.png)](https://bitdeli.com/free \"Bitdeli Badge\")\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoteus%2Flua-odbc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoteus%2Flua-odbc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoteus%2Flua-odbc/lists"}