summaryrefslogtreecommitdiff
path: root/modules/extra/mysql.cpp
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2024-08-14 02:40:48 +0100
committerSadie Powell <sadie@witchery.services>2024-08-14 05:47:55 +0100
commit528b5938ec71abed396248cf5e00f346e685aaea (patch)
treee923fed32be3fb62f946d44c633cd85ce855550b /modules/extra/mysql.cpp
parent03bee1706383d2766923e5d8edbb90d7ad1948b6 (diff)
Automatically determine SQL column type from the field.
Also add more column types to ensure we are storing data in the best format in the database.
Diffstat (limited to 'modules/extra/mysql.cpp')
-rw-r--r--modules/extra/mysql.cpp63
1 files changed, 48 insertions, 15 deletions
diff --git a/modules/extra/mysql.cpp b/modules/extra/mysql.cpp
index 193427fdb..caa02d8f0 100644
--- a/modules/extra/mysql.cpp
+++ b/modules/extra/mysql.cpp
@@ -158,6 +158,29 @@ public:
Anope::string BuildQuery(const Query &q);
Anope::string FromUnixtime(time_t) override;
+
+ Anope::string GetColumnType(Serialize::DataType dt)
+ {
+ switch (dt)
+ {
+ case Serialize::DataType::BOOL:
+ return "TINYINT NOT NULL";
+
+ case Serialize::DataType::FLOAT:
+ return "DOUBLE PRECISION NOT NULL";
+
+ case Serialize::DataType::INT:
+ return "BIGINT NOT NULL";
+
+ case Serialize::DataType::TEXT:
+ return "TEXT";
+
+ case Serialize::DataType::UINT:
+ return "BIGINT UNSIGNED NOT NULL";
+ }
+
+ return "TEXT"; // Should never be reached
+ }
};
/** The SQL thread used to execute queries
@@ -410,17 +433,13 @@ std::vector<Query> MySQLService::CreateTable(const Anope::string &table, const D
if (known_cols.empty())
{
- Anope::string query_text = "CREATE TABLE `" + table + "` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
+ Anope::string query_text = "CREATE TABLE `" + table + "` (`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,"
" `timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
for (const auto &[column, _] : data.data)
{
known_cols.insert(column);
- query_text += ", `" + column + "` ";
- if (data.GetType(column) == Serialize::Data::DT_INT)
- query_text += "int";
- else
- query_text += "text";
+ query_text += ", `" + column + "` " + GetColumnType(data.GetType(column));
}
query_text += ", PRIMARY KEY (`id`), KEY `timestamp_idx` (`timestamp`)) ROW_FORMAT=DYNAMIC";
queries.push_back(query_text);
@@ -434,11 +453,7 @@ std::vector<Query> MySQLService::CreateTable(const Anope::string &table, const D
known_cols.insert(column);
- Anope::string query_text = "ALTER TABLE `" + table + "` ADD `" + column + "` ";
- if (data.GetType(column) == Serialize::Data::DT_INT)
- query_text += "int";
- else
- query_text += "text";
+ Anope::string query_text = "ALTER TABLE `" + table + "` ADD `" + column + "` " + GetColumnType(data.GetType(column));
queries.push_back(query_text);
}
@@ -474,11 +489,29 @@ Query MySQLService::BuildInsert(const Anope::string &table, unsigned int id, Dat
Anope::string buf;
*value >> buf;
- bool escape = true;
- if (buf.empty())
+ auto escape = true;
+ switch (data.GetType(field))
{
- buf = "NULL";
- escape = false;
+ case Serialize::DataType::BOOL:
+ case Serialize::DataType::FLOAT:
+ case Serialize::DataType::INT:
+ case Serialize::DataType::UINT:
+ {
+ if (buf.empty())
+ buf = "0";
+ escape = false;
+ break;
+ }
+
+ case Serialize::DataType::TEXT:
+ {
+ if (buf.empty())
+ {
+ buf = "NULL";
+ escape = false;
+ }
+ break;
+ }
}
query.SetValue(field, buf, escape);