summaryrefslogtreecommitdiff
path: root/include
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 /include
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 'include')
-rw-r--r--include/extensible.h5
-rw-r--r--include/modules/os_oper.h4
-rw-r--r--include/modules/os_session.h12
-rw-r--r--include/modules/sql.h12
-rw-r--r--include/serialize.h38
5 files changed, 46 insertions, 25 deletions
diff --git a/include/extensible.h b/include/extensible.h
index 7ff518516..f248e6db8 100644
--- a/include/extensible.h
+++ b/include/extensible.h
@@ -173,7 +173,7 @@ public:
void ExtensibleSerialize(const Extensible *e, const Serializable *s, Serialize::Data &data) const override
{
T *t = this->Get(e);
- data[this->name] << *t;
+ data.Store(this->name, *t);
}
void ExtensibleUnserialize(Extensible *e, Serializable *s, Serialize::Data &data) override
@@ -194,8 +194,7 @@ public:
void ExtensibleSerialize(const Extensible *e, const Serializable *s, Serialize::Data &data) const override
{
- data.SetType(this->name, Serialize::Data::DT_INT);
- data[this->name] << true;
+ data.Store(this->name, true);
}
void ExtensibleUnserialize(Extensible *e, Serializable *s, Serialize::Data &data) override
diff --git a/include/modules/os_oper.h b/include/modules/os_oper.h
index 8a92efe45..034c2b2fd 100644
--- a/include/modules/os_oper.h
+++ b/include/modules/os_oper.h
@@ -16,8 +16,8 @@ struct MyOper final
void Serialize(Serialize::Data &data) const override
{
- data["name"] << this->name;
- data["type"] << this->ot->GetName();
+ data.Store("name", this->name);
+ data.Store("type", this->ot->GetName());
}
static Serializable *Unserialize(Serializable *obj, Serialize::Data &data)
diff --git a/include/modules/os_session.h b/include/modules/os_session.h
index 4281ed086..0c1da71e6 100644
--- a/include/modules/os_session.h
+++ b/include/modules/os_session.h
@@ -62,12 +62,12 @@ static ServiceReference<SessionService> session_service("SessionService", "sessi
void Exception::Serialize(Serialize::Data &data) const
{
- data["mask"] << this->mask;
- data["limit"] << this->limit;
- data["who"] << this->who;
- data["reason"] << this->reason;
- data["time"] << this->time;
- data["expires"] << this->expires;
+ data.Store("mask", this->mask);
+ data.Store("limit", this->limit);
+ data.Store("who", this->who);
+ data.Store("reason", this->reason);
+ data.Store("time", this->time);
+ data.Store("expires", this->expires);
}
Serializable *Exception::Unserialize(Serializable *obj, Serialize::Data &data)
diff --git a/include/modules/sql.h b/include/modules/sql.h
index 8818a5130..8ff5efc7c 100644
--- a/include/modules/sql.h
+++ b/include/modules/sql.h
@@ -19,7 +19,7 @@ namespace SQL
public:
typedef std::map<Anope::string, std::stringstream *> Map;
Map data;
- std::map<Anope::string, Type> types;
+ std::map<Anope::string, Serialize::DataType> types;
~Data()
{
@@ -60,17 +60,17 @@ namespace SQL
this->data.clear();
}
- void SetType(const Anope::string &key, Type t) override
+ void SetType(const Anope::string &key, Serialize::DataType dt) override
{
- this->types[key] = t;
+ this->types[key] = dt;
}
- Type GetType(const Anope::string &key) const override
+ Serialize::DataType GetType(const Anope::string &key) const override
{
- std::map<Anope::string, Type>::const_iterator it = this->types.find(key);
+ auto it = this->types.find(key);
if (it != this->types.end())
return it->second;
- return DT_TEXT;
+ return Serialize::DataType::TEXT;
}
};
diff --git a/include/serialize.h b/include/serialize.h
index e53295dc0..ff8ed30d3 100644
--- a/include/serialize.h
+++ b/include/serialize.h
@@ -18,22 +18,44 @@
namespace Serialize
{
+ enum class DataType
+ : uint8_t
+ {
+ BOOL,
+ FLOAT,
+ INT,
+ TEXT,
+ UINT,
+ };
+
class Data
{
public:
- enum Type
- {
- DT_TEXT,
- DT_INT
- };
-
virtual ~Data() = default;
virtual std::iostream &operator[](const Anope::string &key) = 0;
+
+ template <typename T>
+ void Store(const Anope::string &key, const T &value)
+ {
+ using Type = std::remove_cv_t<std::remove_reference_t<T>>;
+
+ if constexpr (std::is_same_v<Type, bool>)
+ SetType(key, DataType::BOOL);
+ else if constexpr (std::is_floating_point_v<Type>)
+ SetType(key, DataType::FLOAT);
+ else if constexpr (std::is_integral_v<Type> && std::is_signed_v<Type>)
+ SetType(key, DataType::INT);
+ else if constexpr (std::is_integral_v<Type> && std::is_unsigned_v<Type>)
+ SetType(key, DataType::UINT);
+
+ this->operator[](key) << value;
+ }
+
virtual size_t Hash() const { throw CoreException("Not supported"); }
- virtual void SetType(const Anope::string &key, Type t) { }
- virtual Type GetType(const Anope::string &key) const { return DT_TEXT; }
+ virtual void SetType(const Anope::string &key, DataType dt) { }
+ virtual DataType GetType(const Anope::string &key) const { return DataType::TEXT; }
};
extern void RegisterTypes();