kexi

dbproperties.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "dbproperties.h"
00021 #include <klocale.h>
00022 
00023 using namespace KexiDB;
00024 
00025 DatabaseProperties::DatabaseProperties(Connection *conn)
00026  : KexiDB::Object()
00027  , m_conn(conn)
00028 {
00029 }
00030 
00031 DatabaseProperties::~DatabaseProperties()
00032 {
00033 }
00034 
00035 bool DatabaseProperties::setValue( const QString& _name, const QVariant& value )
00036 {
00037     QString name(_name.stripWhiteSpace());
00038     bool ok;
00039     //we need to know whether update or insert
00040     bool exists = m_conn->resultExists(
00041         QString::fromLatin1("select 1 from kexi__db where db_property=%1")
00042             .arg(m_conn->driver()->escapeString(name)), ok);
00043     if (!ok) {
00044         setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
00045         return false;
00046     }
00047 
00048     if (exists) {
00049         if (!m_conn->executeSQL(
00050             QString::fromLatin1("update kexi__db set db_value=%1 where db_property=%2")
00051             .arg(m_conn->driver()->escapeString(value.toString()))
00052             .arg(m_conn->driver()->escapeString(name))))
00053         {
00054             setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
00055             return false;
00056         }
00057         return true;
00058     }
00059 
00060     if (!m_conn->executeSQL(
00061         QString::fromLatin1("insert into kexi__db (db_property, db_value) values (%1, %2)")
00062         .arg(m_conn->driver()->escapeString(name))
00063         .arg(m_conn->driver()->escapeString(value.toString()))))
00064     {
00065         setError(m_conn, i18n("Could not set value of database property \"%1\".").arg(name));
00066         return false;
00067     }
00068     return true;
00069 }
00070 
00071 bool DatabaseProperties::setCaption( const QString& _name, const QString& caption )
00072 {
00073     QString name(_name.stripWhiteSpace());
00074     //captions have ' ' prefix
00075     name.prepend(" ");
00076     bool ok;
00077     //we need to know whether update or insert
00078     bool exists = m_conn->resultExists(
00079         QString::fromLatin1("select 1 from kexi__db where db_property=%1")
00080             .arg(m_conn->driver()->escapeString(name)), ok);
00081     if (!ok) {
00082         setError(m_conn, i18n("Could not set caption for database property \"%1\".").arg(name));
00083         return false;
00084     }
00085 
00086     if (exists) {
00087         if (!m_conn->executeSQL(
00088             QString::fromLatin1("update kexi__db set db_value=%1 where db_property=%2")
00089             .arg(m_conn->driver()->escapeString(caption))
00090             .arg(m_conn->driver()->escapeString(name))))
00091         {
00092             setError(m_conn, i18n("Could not set caption for database property \"%1\".").arg(name));
00093             return false;
00094         }
00095         return true;
00096     }
00097 
00098     if (!m_conn->executeSQL(
00099         QString::fromLatin1("insert into kexi__db (db_property, db_value) values (%1, %2)")
00100         .arg(m_conn->driver()->escapeString(name))
00101         .arg(m_conn->driver()->escapeString(caption))))
00102     {
00103         setError(m_conn, i18n("Could not set caption for database property \"%1\".").arg(name));
00104         return false;
00105     }
00106     return true;
00107 }
00108 
00109 QVariant DatabaseProperties::value( const QString& _name )
00110 {
00111     QString result;
00112     QString name(_name.stripWhiteSpace());
00113     if (true!=m_conn->querySingleString(
00114         QString::fromLatin1("select db_value from kexi__db where db_property=")
00115         + m_conn->driver()->escapeString(name), result)) {
00116         m_conn->setError(ERR_NO_DB_PROPERTY, i18n("Could not read database property \"%1\".").arg(name));
00117         return QVariant();
00118     }
00119     return result;
00120 }
00121 
00122 QString DatabaseProperties::caption( const QString& _name )
00123 {
00124     QString result;
00125     QString name(_name.stripWhiteSpace());
00126     //captions have ' ' prefix
00127     name.prepend(" ");
00128     if (true!=m_conn->querySingleString(
00129         QString::fromLatin1("select db_value from kexi__db where db_property=")
00130         + m_conn->driver()->escapeString(name), result)) {
00131         setError(m_conn, i18n("Could not read database property \"%1\".").arg(name));
00132         return QString::null;
00133     }
00134     return result;
00135 }
00136 
00137 QStringList DatabaseProperties::names()
00138 {
00139     QStringList result;
00140     if (true!=m_conn->queryStringList(
00141         QString::fromLatin1("select db_value from kexi__db where db_property not like ")
00142             + m_conn->driver()->escapeString(QString::fromLatin1(" %%")), result, 0 /*0-th*/)) {
00143         //                                                        ^^ exclude captions
00144         setError(m_conn, i18n("Could not read database properties."));
00145         return QStringList();
00146     }
00147     return result;
00148 }
KDE Home | KDE Accessibility Home | Description of Access Keys