1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

More incomplete implementation for the MySQL module.

This commit is contained in:
Sandu Liviu Catalin 2016-07-08 00:57:57 +03:00
parent 4e93e58397
commit bba09395d0
12 changed files with 672 additions and 28 deletions

View File

@ -23,6 +23,7 @@
</Compiler>
<Linker>
<Add option="-m32" />
<Add library="mysql" />
<Add directory="../lib/mingw32-d" />
</Linker>
<ExtraCommands>
@ -47,6 +48,7 @@
<Linker>
<Add option="-s" />
<Add option="-m32" />
<Add library="mysql" />
<Add directory="../lib/mingw32" />
</Linker>
<ExtraCommands>
@ -71,6 +73,7 @@
</Compiler>
<Linker>
<Add option="-m64" />
<Add library="mysql" />
<Add directory="../lib/mingw64-d" />
</Linker>
<ExtraCommands>
@ -96,6 +99,7 @@
<Linker>
<Add option="-s" />
<Add option="-m64" />
<Add library="mysql" />
<Add directory="../lib/mingw64" />
</Linker>
<ExtraCommands>
@ -119,6 +123,7 @@
</Compiler>
<Linker>
<Add option="-m32" />
<Add library="mysqlclient" />
<Add directory="../lib/gcc32-d" />
</Linker>
<ExtraCommands>
@ -143,6 +148,7 @@
<Linker>
<Add option="-s" />
<Add option="-m32" />
<Add library="mysqlclient" />
<Add directory="../lib/gcc32" />
</Linker>
<ExtraCommands>
@ -167,6 +173,7 @@
</Compiler>
<Linker>
<Add option="-m64" />
<Add library="mysqlclient" />
<Add directory="../lib/gcc64-d" />
</Linker>
<ExtraCommands>
@ -192,6 +199,7 @@
<Linker>
<Add option="-s" />
<Add option="-m64" />
<Add library="mysqlclient" />
<Add directory="../lib/gcc64" />
</Linker>
<ExtraCommands>
@ -219,6 +227,7 @@
<Linker>
<Add option="-m32" />
<Add option="-static" />
<Add library="mysql" />
<Add directory="../lib/mingw32-d" />
</Linker>
<ExtraCommands>
@ -247,6 +256,7 @@
<Add option="-s" />
<Add option="-m32" />
<Add option="-static" />
<Add library="mysql" />
<Add directory="../lib/mingw32" />
</Linker>
<ExtraCommands>
@ -275,6 +285,7 @@
<Linker>
<Add option="-m64" />
<Add option="-static" />
<Add library="mysql" />
<Add directory="../lib/mingw64-d" />
</Linker>
<ExtraCommands>
@ -304,6 +315,7 @@
<Add option="-s" />
<Add option="-m64" />
<Add option="-static" />
<Add library="mysql" />
<Add directory="../lib/mingw64" />
</Linker>
<ExtraCommands>
@ -331,6 +343,7 @@
<Linker>
<Add option="-m32" />
<Add option="-Bstatic" />
<Add library="mysqlclient" />
<Add directory="../lib/gcc32-d" />
</Linker>
<ExtraCommands>
@ -359,6 +372,7 @@
<Add option="-s" />
<Add option="-m32" />
<Add option="-Bstatic" />
<Add library="mysqlclient" />
<Add directory="../lib/gcc32" />
</Linker>
<ExtraCommands>
@ -387,6 +401,7 @@
<Linker>
<Add option="-m64" />
<Add option="-Bstatic" />
<Add library="mysqlclient" />
<Add directory="../lib/gcc64-d" />
</Linker>
<ExtraCommands>
@ -416,6 +431,7 @@
<Add option="-s" />
<Add option="-m64" />
<Add option="-Bstatic" />
<Add library="mysqlclient" />
<Add directory="../lib/gcc64" />
</Linker>
<ExtraCommands>
@ -435,17 +451,16 @@
<Add directory="../include" />
<Add directory="../config/common" />
</Compiler>
<Linker>
<Add library="mysql" />
</Linker>
<Unit filename="../modules/mysql/Account.cpp" />
<Unit filename="../modules/mysql/Account.hpp" />
<Unit filename="../modules/mysql/Column.cpp" />
<Unit filename="../modules/mysql/Column.hpp" />
<Unit filename="../modules/mysql/Common.cpp" />
<Unit filename="../modules/mysql/Common.hpp" />
<Unit filename="../modules/mysql/Connection.cpp" />
<Unit filename="../modules/mysql/Connection.hpp" />
<Unit filename="../modules/mysql/Convert.cpp" />
<Unit filename="../modules/mysql/Convert.hpp" />
<Unit filename="../modules/mysql/Field.cpp" />
<Unit filename="../modules/mysql/Field.hpp" />
<Unit filename="../modules/mysql/Handle/Connection.cpp" />
<Unit filename="../modules/mysql/Handle/Connection.hpp" />
<Unit filename="../modules/mysql/Handle/ResultSet.cpp" />
@ -453,6 +468,8 @@
<Unit filename="../modules/mysql/Handle/Statement.cpp" />
<Unit filename="../modules/mysql/Handle/Statement.hpp" />
<Unit filename="../modules/mysql/Module.cpp" />
<Unit filename="../modules/mysql/Parameter.cpp" />
<Unit filename="../modules/mysql/Parameter.hpp" />
<Unit filename="../modules/mysql/ResultSet.cpp" />
<Unit filename="../modules/mysql/ResultSet.hpp" />
<Unit filename="../modules/mysql/Savepoint.cpp" />

