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
|
#ifndef INSPIRCD_SQLAPI_2
#define INSPIRCD_SQLAPI_2
#define SQLREQID "SQLv2 Request"
#define SQLRESID "SQLv2 Result"
#define SQLSUCCESS "You shouldn't be reading this (success)"
#include <string>
#include <vector>
#include <map>
#include "modules.h"
enum SQLerrorNum { NO_ERROR, BAD_DBID, BAD_CONN, QSEND_FAIL };
class SQLexception
{
};
class SQLbadColName : public SQLexception
{
public:
SQLbadColName() { }
};
class SQLerror : public classbase
{
SQLerrorNum id;
std::string str;
public:
SQLerror(SQLerrorNum i = NO_ERROR, const std::string &s = "")
: id(i), str(s)
{
}
SQLerrorNum Id()
{
return id;
}
SQLerrorNum Id(SQLerrorNum i)
{
id = i;
return id;
}
void Str(const std::string &s)
{
str = s;
}
const char* Str()
{
if(str.length())
return str.c_str();
switch(id)
{
case NO_ERROR:
return "No error";
case BAD_DBID:
return "Invalid database ID";
case BAD_CONN:
return "Invalid connection";
case QSEND_FAIL:
return "Sending query failed";
default:
return "Unknown error";
}
}
};
class SQLrequest : public Request
{
public:
std::string query;
std::string dbid;
bool pri;
unsigned long id;
SQLerror error;
SQLrequest(Module* s, Module* d, const std::string &q, const std::string &id, bool p = false)
: Request(SQLREQID, s, d), query(q), dbid(id), pri(p), id(0)
{
}
};
class SQLfield
{
public:
/* The data itself */
std::string d;
/* If the field was null */
bool null;
SQLfield(const std::string &data, bool n)
: d(data), null(n)
{
}
};
typedef std::vector<SQLfield> SQLfieldList;
typedef std::map<std::string, SQLfield> SQLfieldMap;
class SQLresult : public Request
{
public:
std::string query;
std::string dbid;
SQLerror error;
SQLresult(Module* s, Module* d)
: Request(SQLRESID, s, d)
{
}
/* Return the number of rows in the result */
virtual int Rows() = 0;
/* Return the number of columns in the result */
virtual int Cols() = 0;
/* Get a string name of the column by an index number */
virtual std::string ColName(int column) = 0;
/* Get an index number for a column from a string name.
* An exception of type SQLbadColName will be thrown if
* the name given is invalid.
*/
virtual int ColNum(const std::string &column) = 0;
/* Get a string value in a given row and column */
virtual SQLfield GetValue(int row, int column) = 0;
/* Return a list of values in a row, this should
* increment an internal counter so you can repeatedly
* call it until it returns an empty vector.
* This returns a reference to an internal object,
* the same object is used for all calls to this function
* and therefore the return value is only valid until
* you call this function again. It is also invalid if
* the SQLresult object is destroyed.
*/
virtual SQLfieldList& GetRow() = 0;
/* As above, but return a map indexed by key name */
virtual SQLfieldMap& GetRowMap() = 0;
/* Like GetRow(), but returns a pointer to a dynamically
* allocated object which must be explicitly freed. For
* portability reasons this must be freed with SQLresult::Free()
*/
virtual SQLfieldList* GetRowPtr() = 0;
/* As above, but return a map indexed by key name */
virtual SQLfieldMap* GetRowMapPtr() = 0;
/* Overloaded function for freeing the lists and maps returned
* above.
*/
virtual void Free(SQLfieldMap* fm) = 0;
virtual void Free(SQLfieldList* fl) = 0;
};
#endif
|