File: common.h

package info (click to toggle)
lua-dbi 0.5%2Bsvn78-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 360 kB
  • sloc: ansic: 2,908; makefile: 77
file content (146 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM < 501)
#include <compat-5.1.h>
#endif

#ifdef _WIN32
    #define LUA_EXPORT __declspec(dllexport)
#else
    #define LUA_EXPORT
#endif

#ifdef _MSC_VER  /* all MS compilers define this (version) */
     #define snprintf _snprintf
#endif

/*
 *
 * Table construction helper functions
 *
 * LUA_PUSH_ATTRIB_* creates string indexed (hashmap)
 * LUA_PUSH_ARRAY_* creates integer indexed (array)
 *
 */

#define LUA_PUSH_ATTRIB_INT(n, v) \
    lua_pushstring(L, n); \
    lua_pushinteger(L, v); \
    lua_rawset(L, -3); 

#define LUA_PUSH_ATTRIB_FLOAT(n, v) \
    lua_pushstring(L, n); \
    lua_pushnumber(L, v); \
    lua_rawset(L, -3); 

#define LUA_PUSH_ATTRIB_STRING(n, v) \
    lua_pushstring(L, n); \
    lua_pushstring(L, v); \
    lua_rawset(L, -3); 

#define LUA_PUSH_ATTRIB_BOOL(n, v) \
    lua_pushstring(L, n); \
    lua_pushboolean(L, v); \
    lua_rawset(L, -3); 

#define LUA_PUSH_ATTRIB_NIL(n) \
    lua_pushstring(L, n); \
    lua_pushnil(L); \
    lua_rawset(L, -3); 



#define LUA_PUSH_ARRAY_INT(n, v) \
    lua_pushinteger(L, v); \
    lua_rawseti(L, -2, n); \
    n++; 

#define LUA_PUSH_ARRAY_FLOAT(n, v) \
    lua_pushnumber(L, v); \
    lua_rawseti(L, -2, n); \
    n++; 

#define LUA_PUSH_ARRAY_STRING(n, v) \
    lua_pushstring(L, v); \
    lua_rawseti(L, -2, n); \
    n++;

#define LUA_PUSH_ARRAY_BOOL(n, v) \
    lua_pushboolean(L, v); \
    lua_rawseti(L, -2, n); \
    n++;

#define LUA_PUSH_ARRAY_NIL(n) \
    lua_pushnil(L); \
    lua_rawseti(L, -2, n); \
    n++;

/*
 *
 * Describes SQL to Lua API type conversions
 *
 */

typedef enum lua_push_type {
    LUA_PUSH_NIL = 0,
    LUA_PUSH_INTEGER,
    LUA_PUSH_NUMBER,
    LUA_PUSH_STRING,
    LUA_PUSH_BOOLEAN,

    LUA_PUSH_MAX
} lua_push_type_t;

/*
 * used for placeholder translations
 * from '?' to the .\d{4}
 */
#define MAX_PLACEHOLDERS        9999 
#define MAX_PLACEHOLDER_SIZE    (1+4) /* .\d{4} */

/*
 *
 * Common error strings
 * defined here for consistency in driver implementations
 *
 */

#define	DBI_ERR_CONNECTION_FAILED   "Failed to connect to database: %s"
#define DBI_ERR_DB_UNAVAILABLE	    "Database not available"
#define DBI_ERR_EXECUTE_INVALID	    "Execute called on a closed or invalid statement"
#define DBI_ERR_EXECUTE_FAILED	    "Execute failed %s"
#define DBI_ERR_FETCH_INVALID	    "Fetch called on a closed or invalid statement"
#define DBI_ERR_FETCH_FAILED	    "Fetch failed %s"
#define DBI_ERR_PARAM_MISCOUNT	    "Statement expected %d parameters but received %d"
#define DBI_ERR_BINDING_PARAMS	    "Error binding statement parameters: %s"
#define DBI_ERR_BINDING_EXEC	    "Error executing statement parameters: %s"
#define DBI_ERR_FETCH_NO_EXECUTE    "Fetch called before execute"
#define DBI_ERR_BINDING_RESULTS	    "Error binding statement results: %s"
#define DBI_ERR_UNKNOWN_PUSH	    "Unknown push type in result set"
#define DBI_ERR_ALLOC_STATEMENT	    "Error allocating statement handle: %s"
#define DBI_ERR_PREP_STATEMENT	    "Error preparing statement handle: %s"
#define DBI_ERR_INVALID_PORT	    "Invalid port: %d"
#define DBI_ERR_ALLOC_RESULT	    "Error allocating result set: %s"
#define DBI_ERR_DESC_RESULT	    "Error describing result set: %s"
#define DBI_ERR_BINDING_TYPE_ERR    "Unknown or unsupported type `%s'"
#define DBI_ERR_INVALID_STATEMENT   "Invalid statement handle"
#define DBI_ERR_NOT_IMPLEMENTED     "Method %s.%s is not implemented"
#define DBI_ERR_QUOTING_STR         "Error quoting string: %s"

/*
 * convert string to lower case
 */
const char *strlower(char *in);

/*
 * replace '?' placeholders with .\d+ placeholders
 * to be compatible with the native driver API
 */
char *replace_placeholders(lua_State *L, char native_prefix, const char *sql);