Core/DBLayer:

- Allow storing floats in prepared statements explicitly (previously would be casted to double)
- Add GetBool ¨wrapper/hack¨ to PreparedResultset class.

--HG--
branch : trunk
This commit is contained in:
Machiavelli
2010-09-19 15:25:50 +02:00
parent b50c931d6e
commit 22571e9443
4 changed files with 34 additions and 3 deletions
@@ -59,7 +59,10 @@ void PreparedStatement::BindParameters()
m_stmt->setInt64(i, statement_data[i].data.i64); m_stmt->setInt64(i, statement_data[i].data.i64);
break; break;
case TYPE_FLOAT: case TYPE_FLOAT:
m_stmt->setDouble(i, statement_data[i].data.f); m_stmt->setFloat(i, statement_data[i].data.f);
break;
case TYPE_DOUBLE:
m_stmt->setDouble(i, statement_data[i].data.d);
break; break;
case TYPE_STRING: case TYPE_STRING:
m_stmt->setString(i, statement_data[i].str.c_str()); m_stmt->setString(i, statement_data[i].str.c_str());
@@ -154,7 +157,7 @@ void PreparedStatement::setInt64(const uint8 index, const int64 value)
statement_data[index].type = TYPE_I64; statement_data[index].type = TYPE_I64;
} }
void PreparedStatement::setDouble(const uint8 index, const double value) void PreparedStatement::setFloat(const uint8 index, const float value)
{ {
if (index >= statement_data.size()) if (index >= statement_data.size())
statement_data.resize(index+1); statement_data.resize(index+1);
@@ -163,6 +166,15 @@ void PreparedStatement::setDouble(const uint8 index, const double value)
statement_data[index].type = TYPE_FLOAT; statement_data[index].type = TYPE_FLOAT;
} }
void PreparedStatement::setDouble(const uint8 index, const double value)
{
if (index >= statement_data.size())
statement_data.resize(index+1);
statement_data[index].data.d = value;
statement_data[index].type = TYPE_DOUBLE;
}
void PreparedStatement::setString(const uint8 index, const std::string& value) void PreparedStatement::setString(const uint8 index, const std::string& value)
{ {
if (index >= statement_data.size()) if (index >= statement_data.size())
@@ -274,6 +286,14 @@ void MySQLPreparedStatement::setInt64(const uint8 index, const int64 value)
setValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(int64), false); setValue(param, MYSQL_TYPE_LONGLONG, &value, sizeof(int64), false);
} }
void MySQLPreparedStatement::setFloat(const uint8 index, const float value)
{
CheckValidIndex(index);
m_paramsSet[index] = true;
MYSQL_BIND* param = &m_bind[index];
setValue(param, MYSQL_TYPE_FLOAT, &value, sizeof(double), (value > 0.0f));
}
void MySQLPreparedStatement::setDouble(const uint8 index, const double value) void MySQLPreparedStatement::setDouble(const uint8 index, const double value)
{ {
CheckValidIndex(index); CheckValidIndex(index);
@@ -33,7 +33,8 @@ union PreparedStatementDataUnion
int32 i32; int32 i32;
uint64 ui64; uint64 ui64;
int64 i64; int64 i64;
double f; // TODO: Maybe split it up in double and float types float f;
double d;
}; };
//- This enum helps us differ data held in above union //- This enum helps us differ data held in above union
@@ -49,6 +50,7 @@ enum PreparedStatementValueType
TYPE_I32, TYPE_I32,
TYPE_I64, TYPE_I64,
TYPE_FLOAT, TYPE_FLOAT,
TYPE_DOUBLE,
TYPE_STRING TYPE_STRING
}; };
@@ -82,6 +84,7 @@ class PreparedStatement
void setInt16(const uint8 index, const int16 value); void setInt16(const uint8 index, const int16 value);
void setInt32(const uint8 index, const int32 value); void setInt32(const uint8 index, const int32 value);
void setInt64(const uint8 index, const int64 value); void setInt64(const uint8 index, const int64 value);
void setFloat(const uint8 index, const float value);
void setDouble(const uint8 index, const double value); void setDouble(const uint8 index, const double value);
void setString(const uint8 index, const std::string& value); void setString(const uint8 index, const std::string& value);
@@ -115,6 +118,7 @@ class MySQLPreparedStatement
void setInt16(const uint8 index, const int16 value); void setInt16(const uint8 index, const int16 value);
void setInt32(const uint8 index, const int32 value); void setInt32(const uint8 index, const int32 value);
void setInt64(const uint8 index, const int64 value); void setInt64(const uint8 index, const int64 value);
void setFloat(const uint8 index, const float value);
void setDouble(const uint8 index, const double value); void setDouble(const uint8 index, const double value);
void setString(const uint8 index, const char* value); void setString(const uint8 index, const char* value);
@@ -185,6 +185,12 @@ void ResultBind::CleanUp()
delete[] m_rBind; delete[] m_rBind;
} }
bool PreparedResultSet::GetBool(uint32 index)
{
// TODO: Perhaps start storing data in genuine bool formats in tables
return GetUInt8(index) == 1 ? true : false;
}
uint8 PreparedResultSet::GetUInt8(uint32 index) uint8 PreparedResultSet::GetUInt8(uint32 index)
{ {
if (!CheckFieldIndex(index)) if (!CheckFieldIndex(index))
+1
View File
@@ -195,6 +195,7 @@ class PreparedResultSet
operator bool() { return num_rows > 0; } operator bool() { return num_rows > 0; }
bool GetBool(uint32 index);
uint8 GetUInt8(uint32 index); uint8 GetUInt8(uint32 index);
int8 GetInt8(uint32 index); int8 GetInt8(uint32 index);
uint16 GetUInt16(uint32 index); uint16 GetUInt16(uint32 index);