View File

@ -41,7 +41,7 @@ void SqThrowLast(HSQUIRRELVM vm, CSStr msg)
// Throw the resulting error message
STHROWF("%s [%s]", msg, val.mPtr);
}
// ------------------------------------------------------------------------------------------------
Object SqFromJSON(HSQUIRRELVM vm, json_t * jval)
{
@ -218,7 +218,13 @@ json_t * SqToJSON(HSQUIRRELVM vm, SQInteger idx)
// Return the resulted array
return arr;
} break;
default: STHROWF("Unknown conversion for type: %s", SqTypeName(vm, idx));
default:
{
// Grab the type name of the object on the stack
const String tn(SqTypeName(vm, idx));
// Now throw the error with the obtained name
STHROWF("Unknown conversion for type: %s", tn.c_str());
}
}
// Should not reach this point
return nullptr;

View File

@ -1,6 +1,12 @@
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
// ------------------------------------------------------------------------------------------------
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
@ -16,7 +22,7 @@ void SqDateToMySQLTime(Object & obj, MYSQL_TIME & t)
// Push the specified object onto the stack
Var< Object >::push(_SqVM, obj);
// Attempt to get the values inside the specified object
if (SQ_FAILED(_SqMod->GetDate(_SqVM, -1, &year, &month, &day)))
if (SQ_FAILED(SqMod_GetDate(_SqVM, -1, &year, &month, &day)))
{
STHROWF("Invalid date specified");
}
@ -45,7 +51,7 @@ void SqTimeToMySQLTime(Object & obj, MYSQL_TIME & t)
// Push the specified object onto the stack
Var< Object >::push(_SqVM, obj);
// Attempt to get the values inside the specified object
if (SQ_FAILED(_SqMod->GetTime(_SqVM, -1, &hour, &minute, &second, &milli)))
if (SQ_FAILED(SqMod_GetTime(_SqVM, -1, &hour, &minute, &second, &milli)))
{
STHROWF("Invalid time specified");
}
@ -71,7 +77,7 @@ void SqDatetimeToMySQLTime(Object & obj, MYSQL_TIME & t)
// Push the specified object onto the stack
Var< Object >::push(_SqVM, obj);
// Attempt to get the values inside the specified object
if (SQ_FAILED(_SqMod->GetDatetime(_SqVM, -1, &year, &month, &day, &hour, &minute, &second, &milli)))
if (SQ_FAILED(SqMod_GetDatetime(_SqVM, -1, &year, &month, &day, &hour, &minute, &second, &milli)))
{
STHROWF("Invalid date and time specified");
}

View File

@ -2,10 +2,7 @@
#define _SQMYSQL_COMMON_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include <mysql.h>
#include "Convert.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {

427
modules/mysql/Convert.cpp Normal file
View File

