kexi

dbproperties.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>
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 "dbproperties.h"
21#include <tdelocale.h>
22
23using namespace KexiDB;
24
25DatabaseProperties::DatabaseProperties(Connection *conn)
26 : KexiDB::Object()
27 , m_conn(conn)
28{
29}
30
31DatabaseProperties::~DatabaseProperties()
32{
33}
34
35bool DatabaseProperties::setValue( const TQString& _name, const TQVariant& value )
36{
37 TQString name(_name.stripWhiteSpace());
38 bool ok;
39 //we need to know whether update or insert
40 bool exists = m_conn->resultExists(
41 TQString::fromLatin1("SELECT 1 FROM kexi__db WHERE db_property=%1")
42 .arg(m_conn->driver()->escapeString(name)), ok);
43 if (!ok) {
44 setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
45 return false;
46 }
47
48 if (exists) {
49 if (!m_conn->executeSQL(
50 TQString::fromLatin1("UPDATE kexi__db SET db_value=%1 WHERE db_property=%2")
51 .arg(m_conn->driver()->escapeString(value.toString()))
52 .arg(m_conn->driver()->escapeString(name))))
53 {
54 setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
55 return false;
56 }
57 return true;
58 }
59
60 if (!m_conn->executeSQL(
61 TQString::fromLatin1("INSERT INTO kexi__db (db_property, db_value) VALUES (%1, %2)")
62 .arg(m_conn->driver()->escapeString(name))
63 .arg(m_conn->driver()->escapeString(value.toString()))))
64 {
65 setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
66 return false;
67 }
68 return true;
69}
70
71bool DatabaseProperties::setCaption( const TQString& _name, const TQString& caption )
72{
73 TQString name(_name.stripWhiteSpace());
74 //captions have ' ' prefix
75 name.prepend(" ");
76 bool ok;
77 //we need to know whether update or insert
78 bool exists = m_conn->resultExists(
79 TQString::fromLatin1("SELECT 1 FROM kexi__db WHERE db_property=%1")
80 .arg(m_conn->driver()->escapeString(name)), ok);
81 if (!ok) {
82 setError(m_conn, i18n("Could not set caption for database property \"%1\".").arg(name));
83 return false;
84 }
85
86 if (exists) {
87 if (!m_conn->executeSQL(
88 TQString::fromLatin1("UPDATE kexi__db SET db_value=%1 WHERE db_property=%2")
89 .arg(m_conn->driver()->escapeString(caption))
90 .arg(m_conn->driver()->escapeString(name))))
91 {
92 setError(m_conn, i18n("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 TQString::fromLatin1("INSERT INTO kexi__db (db_property, db_value) VALUES (%1, %2)")
100 .arg(m_conn->driver()->escapeString(name))
101 .arg(m_conn->driver()->escapeString(caption))))
102 {
103 setError(m_conn, i18n("Could not set caption for database property \"%1\".").arg(name));
104 return false;
105 }
106 return true;
107}
108
109TQVariant DatabaseProperties::value( const TQString& _name )
110{
111 TQString result;
112 TQString name(_name.stripWhiteSpace());
113 if (true!=m_conn->querySingleString(
114 TQString::fromLatin1("SELECT db_value FROM kexi__db WHERE db_property=")
115 + m_conn->driver()->escapeString(name), result)) {
116 m_conn->setError(ERR_NO_DB_PROPERTY, i18n("Could not read database property \"%1\".").arg(name));
117 return TQVariant();
118 }
119 return result;
120}
121
122TQString DatabaseProperties::caption( const TQString& _name )
123{
124 TQString result;
125 TQString name(_name.stripWhiteSpace());
126 //captions have ' ' prefix
127 name.prepend(" ");
128 if (true!=m_conn->querySingleString(
129 TQString::fromLatin1("SELECT db_value FROM kexi__db WHERE db_property=")
130 + m_conn->driver()->escapeString(name), result)) {
131 setError(m_conn, i18n("Could not read database property \"%1\".").arg(name));
132 return TQString();
133 }
134 return result;
135}
136
138{
139 TQStringList result;
140 if (true!=m_conn->queryStringList(
141 TQString::fromLatin1("SELECT db_value FROM kexi__db WHERE db_property NOT LIKE ")
142 + m_conn->driver()->escapeString(TQString::fromLatin1(" %%")), result, 0 /*0-th*/)) {
143 // ^^ exclude captions
144 setError(m_conn, i18n("Could not read database properties."));
145 return TQStringList();
146 }
147 return result;
148}
Provides database connection, allowing queries and data modification.
Definition connection.h:62
bool setValue(const TQString &name, const TQVariant &value)
TQVariant value(const TQString &name)
bool setCaption(const TQString &name, const TQString &caption)
TQString caption(const TQString &name)
virtual void setError(int code=ERR_OTHER, const TQString &msg=TQString())
Definition object.cpp:53
High-level database connectivity library with database backend drivers.