File indexing completed on 2024-04-28 15:58:53
0001 /* This file is part of the KDE project 0002 Copyright (C) 2006 Jarosław Staniek <staniek@kde.org> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 // ** bits of SqliteConnection related to table altering ** 0021 0022 #include "SqliteConnection.h" 0023 #include "KDb.h" 0024 0025 #include <QHash> 0026 #include <QGlobalStatic> 0027 0028 enum SqliteTypeAffinity { //as defined here: 2.1 Determination Of Column Affinity (https://sqlite.org/datatype3.html) 0029 NoAffinity = 0, IntAffinity = 1, TextAffinity = 2, BLOBAffinity = 3 0030 }; 0031 0032 //! @internal 0033 struct SqliteTypeAffinityInternal { 0034 SqliteTypeAffinityInternal() { 0035 affinity.insert(KDbField::Byte, IntAffinity); 0036 affinity.insert(KDbField::ShortInteger, IntAffinity); 0037 affinity.insert(KDbField::Integer, IntAffinity); 0038 affinity.insert(KDbField::BigInteger, IntAffinity); 0039 affinity.insert(KDbField::Boolean, IntAffinity); 0040 affinity.insert(KDbField::Date, TextAffinity); 0041 affinity.insert(KDbField::DateTime, TextAffinity); 0042 affinity.insert(KDbField::Time, TextAffinity); 0043 affinity.insert(KDbField::Float, IntAffinity); 0044 affinity.insert(KDbField::Double, IntAffinity); 0045 affinity.insert(KDbField::Text, TextAffinity); 0046 affinity.insert(KDbField::LongText, TextAffinity); 0047 affinity.insert(KDbField::BLOB, BLOBAffinity); 0048 } 0049 QHash<KDbField::Type, SqliteTypeAffinity> affinity; 0050 }; 0051 0052 Q_GLOBAL_STATIC(SqliteTypeAffinityInternal, KDb_SQLite_affinityForType) 0053 0054 //! @return SQLite type affinity for @a type 0055 //! See doc/dev/alter_table_type_conversions.ods, page 2 for more info 0056 static SqliteTypeAffinity affinityForType(KDbField::Type type) 0057 { 0058 return KDb_SQLite_affinityForType->affinity[type]; 0059 } 0060 0061 tristate SqliteConnection::drv_changeFieldProperty(KDbTableSchema *table, KDbField *field, 0062 const QString& propertyName, const QVariant& value) 0063 { 0064 if (propertyName == QLatin1String("type")) { 0065 bool ok; 0066 KDbField::Type type = KDb::intToFieldType(value.toInt(&ok)); 0067 if (!ok || KDbField::InvalidType == type) { 0068 //! @todo msg 0069 return false; 0070 } 0071 return changeFieldType(table, field, type); 0072 } 0073 // not found 0074 return cancelled; 0075 } 0076 0077 /*! 0078 From https://sqlite.org/datatype3.html : 0079 Version 3 enhances provides the ability to store integer and real numbers in a more compact 0080 format and the capability to store BLOB data. 0081 0082 Each value stored in an SQLite database (or manipulated by the database engine) has one 0083 of the following storage classes: 0084 * NULL. The value is a NULL value. 0085 * INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending 0086 on the magnitude of the value. 0087 * REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number. 0088 * TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE). 0089 * BLOB. The value is a blob of data, stored exactly as it was input. 0090 0091 Column Affinity 0092 In SQLite version 3, the type of a value is associated with the value itself, 0093 not with the column or variable in which the value is stored. 0094 .The type affinity of a column is the recommended type for data stored in that column. 0095 0096 See alter_table_type_conversions.ods for details. 0097 */ 0098 tristate SqliteConnection::changeFieldType(KDbTableSchema *table, KDbField *field, 0099 KDbField::Type type) 0100 { 0101 Q_UNUSED(table); 0102 const KDbField::Type oldType = field->type(); 0103 const SqliteTypeAffinity oldAffinity = affinityForType(oldType); 0104 const SqliteTypeAffinity newAffinity = affinityForType(type); 0105 if (oldAffinity != newAffinity) { 0106 //type affinity will be changed 0107 } 0108 0109 return cancelled; 0110 }