@ -0,0 +1,427 @@
// ------------------------------------------------------------------------------------------------
#include "Convert.hpp"
// ------------------------------------------------------------------------------------------------
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
CSStr SqMySQLTypename(enum_field_types type)
{
switch(type)
{
case MYSQL_TYPE_DECIMAL: return _SC("decimal");
case MYSQL_TYPE_TINY: return _SC("tiny");
case MYSQL_TYPE_SHORT: return _SC("short");
case MYSQL_TYPE_LONG: return _SC("long");
case MYSQL_TYPE_FLOAT: return _SC("float");
case MYSQL_TYPE_DOUBLE: return _SC("double");
case MYSQL_TYPE_NULL: return _SC("null");
case MYSQL_TYPE_TIMESTAMP: return _SC("time-stamp");
case MYSQL_TYPE_LONGLONG: return _SC("long-long");
case MYSQL_TYPE_INT24: return _SC("int24");
case MYSQL_TYPE_DATE: return _SC("date");
case MYSQL_TYPE_TIME: return _SC("time");
case MYSQL_TYPE_DATETIME: return _SC("date-time");
case MYSQL_TYPE_YEAR: return _SC("year");
case MYSQL_TYPE_NEWDATE: return _SC("new-date");
case MYSQL_TYPE_VARCHAR: return _SC("var-char");
case MYSQL_TYPE_BIT: return _SC("bit");
#ifdef MYSQL_TYPE_TIMESTAMP2
case MYSQL_TYPE_TIMESTAMP2: return _SC("time-stamp2");
#endif // MYSQL_TYPE_TIMESTAMP2
#ifdef MYSQL_TYPE_DATETIME2
case MYSQL_TYPE_DATETIME2: return _SC("date-time2");
#endif // MYSQL_TYPE_DATETIME2
#ifdef MYSQL_TYPE_TIME2
case MYSQL_TYPE_TIME2: return _SC("time2");
#endif // MYSQL_TYPE_TIME2
case MYSQL_TYPE_NEWDECIMAL: return _SC("new-decimal");
case MYSQL_TYPE_ENUM: return _SC("enum");
case MYSQL_TYPE_SET: return _SC("set");
case MYSQL_TYPE_TINY_BLOB: return _SC("tiny-blob");
case MYSQL_TYPE_MEDIUM_BLOB: return _SC("medium-blob");
case MYSQL_TYPE_LONG_BLOB: return _SC("long-blob");
case MYSQL_TYPE_BLOB: return _SC("blob");
case MYSQL_TYPE_VAR_STRING: return _SC("var-string");
case MYSQL_TYPE_STRING: return _SC("string");
case MYSQL_TYPE_GEOMETRY: return _SC("geometry");
default: return _SC("unknown");
}
}
// ------------------------------------------------------------------------------------------------
CSStr SqMySQLTypenameC(enum_field_types type)
{
switch(type)
{
case MYSQL_TYPE_DECIMAL: return _SC("Decimal");
case MYSQL_TYPE_TINY: return _SC("Tiny");
case MYSQL_TYPE_SHORT: return _SC("Short");
case MYSQL_TYPE_LONG: return _SC("Long");
case MYSQL_TYPE_FLOAT: return _SC("Float");
case MYSQL_TYPE_DOUBLE: return _SC("Double");
case MYSQL_TYPE_NULL: return _SC("Null");
case MYSQL_TYPE_TIMESTAMP: return _SC("Time-Stamp");
case MYSQL_TYPE_LONGLONG: return _SC("Long-Long");
case MYSQL_TYPE_INT24: return _SC("Int24");
case MYSQL_TYPE_DATE: return _SC("Date");
case MYSQL_TYPE_TIME: return _SC("Time");
case MYSQL_TYPE_DATETIME: return _SC("Date-Time");
case MYSQL_TYPE_YEAR: return _SC("Year");
case MYSQL_TYPE_NEWDATE: return _SC("New-Date");
case MYSQL_TYPE_VARCHAR: return _SC("Var-Char");
case MYSQL_TYPE_BIT: return _SC("Bit");
#ifdef MYSQL_TYPE_TIMESTAMP2
case MYSQL_TYPE_TIMESTAMP2: return _SC("Time-Stamp2");
#endif // MYSQL_TYPE_TIMESTAMP2
#ifdef MYSQL_TYPE_DATETIME2
case MYSQL_TYPE_DATETIME2: return _SC("Date-Time2");
#endif // MYSQL_TYPE_DATETIME2
#ifdef MYSQL_TYPE_TIME2
case MYSQL_TYPE_TIME2: return _SC("Time2");
#endif // MYSQL_TYPE_TIME2
case MYSQL_TYPE_NEWDECIMAL: return _SC("New-Decimal");
case MYSQL_TYPE_ENUM: return _SC("Enum");
case MYSQL_TYPE_SET: return _SC("Set");
case MYSQL_TYPE_TINY_BLOB: return _SC("Tiny-Blob");
case MYSQL_TYPE_MEDIUM_BLOB: return _SC("Medium-Blob");
case MYSQL_TYPE_LONG_BLOB: return _SC("Long-Blob");
case MYSQL_TYPE_BLOB: return _SC("Blob");
case MYSQL_TYPE_VAR_STRING: return _SC("Var-String");
case MYSQL_TYPE_STRING: return _SC("String");
case MYSQL_TYPE_GEOMETRY: return _SC("Geometry");
default: return _SC("Unknown");
}
}
// ------------------------------------------------------------------------------------------------
static inline Int64 MySQLDateStrToSeconds(CSStr value)
{
Uint32 y = 1000, m = 1, d = 1;
// Attempt to extract the numeric values from the string
std::sscanf(value, "%u - %u - %u", &y, &m, &d);
// Calculate the number of seconds and return it
return SqMod_DateRangeToSeconds(1000, 1, 1, y, m, d);
}
// ------------------------------------------------------------------------------------------------
static inline Int64 MySQLTimeStrToSeconds(CSStr value)
{
Int32 h = 0, m = 0, s = 0;
// Attempt to extract the numeric values from the string
std::sscanf(value, "%d : %d : %d", &h, &m, &s);
// Convert the hours to seconds
h *= (60 * 60);
// Add the remaining minutes and seconds and return the result
return (h < 0) ? (h - ((m * 60) + s)) : (h + ((m * 60) + s));
}
// ------------------------------------------------------------------------------------------------
static inline Int64 MySQLDatetimeStrToSeconds(CSStr value)
{
Uint32 y = 1000, mo = 1, d = 1, h = 0, mi = 0, s = 0;
// Attempt to extract the numeric values from the string
std::sscanf(value, "%u - %u - %u %u : %u : %u", &y, &mo, &d, &h, &mi, &s);
// Calculate the number of seconds and return it
return SqMod_DateRangeToSeconds(1000, 1, 1, y, mo, d) + (h * (60 * 60)) + ((mi * 60) + s);
}
// ------------------------------------------------------------------------------------------------
static inline Int64 MySQLTimestampStrToSeconds(CSStr value)
{
Uint32 y = 1000, mo = 1, d = 1, h = 0, mi = 0, s = 0;
// Attempt to extract the numeric values from the string
std::sscanf(value, "%u - %u - %u %u : %u : %u", &y, &mo, &d, &h, &mi, &s);
// Detect if this was time-stamp 0
if (!y && !mo && !d && !h && !mi && !s)
{
return 0;
}
// Calculate the number of seconds and return it
return SqMod_DateRangeToSeconds(1970, 1, 1, y, mo, d) + (h * (60 * 60)) + ((mi * 60) + s);
}
// ------------------------------------------------------------------------------------------------
template < typename T > static inline T MemToNum(const Uint8 * b, Ulong l)
{
union
{
Uint8 a[8];
T n;
};
n = 0ULL;
switch (l)
{
default:
case 8:
{
a[7] = b[0];
a[6] = b[1];
a[5] = b[2];
a[4] = b[3];
a[3] = b[4];
a[2] = b[5];
a[1] = b[6];
a[0] = b[7];
} break;
case 7:
{
a[6] = b[0];
a[5] = b[1];
a[4] = b[2];
a[3] = b[3];
a[2] = b[4];
a[1] = b[5];
a[0] = b[6];
} break;
case 6:
{
a[5] = b[0];
a[4] = b[1];
a[3] = b[2];
a[2] = b[3];
a[1] = b[4];
a[0] = b[5];
} break;
case 5:
{
a[4] = b[0];
a[3] = b[1];
a[2] = b[2];
a[1] = b[3];
a[0] = b[4];
} break;
case 4:
{
a[3] = b[0];
a[2] = b[1];
a[1] = b[2];
a[0] = b[3];
} break;
case 3:
{
a[2] = b[0];
a[1] = b[1];
a[0] = b[2];
} break;
case 2:
{
a[1] = b[0];
a[0] = b[1];
} break;
case 1:
{
a[0] = b[0];
} break;
case 0: break;
}
return n;
}
// ------------------------------------------------------------------------------------------------
template < typename T >
static inline T ConvertToSInt(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
// Is there even a value to attempt to extract?
if (!value || *value == '\0')
{
return 0;
}
// Identify the type of value that must be converted and try to at least approximate something
switch(type)
{
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_GEOMETRY: return static_cast< T >(0);
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG: return ConvTo< T >::From(std::strtol(value, nullptr, 10));
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_ENUM:
case MYSQL_TYPE_SET: return ConvTo< T >::From(std::strtoll(value, nullptr, 10));
case MYSQL_TYPE_FLOAT: return ConvTo< T >::From(std::strtof(value, nullptr));
case MYSQL_TYPE_DOUBLE:
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_NEWDECIMAL: return ConvTo< T >::From(std::strtod(value, nullptr));
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_NEWDATE: return ConvTo< T >::From(MySQLDateStrToSeconds(value));
case MYSQL_TYPE_TIME: return ConvTo< T >::From(MySQLTimeStrToSeconds(value));
case MYSQL_TYPE_TIMESTAMP: return ConvTo< T >::From(MySQLTimestampStrToSeconds(value));
case MYSQL_TYPE_DATETIME: return ConvTo< T >::From(MySQLDatetimeStrToSeconds(value));
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB: return MemToNum< T >(reinterpret_cast< const Uint8 * >(value), length);
default:
{
STHROWF("Unknown conversion from (%s) to (%s)", SqMySQLTypenameC(type), tn);
} break;
}
// Should not reach this point!
return 0;
}
// ------------------------------------------------------------------------------------------------
template < typename T >
static inline T ConvertToUInt(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
// Is there even a value to attempt to extract?
if (!value || *value == '\0')
{
return 0;
}
// Identify the type of value that must be converted and try to at least approximate something
switch(type)
{
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_GEOMETRY: return static_cast< T >(0);
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG: return ConvTo< T >::From(std::strtoul(value, nullptr, 10));
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_ENUM:
case MYSQL_TYPE_SET: return ConvTo< T >::From(std::strtoull(value, nullptr, 10));
case MYSQL_TYPE_FLOAT: return ConvTo< T >::From(std::strtof(value, nullptr));
case MYSQL_TYPE_DOUBLE:
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_NEWDECIMAL: return ConvTo< T >::From(std::strtod(value, nullptr));
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_NEWDATE: return ConvTo< T >::From(MySQLDateStrToSeconds(value));
case MYSQL_TYPE_TIME: return ConvTo< T >::From(MySQLTimeStrToSeconds(value));
case MYSQL_TYPE_TIMESTAMP: return ConvTo< T >::From(MySQLTimestampStrToSeconds(value));
case MYSQL_TYPE_DATETIME: return ConvTo< T >::From(MySQLDatetimeStrToSeconds(value));
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB: return MemToNum< T >(reinterpret_cast< const Uint8 * >(value), length);
default:
{
STHROWF("Unknown conversion from (%s) to (%s)", SqMySQLTypenameC(type), tn);
} break;
}
// Should not reach this point!
return 0;
}
// ------------------------------------------------------------------------------------------------
Int8 DbConvTo< Int8 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return ConvertToUInt< Int8 >(value, length, type, tn);
}
// ------------------------------------------------------------------------------------------------
Uint8 DbConvTo< Uint8 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return ConvertToUInt< Uint8 >(value, length, type, tn);
}
// ------------------------------------------------------------------------------------------------
Int16 DbConvTo< Int16 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return ConvertToUInt< Int16 >(value, length, type, tn);
}
// ------------------------------------------------------------------------------------------------
Uint16 DbConvTo< Uint16 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return ConvertToUInt< Uint16 >(value, length, type, tn);
}
// ------------------------------------------------------------------------------------------------
Int32 DbConvTo< Int32 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return ConvertToUInt< Int32 >(value, length, type, tn);
}
// ------------------------------------------------------------------------------------------------
Uint32 DbConvTo< Uint32 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return ConvertToUInt< Uint32 >(value, length, type, tn);
}
// ------------------------------------------------------------------------------------------------
Int64 DbConvTo< Int64 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return ConvertToUInt< Int64 >(value, length, type, tn);
}
// ------------------------------------------------------------------------------------------------
Uint64 DbConvTo< Uint64 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return ConvertToUInt< Uint64 >(value, length, type, tn);
}
// ------------------------------------------------------------------------------------------------
Float32 DbConvTo< Float32 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
// Is there even a value to attempt to extract?
if (!value || *value == '\0')
{
return 0;
}
// Identify the type of value that must be converted and try to at least approximate something
switch(type)
{
case MYSQL_TYPE_NULL:
case MYSQL_TYPE_GEOMETRY: return static_cast< Float32 >(0);
case MYSQL_TYPE_BIT:
case MYSQL_TYPE_YEAR:
case MYSQL_TYPE_TINY:
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_LONG: return ConvTo< Float32 >::From(std::strtol(value, nullptr, 10));
case MYSQL_TYPE_LONGLONG:
case MYSQL_TYPE_ENUM:
case MYSQL_TYPE_SET: return ConvTo< Float32 >::From(std::strtoll(value, nullptr, 10));
case MYSQL_TYPE_VARCHAR:
case MYSQL_TYPE_VAR_STRING:
case MYSQL_TYPE_STRING:
case MYSQL_TYPE_FLOAT: return std::strtof(value, nullptr);
case MYSQL_TYPE_DOUBLE:
case MYSQL_TYPE_DECIMAL:
case MYSQL_TYPE_NEWDECIMAL: return ConvTo< Float32 >::From(std::strtod(value, nullptr));
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_NEWDATE: return ConvTo< Float32 >::From(MySQLDateStrToSeconds(value));
case MYSQL_TYPE_TIME: return ConvTo< Float32 >::From(MySQLTimeStrToSeconds(value));
case MYSQL_TYPE_TIMESTAMP: return ConvTo< Float32 >::From(MySQLTimestampStrToSeconds(value));
case MYSQL_TYPE_DATETIME: return ConvTo< Float32 >::From(MySQLDatetimeStrToSeconds(value));
case MYSQL_TYPE_TINY_BLOB:
case MYSQL_TYPE_MEDIUM_BLOB:
case MYSQL_TYPE_LONG_BLOB:
case MYSQL_TYPE_BLOB: return MemToNum< Float32 >(reinterpret_cast< const Uint8 * >(value), length);
default:
{
STHROWF("Unknown conversion from (%s) to (%s)", SqMySQLTypenameC(type), tn);
} break;
}
// Should not reach this point!
return 0;
}
// ------------------------------------------------------------------------------------------------
Float64 DbConvTo< Float64 >::From(CSStr value, Ulong length, enum_field_types type, CSStr tn)
{
return 0.0;
}
} // Namespace:: SqMod

