23#include "KDbField_p.h"
24#include "KDbConnection.h"
26#include "KDbExpression.h"
27#include "KDbQuerySchema.h"
34static int g_defaultMaxLength = 0;
46 Q_DISABLE_COPY(FieldTypeNames)
50class FieldTypeGroupNames
53 FieldTypeGroupNames();
58 Q_DISABLE_COPY(FieldTypeGroupNames)
62Q_GLOBAL_STATIC(FieldTypeNames, s_typeNames)
65Q_GLOBAL_STATIC(FieldTypeGroupNames, s_typeGroupNames)
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; \
78FieldTypeNames::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");
100FieldTypeGroupNames::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");
124class 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))
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;
382 return d->expr.
type();
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;
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;
928void 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) {
1047 if (!d->customProperties) {
1050 d->customProperties->
insert(propertyName, value);
Provides database connection, allowing queries and data modification.
KDbDriver * driver() const
virtual QString sqlTypeName(KDbField::Type type, const KDbField &field) const
static QString defaultSqlTypeName(KDbField::Type type)
The KDbExpression class represents a base class for all expressions.
KDbField::Type type() const
void setSubType(const QString &subType)
static int typeGroupsCount()
MaxLengthStrategy maxLengthStrategy() const
void setDescription(const QString &description)
bool isNumericType() const
static Type typeForString(const QString &typeString)
bool isAutoIncrementAllowed() const
static int specialTypesCount()
void setEnumHints(const QVector< QString > &hints)
QHash< QByteArray, QVariant > CustomPropertiesMap
A data type used for handling custom properties of a field.
void setOptions(Options options)
QVariant::Type variantType() const
Converts field's type to QVariant equivalent as accurate as possible.
CustomPropertiesMap customProperties() const
static QVariant convertToType(const QVariant &value, Type type)
Converts value value to variant corresponding to type type.
bool isAutoIncrement() const
void setCaption(const QString &caption)
static void setDefaultMaxLength(int maxLength)
bool isExpression() const
@ DefinedMaxLength
Used if setMaxLength() was called to set specific maximum value or to unlimited (0).
void setPrimaryKey(bool p)
QString captionOrName() const
static QStringList typeNames()
bool isDateTimeType() const
bool isForeignKey() const
QVariant customProperty(const QByteArray &propertyName, const QVariant &defaultValue=QVariant()) const
QString enumHint(int num)
virtual KDbField * copy()
QString description() const
void setDefaultValue(const QVariant &def)
void setMaxLengthStrategy(MaxLengthStrategy strategy)
void setQuery(KDbQuerySchema *query)
static TypeGroup typeGroupForString(const QString &typeGroupString)
static QStringList typeGroupNames()
bool isFPNumericType() const
@ NotEmpty
only legal for string-like and blob fields
void setVisibleDecimalPlaces(int p)
void setParent(KDbFieldList *parent)
Sets parent for this field.
QString typeGroupString() const
void setName(const QString &name)
Constraints constraints() const
void setAutoIncrement(bool a)
void setMaxLength(int maxLength)
bool isIntegerType() const
QString typeString() const
bool hasEmptyProperty() const
void setUniqueKey(bool u)
int visibleDecimalPlaces() const
QString typeGroupName() const
QVariant defaultValue() const
KDbExpression expression()
static int defaultMaxLength()
void setCustomProperty(const QByteArray &propertyName, const QVariant &value)
Sets value value for custom property propertyName.
void setConstraints(Constraints c)
void setExpression(const KDbExpression &expr)
void setForeignKey(bool f)
bool isPrimaryKey() const
TypeGroup typeGroup() const
QVector< QString > enumHints() const
void setTable(KDbTableSchema *table)
KDbQuerySchema provides information about database query.
KDbConnection * connection() const
Type type(const QSqlDatabase &db)
KDB_EXPORT QString variantToString(const QVariant &v)
KDB_EXPORT bool supportsVisibleDecimalPlacesProperty(KDbField::Type type)
QString name(StandardAction id)
QDebug operator<<(QDebug dbg, const PerceptualColor::MultiSpinBoxSection &value)
bool isEmpty() const const
bool isNull() const const
qsizetype length() const const
double toDouble(bool *ok) const const
float toFloat(bool *ok) const const
int toInt(bool *ok, int base) const const
long toLong(bool *ok, int base) const const
uint toUInt(bool *ok, int base) const const
ushort toUShort(bool *ok, int base) const const
QDate fromString(QStringView string, QStringView format, QCalendar cal)
bool isValid(int year, int month, int day)
bool isValid() const const
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QString number(double n, char format, int precision)
QString toLower() const const
bool isValid(int h, int m, int s, int ms)
bool canConvert() const const
bool isNull() const const
const char * typeName() const const