KDb

KDbProperties.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005-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#include "KDbProperties.h"
21#include "KDbConnection.h"
22
23//! @todo IMPORTANT: replace QPointer<KDbConnection> m_conn;
25 : m_conn(conn)
26{
27}
28
29KDbProperties::~KDbProperties()
30{
31}
32
33bool KDbProperties::setValue(const QString& _name, const QVariant& value)
34{
35 QString name(_name.trimmed());
36 //we need to know whether update or insert
37 const tristate result = m_conn->resultExists(
38 KDbEscapedString("SELECT 1 FROM kexi__db WHERE db_property=%1")
39 .arg(m_conn->escapeString(name)));
40 if (~result) {
41 m_result = m_conn->result();
42 m_result.prependMessage(tr("Could not set value of database property \"%1\".").arg(name));
43 return false;
44 }
45
46 if (result == true) {
47 if (!m_conn->executeSql(
48 KDbEscapedString("UPDATE kexi__db SET db_value=%1 WHERE db_property=%2")
49 .arg(m_conn->escapeString(value.toString()))
50 .arg(m_conn->escapeString(name))))
51 {
52 m_result = m_conn->result();
53 m_result.prependMessage(tr("Could not set value of database property \"%1\".").arg(name));
54 return false;
55 }
56 return true;
57 }
58
59 if (!m_conn->executeSql(
60 KDbEscapedString("INSERT INTO kexi__db (db_property, db_value) VALUES (%1, %2)")
61 .arg(m_conn->escapeString(name))
62 .arg(m_conn->escapeString(value.toString()))))
63 {
64 m_result = m_conn->result();
65 m_result.prependMessage(tr("Could not set value of database property \"%1\".").arg(name));
66 return false;
67 }
68 return true;
69}
70
71bool KDbProperties::setCaption(const QString& _name, const QString& caption)
72{
73 QString name(_name.trimmed());
74 //captions have ' ' prefix
75 name.prepend(QLatin1String(" "));
76 //we need to know whether update or insert
77 const tristate result = m_conn->resultExists(
78 KDbEscapedString("SELECT 1 FROM kexi__db WHERE db_property=%1")
79 .arg(m_conn->escapeString(name)));
80 if (~result) {
81 m_result = m_conn->result();
82 m_result.prependMessage(tr("Could not set caption for database property \"%1\".").arg(name));
83 return false;
84 }
85
86 if (result == true) {
87 if (!m_conn->executeSql(
88 KDbEscapedString("UPDATE kexi__db SET db_value=%1 WHERE db_property=%2")
89 .arg(m_conn->escapeString(caption))
90 .arg(m_conn->escapeString(name)))) {
91 m_result = m_conn->result();
92 m_result.prependMessage(tr("Could not set caption for database property \"%1\".").arg(name));
93 return false;
94 }
95 return true;
96 }
97
98 if (!m_conn->executeSql(
99 KDbEscapedString("INSERT INTO kexi__db (db_property, db_value) VALUES (%1, %2)")
100 .arg(m_conn->escapeString(name))
101 .arg(m_conn->escapeString(caption)))) {
102 m_result = m_conn->result();
103 m_result.prependMessage(tr("Could not set caption for database property \"%1\".").arg(name));
104 return false;
105 }
106 return true;
107}
108
110{
111 QString result;
112 QString name(_name.trimmed());
113 if (true != m_conn->querySingleString(
114 KDbEscapedString("SELECT db_value FROM kexi__db WHERE db_property=")
115 + m_conn->escapeString(name), &result))
116 {
117 m_result = m_conn->result();
118 m_result.prependMessage(ERR_NO_DB_PROPERTY, tr("Could not read database property \"%1\".").arg(name));
119 return QVariant();
120 }
121 return result;
122}
123
125{
126 QString result;
127 QString name(_name.trimmed());
128 //captions have ' ' prefix
129 name.prepend(QLatin1String(" "));
130 if (true != m_conn->querySingleString(
131 KDbEscapedString("SELECT db_value FROM kexi__db WHERE db_property=")
132 + m_conn->escapeString(name), &result))
133 {
134 m_result = m_conn->result();
135 m_result.prependMessage(tr("Could not read database property \"%1\".").arg(name));
136 return QString();
137 }
138 return result;
139}
140
142{
143 QStringList result;
144 if (true != m_conn->queryStringList(
145 KDbEscapedString("SELECT db_property FROM kexi__db WHERE db_property NOT LIKE ")
146 + m_conn->escapeString(QString::fromLatin1(" %%")), &result, 0 /*0-th*/))
147 // ^^ exclude captions
148 {
149 m_result = m_conn->result();
150 m_result.prependMessage(tr("Could not read database properties."));
151 return QStringList();
152 }
153 return result;
154}
Provides database connection, allowing queries and data modification.
virtual KDbEscapedString escapeString(const QString &str) const
bool queryStringList(const KDbEscapedString &sql, QStringList *list, int column=0)
tristate querySingleString(const KDbEscapedString &sql, QString *value, int column=0, QueryRecordOptions options=QueryRecordOption::Default)
bool executeSql(const KDbEscapedString &sql)
Executes a new native (raw, backend-specific) SQL query.
tristate resultExists(const KDbEscapedString &sql, QueryRecordOptions options=QueryRecordOption::Default)
Specialized string for escaping.
bool setCaption(const QString &name, const QString &caption)
KDbProperties(KDbConnection *conn)
QStringList names()
QString caption(const QString &name)
QVariant value(const QString &name)
bool setValue(const QString &name, const QVariant &value)
void prependMessage(int code, const QString &message)
Sets result code and prepends message to an existing message.
Definition KDbResult.cpp:80
3-state logical type with three values: true, false and cancelled and convenient operators.
QString fromLatin1(QByteArrayView str)
QString & prepend(QChar ch)
QString trimmed() const const
QString toString() 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.