KDb
23 #include "KDbField_p.h"
24 #include "KDbConnection.h"
25 #include "KDbDriver.h"
26 #include "KDbExpression.h"
27 #include "KDbQuerySchema.h"
29 #include "kdb_debug.h"
34 static int g_defaultMaxLength = 0;
46 Q_DISABLE_COPY(FieldTypeNames)
50 class FieldTypeGroupNames
53 FieldTypeGroupNames();
58 Q_DISABLE_COPY(FieldTypeGroupNames)
67 #define ADDTYPE(type, i18, str) \
68 data[KDbField::type] = i18; \
69 data[KDbField::type+KDbField::Null+1] = QStringLiteral(str); \
70 str2num[ QString::fromLatin1(str).toLower() ] = KDbField::type; \
72 #define ADDGROUP(type, i18, str) \
73 data[KDbField::type] = i18; \
74 data[KDbField::type+KDbField::LastTypeGroup+1] = QStringLiteral(str); \
75 str2num[ QString::fromLatin1(str).toLower() ] = KDbField::type; \
78 FieldTypeNames::FieldTypeNames()
81 ADDTYPE(InvalidType, KDbField::tr(
"Invalid Type"),
"InvalidType");
82 ADDTYPE(Byte, KDbField::tr(
"Byte"),
"Byte");
83 ADDTYPE(ShortInteger, KDbField::tr(
"Short Integer Number"),
"ShortInteger");
84 ADDTYPE(Integer, KDbField::tr(
"Integer Number"),
"Integer");
85 ADDTYPE(BigInteger, KDbField::tr(
"Big Integer Number"),
"BigInteger");
86 ADDTYPE(Boolean, KDbField::tr(
"Yes/No Value"),
"Boolean");
87 ADDTYPE(Date, KDbField::tr(
"Date"),
"Date");
88 ADDTYPE(DateTime, KDbField::tr(
"Date and Time"),
"DateTime");
89 ADDTYPE(Time, KDbField::tr(
"Time"),
"Time");
90 ADDTYPE(Float, KDbField::tr(
"Single Precision Number"),
"Float");
91 ADDTYPE(Double, KDbField::tr(
"Double Precision Number"),
"Double");
92 ADDTYPE(Text, KDbField::tr(
"Text"),
"Text");
93 ADDTYPE(LongText, KDbField::tr(
"Long Text"),
"LongText");
94 ADDTYPE(BLOB, KDbField::tr(
"Object"),
"BLOB");
100 FieldTypeGroupNames::FieldTypeGroupNames()
101 : data((
KDbField::LastTypeGroup + 1)*2)
103 ADDGROUP(InvalidGroup, KDbField::tr(
"Invalid Group"),
"InvalidGroup");
104 ADDGROUP(TextGroup, KDbField::tr(
"Text"),
"TextGroup");
105 ADDGROUP(IntegerGroup, KDbField::tr(
"Integer Number"),
"IntegerGroup");
106 ADDGROUP(FloatGroup, KDbField::tr(
"Floating Point Number"),
"FloatGroup");
107 ADDGROUP(BooleanGroup, KDbField::tr(
"Yes/No"),
"BooleanGroup");
108 ADDGROUP(DateTimeGroup, KDbField::tr(
"Date/Time"),
"DateTimeGroup");
109 ADDGROUP(BLOBGroup, KDbField::tr(
"Object"),
"BLOBGroup");
114 class KDbFieldPrivate
124 class Q_DECL_HIDDEN
KDbField::Private
127 Private(
KDbFieldList *aParent =
nullptr,
int aOrder = -1)
142 , description(aDescription)
143 , precision(aPrecision)
145 , defaultValue(aDefaultValue)
149 Private(
const Private &o)
152 if (o.customProperties) {
153 customProperties =
new CustomPropertiesMap(*o.customProperties);
155 if (!o.expr.isNull()) {
164 delete customProperties;
178 int visibleDecimalPlaces = -1;
198 : d(new Private(aParent, aOrder))
206 :
KDbField(tableSchema, tableSchema->fieldCount())
211 :
KDbField(querySchema, querySchema->fieldCount())
217 :
KDbField(querySchema, querySchema->fieldCount())
222 int maxLength,
int precision,
const QVariant &defaultValue,
224 : d(new Private(name, type, options, precision, defaultValue, caption, description))
232 : d(new Private(*f.d))
236 KDbField::~KDbField()
283 return d->defaultValue;
298 return d->visibleDecimalPlaces;
303 return d->constraints;
328 return d->caption.isEmpty() ? d->name : d->caption;
333 return d->description;
348 return (num < d->hints.size()) ? d->hints.at(num) :
QString();
371 return LastTypeGroup - InvalidGroup + 1;
381 if (!d->expr.isNull()) {
382 return d->expr.type();
418 template <
typename T>
432 return tryConvert<int>(value);
434 return tryConvert<qlonglong>(value);
436 return tryConvert<bool>(value);
438 return tryConvert<QDate>(value);
440 return tryConvert<QDateTime>(value);
442 return tryConvert<QTime>(value);
444 return tryConvert<float>(value);
446 return tryConvert<double>(value);
449 return tryConvert<QString>(value);
451 return tryConvert<QByteArray>(value);
465 return s_typeNames->names;
482 return s_typeGroupNames->names;
487 return s_typeGroupNames->
data.value(
int(LastTypeGroup) + 1 +
typeGroup,
538 case KDbField::DateTime:
578 return DateTimeGroup;
597 d->parent = tableSchema;
612 d->parent = querySchema;
622 if (!d->expr.isNull()) {
624 <<
"because the field has expression assigned!";
647 return g_defaultMaxLength;
657 return d->maxLengthStrategy;
662 d->maxLengthStrategy = strategy;
697 d->visibleDecimalPlaces = p < 0 ? -1 : p;
705 d->options |= Unsigned;
707 d->options ^= Unsigned;
713 d->defaultValue = def;
726 unsigned int v = def.
toUInt(&ok);
727 if (!ok || v > 255) {
735 int v = def.
toInt(&ok);
736 if (!ok || (!(d->options & Unsigned) && (v < -32768 || v > 32767))
737 || ((d->options & Unsigned) && (v < 0 || v > 65535)))
749 if (!ok || (!(d->options & Unsigned)
750 && (-v > (
int)0x07FFFFFFF || v > (
int)(0x080000000 - 1))))
754 d->defaultValue =
QVariant((qint64)v);
763 if (!ok || (!(d->options & Unsigned) && (-v > 0x080000000 || v > (0x080000000-1))))
764 d->defaultValue = QVariant();
766 if (d->options & Unsigned)
767 d->defaultValue=QVariant((quint64) v);
769 d->defaultValue = QVariant((qint64)v);*/
773 unsigned short v = def.
toUShort(&ok);
777 d->defaultValue =
QVariant((
bool)v);
810 if (!ok || ((d->options & Unsigned) && (v < 0.0))) {
819 if (!ok || ((d->options & Unsigned) && (v < 0.0))) {
854 return d->defaultValue.isNull();
863 d->constraints ^= KDbField::AutoInc;
870 d->constraints ^= KDbField::PrimaryKey;
886 d->constraints ^= KDbField::Unique;
897 d->constraints ^= KDbField::ForeignKey;
904 d->constraints ^=KDbField::NotNull;
918 d->constraints ^= KDbField::Indexed;
928 void debug(
QDebug dbg,
const KDbField& field, KDbFieldDebugOptions options)
931 if (options & KDbFieldDebugAddName) {
938 if (field.
options() & KDbField::Unsigned)
943 if (field.
scale() > 0)
969 dbg.
nospace() <<
" EXPRESSION=";
973 if (!customProperties.isEmpty()) {
977 it != customProperties.constEnd(); ++it)
992 debug(dbg, field, KDbFieldDebugAddName);
1008 return !d->expr.isNull();
1024 kdbWarning() <<
"Cannot set expression if parent is set and it is not a query";
1027 if (d->expr == expr) {
1034 const QVariant& defaultValue)
const
1036 if (!d->customProperties) {
1039 return d->customProperties->value(propertyName,
defaultValue);
1047 if (!d->customProperties) {
1050 d->customProperties->insert(propertyName, value);
bool isNull() const const
static int defaultMaxLength()
virtual QString sqlTypeName(KDbField::Type type, const KDbField &field) const
ushort toUShort(bool *ok, int base) const const
bool isNull() const const
QVariant defaultValue() const
double toDouble(bool *ok) const const
bool isPrimaryKey() const
QString description() const
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
static TypeGroup typeGroupForString(const QString &typeGroupString)
QString number(int n, int base)
QVariant customProperty(const QByteArray &propertyName, const QVariant &defaultValue=QVariant()) const
Type type(const QSqlDatabase &db)
@ NotEmpty
only legal for string-like and blob fields
KDbConnection * connection() const
QHash< QByteArray, QVariant > CustomPropertiesMap
A data type used for handling custom properties of a field.
bool isValid() const const
int visibleDecimalPlaces() const
KDbExpression expression()
void setExpression(const KDbExpression &expr)
bool isFPNumericType() const
virtual KDbField * copy()
void setTable(KDbTableSchema *table)
void setAutoIncrement(bool a)
void setUniqueKey(bool u)
uint toUInt(bool *ok, int base) const const
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
static QString defaultSqlTypeName(KDbField::Type type)
CustomPropertiesMap customProperties() const
void setVisibleDecimalPlaces(int p)
static QVariant convertToType(const QVariant &value, Type type)
Converts value value to variant corresponding to type type.
float toFloat(bool *ok) const const
void setOptions(Options options)
QString typeString() const
void setPrimaryKey(bool p)
static int typeGroupsCount()
bool isAutoIncrementAllowed() const
bool isIntegerType() const
void setMaxLengthStrategy(MaxLengthStrategy strategy)
void setConstraints(Constraints c)
static QStringList typeGroupNames()
QVariant::Type variantType() const
Converts field's type to QVariant equivalent as accurate as possible.
QString typeGroupString() const
void setDescription(const QString &description)
KDB_EXPORT bool supportsVisibleDecimalPlacesProperty(KDbField::Type type)
bool isEmpty() const const
bool isForeignKey() const
void setSubType(const QString &subType)
void setForeignKey(bool f)
Constraints constraints() const
QString enumHint(int num)
TypeGroup typeGroup() const
bool isValid() const const
QVector< QString > enumHints() const
bool isNumericType() const
void setCaption(const QString &caption)
bool isDateTimeType() const
void setName(const QString &name)
long toLong(bool *ok, int base) const const
bool canConvert(int targetTypeId) const const
QString typeGroupName() const
void setEnumHints(const QVector< QString > &hints)
@ DefinedMaxLength
Used if setMaxLength() was called to set specific maximum value or to unlimited (0).
QString toLower() const const
static Type typeForString(const QString &typeString)
int toInt(bool *ok, int base) const const
QDate fromString(const QString &string, Qt::DateFormat format)
bool isAutoIncrement() const
bool isEmpty() const const
KDbQuerySchema provides information about database query.
KDB_EXPORT QString variantToString(const QVariant &v)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void setDefaultValue(const QVariant &def)
QString fromLatin1(const char *str, int size)
void setCustomProperty(const QByteArray &propertyName, const QVariant &value)
Sets value value for custom property propertyName.
void setParent(KDbFieldList *parent)
Sets parent for this field.
const char * name(StandardAction id)
void setMaxLength(int maxLength)
static void setDefaultMaxLength(int maxLength)
bool isValid() const const
bool hasEmptyProperty() const
void setQuery(KDbQuerySchema *query)
bool isExpression() const
MaxLengthStrategy maxLengthStrategy() const
static QStringList typeNames()
static int specialTypesCount()
Provides database connection, allowing queries and data modification.
KDbDriver * driver() const
The KDbExpression class represents a base class for all expressions.
const char * typeName() const const
QString captionOrName() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Jun 9 2023 04:07:15 by
doxygen 1.8.17 written
by
Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.