Skip to content

Commit

Permalink
Fix up constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam Allan committed Feb 5, 2017
1 parent 5454122 commit 5ccbad8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
22 changes: 18 additions & 4 deletions docs/db2.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ All the DB2 functions are within the `db2` namespace.
* `getColumn` - Grab the value of a specific column.
* `printError` - If the statement has crashed internally, use `db2.printError` to see more information.

### Constant list

* `SQLRETURN`s:
* `db2.SQL_SUCCESS`
* `db2.SQL_SUCCESS_WITH_INFO`
* `db2.SQL_NO_DATA_FOUND`
* `db2.SQL_NEED_DATA`
* `db2.SQL_NO_DATA`
* `db2.SQL_ERROR`
* `db2.SQL_INVALID_HANDLE`
* `SQL_STILL_EXECUTING`
* `db2.NULL`

***

### `db2.allocEnv()` - Allocate an environment

##### Notes

* Allocate an environment to connect and run statements in.
* Returns environment handle as `number`.
* Always keep the handle stored so you are able to correctly free the environment at the end of the script.
* Returns environment handle as `number`. Returns `db2.SQL_ERROR` if there was an error.

```lua
local env = db2.allocEnv()
Expand All @@ -45,7 +58,7 @@ All the DB2 functions are within the `db2` namespace.
##### Notes

* Allocate a connection to run statements in.
* Returns connection handle as `number`.
* Returns connection handle as `number`. Returns `db2.SQL_ERROR` if there was an error.
* Always keep the handle stored so you are able to correctly disconnect at the end of the script.

```lua
Expand All @@ -71,6 +84,7 @@ All the DB2 functions are within the `db2` namespace.
##### Notes

* Only supports local databases currently. Support for external databases/system with username and password are coming, eventually.
* Returns SQLRETURN value as `number`.

```lua
local env = db2.allocEnv()
Expand All @@ -96,7 +110,7 @@ All the DB2 functions are within the `db2` namespace.

##### Notes

* Returns statement handle as `number`.
* Returns statement handle as `number`. Returns `db2.SQL_ERROR` if there was an error.
* Always keep the handle stored so you are able to correctly free/close the statement at the end of the script.

```lua
Expand Down Expand Up @@ -162,7 +176,7 @@ All the DB2 functions are within the `db2` namespace.

##### Notes

* Returns SQLRETURN value as `number`. (`0` is successful)
* Returns SQLRETURN value as `number`.
* To get the columns out of a fetch, use `db2.getColumn(stmt, col, type, len)`.

```lua
Expand Down
32 changes: 6 additions & 26 deletions src/ldb2lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static int db2_allocEnv(lua_State *L) {
int sqltrue = SQL_TRUE;

if (SQLAllocEnv(&env) == SQL_ERROR) {
lua_pushnumber(L, 0); //0 = ERROR
lua_pushnumber(L, SQL_ERROR); //0 = ERROR
return luaL_execresult(L, SQL_ERROR);

} else {
Expand All @@ -48,7 +48,7 @@ static int db2_allocConnect(lua_State *L) {
SQLHDBC hdl = 0;

if (SQLAllocConnect(env, &hdl) != SQL_SUCCESS) {
lua_pushnumber(L, 0); //0 = ERROR
lua_pushnumber(L, SQL_ERROR); //0 = ERROR
return luaL_execresult(L, SQL_ERROR);
} else {

Expand All @@ -71,7 +71,7 @@ static int db2_Connect(lua_State *L) {
SQLRETURN res = SQLConnect(hdl, database, SQL_NTS, 0, SQL_NTS, 0, SQL_NTS);

if (res != SQL_SUCCESS) {
lua_pushnumber(L, 0); //0 = ERROR
lua_pushnumber(L, res);
return luaL_execresult(L, SQL_ERROR);
} else {
SQLINTEGER sqltrue = SQL_TRUE;
Expand All @@ -80,7 +80,7 @@ static int db2_Connect(lua_State *L) {
SQLSetConnectAttr(hdl, SQL_ATTR_DBC_SYS_NAMING, &sqltrue, 0);
SQLSetConnectAttr(hdl, SQL_ATTR_COMMIT, &nocmt, 0);

lua_pushnumber(L, 1);
lua_pushnumber(L, res);
return 1;
}
}
Expand All @@ -94,10 +94,10 @@ static int db2_Disconnect(lua_State *L) {

static int db2_allocStatement(lua_State *L) {
SQLHDBC hdl = lua_tointeger(L, 1);
SQLHSTMT stmt = 0;
SQLHSTMT stmt = 0;

if (SQLAllocStmt(hdl, &stmt) != SQL_SUCCESS) {
lua_pushnumber(L, 0); //0 = ERROR
lua_pushnumber(L, SQL_ERROR); //0 = ERROR
return luaL_execresult(L, SQL_ERROR);
} else {
lua_pushnumber(L, stmt);
Expand Down Expand Up @@ -190,11 +190,6 @@ static const luaL_Reg db2lib[] = {
{"printError", db2_printError},

{"NULL", NULL},
{"SQLCHAR", NULL},
{"SQLVARCHAR", NULL},
{"SQLNUMERIC", NULL},
{"SQLSMALLINT", NULL},
{"SQLDECIMAL", NULL},

{"SQL_SUCCESS", NULL},
{"SQL_SUCCESS_WITH_INFO", NULL},
Expand All @@ -218,21 +213,6 @@ LUAMOD_API int luaopen_db2 (lua_State *L) {
lua_pushstring(L, null);
lua_setfield(L, -2, "NULL");

lua_pushnumber(L, 1);
lua_setfield(L, -2, "SQLCHAR");

lua_pushnumber(L, 1);
lua_setfield(L, -2, "SQLVARCHAR");

lua_pushnumber(L, 2);
lua_setfield(L, -2, "SQLNUMERIC");

lua_pushnumber(L, 3);
lua_setfield(L, -2, "SQLSMALLINT");

lua_pushnumber(L, 4);
lua_setfield(L, -2, "SQLDECIMAL");

lua_pushnumber(L, 0);
lua_setfield(L, -2, "SQL_SUCCESS");

Expand Down

0 comments on commit 5ccbad8

Please sign in to comment.