Details
-
Suggestion
-
Resolution: Unresolved
-
Not Evaluated
-
None
-
None
-
None
Description
Postgres has geometric types https://www.postgresql.org/docs/current/datatype-geometric.html as has QT.
I have a feature-request that the PostgreSQL-driver supports those geometric types.
Currently you need to do this by hand.
for write:
QString formatValue( const QSqlDriver& driver, const QVariant& qVariant ) { switch ( qVariant.metaType().id() ) { case QMetaType::QPointF: { auto point = qVariant.toPointF(); return QString( "point(%1, %2)" ).arg( formatValue( driver, point.x() ), formatValue( driver, point.y() ) ); } case QMetaType::QRectF: { auto rect = qVariant.toRectF(); return QString( "box(%1, %2)" ) .arg( formatValue( driver, rect.bottomRight() ), formatValue( driver, rect.topLeft() ) ); } default: QSqlField field( QString(), qVariant.metaType() ); field.setValue( qVariant ); return driver.formatValue( field ); } }
and for read:
QStringList commaSplitWithoutBrackets( const QSqlQuery& query, int fieldNumber ) { QString fromSql = query.value( fieldNumber ).toString(); ASSERT( fromSql.front() == '(' || fromSql.back() == ')' ); fromSql.replace( '(', "" ); fromSql.replace( ')', "" ); return fromSql.split( ',' ); } QPointF readQPointF( const QSqlQuery& query, int fieldNumber ) { auto split = commaSplitWithoutBrackets( query, fieldNumber ); if ( split.size() != 2 ) { ASSERT_FALSE; return {}; } return QPointF( split.at( 0 ).toDouble(), split.at( 1 ).toDouble() ); } QRectF readQRectF( const QSqlQuery& query, int fieldNumber ) { auto split = commaSplitWithoutBrackets( query, fieldNumber ); if ( split.size() != 4 ) { ASSERT_FALSE; return {}; } return QRectF( QPointF( split.at( 2 ).toDouble(), split.at( 3 ).toDouble() ), QPointF( split.at( 0 ).toDouble(), split.at( 1 ).toDouble() ) ); }
Expected:
write works with just:
QString formatValue( const QSqlDriver& driver, const QVariant& qVariant ) { QSqlField field( QString(), qVariant.metaType() ); field.setValue( qVariant ); return driver.formatValue( field ); }
read works like this:
QPointF readQPointF( const QSqlQuery& query, int fieldNumber ) { return query.value(fieldNumber).toPointF(); } QRectF readQRectF( const QSqlQuery& query, int fieldNumber ) { return query.value(fieldNumber).toRectF(); }