summaryrefslogtreecommitdiff
path: root/lib/LuaSQL.cc
blob: 2563ff697f32174589361e51a39f56e4b2dc6301 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 *  Baltisot
 *  Copyright (C) 1999-2008 Nicolas "Pixel" Noble
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#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, { BLUA_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, { BLUA_STRING, BLUA_STRING, BLUA_STRING, BLUA_STRING, BLUA_NUMBER, BLUA_STRING, BLUA_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);
        {
            try {
                LuaSQLConnection c(new SQLConnection(host, user, passwd, db, port, socket, cflags));
                c.pushdestruct(L);
            }
            catch (GeneralException e) {
                L->error("Unable to connect to MySQL server: " + String(e.GetMsg()));
            }
        }
        break;
    }

    return 1;
}