[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdb] /: Update API for types: add 'field' to the name to indicate they are about field types
From:       Jaroslaw Staniek <staniek () kde ! org>
Date:       2015-06-30 22:09:07
Message-ID: E1ZA3iF-00042Q-11 () scm ! kde ! org
[Download RAW message or body]

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 field types

+ replace KDb::TypeGroupList with QList<KDbField::Type>
+ return a list with invalid type for fieldTypeNamesForGroup(KDbField::InvalidGroup) \
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_data()
     T(KDbToken::SQL_NULL, QVariant(), KDbToken::OR, \
KDbToken::CHARACTER_STRING_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, \
                KDbField::Integer);
     T(KDbToken::INTEGER_CONST, 50, '-', KDbToken::INTEGER_CONST, 20, \
                KDbField::Integer);
     T(KDbToken::INTEGER_CONST, 50, '*', KDbToken::INTEGER_CONST, 20, \
                KDbField::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 QString \
                &tableName,
                            + " AND " + keyname3 + "=" + \
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().isNull())
+    if (KDbField::hasEmptyProperty(type) && v.toString().isEmpty() && \
!v.toString().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 = KDbField::FirstType; t <= KDbField::LastType; t = \
KDbField::Type(int(t) + 1)) { +        for (KDbField::Type t = KDbField::InvalidType; \
t <= KDbField::LastType; t = KDbField::Type(int(t) + 1)) {  const KDbField::TypeGroup \
                tg = KDbField::typeGroup(t);
-            KDb::TypeGroupList list;
+            QList<KDbField::Type> list;
             QStringList name_list, str_list;
             if (tlist.contains(tg)) {
                 list = tlist.value(tg);
@@ -347,7 +347,7 @@ struct TypeCache {
         def_tlist[ KDbField::BLOBGroup ] = KDbField::BLOB;
     }
 
-    QHash< KDbField::TypeGroup, KDb::TypeGroupList > tlist;
+    QHash< KDbField::TypeGroup, QList<KDbField::Type> > 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<KDbField::Type> KDb::fieldTypesForGroup(KDbField::TypeGroup typeGroup)
 {
     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 <= KDbField::LastTypeGroup) ? \
KDb_typeCache->def_tlist.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<QVariant> values;
 };
 
-//! Used in KDb::emptyValueForType()
-Q_GLOBAL_STATIC(KDb_EmptyValueForTypeCache, KDb_emptyValueForTypeCache)
+//! Used in KDb::emptyValueForFieldType()
+Q_GLOBAL_STATIC(KDb_EmptyValueForFieldTypeCache, KDb_emptyValueForFieldTypeCache)
 
-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 <= KDbField::LastType) ? type : \
KDbField::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<QVariant> values;
 };
-//! Used in KDb::notEmptyValueForType()
-Q_GLOBAL_STATIC(KDb_NotEmptyValueForTypeCache, KDb_notEmptyValueForTypeCache)
+//! Used in KDb::notEmptyValueForFieldType()
+Q_GLOBAL_STATIC(KDb_NotEmptyValueForFieldTypeCache, \
KDb_notEmptyValueForFieldTypeCache)  
-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 <= KDbField::LastType) ? type : \
KDbField::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 *maxValue)
+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 = 0;
-        *maxValue = 255;
+        *minValue = signedness == KDb::Signed ? -0x80 : 0;
+        *maxValue = signedness == KDb::Signed ? 0x7F : 0xFF;
         break;
     case KDbField::ShortInteger:
-        *minValue = -32768;
-        *maxValue = 32767;
+        *minValue = signedness == KDb::Signed ? -0x8000 : 0;
+        *maxValue = signedness == 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 = (int) - 0x07FFFFFFF;
-        *maxValue = (int)(0x080000000 - 1);
+        *minValue = signedness == KDb::Signed ? qlonglong(-0x07FFFFFFF) : \
qlonglong(0); +        *maxValue = signedness == KDb::Signed ? qlonglong(0x07FFFFFFF) \
: qlonglong(0x0FFFFFFFF);  }
 }
 
