Introduce SqlFieldType Abstraction and Enhance MySQL Result Handling; - #2589
Conversation
an-tao
left a comment
There was a problem hiding this comment.
Since this PR introduces a new public type abstraction, could we add a metadata test covering the MySQL field types?
At minimum, I think the test should cover:
TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT
FLOAT
DOUBLE
DECIMAL(M,D)
CHAR/VARCHAR
TEXT/TINYTEXT/MEDIUMTEXT/LONGTEXT
BLOB/TINYBLOB/MEDIUMBLOB/LONGBLOB
BINARY/VARBINARY
DATE/TIME/DATETIME
BIT
JSON
YEAR
This would also make it much harder to accidentally regress the mapping when new database metadata support is added.
…, including numeric, text, binary, date/time, JSON, ENUM/SET, BOOLEAN, and GEOMETRY types.
…nversion, including numeric, text, binary, date/time, JSON, ENUM/SET, BOOLEAN, and GEOMETRY types." This reverts commit 948d899.
…, including numeric, text, binary, date/time, JSON, ENUM/SET, BOOLEAN, and GEOMETRY types.
|
Hi @an-tao Comprehensive MySQL type mapping—all the major categories covered including the commonly-skipped ones (ENUM, SET, GEOMETRY). Good foundation for schema tooling. Merge. |
|
@karthikeyan-netizen Thanks for this PR. This is a very useful addition to Drogon ORM, especially the ability to inspect column type information from Result. I think this is a valuable direction. I do have some concerns about the current SqlFieldType / ColumnMeta design, though. I'm not sure it can describe SQL types correctly in all cases, since SQL types are more complicated than a single enum can represent. The main problem is that a database type has several different aspects:
These don't always map 1:1. For example: DECIMAL(10,2)should have: but Similarly: cannot all be cleanly represented by a single logical type without losing some information. I wonder if struct ColumnMeta
{
// Logical type
SqlType type;
// Database-specific type
std::string nativeType;
// Type attributes
std::optional<int64_t> length;
std::optional<int> precision;
std::optional<int> scale;
bool nullable;
bool unsigned_;
};For example: This makes it clear that I think this distinction is important if we want this API to be useful for other databases later. Otherwise, There are also a few concrete issues in the current implementation:
I don't think all of these have to be fixed in this PR, but I would suggest defining the semantics of |
|
Thanks for the valuable feedback. You've identified the core issue perfectly—a single enum can't capture the nuances of SQL type semantics, especially across different databases. Your proposed You're right about the concrete issues too:
These shouldn't all block this PR, but you're right that locking in the API semantics first is critical. Once this is public, changing the contract becomes painful. I'll redesign this properly—separate the concerns, document the mapping rules clearly, and make sure we get the edge cases right. Drogon deserves a solid foundation here. Will follow up with a revised approach soon. Thanks for the thorough review. |
…sion/scale handling, and isNullable() / isUnsigned() accessors.
|
Hi @an-tao, I’ve revised the implementation based on the feedback and clarified the type metadata design. The changes now separate:
For MySQL type handling:
I’ve also documented that the inferred DECIMAL precision comes from result metadata and may not always represent the exact declared schema precision, particularly for expressions or derived columns.
For the complete MySQL type mapping, please check the |
Summary
This pull request introduces a new
SqlFieldTypeenumeration and extends MySQL result handling to provide explicit SQL field type information, safer field access, and improved metadata handling.The goal is to provide a consistent, type-safe representation of MySQL field types while maintaining backward compatibility with the existing result-handling APIs.
Motivation
Drogon currently exposes MySQL field metadata without a dedicated abstraction for representing the underlying SQL field type.
This PR addresses that by introducing:
SqlFieldTypeenumeration representing MySQL field typesSqlFieldTypemapping logicThis provides stronger type awareness in MySQL result handling and establishes a foundation for future database-related enhancements.
Changes Introduced
1.
SqlFieldTypeEnumerationAdded
SqlFieldTypeto represent MySQLFIELD_TYPE_*values.Added mappings from MySQL native field types to
SqlFieldType.Integrated the new type information into:
MysqlResultImplResultImplResultFieldProvides type-safe and explicit field type interpretation across the result-handling layer.
2. MySQL Result Handling Enhancements
Extended
MysqlResultImplto expose and useSqlFieldType.Added helper methods for inspecting field types and metadata.
Added structured
ColumnMetainformation for each result column, including:Preserved native MySQL type information where multiple native types map to the same logical type.
3. Safety and Code Quality Improvements
4. API and Implementation Integration
Updated affected components including:
field.*Result.*Existing PostgreSQL and SQLite implementations are unchanged.
Type Mapping
The implementation distinguishes between the logical SQL type and the native MySQL type.
For example:
The native MySQL type is preserved separately so that information such as
ENUMversusSET, orVARCHARversusCHAR, is not lost when using the logical type abstraction.Compatibility
Validation
The changes have been tested in a production-like environment for approximately one month.
Validation included:
No regressions or significant performance issues were observed during testing.
Notes
Feedback is welcome regarding:
SqlFieldTypeColumnMetametadata representation