summaryrefslogtreecommitdiff
path: root/lib/LuaSQL.cc
blob: eb9c5c2aee74bc2e74822ebf5a0e6efd503ca77f (plain)
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
#ifdef _WIN32
#include <winsock.h>
#endif
#include <mysql.h>
#include "LuaSQL.h"

LuaSQLConnection::LuaSQLConnection(SQLConnection * _c) : c(_c) { }

enum SQLCONNECTION_methods_t {
    SQLCONNECTION_QUERY = 0,
    SQLCONNECTION_NUMROWS,
    SQLCONNECTION_NUMFIELDS,
    SQLCONNECTION_FETCHROW,
    SQLCONNECTION_NUMAFFECTEDROWS,
    SQLCONNECTION_INSERTID,
    SQLCONNECTION_ERRNO,
    SQLCONNECTION_ERROR,
};

enum SQLCONNECTION_functions_t {
    SQLCONNECTION_NEWSQLCONNECTION = 0,
};

struct lua_functypes_t SQLConnection_methods[] = {
    { SQLCONNECTION_QUERY,           "Query",           1, 1, {LUA_STRING} },
    { SQLCONNECTION_NUMROWS,         "NumRows",         0, 0, { } },
    { SQLCONNECTION_NUMFIELDS,       "NumFields",       0, 0, { } },
    { SQLCONNECTION_FETCHROW,        "FetchRow",        0, 0, { } },
    { SQLCONNECTION_NUMAFFECTEDROWS, "NumAffectedRows", 0, 0, { } },
    { SQLCONNECTION_INSERTID,        "InsertId",        0, 0, { } },
    { SQLCONNECTION_ERRNO,           "ErrNO",           0, 0, { } },
    { SQLCONNECTION_ERROR,           "Error",           0, 0, { } },
    { -1, 0, 0, 0, 0 }
};

struct lua_functypes_t SQLConnection_functions[] = {
    { SQLCONNECTION_NEWSQLCONNECTION,   "SQLConnection" ,     4, 7, {LUA_STRING, LUA_STRING, LUA_STRING, LUA_STRING, LUA_NUMBER, LUA_STRING, LUA_NUMBER } },
    { -1, 0, 0, 0, 0 }
};

class sLua_SQLConnection : public Base {
  public:
    DECLARE_METHOD(SQLConnection, SQLCONNECTION_QUERY);
    DECLARE_METHOD(SQLConnection, SQLCONNECTION_NUMROWS);
    DECLARE_METHOD(SQLConnection, SQLCONNECTION_NUMFIELDS);
    DECLARE_METHOD(SQLConnection, SQLCONNECTION_FETCHROW);
    DECLARE_METHOD(SQLConnection, SQLCONNECTION_NUMAFFECTEDROWS);
    DECLARE_METHOD(SQLConnection, SQLCONNECTION_INSERTID);
    DECLARE_METHOD(SQLConnection, SQLCONNECTION_ERRNO);
    DECLARE_METHOD(SQLConnection, SQLCONNECTION_ERROR);

    DECLARE_FUNCTION(SQLConnection, SQLCONNECTION_NEWSQLCONNECTION);
  private:
    static int SQLConnection_proceed(Lua * L, int n, SQLConnection * obj, int caller);
    static int SQLConnection_proceed_statics(Lua * L, int n, int caller);
};

void LuaSQLConnection::pushmembers(Lua * L) {
    pushme(L, c, "SQLConnection");

    PUSH_METHOD(SQLConnection, SQLCONNECTION_QUERY);
    PUSH_METHOD(SQLConnection, SQLCONNECTION_NUMROWS);
    PUSH_METHOD(SQLConnection, SQLCONNECTION_NUMFIELDS);
    PUSH_METHOD(SQLConnection, SQLCONNECTION_FETCHROW);
    PUSH_METHOD(SQLConnection, SQLCONNECTION_NUMAFFECTEDROWS);
    PUSH_METHOD(SQLConnection, SQLCONNECTION_INSERTID);
    PUSH_METHOD(SQLConnection, SQLCONNECTION_ERRNO);
    PUSH_METHOD(SQLConnection, SQLCONNECTION_ERROR);
}

void LuaSQLConnection::pushstatics(Lua * L) throw (GeneralException) {
    CHECK_METHODS(SQLConnection);
    CHECK_FUNCTIONS(SQLConnection);

    PUSH_FUNCTION(SQLConnection, SQLCONNECTION_NEWSQLCONNECTION);
}

int sLua_SQLConnection::SQLConnection_proceed(Lua * L, int n, SQLConnection * c, int caller) {
    String s;
    AssocArray r;
    AssocArrayIterator i;

    switch (caller) {
    case SQLCONNECTION_QUERY:
	s = L->tostring(2);
	L->push((lua_Number) c->query(s));
        break;
    case SQLCONNECTION_NUMROWS:
	L->push((lua_Number) c->numrows());
	break;
    case SQLCONNECTION_NUMFIELDS:
	L->push((lua_Number) c->numfields());
	break;
    case SQLCONNECTION_FETCHROW:
	r = c->fetchrow();
	L->newtable();
	for (i = r.begin(); i != r.end(); i++) {
	    L->push(i->first);
	    L->push(i->second);
	    L->settable();
	}
	break;
    case SQLCONNECTION_NUMAFFECTEDROWS:
	L->push((lua_Number) c->numaffectedrows());
	break;
    case SQLCONNECTION_INSERTID:
	L->push((lua_Number) c->insertid());
	break;
//    case SQLCONNECTION_ERRNO:
//	L->push((lua_Number) c->errno());
//	break;
    case SQLCONNECTION_ERROR:
	L->push(c->error());
	break;
    }

    return 1;
}

int sLua_SQLConnection::SQLConnection_proceed_statics(Lua * L, int n, int caller) {
    String host, user, passwd, db, socket = "";
    int port = 3306, cflags = 0;

    switch (caller) {
    case SQLCONNECTION_NEWSQLCONNECTION:
        host = L->tostring(1);
        user = L->tostring(2);
        passwd = L->tostring(3);
        db = L->tostring(4);
	if (n >= 5)
	    port = L->tonumber(5);
        if (n >= 6)
            socket = L->tostring(6);
	if (n >= 7)
	    cflags = L->tonumber(7);
        {
            LuaSQLConnection c(new SQLConnection(host, user, passwd, db, port, socket, cflags));
            c.pushdestruct(L);
        }
        break;
    }

    return 1;
}