forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElunaQueryMethods.h
359 lines (329 loc) · 9.85 KB
/
ElunaQueryMethods.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright (C) 2010 - 2016 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#ifndef QUERYMETHODS_H
#define QUERYMETHODS_H
#ifndef TRINITY
#define RESULT result
#else
#define RESULT (*result)
#endif
/***
* The result of a database query.
*
* E.g. the return value of [Global:WorldDBQuery].
*
* Inherits all methods from: none
*/
namespace LuaQuery
{
static void CheckFields(lua_State* L, ElunaQuery* result)
{
uint32 field = Eluna::CHECKVAL<uint32>(L, 2);
uint32 count = RESULT->GetFieldCount();
if (field >= count)
{
char arr[256];
sprintf(arr, "trying to access invalid field index %u. There are %u fields available and the indexes start from 0", field, count);
luaL_argerror(L, 2, arr);
}
}
/**
* Returns `true` if the specified column of the current row is `NULL`, otherwise `false`.
*
* @param uint32 column
* @return bool isNull
*/
int IsNull(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
#ifndef TRINITY
Eluna::Push(L, RESULT->Fetch()[col].IsNULL());
#else
Eluna::Push(L, RESULT->Fetch()[col].IsNull());
#endif
return 1;
}
/**
* Returns the number of columns in the result set.
*
* @return uint32 columnCount
*/
int GetColumnCount(lua_State* L, ElunaQuery* result)
{
Eluna::Push(L, RESULT->GetFieldCount());
return 1;
}
/**
* Returns the number of rows in the result set.
*
* @return uint32 rowCount
*/
int GetRowCount(lua_State* L, ElunaQuery* result)
{
if (RESULT->GetRowCount() > (uint32)-1)
Eluna::Push(L, (uint32)-1);
else
Eluna::Push(L, (uint32)(RESULT->GetRowCount()));
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to a boolean.
*
* @param uint32 column
* @return bool data
*/
int GetBool(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetBool());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to an unsigned 8-bit integer.
*
* @param uint32 column
* @return uint8 data
*/
int GetUInt8(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetUInt8());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to an unsigned 16-bit integer.
*
* @param uint32 column
* @return uint16 data
*/
int GetUInt16(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetUInt16());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to an unsigned 32-bit integer.
*
* @param uint32 column
* @return uint32 data
*/
int GetUInt32(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetUInt32());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to an unsigned 64-bit integer.
*
* @param uint32 column
* @return uint64 data
*/
int GetUInt64(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetUInt64());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to a signed 8-bit integer.
*
* @param uint32 column
* @return int8 data
*/
int GetInt8(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetInt8());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to a signed 16-bit integer.
*
* @param uint32 column
* @return int16 data
*/
int GetInt16(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetInt16());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to a signed 32-bit integer.
*
* @param uint32 column
* @return int32 data
*/
int GetInt32(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetInt32());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to a signed 64-bit integer.
*
* @param uint32 column
* @return int64 data
*/
int GetInt64(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetInt64());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to a 32-bit floating point value.
*
* @param uint32 column
* @return float data
*/
int GetFloat(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetFloat());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to a 64-bit floating point value.
*
* @param uint32 column
* @return double data
*/
int GetDouble(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
Eluna::Push(L, RESULT->Fetch()[col].GetDouble());
return 1;
}
/**
* Returns the data in the specified column of the current row, casted to a string.
*
* @param uint32 column
* @return string data
*/
int GetString(lua_State* L, ElunaQuery* result)
{
uint32 col = Eluna::CHECKVAL<uint32>(L, 2);
CheckFields(L, result);
#ifndef TRINITY
Eluna::Push(L, RESULT->Fetch()[col].GetString());
#else
Eluna::Push(L, RESULT->Fetch()[col].GetCString());
#endif
return 1;
}
/**
* Advances the [ElunaQuery] to the next row in the result set.
*
* *Do not* call this immediately after a query, or you'll skip the first row.
*
* Returns `false` if there was no new row, otherwise `true`.
*
* @return bool hadNextRow
*/
int NextRow(lua_State* L, ElunaQuery* result)
{
Eluna::Push(L, RESULT->NextRow());
return 1;
}
/**
* Returns a table from the current row where keys are field names and values are the row's values.
*
* All numerical values will be numbers and everything else is returned as a string.
*
* **For example,** the query:
*
* SELECT entry, name FROM creature_template
*
* would result in a table like:
*
* { entry = 123, name = "some creature name" }
*
* To move to next row use [ElunaQuery:NextRow].
*
* @return table rowData : table filled with row columns and data where `T[column] = data`
*/
int GetRow(lua_State* L, ElunaQuery* result)
{
uint32 col = RESULT->GetFieldCount();
Field* row = RESULT->Fetch();
lua_createtable(L, 0, col);
int tbl = lua_gettop(L);
#ifndef TRINITY
const QueryFieldNames& names = RESULT->GetFieldNames();
#endif
for (uint32 i = 0; i < col; ++i)
{
#ifdef TRINITY
Eluna::Push(L, RESULT->GetFieldName(i));
const char* str = row[i].GetCString();
if (row[i].IsNull() || !str)
Eluna::Push(L);
else
{
// MYSQL_TYPE_LONGLONG Interpreted as string for lua
switch (row[i].GetType())
{
case DatabaseFieldTypes::Int8:
case DatabaseFieldTypes::Int16:
case DatabaseFieldTypes::Int32:
case DatabaseFieldTypes::Int64:
case DatabaseFieldTypes::Float:
case DatabaseFieldTypes::Double:
Eluna::Push(L, strtod(str, NULL));
break;
default:
Eluna::Push(L, str);
break;
}
}
#else
Eluna::Push(L, names[i]);
const char* str = row[i].GetString();
if (row[i].IsNULL() || !str)
Eluna::Push(L);
else
{
// MYSQL_TYPE_LONGLONG Interpreted as string for lua
switch (row[i].GetType())
{
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
Eluna::Push(L, strtod(str, NULL));
break;
default:
Eluna::Push(L, str);
break;
}
}
#endif
lua_rawset(L, tbl);
}
lua_settop(L, tbl);
return 1;
}
};
#undef RESULT
#endif