-KDbField::Type KDb::maximumForIntegerTypes(KDbField::Type t1, KDbField::Type t2)
+KDbField::Type KDb::maximumForIntegerFieldTypes(KDbField::Type t1, KDbField::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 == 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() == KDbField::BLOB)
+    else if (type == 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::Type \
                keytype2, const QVariant& keyval2,
                                     const QString &keyname3, KDbField::Type \
keytype3, const QVariant& keyval3);  
-typedef QList<KDbField::Type> TypeGroupList;
+/*! @return list of field types for field type group @a typeGroup. */
+KDB_EXPORT const QList<KDbField::Type> fieldTypesForGroup(KDbField::TypeGroup \
typeGroup);  
-/*! @return list of types for type group @a typeGroup. */
-KDB_EXPORT const KDb::TypeGroupList typesForGroup(KDbField::TypeGroup typeGroup);
+/*! @return list of translated field type names for field type group @a typeGroup. \
*/ +KDB_EXPORT QStringList fieldTypeNamesForGroup(KDbField::TypeGroup typeGroup);
 
-/*! @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 \
typeGroup);  
-/*! @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 typeGroup);
+KDB_EXPORT KDbField::Type defaultFieldTypeForGroup(KDbField::TypeGroup typeGroup);
 
-/*! @return a slightly simplified type name for @a field.
- For BLOB type it returns i18n-ed "Image" string or other, depending on the 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 returned. */
+/*! @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 "Number" 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 resultable,
  and string pointed by @a details to details of this error (server message and \
result number). @@ -289,24 +288,24 @@ KDB_EXPORT QDomElement \
saveNumberElementToDom(QDomDocument *doc, QDomElement *pa  KDB_EXPORT QDomElement \
saveBooleanElementToDom(QDomDocument *doc, QDomElement *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 for BLOB \
                type.
- For date, time and date/time types current date, time, date+time is returned, \
                respectively.
- Returns null QVariant for unsupported values like KDbField::InvalidType.
- This function is efficient (uses a cache) and is heavily used by the \
                KDbAlterTableHandler
- 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 having
- "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 \
database 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 having \
"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 \
                KDbAlterTableHandler
- 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 integers pointed
- by @a minValue and @a maxValue. Supported types are Byte, ShortInteger, Integer 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 protocol \
compatibility. + Supported types are Byte, ShortInteger, Integer and BigInteger.
+ The value of @a signedness controls the values; they can be limited to unsigned or \
not.  Results for BigInteger or non-integer types are the same as for Integer due to \
limitation  of int type. Signed integers are assumed. @a minValue and @a maxValue \
                must 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 *minValue, \
qlonglong *maxValue, +                                      KDb::Signedness \
signedness = 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 involves 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, \
Byte) is Byte but result  of 100 * 100 exceeds the range of Byte. */
-KDB_EXPORT KDbField::Type maximumForIntegerTypes(KDbField::Type t1, KDbField::Type \
t2); +KDB_EXPORT KDbField::Type maximumForIntegerFieldTypes(KDbField::Type t1, \
KDbField::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 is 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 = d->conn->driver()->valueToSQL(
-                                      f->type(), KDb::emptyValueForType(f->type()));
+                                      f->type(), \
KDb::emptyValueForFieldType(f->type()));  } else if (f->isNotEmpty()) {
                 //this field cannot be empty - use any nonempty value..., e.g. " " \
for text or 0 for number  sourceSQLString = d->conn->driver()->valueToSQL(
-                                      f->type(), \
KDb::notEmptyValueForType(f->type())); +                                      \
f->type(), KDb::notEmptyValueForFieldType(f->type()));  }
 //! @todo support unique, validatationRule, unsigned flags...
 //! @todo check for foreignKey values...
diff --git a/src/expression/KDbBinaryExpression.cpp \
b/src/expression/KDbBinaryExpression.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 == 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 expression 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, \
maximumForIntegerFieldTypes(Byte, Byte) is Byte but result  of 100 * 100 exceeds the \
range of Byte.  
              Solution: for types smaller than Integer (e.g. Byte and ShortInteger) \
we are returning  Integer type.
             */
-            KDbField::Type t = KDb::maximumForIntegerTypes(lt, rt);
+            KDbField::Type t = KDb::maximumForIntegerFieldTypes(lt, rt);
             if (t == KDbField::Byte || t == 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::isEmptyValue(f, \
val))) { +            if (!f->isAutoIncrement() && (val.isNull() || \
KDb::isEmptyValue(f->type(), val))) {  //NOT EMPTY violated
                 d->result.msg = tr("\"%1\" column requires a value to be \
                entered.").arg(f->captionOrName())
                                 + QLatin1String("\n\n") + \
KDbTableViewData::messageYouCanImproveData();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic