From kde-commits Tue Jun 30 22:09:07 2015 From: Jaroslaw Staniek Date: Tue, 30 Jun 2015 22:09:07 +0000 To: kde-commits Subject: [kdb] /: Update API for types: add 'field' to the name to indicate they are about field types Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=143570216011542 Git commit bb63dac5c767e6665a79ae42bd7f0de102faf67b by Jaroslaw Staniek. Committed on 30/06/2015 at 16:51. Pushed by staniek into branch 'master'. Update API for types: add 'field' to the name to indicate they are about fi= eld types + replace KDb::TypeGroupList with QList + return a list with invalid type for fieldTypeNamesForGroup(KDbField::Inva= lidGroup) and fieldTypeStringsForGroup(KDbField::InvalidGroup) calls + getLimitsForFieldType(): support signed types M +1 -1 autotests/ExpressionsTest.cpp M +39 -38 src/KDb.cpp M +42 -40 src/KDb.h M +2 -2 src/KDbAlter.cpp M +3 -3 src/expression/KDbBinaryExpression.cpp M +1 -1 src/views/KDbTableViewData.cpp http://commits.kde.org/kdb/bb63dac5c767e6665a79ae42bd7f0de102faf67b diff --git a/autotests/ExpressionsTest.cpp b/autotests/ExpressionsTest.cpp index 1060974..df36161 100644 --- a/autotests/ExpressionsTest.cpp +++ b/autotests/ExpressionsTest.cpp @@ -1237,7 +1237,7 @@ void ExpressionsTest::testBinaryExpressionValidate_da= ta() T(KDbToken::SQL_NULL, QVariant(), KDbToken::OR, KDbToken::CHARACTER_ST= RING_LITERAL, "xyz", KDbField::InvalidType); // integer // -- KDb::ArithmeticExpression only: resulting type is Integer or more - // see explanation for KDb::maximumForIntegerTypes() + // see explanation for KDb::maximumForIntegerFieldTypes() T(KDbToken::INTEGER_CONST, 50, '+', KDbToken::INTEGER_CONST, 20, KDbFi= eld::Integer); T(KDbToken::INTEGER_CONST, 50, '-', KDbToken::INTEGER_CONST, 20, KDbFi= eld::Integer); T(KDbToken::INTEGER_CONST, 50, '*', KDbToken::INTEGER_CONST, 20, KDbFi= eld::Integer); diff --git a/src/KDb.cpp b/src/KDb.cpp index 5d7c90a..cb716cb 100644 --- a/src/KDb.cpp +++ b/src/KDb.cpp @@ -303,9 +303,9 @@ bool KDb::deleteRecord(KDbConnection* conn, const QStri= ng &tableName, + " AND " + keyname3 + "=3D" + conn->driver()->= valueToSQL(keytype3, keyval3)); } = -bool KDb::isEmptyValue(KDbField *f, const QVariant &v) +bool KDb::isEmptyValue(KDbField::Type type, const QVariant &v) { - if (f->hasEmptyProperty() && v.toString().isEmpty() && !v.toString().i= sNull()) + if (KDbField::hasEmptyProperty(type) && v.toString().isEmpty() && !v.t= oString().isNull()) return true; return v.isNull(); } @@ -321,9 +321,9 @@ KDbEscapedString KDb::sqlWhere(KDbDriver *drv, KDbField= ::Type t, //! Cache struct TypeCache { TypeCache() { - for (KDbField::Type t =3D KDbField::FirstType; t <=3D KDbField::La= stType; t =3D KDbField::Type(int(t) + 1)) { + for (KDbField::Type t =3D KDbField::InvalidType; t <=3D KDbField::= LastType; t =3D KDbField::Type(int(t) + 1)) { const KDbField::TypeGroup tg =3D KDbField::typeGroup(t); - KDb::TypeGroupList list; + QList list; QStringList name_list, str_list; if (tlist.contains(tg)) { list =3D tlist.value(tg); @@ -347,7 +347,7 @@ struct TypeCache { def_tlist[ KDbField::BLOBGroup ] =3D KDbField::BLOB; } = - QHash< KDbField::TypeGroup, KDb::TypeGroupList > tlist; + QHash< KDbField::TypeGroup, QList > tlist; QHash< KDbField::TypeGroup, QStringList > nlist; QHash< KDbField::TypeGroup, QStringList > slist; QHash< KDbField::TypeGroup, KDbField::Type > def_tlist; @@ -355,22 +355,22 @@ struct TypeCache { = Q_GLOBAL_STATIC(TypeCache, KDb_typeCache) = -const KDb::TypeGroupList KDb::typesForGroup(KDbField::TypeGroup typeGroup) +const QList KDb::fieldTypesForGroup(KDbField::TypeGroup ty= peGroup) { return KDb_typeCache->tlist.value(typeGroup); } = -QStringList KDb::typeNamesForGroup(KDbField::TypeGroup typeGroup) +QStringList KDb::fieldTypeNamesForGroup(KDbField::TypeGroup typeGroup) { return KDb_typeCache->nlist.value(typeGroup); } = -QStringList KDb::typeStringsForGroup(KDbField::TypeGroup typeGroup) +QStringList KDb::fieldTypeStringsForGroup(KDbField::TypeGroup typeGroup) { return KDb_typeCache->slist.value(typeGroup); } = -KDbField::Type KDb::defaultTypeForGroup(KDbField::TypeGroup typeGroup) +KDbField::Type KDb::defaultFieldTypeForGroup(KDbField::TypeGroup typeGroup) { return (typeGroup <=3D KDbField::LastTypeGroup) ? KDb_typeCache->def_t= list.value(typeGroup) : KDbField::InvalidType; } @@ -1071,9 +1071,9 @@ QDomElement KDb::saveBooleanElementToDom(QDomDocument= *doc, QDomElement *parentE return el; } = -//! @internal Used in KDb::emptyValueForType() -struct KDb_EmptyValueForTypeCache { - KDb_EmptyValueForTypeCache() +//! @internal Used in KDb::emptyValueForFieldType() +struct KDb_EmptyValueForFieldTypeCache { + KDb_EmptyValueForFieldTypeCache() : values(int(KDbField::LastType) + 1) { #define ADD(t, value) values.insert(t, value); ADD(KDbField::Byte, 0); @@ -1092,12 +1092,12 @@ struct KDb_EmptyValueForTypeCache { QVector values; }; = -//! Used in KDb::emptyValueForType() -Q_GLOBAL_STATIC(KDb_EmptyValueForTypeCache, KDb_emptyValueForTypeCache) +//! Used in KDb::emptyValueForFieldType() +Q_GLOBAL_STATIC(KDb_EmptyValueForFieldTypeCache, KDb_emptyValueForFieldTyp= eCache) = -QVariant KDb::emptyValueForType(KDbField::Type type) +QVariant KDb::emptyValueForFieldType(KDbField::Type type) { - const QVariant val(KDb_emptyValueForTypeCache->values.at( + const QVariant val(KDb_emptyValueForFieldTypeCache->values.at( (type <=3D KDbField::LastType) ? type : KDbFiel= d::InvalidType)); if (!val.isNull()) return val; @@ -1113,9 +1113,9 @@ QVariant KDb::emptyValueForType(KDbField::Type type) return QVariant(); } = -//! @internal Used in KDb::notEmptyValueForType() -struct KDb_NotEmptyValueForTypeCache { - KDb_NotEmptyValueForTypeCache() +//! @internal Used in KDb::notEmptyValueForFieldType() +struct KDb_NotEmptyValueForFieldTypeCache { + KDb_NotEmptyValueForFieldTypeCache() : values(int(KDbField::LastType) + 1) { #define ADD(t, value) values.insert(t, value); // copy most of the values @@ -1139,18 +1139,18 @@ struct KDb_NotEmptyValueForTypeCache { ADD(i, ba); continue; } - ADD(i, KDb::emptyValueForType((KDbField::Type)i)); + ADD(i, KDb::emptyValueForFieldType((KDbField::Type)i)); } #undef ADD } QVector values; }; -//! Used in KDb::notEmptyValueForType() -Q_GLOBAL_STATIC(KDb_NotEmptyValueForTypeCache, KDb_notEmptyValueForTypeCac= he) +//! Used in KDb::notEmptyValueForFieldType() +Q_GLOBAL_STATIC(KDb_NotEmptyValueForFieldTypeCache, KDb_notEmptyValueForFi= eldTypeCache) = -QVariant KDb::notEmptyValueForType(KDbField::Type type) +QVariant KDb::notEmptyValueForFieldType(KDbField::Type type) { - const QVariant val(KDb_notEmptyValueForTypeCache->values.at( + const QVariant val(KDb_notEmptyValueForFieldTypeCache->values.at( (type <=3D KDbField::LastType) ? type : KDbFiel= d::InvalidType)); if (!val.isNull()) return val; @@ -1493,29 +1493,30 @@ bool KDb::isDefaultValueAllowed(KDbField* field) return field && !field->isUniqueKey(); } = -void KDb::getLimitsForType(KDbField::Type type, int *minValue, int *maxVal= ue) +void KDb::getLimitsForFieldType(KDbField::Type type, qlonglong *minValue, = qlonglong *maxValue, + Signedness signedness) { Q_ASSERT(minValue); Q_ASSERT(maxValue); switch (type) { case KDbField::Byte: //! @todo always ok? - *minValue =3D 0; - *maxValue =3D 255; + *minValue =3D signedness =3D=3D KDb::Signed ? -0x80 : 0; + *maxValue =3D signedness =3D=3D KDb::Signed ? 0x7F : 0xFF; break; case KDbField::ShortInteger: - *minValue =3D -32768; - *maxValue =3D 32767; + *minValue =3D signedness =3D=3D KDb::Signed ? -0x8000 : 0; + *maxValue =3D signedness =3D=3D KDb::Signed ? 0x7FFF : 0xFFFF; break; case KDbField::Integer: - case KDbField::BigInteger: //cannot return anything larger + case KDbField::BigInteger: //!< @todo cannot return anything larger? default: - *minValue =3D (int) - 0x07FFFFFFF; - *maxValue =3D (int)(0x080000000 - 1); + *minValue =3D signedness =3D=3D KDb::Signed ? qlonglong(-0x07FFFFF= FF) : qlonglong(0); + *maxValue =3D signedness =3D=3D KDb::Signed ? qlonglong(0x07FFFFFF= F) : qlonglong(0x0FFFFFFFF); } } = -KDbField::Type KDb::maximumForIntegerTypes(KDbField::Type t1, KDbField::Ty= pe t2) +KDbField::Type KDb::maximumForIntegerFieldTypes(KDbField::Type t1, KDbFiel= d::Type t2) { if (!KDbField::isIntegerType(t1) || !KDbField::isIntegerType(t2)) return KDbField::InvalidType; @@ -1527,18 +1528,18 @@ KDbField::Type KDb::maximumForIntegerTypes(KDbField= ::Type t1, KDbField::Type t2) return t1; if (t1 =3D=3D KDbField::BigInteger) return t1; - return KDb::maximumForIntegerTypes(t2, t1); //swap + return KDb::maximumForIntegerFieldTypes(t2, t1); //swap } = -QString KDb::simplifiedTypeName(const KDbField& field) +QString KDb::simplifiedFieldTypeName(KDbField::Type type) { - if (field.isNumericType()) + if (KDbField::isNumericType(type)) return QObject::tr("Number"); //simplify - else if (field.type() =3D=3D KDbField::BLOB) + else if (type =3D=3D KDbField::BLOB) //! @todo support names of other BLOB subtypes return QObject::tr("Image"); //simplify = - return field.typeGroupName(); + return KDbField::typeGroupName(KDbField::typeGroup(type)); } = QString KDb::defaultFileBasedDriverMimeType() diff --git a/src/KDb.h b/src/KDb.h index 275e461..7835464 100644 --- a/src/KDb.h +++ b/src/KDb.h @@ -75,33 +75,32 @@ KDB_EXPORT bool deleteRecord(KDbConnection* conn, const= QString &tableName, const QString &keyname2, KDbField::Typ= e keytype2, const QVariant& keyval2, const QString &keyname3, KDbField::Typ= e keytype3, const QVariant& keyval3); = -typedef QList TypeGroupList; +/*! @return list of field types for field type group @a typeGroup. */ +KDB_EXPORT const QList fieldTypesForGroup(KDbField::TypeGr= oup typeGroup); = -/*! @return list of types for type group @a typeGroup. */ -KDB_EXPORT const KDb::TypeGroupList typesForGroup(KDbField::TypeGroup type= Group); +/*! @return list of translated field type names for field type group @a ty= peGroup. */ +KDB_EXPORT QStringList fieldTypeNamesForGroup(KDbField::TypeGroup typeGrou= p); = -/*! @return list of i18n-ed type names for type group @a typeGroup. */ -KDB_EXPORT QStringList typeNamesForGroup(KDbField::TypeGroup typeGroup); +/*! @return list of (nontranslated) field type names for field type group = @a typeGroup. */ +KDB_EXPORT QStringList fieldTypeStringsForGroup(KDbField::TypeGroup typeGr= oup); = -/*! @return list of (not-i18n-ed) type names for type group @a typeGroup. = */ -KDB_EXPORT QStringList typeStringsForGroup(KDbField::TypeGroup typeGroup); - -/*! @return default field type for type group @a typeGroup, +/*! @return default field type for field type group @a typeGroup, for example, KDbField::Integer for KDbField::IntegerGroup. It is used e.g. in KexiAlterTableDialog, to properly fill 'type' property when user selects type group for a field. */ -KDB_EXPORT KDbField::Type defaultTypeForGroup(KDbField::TypeGroup typeGrou= p); +KDB_EXPORT KDbField::Type defaultFieldTypeForGroup(KDbField::TypeGroup typ= eGroup); = -/*! @return a slightly simplified type name for @a field. - For BLOB type it returns i18n-ed "Image" string or other, depending on th= e mime type. - For numbers (either floating-point or integer) it returns i18n-ed "Number= : string. - For other types it the same string as KDbField::typeGroupName() is return= ed. */ +/*! @return a slightly simplified field type name type @a type. + For KDbField::BLOB type it returns a translated "Image" string or other, = depending on the mime type. + For numbers (either floating-point or integer) it returns a translated "N= umber" string. + For other types KDbField::typeGroupName() is returned. */ //! @todo support names of other BLOB subtypes -KDB_EXPORT QString simplifiedTypeName(const KDbField& field); +KDB_EXPORT QString simplifiedFieldTypeName(KDbField::Type type); = -/*! @return true if @a v represents an empty (but not null) value. - Values of some types (as for strings) can be both empty and not null. */ -KDB_EXPORT bool isEmptyValue(KDbField *f, const QVariant &v); +/*! @return true if value @a v represents an empty (but not null) value. + This depends on field type @a type. Values of some types (such as strings) + can be both empty and not null. */ +KDB_EXPORT bool isEmptyValue(KDbField::Type type, const QVariant &v); = /*! Sets string pointed by @a msg to an error message retrieved from @a re= sultable, and string pointed by @a details to details of this error (server message= and result number). @@ -289,24 +288,24 @@ KDB_EXPORT QDomElement saveNumberElementToDom(QDomDoc= ument *doc, QDomElement *pa KDB_EXPORT QDomElement saveBooleanElementToDom(QDomDocument *doc, QDomElem= ent *parentEl, const QString& elementName, bool value); = -/*! @return an empty value that can be set for a database field of type @a= type having - "null" property set. Empty string is returned for text type, 0 for integer - or floating-point types, false for boolean type, empty null byte array fo= r BLOB type. - For date, time and date/time types current date, time, date+time is retur= ned, respectively. - Returns null QVariant for unsupported values like KDbField::InvalidType. - This function is efficient (uses a cache) and is heavily used by the KDbA= lterTableHandler - for filling new columns. */ -KDB_EXPORT QVariant emptyValueForType(KDbField::Type type); - -/*! @return a value that can be set for a database field of type @a type h= aving - "notEmpty" property set. It works in a similar way as - @ref QVariant emptyValueForType( KDbField::Type type ) with the following= differences: +//! @return equivalent of empty (default) value that can be set for a data= base field of type @a type +/*! In particular returns: + - empty string for text types, + - 0 for integer and floating-point types, + - false for boolean types, + - a null byte array for BLOB type, + - current date, time, date+time is returned (measured at client side) for= date, time and + date/time types respectively, + - a null QVariant for unsupported values such as KDbField::InvalidType. */ +KDB_EXPORT QVariant emptyValueForFieldType(KDbField::Type type); + +//! @return a value that can be set for a database field of type @a type h= aving "notEmpty" property set. +/*! It works in a similar way as @ref QVariant KDb::emptyValueForFieldType= (KDbField::Type type) + with the following differences: - " " string (a single space) is returned for Text and LongText types - a byte array with saved "filenew" PNG image (icon) for BLOB type - Returns null QVariant for unsupported values like KDbField::InvalidType. - This function is efficient (uses a cache) and is heavily used by the KDbA= lterTableHandler - for filling new columns. */ -KDB_EXPORT QVariant notEmptyValueForType(KDbField::Type type); + Returns null QVariant for unsupported values like KDbField::InvalidType. = */ +KDB_EXPORT QVariant notEmptyValueForFieldType(KDbField::Type type); = /*! @return true if the @a word is an reserved KDbSQL keyword See generated/sqlkeywords.cpp. @@ -402,19 +401,22 @@ KDB_EXPORT QVariant stringToVariant(const QString& s,= QVariant::Type type, bool* False is returned also if @a field is 0. */ KDB_EXPORT bool isDefaultValueAllowed(KDbField* field); = -/*! Gets limits for values of type @a type. The result is put into integer= s pointed - by @a minValue and @a maxValue. Supported types are Byte, ShortInteger, I= nteger and BigInteger. +//! Provides limits for values of type @a type +/*! The result is put into integers pointed by @a minValue and @a maxValue. + The limits are machine-independent,. what is useful for format and protoc= ol compatibility. + Supported types are Byte, ShortInteger, Integer and BigInteger. + The value of @a signedness controls the values; they can be limited to un= signed or not. Results for BigInteger or non-integer types are the same as for Integer d= ue to limitation of int type. Signed integers are assumed. @a minValue and @a maxValue mus= t not be 0. */ -//! @todo add support for unsigned flag -KDB_EXPORT void getLimitsForType(KDbField::Type type, int *minValue, int *= maxValue); +KDB_EXPORT void getLimitsForFieldType(KDbField::Type type, qlonglong *minV= alue, qlonglong *maxValue, + KDb::Signedness signedness =3D KDb::= Signed); = /*! @return type that's maximum of two integer types @a t1 and @a t2, e.g.= Integer for (Byte, Integer). If one of the types is not of the integer group, KDbField::InvalidType is= returned. Returned type may not fit to the result of evaluated expression that invo= lves the arguments. - For example, 100 is within Byte type, maximumForIntegerTypes(Byte, Byte) = is Byte but result + For example, 100 is within Byte type, maximumForIntegerFieldTypes(Byte, B= yte) is Byte but result of 100 * 100 exceeds the range of Byte. */ -KDB_EXPORT KDbField::Type maximumForIntegerTypes(KDbField::Type t1, KDbFie= ld::Type t2); +KDB_EXPORT KDbField::Type maximumForIntegerFieldTypes(KDbField::Type t1, K= DbField::Type t2); = /*! @return QVariant value converted from null-terminated @a data string. In case of BLOB type, @a data is not null terminated, so passing length i= s needed. */ diff --git a/src/KDbAlter.cpp b/src/KDbAlter.cpp index d85633f..d8b5be1 100644 --- a/src/KDbAlter.cpp +++ b/src/KDbAlter.cpp @@ -1033,11 +1033,11 @@ KDbTableSchema* KDbAlterTableHandler::execute(const= QString& tableName, Executio } else if (f->isNotNull()) { //this field cannot be null sourceSQLString =3D d->conn->driver()->valueToSQL( - f->type(), KDb::emptyValueForType(f-= >type())); + f->type(), KDb::emptyValueForFieldTy= pe(f->type())); } else if (f->isNotEmpty()) { //this field cannot be empty - use any nonempty value..., = e.g. " " for text or 0 for number sourceSQLString =3D d->conn->driver()->valueToSQL( - f->type(), KDb::notEmptyValueForType= (f->type())); + f->type(), KDb::notEmptyValueForFiel= dType(f->type())); } //! @todo support unique, validatationRule, unsigned flags... //! @todo check for foreignKey values... diff --git a/src/expression/KDbBinaryExpression.cpp b/src/expression/KDbBin= aryExpression.cpp index 3201bf9..c104299 100644 --- a/src/expression/KDbBinaryExpression.cpp +++ b/src/expression/KDbBinaryExpression.cpp @@ -170,16 +170,16 @@ KDbField::Type KDbBinaryExpressionData::typeInternal(= KDb::ExpressionCallStack* c = if (expressionClass =3D=3D KDb::ArithmeticExpression) { if (ltInt && rtInt) { - /* From documentation of KDb::maximumForIntegerTypes(): + /* From documentation of KDb::maximumForIntegerFieldTypes(): In case of KDb::ArithmeticExpression: returned type may not fit to the result of evaluated expressi= on that involves the arguments. - For example, 100 is within Byte type, maximumForIntegerTypes(= Byte, Byte) is Byte but result + For example, 100 is within Byte type, maximumForIntegerFieldT= ypes(Byte, Byte) is Byte but result of 100 * 100 exceeds the range of Byte. = Solution: for types smaller than Integer (e.g. Byte and Short= Integer) we are returning Integer type. */ - KDbField::Type t =3D KDb::maximumForIntegerTypes(lt, rt); + KDbField::Type t =3D KDb::maximumForIntegerFieldTypes(lt, rt); if (t =3D=3D KDbField::Byte || t =3D=3D KDbField::ShortInteger= ) { return KDbField::Integer; } diff --git a/src/views/KDbTableViewData.cpp b/src/views/KDbTableViewData.cpp index 3ddf5f3..bccda1f 100644 --- a/src/views/KDbTableViewData.cpp +++ b/src/views/KDbTableViewData.cpp @@ -708,7 +708,7 @@ bool KDbTableViewData::saveRecord(KDbRecordData *record= , bool insert, bool repai } if (f->isNotEmpty()) { saveRecordGetValue(&pval, d->cursor, d->pRecordEditBuffer, &it= _f, record, f, &val, col); - if (!f->isAutoIncrement() && (val.isNull() || KDb::isEmptyValu= e(f, val))) { + if (!f->isAutoIncrement() && (val.isNull() || KDb::isEmptyValu= e(f->type(), val))) { //NOT EMPTY violated d->result.msg =3D tr("\"%1\" column requires a value to be= entered.").arg(f->captionOrName()) + QLatin1String("\n\n") + KDbTableViewData= ::messageYouCanImproveData();