File indexing completed on 2024-11-03 04:18:54

0001 /* This file is part of the KDE project
0002    Copyright (C) 2005-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 #include "KDbProperties.h"
0021 #include "KDbConnection.h"
0022 
0023 //! @todo IMPORTANT: replace QPointer<KDbConnection> m_conn;
0024 KDbProperties::KDbProperties(KDbConnection *conn)
0025         : m_conn(conn)
0026 {
0027 }
0028 
0029 KDbProperties::~KDbProperties()
0030 {
0031 }
0032 
0033 bool KDbProperties::setValue(const QString& _name, const QVariant& value)
0034 {
0035     QString name(_name.trimmed());
0036     //we need to know whether update or insert
0037     const tristate result = m_conn->resultExists(
0038                       KDbEscapedString("SELECT 1 FROM kexi__db WHERE db_property=%1")
0039                       .arg(m_conn->escapeString(name)));
0040     if (~result) {
0041         m_result = m_conn->result();
0042         m_result.prependMessage(tr("Could not set value of database property \"%1\".").arg(name));
0043         return false;
0044     }
0045 
0046     if (result == true) {
0047         if (!m_conn->executeSql(
0048                     KDbEscapedString("UPDATE kexi__db SET db_value=%1 WHERE db_property=%2")
0049                     .arg(m_conn->escapeString(value.toString()))
0050                     .arg(m_conn->escapeString(name))))
0051         {
0052             m_result = m_conn->result();
0053             m_result.prependMessage(tr("Could not set value of database property \"%1\".").arg(name));
0054             return false;
0055         }
0056         return true;
0057     }
0058 
0059     if (!m_conn->executeSql(
0060                 KDbEscapedString("INSERT INTO kexi__db (db_property, db_value) VALUES (%1, %2)")
0061                     .arg(m_conn->escapeString(name))
0062                     .arg(m_conn->escapeString(value.toString()))))
0063     {
0064         m_result = m_conn->result();
0065         m_result.prependMessage(tr("Could not set value of database property \"%1\".").arg(name));
0066         return false;
0067     }
0068     return true;
0069 }
0070 
0071 bool KDbProperties::setCaption(const QString& _name, const QString& caption)
0072 {
0073     QString name(_name.trimmed());
0074     //captions have ' ' prefix
0075     name.prepend(QLatin1String(" "));
0076     //we need to know whether update or insert
0077     const tristate result = m_conn->resultExists(
0078                       KDbEscapedString("SELECT 1 FROM kexi__db WHERE db_property=%1")
0079                         .arg(m_conn->escapeString(name)));
0080     if (~result) {
0081         m_result = m_conn->result();
0082         m_result.prependMessage(tr("Could not set caption for database property \"%1\".").arg(name));
0083         return false;
0084     }
0085 
0086     if (result == true) {
0087         if (!m_conn->executeSql(
0088                     KDbEscapedString("UPDATE kexi__db SET db_value=%1 WHERE db_property=%2")
0089                         .arg(m_conn->escapeString(caption))
0090                         .arg(m_conn->escapeString(name)))) {
0091             m_result = m_conn->result();
0092             m_result.prependMessage(tr("Could not set caption for database property \"%1\".").arg(name));
0093             return false;
0094         }
0095         return true;
0096     }
0097 
0098     if (!m_conn->executeSql(
0099                 KDbEscapedString("INSERT INTO kexi__db (db_property, db_value) VALUES (%1, %2)")
0100                     .arg(m_conn->escapeString(name))
0101                     .arg(m_conn->escapeString(caption)))) {
0102         m_result = m_conn->result();
0103         m_result.prependMessage(tr("Could not set caption for database property \"%1\".").arg(name));
0104         return false;
0105     }
0106     return true;
0107 }
0108 
0109 QVariant KDbProperties::value(const QString& _name)
0110 {
0111     QString result;
0112     QString name(_name.trimmed());
0113     if (true != m_conn->querySingleString(
0114                 KDbEscapedString("SELECT db_value FROM kexi__db WHERE db_property=")
0115                     + m_conn->escapeString(name), &result))
0116     {
0117         m_result = m_conn->result();
0118         m_result.prependMessage(ERR_NO_DB_PROPERTY, tr("Could not read database property \"%1\".").arg(name));
0119         return QVariant();
0120     }
0121     return result;
0122 }
0123 
0124 QString KDbProperties::caption(const QString& _name)
0125 {
0126     QString result;
0127     QString name(_name.trimmed());
0128     //captions have ' ' prefix
0129     name.prepend(QLatin1String(" "));
0130     if (true != m_conn->querySingleString(
0131                 KDbEscapedString("SELECT db_value FROM kexi__db WHERE db_property=")
0132                     + m_conn->escapeString(name), &result))
0133     {
0134         m_result = m_conn->result();
0135         m_result.prependMessage(tr("Could not read database property \"%1\".").arg(name));
0136         return QString();
0137     }
0138     return result;
0139 }
0140 
0141 QStringList KDbProperties::names()
0142 {
0143     QStringList result;
0144     if (true != m_conn->queryStringList(
0145                 KDbEscapedString("SELECT db_property FROM kexi__db WHERE db_property NOT LIKE ")
0146                 + m_conn->escapeString(QString::fromLatin1(" %%")), &result, 0 /*0-th*/))
0147         //                                                        ^^ exclude captions
0148     {
0149         m_result = m_conn->result();
0150         m_result.prependMessage(tr("Could not read database properties."));
0151         return QStringList();
0152     }
0153     return result;
0154 }