Mon, 01 Dec 2008 07:22:44 +0000
Added new typechecks and errors
1 | 1 | #include <string.h> |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | ||
5 | #include <lua.h> | |
6 | #include <lauxlib.h> | |
7 | #include <lualib.h> | |
8 | ||
9 | #if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501) | |
10 | #include <compat-5.1.h> | |
11 | #endif | |
12 | ||
2 | 13 | /* |
14 | * | |
15 | * Table construction helper functions | |
16 | * | |
17 | * LUA_PUSH_ATTRIB_* creates string indexed (hashmap) | |
18 | * LUA_PUSH_ATTRIB_* creates integer indexed (array) | |
19 | * | |
20 | */ | |
1 | 21 | |
22 | #define LUA_PUSH_ATTRIB_INT(n, v) \ | |
23 | lua_pushstring(L, n); \ | |
24 | lua_pushinteger(L, v); \ | |
25 | lua_rawset(L, -3); | |
26 | ||
27 | #define LUA_PUSH_ATTRIB_FLOAT(n, v) \ | |
28 | lua_pushstring(L, n); \ | |
29 | lua_pushnumber(L, v); \ | |
30 | lua_rawset(L, -3); | |
31 | ||
32 | #define LUA_PUSH_ATTRIB_STRING(n, v) \ | |
33 | lua_pushstring(L, n); \ | |
34 | lua_pushstring(L, v); \ | |
35 | lua_rawset(L, -3); | |
36 | ||
37 | #define LUA_PUSH_ATTRIB_BOOL(n, v) \ | |
38 | lua_pushstring(L, n); \ | |
39 | lua_pushboolean(L, v); \ | |
40 | lua_rawset(L, -3); | |
41 | ||
42 | #define LUA_PUSH_ATTRIB_NIL(n) \ | |
43 | lua_pushstring(L, n); \ | |
44 | lua_pushnil(L); \ | |
45 | lua_rawset(L, -3); | |
46 | ||
47 | ||
48 | ||
49 | #define LUA_PUSH_ARRAY_INT(n, v) \ | |
50 | lua_pushinteger(L, v); \ | |
51 | lua_rawseti(L, -2, n); \ | |
52 | n++; | |
53 | ||
54 | #define LUA_PUSH_ARRAY_FLOAT(n, v) \ | |
55 | lua_pushnumber(L, v); \ | |
56 | lua_rawseti(L, -2, n); \ | |
57 | n++; | |
58 | ||
59 | #define LUA_PUSH_ARRAY_STRING(n, v) \ | |
60 | lua_pushstring(L, v); \ | |
61 | lua_rawseti(L, -2, n); \ | |
62 | n++; | |
63 | ||
64 | #define LUA_PUSH_ARRAY_BOOL(n, v) \ | |
65 | lua_pushboolean(L, v); \ | |
66 | lua_rawseti(L, -2, n); \ | |
67 | n++; | |
68 | ||
69 | #define LUA_PUSH_ARRAY_NIL(n) \ | |
70 | lua_pushnil(L); \ | |
71 | lua_rawseti(L, -2, n); \ | |
72 | n++; | |
73 | ||
2 | 74 | /* |
75 | * | |
4 | 76 | * Describes SQL to Lua API type conversions |
2 | 77 | * |
78 | */ | |
1 | 79 | |
80 | typedef enum lua_push_type { | |
81 | LUA_PUSH_NIL = 0, | |
82 | LUA_PUSH_INTEGER, | |
83 | LUA_PUSH_NUMBER, | |
84 | LUA_PUSH_STRING, | |
85 | LUA_PUSH_BOOLEAN, | |
86 | ||
87 | LUA_PUSH_MAX | |
88 | } lua_push_type_t; | |
89 | ||
4 | 90 | /* |
91 | * | |
92 | * Common error strings | |
93 | * defined here for consistency in driver implementations | |
94 | * | |
95 | */ | |
96 | ||
97 | #define DBI_ERR_CONNECTION_FAILED "Failed to connect to database: %s" | |
98 | #define DBI_ERR_DB_UNAVAILABLE "Database not available" | |
99 | #define DBI_ERR_EXECUTE_INVALID "Execute called on a closed or invalid statement" | |
100 | #define DBI_ERR_EXECUTE_FAILED "Execute failed %s" | |
101 | #define DBI_ERR_FETCH_INVALID "Fetch called on a closed or invalid statement" | |
102 | #define DBI_ERR_FETCH_FAILED "Fetch failed %s" | |
103 | #define DBI_ERR_PARAM_MISCOUNT "Statement expected %d parameters but received %d" | |
104 | #define DBI_ERR_BINDING_PARAMS "Error binding statement parameters: %s" | |
105 | #define DBI_ERR_BINDING_EXEC "Error executing statement parameters: %s" | |
106 | #define DBI_ERR_FETCH_NO_EXECUTE "Fetch called before execute" | |
107 | #define DBI_ERR_BINDING_RESULTS "Error binding statement results: %s" | |
108 | #define DBI_ERR_UNKNOWN_PUSH "Unknown push type in result set" | |
109 | #define DBI_ERR_ALLOC_STATEMENT "Error allocating statement handle: %s" | |
110 | #define DBI_ERR_PREP_STATEMENT "Error preparing statement handle: %s" | |
111 | #define DBI_ERR_INVALID_PORT "Invalid port: %d" | |
112 | #define DBI_ERR_ALLOC_RESULT "Error allocating result set: %s" | |
10 | 113 | #define DBI_ERR_BINDING_TYPE_ERR "Unknown or unsupported type `%s'" |