| 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 | |
| 13 | #ifdef _WIN32 |
| 14 | #define LUA_EXPORT __declspec(dllexport) |
| 15 | #else |
| 16 | #define LUA_EXPORT |
| 17 | #endif |
| 18 | |
| 19 | #ifdef _MSC_VER /* all MS compilers define this (version) */ |
| 20 | #define snprintf _snprintf |
| 21 | #endif |
| 22 | |
| 23 | /* |
| 24 | * |
| 25 | * Table construction helper functions |
| 26 | * |
| 27 | * LUA_PUSH_ATTRIB_* creates string indexed (hashmap) |
| 28 | * LUA_PUSH_ARRAY_* creates integer indexed (array) |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | #define LUA_PUSH_ATTRIB_INT(n, v) \ |
| 33 | lua_pushstring(L, n); \ |
| 34 | lua_pushinteger(L, v); \ |
| 35 | lua_rawset(L, -3); |
| 36 | |
| 37 | #define LUA_PUSH_ATTRIB_FLOAT(n, v) \ |
| 38 | lua_pushstring(L, n); \ |
| 39 | lua_pushnumber(L, v); \ |
| 40 | lua_rawset(L, -3); |
| 41 | |
| 42 | #define LUA_PUSH_ATTRIB_STRING(n, v) \ |
| 43 | lua_pushstring(L, n); \ |
| 44 | lua_pushstring(L, v); \ |
| 45 | lua_rawset(L, -3); |
| 46 | |
| 47 | #define LUA_PUSH_ATTRIB_BOOL(n, v) \ |
| 48 | lua_pushstring(L, n); \ |
| 49 | lua_pushboolean(L, v); \ |
| 50 | lua_rawset(L, -3); |
| 51 | |
| 52 | #define LUA_PUSH_ATTRIB_NIL(n) \ |
| 53 | lua_pushstring(L, n); \ |
| 54 | lua_pushnil(L); \ |
| 55 | lua_rawset(L, -3); |
| 56 | |
| 57 | |
| 58 | |
| 59 | #define LUA_PUSH_ARRAY_INT(n, v) \ |
| 60 | lua_pushinteger(L, v); \ |
| 61 | lua_rawseti(L, -2, n); \ |
| 62 | n++; |
| 63 | |
| 64 | #define LUA_PUSH_ARRAY_FLOAT(n, v) \ |
| 65 | lua_pushnumber(L, v); \ |
| 66 | lua_rawseti(L, -2, n); \ |
| 67 | n++; |
| 68 | |
| 69 | #define LUA_PUSH_ARRAY_STRING(n, v) \ |
| 70 | lua_pushstring(L, v); \ |
| 71 | lua_rawseti(L, -2, n); \ |
| 72 | n++; |
| 73 | |
| 74 | #define LUA_PUSH_ARRAY_BOOL(n, v) \ |
| 75 | lua_pushboolean(L, v); \ |
| 76 | lua_rawseti(L, -2, n); \ |
| 77 | n++; |
| 78 | |
| 79 | #define LUA_PUSH_ARRAY_NIL(n) \ |
| 80 | lua_pushnil(L); \ |
| 81 | lua_rawseti(L, -2, n); \ |
| 82 | n++; |
| 83 | |
| 84 | /* |
| 85 | * |
| 86 | * Describes SQL to Lua API type conversions |
| 87 | * |
| 88 | */ |
| 89 | |
| 90 | typedef enum lua_push_type { |
| 91 | LUA_PUSH_NIL = 0, |
| 92 | LUA_PUSH_INTEGER, |
| 93 | LUA_PUSH_NUMBER, |
| 94 | LUA_PUSH_STRING, |
| 95 | LUA_PUSH_BOOLEAN, |
| 96 | |
| 97 | LUA_PUSH_MAX |
| 98 | } lua_push_type_t; |
| 99 | |
| 100 | /* |
| 101 | * used for placeholder translations |
| 102 | * from '?' to the .\d{4} |
| 103 | */ |
| 104 | #define MAX_PLACEHOLDERS 9999 |
| 105 | #define MAX_PLACEHOLDER_SIZE (1+4) /* .\d{4} */ |
| 106 | |
| 107 | /* |
| 108 | * |
| 109 | * Common error strings |
| 110 | * defined here for consistency in driver implementations |
| 111 | * |
| 112 | */ |
| 113 | |
| 114 | #define DBI_ERR_CONNECTION_FAILED "Failed to connect to database: %s" |
| 115 | #define DBI_ERR_DB_UNAVAILABLE "Database not available" |
| 116 | #define DBI_ERR_EXECUTE_INVALID "Execute called on a closed or invalid statement" |
| 117 | #define DBI_ERR_EXECUTE_FAILED "Execute failed %s" |
| 118 | #define DBI_ERR_FETCH_INVALID "Fetch called on a closed or invalid statement" |
| 119 | #define DBI_ERR_FETCH_FAILED "Fetch failed %s" |
| 120 | #define DBI_ERR_PARAM_MISCOUNT "Statement expected %d parameters but received %d" |
| 121 | #define DBI_ERR_BINDING_PARAMS "Error binding statement parameters: %s" |
| 122 | #define DBI_ERR_BINDING_EXEC "Error executing statement parameters: %s" |
| 123 | #define DBI_ERR_FETCH_NO_EXECUTE "Fetch called before execute" |
| 124 | #define DBI_ERR_BINDING_RESULTS "Error binding statement results: %s" |
| 125 | #define DBI_ERR_UNKNOWN_PUSH "Unknown push type in result set" |
| 126 | #define DBI_ERR_ALLOC_STATEMENT "Error allocating statement handle: %s" |
| 127 | #define DBI_ERR_PREP_STATEMENT "Error preparing statement handle: %s" |
| 128 | #define DBI_ERR_INVALID_PORT "Invalid port: %d" |
| 129 | #define DBI_ERR_ALLOC_RESULT "Error allocating result set: %s" |
| 130 | #define DBI_ERR_DESC_RESULT "Error describing result set: %s" |
| 131 | #define DBI_ERR_BINDING_TYPE_ERR "Unknown or unsupported type `%s'" |
| 132 | #define DBI_ERR_INVALID_STATEMENT "Invalid statement handle" |
| 133 | #define DBI_ERR_NOT_IMPLEMENTED "Method %s.%s is not implemented" |
| 134 | #define DBI_ERR_QUOTING_STR "Error quoting string: %s" |
| 135 | |
| 136 | /* |
| 137 | * convert string to lower case |
| 138 | */ |
| 139 | const char *strlower(char *in); |
| 140 | |
| 141 | /* |
| 142 | * replace '?' placeholders with .\d+ placeholders |
| 143 | * to be compatible with the native driver API |
| 144 | */ |
| 145 | char *replace_placeholders(lua_State *L, char native_prefix, const char *sql); |