kexi

kexiqueryparameters.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This library 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 library 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 library; see the file COPYING.LIB.  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 "kexiqueryparameters.h"
00021 
00022 #include <kdebug.h>
00023 #include <klocale.h>
00024 #include <kinputdialog.h>
00025 #include <knumvalidator.h>
00026 
00027 #include <kexidb/queryschemaparameter.h>
00028 #include <kexidb/utils.h>
00029 #include "utils/kexidatetimeformatter.h"
00030 
00031 //static
00032 QValueList<QVariant> KexiQueryParameters::getParameters(QWidget *parent, 
00033     const KexiDB::Driver &driver, KexiDB::QuerySchema& querySchema, bool &ok)
00034 {
00035     Q_UNUSED(driver);
00036     ok = false;
00037     const KexiDB::QuerySchemaParameterList params( querySchema.parameters() );
00038     QValueList<QVariant> values;
00039     const QString caption( i18n("Enter Query Parameter Value", "Enter Parameter Value") );
00040     foreach(KexiDB::QuerySchemaParameterListConstIterator, it, params) {
00041         switch ((*it).type) {
00042         case KexiDB::Field::Byte:
00043         case KexiDB::Field::ShortInteger:
00044         case KexiDB::Field::Integer:
00045         case KexiDB::Field::BigInteger: {
00047             int minValue, maxValue;
00049             KexiDB::getLimitsForType((*it).type, minValue, maxValue);
00050             const int result = KInputDialog::getInteger(
00051                 caption, (*it).message, 0, minValue, maxValue, 1/*step*/, 10/*base*/, &ok, parent);
00052             if (!ok)
00053                 return QValueList<QVariant>(); //cancelled
00054             values.append(result);
00055             break;
00056         }
00057         case KexiDB::Field::Boolean: {
00058             QStringList list;
00059             list << i18n("Boolean True - Yes", "Yes") << i18n("Boolean False - No", "No");
00060             const QString result = KInputDialog::getItem(
00061                 caption, (*it).message, list, 0/*current*/, false , &ok, parent);
00062             if (!ok || result.isEmpty())
00063                 return QValueList<QVariant>(); //cancelled
00064             values.append( QVariant( result==list.first(), 1 ) );
00065             break;
00066         }
00067         case KexiDB::Field::Date: {
00068             KexiDateFormatter df;
00069             const QString result = KInputDialog::getText( 
00070                 caption, (*it).message, QString::null, &ok, parent, 0/*name*/, 
00072                 0/*validator*/, df.inputMask() );
00073             if (!ok)
00074                 return QValueList<QVariant>(); //cancelled
00075             values.append( df.stringToDate(result) );
00076             break;
00077         }
00078         case KexiDB::Field::DateTime: {
00079             KexiDateFormatter df;
00080             KexiTimeFormatter tf;
00081             const QString result = KInputDialog::getText( 
00082                 caption, (*it).message, QString::null, &ok, parent, 0/*name*/, 
00084                 0/*validator*/, dateTimeInputMask(df, tf) );
00085             if (!ok)
00086                 return QValueList<QVariant>(); //cancelled
00087             values.append( stringToDateTime(df, tf, result) );
00088             break;
00089         }
00090         case KexiDB::Field::Time: {
00091             KexiTimeFormatter tf;
00092             const QString result = KInputDialog::getText( 
00093                 caption, (*it).message, QString::null, &ok, parent, 0/*name*/, 
00095                 0/*validator*/, tf.inputMask() );
00096             if (!ok)
00097                 return QValueList<QVariant>(); //cancelled
00098             values.append( tf.stringToTime(result) );
00099             break;
00100         }
00101         case KexiDB::Field::Float:
00102         case KexiDB::Field::Double: {
00103             // KInputDialog::getDouble() does not work well, use getText and double validator
00104             KDoubleValidator validator(0);
00105             const QString textResult = KInputDialog::getText( caption, (*it).message, QString::null, &ok, 
00106                 parent, 0, &validator);
00107             if (!ok || textResult.isEmpty())
00108                 return QValueList<QVariant>(); //cancelled
00111             const double result = textResult.toDouble(&ok); //this is also good for float (to avoid rounding)
00112             if (!ok)
00113                 return QValueList<QVariant>();
00114             values.append( result );
00115             break;
00116         }
00117         case KexiDB::Field::Text:
00118         case KexiDB::Field::LongText: {
00119             const QString result = KInputDialog::getText( 
00120                 caption, (*it).message, QString::null, &ok, parent);
00121             if (!ok)
00122                 return QValueList<QVariant>(); //cancelled
00123             values.append( result );
00124             break;
00125         }
00126         case KexiDB::Field::BLOB: {
00128             values.append( QByteArray() );
00129         }
00130         default:
00131             kexiwarn << "KexiQueryParameters::getParameters() unsupported type " << KexiDB::Field::typeName((*it).type)
00132             << " for parameter \"" << (*it).message << "\" - aborting query execution!" << endl;
00133             return QValueList<QVariant>();
00134         }
00135     }
00136     ok = true;
00137     return values;
00138 }
00139 
KDE Home | KDE Accessibility Home | Description of Access Keys