Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support for MySQL timestamp in the POCO C++ libraries

The POCO libraries support MySQL DATE, TIME and DATETIME columns, but not TIMESTAMP. Selecting values from a TIMESTAMP column raises an "unknown field type" exception, since MYSQL_TYPE_TIMESTAMP is not supported in "Poco/Data/MySQL/ResultMetadata.cpp".

In my project I had to change several columns to DATETIME to make it work. This was not a big problem, still I wonder what the reason for this limitation is. If I had to work with an existing database schema that I couldn't alter, I'd be in serious trouble.

Timestamp columns are widely used, hence I don't believe they were simply omitted. Is there an implementation problem as to Timestamp columns? Is there a workaround I could use? Is it planned to add MySQL timestamp support to POCO in the future?

like image 333
Friedrich Wilckens Avatar asked Oct 30 '22 02:10

Friedrich Wilckens


1 Answers

Download the source of POCO libraries then modify the file Data/MySQL/src/ResultMetadata.cpp

std::size_t fieldSize(const MYSQL_FIELD& field)
    /// Convert field MySQL-type and field MySQL-length to actual field length
{
    ...

    case MYSQL_TYPE_DATE:
    case MYSQL_TYPE_TIME:
    case MYSQL_TYPE_DATETIME:
    case MYSQL_TYPE_TIMESTAMP: // <-- add this line
        return sizeof(MYSQL_TIME);

    ...

}   


Poco::Data::MetaColumn::ColumnDataType fieldType(const MYSQL_FIELD& field)
    /// Convert field MySQL-type to Poco-type   
{
    ...

    case MYSQL_TYPE_DATETIME:
    case MYSQL_TYPE_TIMESTAMP: // <-- add this line
        return Poco::Data::MetaColumn::FDT_TIMESTAMP;

    ...

}   

Compile the library and you will have support for TIMESTAMP fields.

like image 195
Patricklaf Avatar answered Nov 15 '22 06:11

Patricklaf