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
|
/// AttributeLoader.hpp
/// Quick 'N Dirty
#ifndef ATTRIBUTE_LOADER_HPP_INCLUDED
#define ATTRIBUTE_LOADER_HPP_INCLUDED
#include <map>
#include <stdexcept>
#include <string>
std::map<std::string, Attribute> loadAttributes(const ResultSet& rs)
{
std::map<std::string, Attribute> attributes;
if(rs)
{
sql::ResultSetMetaData* meta = rs->getMetaData();
for(unsigned int i = 1; i < meta->getColumnCount(); ++i)
{
std::string alias = meta->getColumnName(i);
switch(meta->getColumnType(i))
{
case sql::DataType::TINYINT: // 8b
if(meta->isSigned(i))
{
attributes.insert(std::make_pair(alias, Attribute(static_cast<signed char>(rs->getInt(alias)))));
}
else
{
attributes.insert(std::make_pair(alias, Attribute(static_cast<unsigned char>(rs->getUInt(alias)))));
}
break;
case sql::DataType::SMALLINT: // 16b
if(meta->isSigned(i))
{
attributes.insert(std::make_pair(alias, Attribute(static_cast<signed short>(rs->getInt(alias)))));
}
else
{
attributes.insert(std::make_pair(alias, Attribute(static_cast<unsigned short>(rs->getUInt(alias)))));
}
break;
case sql::DataType::MEDIUMINT: // 24b
case sql::DataType::INTEGER: // 32b
if(meta->isSigned(i))
{
attributes.insert(std::make_pair(alias, Attribute(static_cast<signed int>(rs->getInt(alias)))));
}
else
{
attributes.insert(std::make_pair(alias, Attribute(static_cast<unsigned int>(rs->getUInt(alias)))));
}
break;
case sql::DataType::BIGINT: // 64b
if(meta->isSigned(i))
{
attributes.insert(std::make_pair(alias, Attribute(static_cast<signed long long>(rs->getInt64(alias)))));
}
else
{
attributes.insert(std::make_pair(alias, Attribute(static_cast<unsigned long long>(rs->getUInt64(alias)))));
}
break;
case sql::DataType::REAL: // 32b
attributes.insert(std::make_pair(alias, Attribute(static_cast<float>(rs->getDouble(alias)))));
break;
case sql::DataType::DOUBLE: // 64b
attributes.insert(std::make_pair(alias, Attribute(static_cast<long double>(rs->getDouble(alias)))));
break;
case sql::DataType::DECIMAL:
case sql::DataType::NUMERIC: // Unspecified
attributes.insert(std::make_pair(alias, Attribute(static_cast<double>(rs->getDouble(alias)))));
break;
case sql::DataType::VARCHAR:
attributes.insert(std::make_pair(alias, Attribute(static_cast<std::string>(rs->getString(alias).asStdString()))));
break;
}
}
}
return attributes;
}
void saveAttributes(unsigned long long user, const std::map<std::string, Attribute>& attributes)
{
sql::SQLString query = "UPDATE `test_table` SET `";
for(std::map<std::string, Attribute>::const_iterator iter = attributes.cbegin(); iter != attributes.cend(); ++iter)
{
const std::type_info& type = iter->second.type();
sql::SQLString value = "";
if(type == typeid(bool))
{
value = boost::lexical_cast<std::string>(iter->second.getData<bool>());
}
else if(type == typeid(unsigned char))
{
value = boost::lexical_cast<std::string>(static_cast<unsigned int>(iter->second.getData<unsigned char>()));
}
else if(type == typeid(signed char))
{
value = boost::lexical_cast<std::string>(static_cast<signed int>(iter->second.getData<signed char>()));
}
else if(type == typeid(unsigned short))
{
value = boost::lexical_cast<std::string>(iter->second.getData<unsigned short>());
}
else if(type == typeid(signed short))
{
value = boost::lexical_cast<std::string>(iter->second.getData<signed short>());
}
else if(type == typeid(unsigned int))
{
value = boost::lexical_cast<std::string>(iter->second.getData<unsigned int>());
}
else if(type == typeid(signed int))
{
value = boost::lexical_cast<std::string>(iter->second.getData<signed int>());
}
else if(type == typeid(unsigned long long))
{
value = boost::lexical_cast<std::string>(iter->second.getData<unsigned long long>());
}
else if(type == typeid(signed long long))
{
value = boost::lexical_cast<std::string>(iter->second.getData<signed long long>());
}
else if(type == typeid(std::string))
{
value = boost::lexical_cast<std::string>(iter->second.getData<std::string>());
}
else
{
throw std::logic_error("What type are you trying to feed me!?");
}
query.append(iter->first).append("` = '").append(value).append("', `");
}
query = query.substr(0, query.length() - 3).append(" WHERE `user` = '").append(boost::lexical_cast<std::string>(user)).append("'");
int rows = this->connector.executeUpdate(query); // <-- Todo: rewrite database wrapper to use exceptions
if(rows == -1)
{
throw std::logic_error("Something bad happened...");
}
}
#endif
|