File indexing completed on 2024-05-12 16:40:56

0001 /* This file is part of the KDE project
0002    Copyright (C) 2006-2015 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kexiqueryparameters.h"
0021 #include <kexi_global.h>
0022 #include "utils/kexidatetimeformatter.h"
0023 
0024 #include <KDbQuerySchemaParameter>
0025 #include <KDbUtils>
0026 #include <KDbDriver>
0027 
0028 #include <KLocalizedString>
0029 
0030 #include <QDebug>
0031 #include <QInputDialog>
0032 
0033 // static
0034 QList<QVariant> KexiQueryParameters::getParameters(QWidget *parent, KDbConnection *conn,
0035                                                    KDbQuerySchema *querySchema, bool *ok)
0036 {
0037     Q_ASSERT(ok);
0038     Q_ASSERT(querySchema);
0039     *ok = false;
0040     const QList<KDbQuerySchemaParameter> params(querySchema->parameters(conn));
0041     QList<QVariant> values;
0042     const QString caption(xi18nc("@title:window Enter Query Parameter Value", "Enter Parameter Value"));
0043     foreach(const KDbQuerySchemaParameter &parameter, params) {
0044         switch (parameter.type()) {
0045         case KDbField::Byte:
0046         case KDbField::ShortInteger:
0047         case KDbField::Integer:
0048         case KDbField::BigInteger: {
0049 //! @todo problem for ranges in case of BigInteger - will disappear when we remove use of QInputDialog
0050             qlonglong minValue, maxValue;
0051 //! @todo add support for unsigned parameter here
0052             KDb::getLimitsForFieldType(parameter.type(), &minValue, &maxValue);
0053             const int result = QInputDialog::getInt(parent, caption, parameter.message(),
0054                                                     0, minValue, maxValue, 1/*step*/, ok);
0055             if (!*ok)
0056                 return QList<QVariant>(); //cancelled
0057             values.append(result);
0058             break;
0059         }
0060         case KDbField::Boolean: {
0061             QStringList list;
0062             list << xi18nc("Boolean True - Yes", "Yes") << xi18nc("Boolean False - No", "No");
0063             const QString result = QInputDialog::getItem(parent, caption, parameter.message(),
0064                                                          list, 0/*current*/, false /* !editable*/,
0065                                                          ok);
0066             if (!*ok || result.isEmpty())
0067                 return QList<QVariant>(); //cancelled
0068             values.append(result == list.first());
0069             break;
0070         }
0071         case KDbField::Date: {
0072             //! @todo KEXI3 re-add the KexiDateFormatter's inputMask for QInputDialog
0073             KexiDateFormatter df;
0074             const QString result = QInputDialog::getText(parent, caption, parameter.message(),
0075                                                          QLineEdit::Normal, QString(), ok);
0076             //! @todo KEXI3 add time validator
0077             // 0/*validator*/, df.inputMask());
0078             if (!*ok)
0079                 return QList<QVariant>(); //cancelled
0080             values.append(df.fromString(result));
0081             break;
0082         }
0083         case KDbField::DateTime: {
0084             //! @todo KEXI3 re-add the KexiDateTimeFormatter's inputMask for QInputDialog
0085             KexiDateFormatter df;
0086             KexiTimeFormatter tf;
0087             const QString result = QInputDialog::getText(parent, caption, parameter.message(),
0088                                                          QLineEdit::Normal, QString(), ok);
0089             //! @todo KEXI3 add date time validator
0090             // 0/*validator*/, KexiDateTimeFormatter::inputMask(df, tf));
0091             if (!*ok)
0092                 return QList<QVariant>(); //cancelled
0093             values.append(KexiDateTimeFormatter::fromString(df, tf, result));
0094             break;
0095         }
0096         case KDbField::Time: {
0097             //! @todo KEXI3 re-add the KexiTimeFormatter's inputMask for QInputDialog
0098             KexiTimeFormatter tf;
0099             const QString result = QInputDialog::getText(parent, caption, parameter.message(),
0100                                                          QLineEdit::Normal, QString(), ok);
0101 //! @todo add validator
0102             // 0/*validator*/, tf.inputMask());
0103             if (!*ok)
0104                 return QList<QVariant>(); //cancelled
0105             values.append(tf.fromString(result));
0106             break;
0107         }
0108         case KDbField::Float:
0109         case KDbField::Double: {
0110             // QInputDialog::getDouble() does not work well, use getText and double validator
0111             //! @todo KEXI3 re-add double validator
0112             // QDoubleValidator validator(0);
0113             const QString textResult
0114                 = QInputDialog::getText(parent, caption, parameter.message(), QLineEdit::Normal,
0115                                         QString(), ok);
0116             if (!*ok || textResult.isEmpty())
0117                 return QList<QVariant>(); //cancelled
0118 //! @todo this value will be still rounded: consider storing them as a decimal type
0119 //!    (e.g. using a special qint64+decimalplace class)
0120             const double result = textResult.toDouble(ok); //this is also good for float (to avoid rounding)
0121             if (!*ok)
0122                 return QList<QVariant>();
0123             values.append(result);
0124             break;
0125         }
0126         case KDbField::Text:
0127         case KDbField::LongText: {
0128             const QString result = QInputDialog::getText(parent, caption, parameter.message(),
0129                                                          QLineEdit::Normal, QString(), ok);
0130             if (!*ok)
0131                 return QList<QVariant>(); //cancelled
0132             values.append(result);
0133             break;
0134         }
0135         case KDbField::BLOB: {
0136 //! @todo BLOB input unsupported
0137             values.append(QByteArray());
0138             break;
0139         }
0140         default:
0141             qWarning() << "unsupported type " << KDbField::typeName(parameter.type())
0142                 << "for parameter \"" << parameter.message() << "\" - aborting query execution!";
0143             return QList<QVariant>();
0144         }
0145     }
0146     *ok = true;
0147     return values;
0148 }