diff --git a/kexi/plugins/reports/kexidbreportdata.h b/kexi/plugins/reports/kexidbreportdata.h --- a/kexi/plugins/reports/kexidbreportdata.h +++ b/kexi/plugins/reports/kexidbreportdata.h @@ -25,6 +25,7 @@ #include #include +#include #include /** @@ -65,10 +66,8 @@ virtual qint64 recordCount() const; //Utility Functions -#ifdef KREPORT_SCRIPTING - virtual QStringList scriptList(const QString& language) const; - virtual QString scriptCode(const QString& script, const QString& language) const; -#endif + virtual QStringList scriptList() const; + virtual QString scriptCode(const QString& script) const; virtual QStringList dataSources() const; virtual KoReportData* data(const QString&); diff --git a/kexi/plugins/reports/kexidbreportdata.cpp b/kexi/plugins/reports/kexidbreportdata.cpp --- a/kexi/plugins/reports/kexidbreportdata.cpp +++ b/kexi/plugins/reports/kexidbreportdata.cpp @@ -231,7 +231,7 @@ { int i = fieldNumber ( fld ); - if ( d->cursor ) + if (d->cursor && i >= 0) return d->cursor->value ( i ); return QVariant(); @@ -285,17 +285,21 @@ return 1; } -#ifdef KREPORT_SCRIPTING -QStringList KexiDBReportData::scriptList(const QString& interpreter) const +static bool isInterpreterSupported(const QString &interpreterName) +{ + return 0 == interpreterName.compare(QLatin1String("javascript"), Qt::CaseInsensitive) + || 0 == interpreterName.compare(QLatin1String("qtscript"), Qt::CaseInsensitive); +} + +QStringList KexiDBReportData::scriptList() const { QStringList scripts; if( d->connection) { QList scriptids = d->connection->objectIds(KexiPart::ScriptObjectType); QStringList scriptnames = d->connection->objectNames(KexiPart::ScriptObjectType); qDebug() << scriptids << scriptnames; - qDebug() << interpreter; //A blank entry scripts << ""; @@ -311,7 +315,9 @@ QDomElement scriptelem = domdoc.namedItem("script").toElement(); if (parsed && !scriptelem.isNull()) { - if (interpreter == scriptelem.attribute("language") && scriptelem.attribute("scripttype") == "object") { + if (scriptelem.attribute("scripttype") == "object" + && isInterpreterSupported(scriptelem.attribute("language"))) + { scripts << scriptnames[i]; } } else { @@ -328,7 +334,7 @@ return scripts; } -QString KexiDBReportData::scriptCode(const QString& scriptname, const QString& language) const +QString KexiDBReportData::scriptCode(const QString& scriptname) const { QString scripts; @@ -358,11 +364,12 @@ } QString interpretername = scriptelem.attribute("language"); - qDebug() << language << interpretername; qDebug() << scriptelem.attribute("scripttype"); qDebug() << scriptname << scriptnames[i]; - if (language == interpretername && (scriptelem.attribute("scripttype") == "module" || scriptname == scriptnames[i])) { + if ((isInterpreterSupported(interpretername) && scriptelem.attribute("scripttype") == "module") + || scriptname == scriptnames[i]) + { scripts += '\n' + scriptelem.text().toUtf8(); } ++i; @@ -373,7 +380,6 @@ } return scripts; } -#endif QStringList KexiDBReportData::dataSources() const { diff --git a/kexi/plugins/reports/kexireportview.h b/kexi/plugins/reports/kexireportview.h --- a/kexi/plugins/reports/kexireportview.h +++ b/kexi/plugins/reports/kexireportview.h @@ -23,6 +23,7 @@ #include +#include #include #include @@ -32,10 +33,8 @@ class KoReportPreRenderer; class ORODocument; class KReportView; -#ifdef KREPORT_SCRIPTING -class KexiScriptAdaptor; +//! @todo KEXI3 class KexiScriptAdaptor; class KRScriptFunctions; -#endif #ifndef KEXI_MOBILE class KexiRecordNavigator; #endif @@ -74,10 +73,8 @@ KexiReportPart::TempData* tempData() const; KoReportData* sourceData(QDomElement e); -#ifdef KREPORT_SCRIPTING - KexiScriptAdaptor *m_kexi; + //! @todo KEXI3 KexiScriptAdaptor *m_kexi; KRScriptFunctions *m_functions; -#endif KoReportRendererFactory m_factory; //! @todo KEXI3 add equivalent of kfiledialog:/// for lastExportPathOrVariable diff --git a/kexi/plugins/reports/kexireportview.cpp b/kexi/plugins/reports/kexireportview.cpp --- a/kexi/plugins/reports/kexireportview.cpp +++ b/kexi/plugins/reports/kexireportview.cpp @@ -31,13 +31,12 @@ #include #include -#ifdef KREPORT_SCRIPTING -#include "../scripting/kexiscripting/kexiscriptadaptor.h" -#endif +//! @todo KEXI3 #include "../scripting/kexiscripting/kexiscriptadaptor.h" #include #include #include +#include #include "krscriptfunctions.h" #include @@ -167,10 +166,6 @@ { qDebug(); delete m_preRenderer; -#ifdef KREPORT_SCRIPTING - delete m_kexi; - delete m_functions; -#endif delete m_reportDocument; } @@ -402,23 +397,18 @@ m_preRenderer->setName(window()->partItem()->name()); -#ifdef KREPORT_SCRIPTING //Add a kexi object to provide kexidb and extra functionality - if(!m_kexi) { - m_kexi = new KexiScriptAdaptor(); - } - m_preRenderer->registerScriptObject(m_kexi, "Kexi"); +//! @todo KEXI3 if we want this if(!m_kexi) { +// m_kexi = new KexiScriptAdaptor(); +// } +// m_preRenderer->registerScriptObject(m_kexi, "Kexi"); //If using a kexidb source, add a functions scripting object if (tempData()->connectionDefinition.attribute("type") == "internal") { - //Delete old functions - if (m_functions) { - delete m_functions; - } - m_functions = new KRScriptFunctions(reportData, KexiMainWindowIface::global()->project()->dbConnection()); m_preRenderer->registerScriptObject(m_functions, "field"); + connect(m_preRenderer, SIGNAL(groupChanged(QMap)), + m_functions, SLOT(setGroupData(QMap))); } -#endif if (m_reportDocument) { qDebug() << "=======================================Deleting old document"; diff --git a/kexi/plugins/reports/krscriptfunctions.h b/kexi/plugins/reports/krscriptfunctions.h --- a/kexi/plugins/reports/krscriptfunctions.h +++ b/kexi/plugins/reports/krscriptfunctions.h @@ -26,11 +26,12 @@ #include #include +#include /** @author */ -class KRScriptFunctions : public QObject +class KRScriptFunctions : public KReportGroupTracker { Q_OBJECT public: @@ -44,9 +45,12 @@ QString m_source; qreal math(const QString &, const QString &); - KDbEscapedString m_where; + QMap m_groupData; + + KDbEscapedString where(); + public Q_SLOTS: - void setWhere(const KDbEscapedString&); + virtual void setGroupData(const QMap &groupData); qreal sum(const QString &); qreal avg(const QString &); diff --git a/kexi/plugins/reports/krscriptfunctions.cpp b/kexi/plugins/reports/krscriptfunctions.cpp --- a/kexi/plugins/reports/krscriptfunctions.cpp +++ b/kexi/plugins/reports/krscriptfunctions.cpp @@ -28,38 +28,40 @@ { m_cursor = kodata; m_connection = conn; - m_source = kodata->sourceName(); + + if (kodata) { + m_source = kodata->sourceName(); + } } KRScriptFunctions::~KRScriptFunctions() { } -void KRScriptFunctions::setWhere(const KDbEscapedString&w) +void KRScriptFunctions::setGroupData(const QMap& groupData) { - m_where = w; + m_groupData = groupData; } qreal KRScriptFunctions::math(const QString &function, const QString &field) { - qreal ret; + QString ret = QLatin1String("0.0"); + + if (!m_connection) { + return 0.0; + } + KDbEscapedString sql = KDbEscapedString("SELECT " + function + "(" + field + ") FROM (" + m_source + ")"); - if (!m_where.isEmpty()) { - sql += " WHERE(" + m_where + ')'; + if (!m_groupData.isEmpty()) { + sql += " WHERE(" + where() + ')'; } qDebug() << sql; - KDbCursor *curs = m_connection->executeQuery(sql); - if (curs) { - ret = curs->value(0).toDouble(); - } else { - ret = 0.0; - } - delete curs; + m_connection->querySingleString(sql,&ret); - return ret; + return ret.toDouble(); } qreal KRScriptFunctions::sum(const QString &field) @@ -106,3 +108,15 @@ return val; } +KDbEscapedString KRScriptFunctions::where() +{ + QByteArray w; + QMap::const_iterator i = m_groupData.constBegin(); + while (i != m_groupData.constEnd()) { + w += QLatin1Char('(') + i.key() + QLatin1String(" = '") + i.value().toString() + QLatin1String("') AND "); + ++i; + } + w.chop(4); + //kreportDebug() << w; + return KDbEscapedString(w); +}