2010-10-07 15:54:07 +02:00
|
|
|
/*
|
2020-01-02 06:44:10 +01:00
|
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
2010-10-07 15:54:07 +02:00
|
|
|
*
|
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
2008-10-02 16:23:55 -05:00
|
|
|
*/
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2010-09-24 22:16:21 +02:00
|
|
|
#ifndef _FIELD_H
|
|
|
|
|
#define _FIELD_H
|
|
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
#include "Define.h"
|
|
|
|
|
#include "DatabaseEnvFwd.h"
|
2020-08-02 22:52:21 +02:00
|
|
|
#include <array>
|
2020-08-28 00:11:16 +02:00
|
|
|
#include <string>
|
|
|
|
|
#include <string_view>
|
2017-05-12 18:49:51 +02:00
|
|
|
#include <vector>
|
2010-09-24 22:16:21 +02:00
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
enum class DatabaseFieldTypes : uint8
|
|
|
|
|
{
|
|
|
|
|
Null,
|
|
|
|
|
Int8,
|
|
|
|
|
Int16,
|
|
|
|
|
Int32,
|
|
|
|
|
Int64,
|
|
|
|
|
Float,
|
|
|
|
|
Double,
|
|
|
|
|
Decimal,
|
|
|
|
|
Date,
|
|
|
|
|
Binary
|
|
|
|
|
};
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2020-07-05 12:17:47 +02:00
|
|
|
struct QueryResultFieldMetadata
|
|
|
|
|
{
|
|
|
|
|
char const* TableName = nullptr;
|
|
|
|
|
char const* TableAlias = nullptr;
|
|
|
|
|
char const* Name = nullptr;
|
|
|
|
|
char const* Alias = nullptr;
|
|
|
|
|
char const* TypeName = nullptr;
|
|
|
|
|
uint32 Index = 0;
|
|
|
|
|
DatabaseFieldTypes Type = DatabaseFieldTypes::Null;
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-13 11:13:04 +02:00
|
|
|
/**
|
|
|
|
|
@class Field
|
|
|
|
|
|
|
|
|
|
@brief Class used to access individual fields of database query result
|
|
|
|
|
|
|
|
|
|
Guideline on field type matching:
|
|
|
|
|
|
|
|
|
|
| MySQL type | method to use |
|
|
|
|
|
|------------------------|----------------------------------------|
|
|
|
|
|
| TINYINT | GetBool, GetInt8, GetUInt8 |
|
|
|
|
|
| SMALLINT | GetInt16, GetUInt16 |
|
|
|
|
|
| MEDIUMINT, INT | GetInt32, GetUInt32 |
|
|
|
|
|
| BIGINT | GetInt64, GetUInt64 |
|
|
|
|
|
| FLOAT | GetFloat |
|
|
|
|
|
| DOUBLE, DECIMAL | GetDouble |
|
|
|
|
|
| CHAR, VARCHAR, | GetCString, GetString |
|
|
|
|
|
| TINYTEXT, MEDIUMTEXT, | GetCString, GetString |
|
|
|
|
|
| TEXT, LONGTEXT | GetCString, GetString |
|
|
|
|
|
| TINYBLOB, MEDIUMBLOB, | GetBinary, GetString |
|
|
|
|
|
| BLOB, LONGBLOB | GetBinary, GetString |
|
|
|
|
|
| BINARY, VARBINARY | GetBinary |
|
|
|
|
|
|
|
|
|
|
Return types of aggregate functions:
|
|
|
|
|
|
|
|
|
|
| Function | Type |
|
|
|
|
|
|----------|-------------------|
|
|
|
|
|
| MIN, MAX | Same as the field |
|
|
|
|
|
| SUM, AVG | DECIMAL |
|
|
|
|
|
| COUNT | BIGINT |
|
|
|
|
|
*/
|
2016-03-11 15:57:26 +01:00
|
|
|
class TC_DATABASE_API Field
|
2008-10-02 16:23:55 -05:00
|
|
|
{
|
2010-09-24 22:16:21 +02:00
|
|
|
friend class ResultSet;
|
|
|
|
|
friend class PreparedResultSet;
|
|
|
|
|
|
2008-10-02 16:23:55 -05:00
|
|
|
public:
|
2015-08-26 17:00:26 +02:00
|
|
|
Field();
|
|
|
|
|
~Field();
|
2010-09-25 22:03:57 +02:00
|
|
|
|
2010-09-24 22:16:21 +02:00
|
|
|
bool GetBool() const // Wrapper, actually gets integer
|
|
|
|
|
{
|
|
|
|
|
return GetUInt8() == 1 ? true : false;
|
|
|
|
|
}
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
uint8 GetUInt8() const;
|
|
|
|
|
int8 GetInt8() const;
|
|
|
|
|
uint16 GetUInt16() const;
|
|
|
|
|
int16 GetInt16() const;
|
|
|
|
|
uint32 GetUInt32() const;
|
|
|
|
|
int32 GetInt32() const;
|
|
|
|
|
uint64 GetUInt64() const;
|
|
|
|
|
int64 GetInt64() const;
|
|
|
|
|
float GetFloat() const;
|
|
|
|
|
double GetDouble() const;
|
|
|
|
|
char const* GetCString() const;
|
|
|
|
|
std::string GetString() const;
|
2020-08-28 00:11:16 +02:00
|
|
|
std::string_view GetStringView() const;
|
2017-05-12 18:49:51 +02:00
|
|
|
std::vector<uint8> GetBinary() const;
|
2020-08-02 22:52:21 +02:00
|
|
|
template <size_t S>
|
|
|
|
|
std::array<uint8, S> GetBinary() const
|
|
|
|
|
{
|
|
|
|
|
std::array<uint8, S> buf;
|
|
|
|
|
GetBinarySizeChecked(buf.data(), S);
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-02 12:22:50 +01:00
|
|
|
bool IsNull() const
|
|
|
|
|
{
|
2020-07-05 12:17:47 +02:00
|
|
|
return data.value == nullptr;
|
2013-01-02 12:22:50 +01:00
|
|
|
}
|
|
|
|
|
|
2015-08-26 17:00:26 +02:00
|
|
|
protected:
|
2010-09-25 22:03:57 +02:00
|
|
|
struct
|
2010-09-24 22:16:21 +02:00
|
|
|
{
|
2020-07-05 12:17:47 +02:00
|
|
|
char const* value; // Actual data in memory
|
|
|
|
|
uint32 length; // Length
|
|
|
|
|
bool raw; // Raw bytes? (Prepared statement or ad hoc)
|
2010-12-19 17:48:47 +01:00
|
|
|
} data;
|
2010-09-25 22:03:57 +02:00
|
|
|
|
2020-07-05 12:17:47 +02:00
|
|
|
void SetByteValue(char const* newValue, uint32 length);
|
|
|
|
|
void SetStructuredValue(char const* newValue, uint32 length);
|
2009-10-17 15:51:44 -07:00
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
bool IsType(DatabaseFieldTypes type) const;
|
2012-03-25 14:10:06 +03:00
|
|
|
|
2017-05-12 18:49:51 +02:00
|
|
|
bool IsNumeric() const;
|
2012-04-02 03:30:19 +02:00
|
|
|
|
|
|
|
|
private:
|
2020-07-05 12:17:47 +02:00
|
|
|
QueryResultFieldMetadata const* meta;
|
2020-01-08 18:09:08 +01:00
|
|
|
void LogWrongType(char const* getter) const;
|
2020-07-05 12:17:47 +02:00
|
|
|
void SetMetadata(QueryResultFieldMetadata const* fieldMeta);
|
2020-08-02 22:52:21 +02:00
|
|
|
|
|
|
|
|
void GetBinarySizeChecked(uint8* buf, size_t size) const;
|
2008-10-02 16:23:55 -05:00
|
|
|
};
|
2010-09-24 22:16:21 +02:00
|
|
|
|
2008-10-02 16:23:55 -05:00
|
|
|
#endif
|