In the current database, I select all from a pet table, there is exactly 1 record and it has two DATE field, one for the birth date and one for the death date.
My record has a value for the birth date and a NULL for the death date.
Header files
#include <iostream>
#include <mysqlx/xdevapi.h>
using directives
using ::std::cout;
using ::std::endl;
using namespace ::mysqlx;
First I connect the session
Session sess("<session uri>");
Then I output some information about the table
std::string schema_name = sess.getDefaultSchemaName();
cout << "schema_name : " << schema_name << endl;
Schema db_schema = sess.getSchema( schema_name, true );
std::string table_name { "pet" };
Table pet_table = db_schema.getTable( table_name, true );
cout << "table " << table_name << " has " << pet_table.count() << " row(s). is_view=" << std::boolalpha << pet_table.isView() << std::endl;
Finally I prepare my query
std::stringstream query_writer;
query_writer << "SELECT * FROM " << table_name;
std::string query_str = query_writer.str();
SqlStatement query = sess.sql(query_str);
Then get my results:
SqlResult res = query.execute();
Row pet;
while( (pet = res.fetchOne()) ) {
int count = res.getColumnCount();
for( int column = 0; column < count; column++ ) {
const Column& current_column = res.getColumn( column );
const Value& field_value = pet[column];
const std::string& field_name = current_column.getColumnName();
Type field_type= current_column.getType();
// HOW to Interpret this?
const bytes& raw_bytes = field_value.getRawBytes();
}
}
It was pointed out to me that DATE fields encoding are described in this link:
https://dev.mysql.com/doc/internals/en/date-and-time-data-type-representation.html
but non-null DATE field raw_bytes is 4 bytes long, not 3 bytes long, so where is the correct documentation?
Originally, the table had a DATE field, now it is changed for a DATETIME field:
mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | datetime | YES | | NULL | |
| death | datetime | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
Here are the records in the tabe:
mysql> select * from pet;
+----------+-------+---------+------+---------------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+---------------------+-------+
| Puffball | Diane | hamster | f | 1999-03-30 00:00:00 | NULL |
+----------+-------+---------+------+---------------------+-------+
1 row in set (0.00 sec)