KDb

SqliteAlter.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2006 Jarosław Staniek <staniek@kde.org>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20// ** bits of SqliteConnection related to table altering **
21
22#include "SqliteConnection.h"
23#include "KDb.h"
24
25#include <QHash>
26#include <QGlobalStatic>
27
28enum SqliteTypeAffinity { //as defined here: 2.1 Determination Of Column Affinity (https://sqlite.org/datatype3.html)
29 NoAffinity = 0, IntAffinity = 1, TextAffinity = 2, BLOBAffinity = 3
30};
31
32//! @internal
33struct SqliteTypeAffinityInternal {
34 SqliteTypeAffinityInternal() {
35 affinity.insert(KDbField::Byte, IntAffinity);
36 affinity.insert(KDbField::ShortInteger, IntAffinity);
37 affinity.insert(KDbField::Integer, IntAffinity);
38 affinity.insert(KDbField::BigInteger, IntAffinity);
39 affinity.insert(KDbField::Boolean, IntAffinity);
40 affinity.insert(KDbField::Date, TextAffinity);
41 affinity.insert(KDbField::DateTime, TextAffinity);
42 affinity.insert(KDbField::Time, TextAffinity);
43 affinity.insert(KDbField::Float, IntAffinity);
44 affinity.insert(KDbField::Double, IntAffinity);
45 affinity.insert(KDbField::Text, TextAffinity);
46 affinity.insert(KDbField::LongText, TextAffinity);
47 affinity.insert(KDbField::BLOB, BLOBAffinity);
48 }
50};
51
52Q_GLOBAL_STATIC(SqliteTypeAffinityInternal, KDb_SQLite_affinityForType)
53
54//! @return SQLite type affinity for @a type
55//! See doc/dev/alter_table_type_conversions.ods, page 2 for more info
56static SqliteTypeAffinity affinityForType(KDbField::Type type)
57{
58 return KDb_SQLite_affinityForType->affinity[type];
59}
60
62 const QString& propertyName, const QVariant& value)
63{
64 if (propertyName == QLatin1String("type")) {
65 bool ok;
66 KDbField::Type type = KDb::intToFieldType(value.toInt(&ok));
67 if (!ok || KDbField::InvalidType == type) {
68 //! @todo msg
69 return false;
70 }
71 return changeFieldType(table, field, type);
72 }
73 // not found
74 return cancelled;
75}
76
77/*!
78 From https://sqlite.org/datatype3.html :
79 Version 3 enhances provides the ability to store integer and real numbers in a more compact
80 format and the capability to store BLOB data.
81
82 Each value stored in an SQLite database (or manipulated by the database engine) has one
83 of the following storage classes:
84 * NULL. The value is a NULL value.
85 * INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending
86 on the magnitude of the value.
87 * REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
88 * TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
89 * BLOB. The value is a blob of data, stored exactly as it was input.
90
91 Column Affinity
92 In SQLite version 3, the type of a value is associated with the value itself,
93 not with the column or variable in which the value is stored.
94.The type affinity of a column is the recommended type for data stored in that column.
95
96 See alter_table_type_conversions.ods for details.
97*/
99 KDbField::Type type)
100{
101 Q_UNUSED(table);
102 const KDbField::Type oldType = field->type();
103 const SqliteTypeAffinity oldAffinity = affinityForType(oldType);
104 const SqliteTypeAffinity newAffinity = affinityForType(type);
105 if (oldAffinity != newAffinity) {
106 //type affinity will be changed
107 }
108
109 return cancelled;
110}
Meta-data for a field.
Definition KDbField.h:72
@ Integer
Definition KDbField.h:90
@ Boolean
Definition KDbField.h:92
@ ShortInteger
Definition KDbField.h:89
@ InvalidType
Definition KDbField.h:86
@ BigInteger
Definition KDbField.h:91
@ LongText
Definition KDbField.h:99
Type type() const
Definition KDbField.cpp:379
tristate drv_changeFieldProperty(KDbTableSchema *table, KDbField *field, const QString &propertyName, const QVariant &value) override
tristate changeFieldType(KDbTableSchema *table, KDbField *field, KDbField::Type type)
for drv_changeFieldProperty()
3-state logical type with three values: true, false and cancelled and convenient operators.
KDB_EXPORT KDbField::Type intToFieldType(int type)
Definition KDb.cpp:670
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
iterator insert(const Key &key, const T &value)
int toInt(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:11:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.