summaryrefslogtreecommitdiffstats
path: root/kexi/kexidb/dbproperties.cpp
blob: 490a7390fff78930518343786d8ba968d7d60f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* This file is part of the KDE project
   Copyright (C) 2005-2006 Jaroslaw Staniek <[email protected]>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "dbproperties.h"
#include <klocale.h>

using namespace KexiDB;

DatabaseProperties::DatabaseProperties(Connection *conn)
 : KexiDB::Object()
 , m_conn(conn)
{
}

DatabaseProperties::~DatabaseProperties()
{
}

bool DatabaseProperties::setValue( const TQString& _name, const TQVariant& value )
{
	TQString name(_name.stripWhiteSpace());
	bool ok;
	//we need to know whether update or insert
	bool exists = m_conn->resultExists(
		TQString::fromLatin1("SELECT 1 FROM kexi__db WHERE db_property=%1")
			.arg(m_conn->driver()->escapeString(name)), ok);
	if (!ok) {
		setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
		return false;
	}

	if (exists) {
		if (!m_conn->executeSQL(
			TQString::fromLatin1("UPDATE kexi__db SET db_value=%1 WHERE db_property=%2")
			.arg(m_conn->driver()->escapeString(value.toString()))
			.arg(m_conn->driver()->escapeString(name))))
		{
			setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
			return false;
		}
		return true;
	}

	if (!m_conn->executeSQL(
		TQString::fromLatin1("INSERT INTO kexi__db (db_property, db_value) VALUES (%1, %2)")
		.arg(m_conn->driver()->escapeString(name))
		.arg(m_conn->driver()->escapeString(value.toString()))))
	{
		setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
		return false;
	}
	return true;
}

bool DatabaseProperties::setCaption( const TQString& _name, const TQString& caption )
{
	TQString name(_name.stripWhiteSpace());
	//captions have ' ' prefix
	name.prepend(" ");
	bool ok;
	//we need to know whether update or insert
	bool exists = m_conn->resultExists(
		TQString::fromLatin1("SELECT 1 FROM kexi__db WHERE db_property=%1")
			.arg(m_conn->driver()->escapeString(name)), ok);
	if (!ok) {
		setError(m_conn, i18n("Could not set caption for database property \"%1\".").arg(name));
		return false;
	}

	if (exists) {
		if (!m_conn->executeSQL(
			TQString::fromLatin1("UPDATE kexi__db SET db_value=%1 WHERE db_property=%2")
			.arg(m_conn->driver()->escapeString(caption))
			.arg(m_conn->driver()->escapeString(name))))
		{
			setError(m_conn, i18n("Could not set caption for database property \"%1\".").arg(name));
			return false;
		}
		return true;
	}

	if (!m_conn->executeSQL(
		TQString::fromLatin1("INSERT INTO kexi__db (db_property, db_value) VALUES (%1, %2)")
		.arg(m_conn->driver()->escapeString(name))
		.arg(m_conn->driver()->escapeString(caption))))
	{
		setError(m_conn, i18n("Could not set caption for database property \"%1\".").arg(name));
		return false;
	}
	return true;
}

TQVariant DatabaseProperties::value( const TQString& _name )
{
	TQString result;
	TQString name(_name.stripWhiteSpace());
	if (true!=m_conn->querySingleString(
		TQString::fromLatin1("SELECT db_value FROM kexi__db WHERE db_property=")
		+ m_conn->driver()->escapeString(name), result)) {
		m_conn->setError(ERR_NO_DB_PROPERTY, i18n("Could not read database property \"%1\".").arg(name));
		return TQVariant();
	}
	return result;
}

TQString DatabaseProperties::caption( const TQString& _name )
{
	TQString result;
	TQString name(_name.stripWhiteSpace());
	//captions have ' ' prefix
	name.prepend(" ");
	if (true!=m_conn->querySingleString(
		TQString::fromLatin1("SELECT db_value FROM kexi__db WHERE db_property=")
		+ m_conn->driver()->escapeString(name), result)) {
		setError(m_conn, i18n("Could not read database property \"%1\".").arg(name));
		return TQString();
	}
	return result;
}

TQStringList DatabaseProperties::names()
{
	TQStringList result;
	if (true!=m_conn->queryStringList(
		TQString::fromLatin1("SELECT db_value FROM kexi__db WHERE db_property NOT LIKE ")
		    + m_conn->driver()->escapeString(TQString::fromLatin1(" %%")), result, 0 /*0-th*/)) {
		//                                                        ^^ exclude captions
		setError(m_conn, i18n("Could not read database properties."));
		return TQStringList();
	}
	return result;
}