23 #include <QDataStream>
26 #include <QJsonDocument>
37 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
48 const QString &
typeName,
int len,
int prec,
const QString &comment,
49 QVariant::Type subType )
76 return *( other.d ) == *d;
81 return !( *
this == other );
91 if ( !d->alias.isEmpty() )
99 if (
alias().isEmpty() )
103 return QStringLiteral(
"%1 (%2)" ).arg(
name() ).arg(
alias() );
111 typeStr += QStringLiteral(
"(%1, %2)" ).arg(
length() ).arg(
precision() );
113 typeStr += QStringLiteral(
"(%1)" ).arg(
length() );
115 if ( showConstraints )
118 ? QStringLiteral(
" NOT NULL" )
119 : QStringLiteral(
" NULL" );
122 ? QStringLiteral(
" UNIQUE" )
161 return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
166 return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
211 return d->defaultValueDefinition;
226 return d->constraints;
253 if ( d->type == QVariant::Double )
256 if ( QLocale().decimalPoint() !=
'.' ||
257 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
259 if ( d->precision > 0 )
261 if ( -1 < v.toDouble() && v.toDouble() < 1 )
263 return QLocale().toString( v.toDouble(),
'g', d->precision );
267 return QLocale().toString( v.toDouble(),
'f', d->precision );
274 QString s( v.toString() );
275 int dotPosition( s.indexOf(
'.' ) );
277 if ( dotPosition < 0 && s.indexOf(
'e' ) < 0 )
280 return QLocale().toString( v.toDouble(),
'f',
precision );
285 else precision = s.length() - dotPosition - 1;
287 if ( -1 < v.toDouble() && v.toDouble() < 1 )
289 return QLocale().toString( v.toDouble(),
'g',
precision );
293 return QLocale().toString( v.toDouble(),
'f',
precision );
299 else if ( d->type == QVariant::Double && d->precision > 0 )
301 if ( -1 < v.toDouble() && v.toDouble() < 1 )
303 return QString::number( v.toDouble(),
'g', d->precision );
307 return QString::number( v.toDouble(),
'f', d->precision );
313 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
316 qlonglong converted( v.toLongLong( &ok ) );
318 return QLocale().toString( converted );
320 else if ( d->typeName.compare( QLatin1String(
"json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String(
"jsonb" ) )
322 QJsonDocument doc = QJsonDocument::fromVariant( v );
323 return QString::fromUtf8( doc.toJson().data() );
325 else if ( d->type == QVariant::ByteArray )
327 return QObject::tr(
"BLOB" );
343 v.convert( d->type );
347 if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
349 v = QVariant( d->type );
355 if ( d->type == QVariant::Double && v.type() == QVariant::String )
358 if ( !tmp.convert( d->type ) )
369 if ( QLocale().decimalPoint() !=
'.' )
371 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
382 if ( d->type == QVariant::Int && v.type() == QVariant::String )
385 if ( !tmp.convert( d->type ) )
399 if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
402 if ( !tmp.convert( d->type ) )
417 if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
420 double dbl = v.toDouble( &ok );
424 v = QVariant( d->type );
428 double round = std::round( dbl );
429 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
432 v = QVariant( d->type );
435 v = QVariant(
static_cast< int >( std::round( dbl ) ) );
441 if ( d->type == QVariant::LongLong && v.canConvert( QVariant::Double ) )
445 if ( !tmp.convert( d->type ) )
448 double dbl = v.toDouble( &ok );
452 v = QVariant( d->type );
456 double round = std::round( dbl );
457 if ( round > std::numeric_limits<long long>::max() || round < -std::numeric_limits<long long>::max() )
460 v = QVariant( d->type );
463 v = QVariant(
static_cast< long long >( std::round( dbl ) ) );
468 if ( !v.convert( d->type ) )
470 v = QVariant( d->type );
474 if ( d->type == QVariant::Double && d->precision > 0 )
476 double s = std::pow( 10, d->precision );
477 double d = v.toDouble() * s;
478 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
482 if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
484 v = v.toString().left( d->length );
493 d->editorWidgetSetup = v;
498 return d->editorWidgetSetup;
510 out << static_cast< quint32 >( field.
type() );
515 out << field.
alias();
527 out << static_cast< quint32 >( field.
subType() );
538 quint32 originNotNull;
539 quint32 originUnique;
540 quint32 originExpression;
541 quint32 strengthNotNull;
542 quint32 strengthUnique;
543 quint32 strengthExpression;
551 QString defaultValueExpression;
552 QString constraintExpression;
553 QString constraintDescription;
556 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
557 constraintExpression >> constraintDescription >> subType;
559 field.
setType(
static_cast< QVariant::Type
>( type ) );
561 field.
setLength(
static_cast< int >( length ) );
590 field.
setSubType(
static_cast< QVariant::Type
>( subType ) );