118
modules/mysql/Convert.hpp Normal file
View File

@ -0,0 +1,118 @@
#ifndef _SQMYSQL_CONVERT_HPP_
#define _SQMYSQL_CONVERT_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include <mysql.h>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
*/
struct DbDecimal;
struct DbDate;
struct DbTime;
struct DbDatetime;
/* ------------------------------------------------------------------------------------------------
* Retrieve the lowercase name of a MySQL data-type.
*/
CSStr SqMySQLTypename(enum_field_types type);
/* ------------------------------------------------------------------------------------------------
* Retrieve the capitalized name of a MySQL data-type.
*/
CSStr SqMySQLTypenameC(enum_field_types type);
/* ------------------------------------------------------------------------------------------------
* Utility used to convert from database types to known types.
*/
template < typename T > struct DbConvTo;
/* ------------------------------------------------------------------------------------------------
* Specialization for signed 8 bit integer.
*/
template<> struct DbConvTo< Int8 >
{
static Int8 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Int8"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for unsigned 8 bit integer.
*/
template<> struct DbConvTo< Uint8 >
{
static Uint8 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Uint8"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for signed 16 bit integer.
*/
template<> struct DbConvTo< Int16 >
{
static Int16 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Int16"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for unsigned 16 bit integer.
*/
template<> struct DbConvTo< Uint16 >
{
static Uint16 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Uint16"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for signed 32 bit integer.
*/
template<> struct DbConvTo< Int32 >
{
static Int32 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Int32"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for unsigned 32 bit integer.
*/
template<> struct DbConvTo< Uint32 >
{
static Uint32 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Uint32"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for signed 64 bit integer.
*/
template<> struct DbConvTo< Int64 >
{
static Int64 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Int64"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for unsigned 64 bit integer.
*/
template<> struct DbConvTo< Uint64 >
{
static Uint64 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Uint64"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for 32 floating point.
*/
template<> struct DbConvTo< Float32 >
{
static Float32 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Float32"));
};
/* ------------------------------------------------------------------------------------------------
* Specialization for 64 floating point.
*/
template<> struct DbConvTo< Float64 >
{
static Float64 From(CSStr value, Ulong length, enum_field_types type, CSStr tn = _SC("Float64"));
};
} // Namespace:: SqMod
#endif // _SQMYSQL_CONVERT_HPP_

View File

@ -1,5 +1,5 @@
// ------------------------------------------------------------------------------------------------
#include "Account.hpp"
#include "Field.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {

61
modules/mysql/Field.hpp Normal file
View File

@ -0,0 +1,61 @@
#ifndef _SQMYSQL_FIELD_HPP_
#define _SQMYSQL_FIELD_HPP_
// ------------------------------------------------------------------------------------------------
#include "Handle/ResultSet.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* ...
*/
class Field
{
// --------------------------------------------------------------------------------------------
friend class ResultSet;
private:
// --------------------------------------------------------------------------------------------
ResRef m_Handle; // Reference to the actual database result-set.
Uint32 m_Index; // Index to the managed field.
protected:
/* --------------------------------------------------------------------------------------------
* Handle constructor.
*/
Field(const ResRef & hnd, Uint32 idx)
: m_Handle(hnd), m_Index(idx)
{
/* ... */
}
public:
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Field(const Field & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Field(Field && o) = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Field & operator = (const Field & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Field & operator = (Field && o) = default;
};
} // Namespace:: SqMod
#endif // _SQMYSQL_FIELD_HPP_

View File

@ -37,8 +37,10 @@ void OnSquirrelInitialize()
}
else
{
// Expand the Squirrel plug-in API into global functions
sqmod_api_expand(_SqMod);
// Obtain the Squirrel API
_SqAPI = _SqMod->GetSquirrelAPI();
_SqAPI = SqMod_GetSquirrelAPI();
// Expand the Squirrel API into global functions
sq_api_expand(_SqAPI);
}
@ -55,7 +57,7 @@ void OnSquirrelLoad()
return; // Unable to proceed.
}
// Obtain the Squirrel API and VM
_SqVM = _SqMod->GetSquirrelVM();
_SqVM = SqMod_GetSquirrelVM();
// Make sure that a valid virtual machine exists
if (!_SqVM)
{

View File

@ -0,0 +1,10 @@
// ------------------------------------------------------------------------------------------------
#include "Parameter.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
} // Namespace:: SqMod

View File

@ -1,5 +1,5 @@
#ifndef _SQMYSQL_COLUMN_HPP_
#define _SQMYSQL_COLUMN_HPP_
#ifndef _SQMYSQL_PARAMETER_HPP_
#define _SQMYSQL_PARAMETER_HPP_
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
@ -14,4 +14,4 @@ namespace SqMod {
} // Namespace:: SqMod
#endif // _SQMYSQL_COLUMN_HPP_
#endif // _SQMYSQL_PARAMETER_HPP_

View File

@ -116,7 +116,7 @@ SQInteger ResultSet::GetInt8(Uint32 idx) const
return ConvTo< Int8 >::From(m_Handle->mBinds[idx].mInt64);
}
// Retrieve the value directly from the row
return ConvTo< Int8 >::From(std::strtol(m_Handle->mRow[idx], nullptr, 10));
return DbConvTo< Int8 >::From(m_Handle->mRow[idx], m_Handle->mLengths[idx], m_Handle->mFields[idx].type);
}
// ------------------------------------------------------------------------------------------------
@ -129,7 +129,7 @@ SQInteger ResultSet::GetUint8(Uint32 idx) const
return ConvTo< Uint8 >::From(m_Handle->mBinds[idx].mInt64);
}
// Retrieve the value directly from the row
return ConvTo< Uint8 >::From(std::strtoul(m_Handle->mRow[idx], nullptr, 10));
return DbConvTo< Uint8 >::From(m_Handle->mRow[idx], m_Handle->mLengths[idx], m_Handle->mFields[idx].type);
}
// ------------------------------------------------------------------------------------------------
@ -142,7 +142,7 @@ SQInteger ResultSet::GetInt16(Uint32 idx) const
return ConvTo< Int16 >::From(m_Handle->mBinds[idx].mInt64);
}
// Retrieve the value directly from the row
return ConvTo< Int16 >::From(std::strtol(m_Handle->mRow[idx], nullptr, 10));
return DbConvTo< Int16 >::From(m_Handle->mRow[idx], m_Handle->mLengths[idx], m_Handle->mFields[idx].type);
}
// ------------------------------------------------------------------------------------------------
@ -155,7 +155,7 @@ SQInteger ResultSet::GetUint16(Uint32 idx) const
return ConvTo< Uint16 >::From(m_Handle->mBinds[idx].mInt64);
}
// Retrieve the value directly from the row
return ConvTo< Uint16 >::From(std::strtoul(m_Handle->mRow[idx], nullptr, 10));
return DbConvTo< Uint16 >::From(m_Handle->mRow[idx], m_Handle->mLengths[idx], m_Handle->mFields[idx].type);
}
// ------------------------------------------------------------------------------------------------
@ -168,7 +168,7 @@ SQInteger ResultSet::GetInt32(Uint32 idx) const
return ConvTo< Int32 >::From(m_Handle->mBinds[idx].mInt64);
}
// Retrieve the value directly from the row
return ConvTo< Int32 >::From(std::strtol(m_Handle->mRow[idx], nullptr, 10));
return DbConvTo< Int32 >::From(m_Handle->mRow[idx], m_Handle->mLengths[idx], m_Handle->mFields[idx].type);
}
// ------------------------------------------------------------------------------------------------
@ -181,7 +181,7 @@ SQInteger ResultSet::GetUint32(Uint32 idx) const
return ConvTo< Uint32 >::From(m_Handle->mBinds[idx].mInt64);
}
// Retrieve the value directly from the row
return ConvTo< Uint32 >::From(std::strtoul(m_Handle->mRow[idx], nullptr, 10));
return DbConvTo< Uint32 >::From(m_Handle->mRow[idx], m_Handle->mLengths[idx], m_Handle->mFields[idx].type);
}
// ------------------------------------------------------------------------------------------------
@ -194,7 +194,7 @@ Int64 ResultSet::GetInt64(Uint32 idx) const
return ConvTo< Int64 >::From(m_Handle->mBinds[idx].mInt64);
}
// Retrieve the value directly from the row
return std::strtoll(m_Handle->mRow[idx], nullptr, 10);
return DbConvTo< Int64 >::From(m_Handle->mRow[idx], m_Handle->mLengths[idx], m_Handle->mFields[idx].type);
}
// ------------------------------------------------------------------------------------------------
@ -207,7 +207,7 @@ Uint64 ResultSet::GetUint64(Uint32 idx) const
return ConvTo< Uint64 >::From(m_Handle->mBinds[idx].mInt64);
}
// Retrieve the value directly from the row
return std::strtoull(m_Handle->mRow[idx], nullptr, 10);
return DbConvTo< Uint64 >::From(m_Handle->mRow[idx], m_Handle->mLengths[idx], m_Handle->mFields[idx].type);
}
// ------------------------------------------------------------------------------------------------