summaryrefslogtreecommitdiffstats
path: root/kexi/widget/kexiqueryparameters.cpp
blob: 449c265c557b44d23353e25b0100fac59fdc042f (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
/* This file is part of the KDE project
   Copyright (C) 2006 Jaroslaw Staniek <[email protected]>

   This library 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 library 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 library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexiqueryparameters.h"

#include <kdebug.h>
#include <klocale.h>
#include <kinputdialog.h>
#include <knumvalidator.h>

#include <kexidb/queryschemaparameter.h>
#include <kexidb/utils.h>
#include "utils/kexidatetimeformatter.h"

//static
QValueList<QVariant> KexiQueryParameters::getParameters(QWidget *parent, 
	const KexiDB::Driver &driver, KexiDB::QuerySchema& querySchema, bool &ok)
{
	Q_UNUSED(driver);
	ok = false;
	const KexiDB::QuerySchemaParameterList params( querySchema.parameters() );
	QValueList<QVariant> values;
	const QString caption( i18n("Enter Query Parameter Value", "Enter Parameter Value") );
	foreach(KexiDB::QuerySchemaParameterListConstIterator, it, params) {
		switch ((*it).type) {
		case KexiDB::Field::Byte:
		case KexiDB::Field::ShortInteger:
		case KexiDB::Field::Integer:
		case KexiDB::Field::BigInteger: {
//! @todo problem for ranges in case of BigInteger - will disappear when we remove use of KInputDialog
			int minValue, maxValue;
//! @todo add support for unsigned parameter here
			KexiDB::getLimitsForType((*it).type, minValue, maxValue);
			const int result = KInputDialog::getInteger(
				caption, (*it).message, 0, minValue, maxValue, 1/*step*/, 10/*base*/, &ok, parent);
			if (!ok)
				return QValueList<QVariant>(); //cancelled
			values.append(result);
			break;
		}
		case KexiDB::Field::Boolean: {
			QStringList list;
			list << i18n("Boolean True - Yes", "Yes") << i18n("Boolean False - No", "No");
			const QString result = KInputDialog::getItem(
				caption, (*it).message, list, 0/*current*/, false /*!editable*/, &ok, parent);
			if (!ok || result.isEmpty())
				return QValueList<QVariant>(); //cancelled
			values.append( QVariant( result==list.first(), 1 ) );
			break;
		}
		case KexiDB::Field::Date: {
			KexiDateFormatter df;
			const QString result = KInputDialog::getText( 
				caption, (*it).message, QString::null, &ok, parent, 0/*name*/, 
//! @todo add validator
				0/*validator*/, df.inputMask() );
			if (!ok)
				return QValueList<QVariant>(); //cancelled
			values.append( df.stringToDate(result) );
			break;
		}
		case KexiDB::Field::DateTime: {
			KexiDateFormatter df;
			KexiTimeFormatter tf;
			const QString result = KInputDialog::getText( 
				caption, (*it).message, QString::null, &ok, parent, 0/*name*/, 
//! @todo add validator
				0/*validator*/, dateTimeInputMask(df, tf) );
			if (!ok)
				return QValueList<QVariant>(); //cancelled
			values.append( stringToDateTime(df, tf, result) );
			break;
		}
		case KexiDB::Field::Time: {
			KexiTimeFormatter tf;
			const QString result = KInputDialog::getText( 
				caption, (*it).message, QString::null, &ok, parent, 0/*name*/, 
//! @todo add validator
				0/*validator*/, tf.inputMask() );
			if (!ok)
				return QValueList<QVariant>(); //cancelled
			values.append( tf.stringToTime(result) );
			break;
		}
		case KexiDB::Field::Float:
		case KexiDB::Field::Double: {
			// KInputDialog::getDouble() does not work well, use getText and double validator
			KDoubleValidator validator(0);
			const QString textResult = KInputDialog::getText( caption, (*it).message, QString::null, &ok, 
				parent, 0, &validator);
			if (!ok || textResult.isEmpty())
				return QValueList<QVariant>(); //cancelled
//! @todo this value will be still rounded: consider storing them as a decimal type 
//! 			(e.g. using a special Q_LLONG+decimalplace class)
			const double result = textResult.toDouble(&ok); //this is also good for float (to avoid rounding)
			if (!ok)
				return QValueList<QVariant>();
			values.append( result );
			break;
		}
		case KexiDB::Field::Text:
		case KexiDB::Field::LongText: {
			const QString result = KInputDialog::getText( 
				caption, (*it).message, QString::null, &ok, parent);
			if (!ok)
				return QValueList<QVariant>(); //cancelled
			values.append( result );
			break;
		}
		case KexiDB::Field::BLOB: {
//! @todo BLOB input unsupported
			values.append( QByteArray() );
		}
		default:
			kexiwarn << "KexiQueryParameters::getParameters() unsupported type " << KexiDB::Field::typeName((*it).type)
			<< " for parameter \"" << (*it).message << "\" - aborting query execution!" << endl;
			return QValueList<QVariant>();
		}
	}
	ok = true;
	return values;
}