summaryrefslogtreecommitdiffstats
path: root/src/declarative/qml/qmlsqldatabase.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/qml/qmlsqldatabase.cpp')
-rw-r--r--src/declarative/qml/qmlsqldatabase.cpp148
1 files changed, 31 insertions, 117 deletions
diff --git a/src/declarative/qml/qmlsqldatabase.cpp b/src/declarative/qml/qmlsqldatabase.cpp
index 9e4c198..d11e3cb 100644
--- a/src/declarative/qml/qmlsqldatabase.cpp
+++ b/src/declarative/qml/qmlsqldatabase.cpp
@@ -63,8 +63,6 @@
Q_DECLARE_METATYPE(QSqlDatabase)
Q_DECLARE_METATYPE(QSqlQuery)
-class QmlSqlQueryScriptClassPropertyIterator;
-
class QmlSqlQueryScriptClass: public QScriptClass {
public:
QmlSqlQueryScriptClass(QScriptEngine *engine) : QScriptClass(engine)
@@ -75,22 +73,13 @@ public:
QueryFlags queryProperty(const QScriptValue &object,
const QScriptString &name,
- QueryFlags flags, uint *id)
+ QueryFlags flags, uint *)
{
if (flags & HandlesReadAccess) {
if (name == str_length) {
return HandlesReadAccess;
} else if (name == str_forwardOnly) {
return flags;
- } else {
- bool ok;
- qint32 pos = name.toString().toInt(&ok);
- if (pos < 0 || !ok)
- return 0;
- QSqlQuery query = qscriptvalue_cast<QSqlQuery>(object.data());
- *id = pos;
- if (*id < (uint)query.size())
- return HandlesReadAccess;
}
}
if (flags & HandlesWriteAccess)
@@ -100,29 +89,23 @@ public:
}
QScriptValue property(const QScriptValue &object,
- const QScriptString &name, uint id)
+ const QScriptString &name, uint)
{
QSqlQuery query = qscriptvalue_cast<QSqlQuery>(object.data());
if (name == str_length) {
int s = query.size();
if (s<0) {
// Inefficient.
- query.last();
- return query.at()+1;
+ if (query.last()) {
+ return query.at()+1;
+ } else {
+ return 0;
+ }
} else {
return s;
}
} else if (name == str_forwardOnly) {
return query.isForwardOnly();
- } else {
- if ((uint)query.at() == id || query.seek(id)) { // Qt 4.6 doesn't optimize seek(at())
- QSqlRecord r = query.record();
- QScriptValue row = engine()->newObject();
- for (int j=0; j<r.count(); ++j) {
- row.setProperty(r.fieldName(j), QScriptValue(engine(),r.value(j).toString()));
- }
- return row;
- }
}
return engine()->undefinedValue();
}
@@ -145,81 +128,15 @@ public:
return QScriptValue::Undeletable;
}
- QScriptClassPropertyIterator *newIterator(const QScriptValue &object);
-
private:
QScriptString str_length;
QScriptString str_forwardOnly;
};
-class QmlSqlQueryScriptClassPropertyIterator : public QScriptClassPropertyIterator
-{
-public:
- QmlSqlQueryScriptClassPropertyIterator(const QScriptValue &object)
- : QScriptClassPropertyIterator(object)
- {
- toFront();
- }
+// If the spec changes to allow iteration, check git history...
+// class QmlSqlQueryScriptClassPropertyIterator : public QScriptClassPropertyIterator
- ~QmlSqlQueryScriptClassPropertyIterator()
- {
- }
-
- bool hasNext() const
- {
- QSqlQuery query = qscriptvalue_cast<QSqlQuery>(object().data());
- return query.at() == m_index || query.seek(m_index); // Qt 4.6 doesn't optimize seek(at())
- }
- void next()
- {
- m_last = m_index;
- ++m_index;
- }
-
- bool hasPrevious() const
- {
- return (m_index > 0);
- }
-
- void previous()
- {
- --m_index;
- m_last = m_index;
- }
-
- void toFront()
- {
- m_index = 0;
- m_last = -1;
- }
-
- void toBack()
- {
- QSqlQuery query = qscriptvalue_cast<QSqlQuery>(object().data());
- m_index = query.size();
- m_last = -1;
- }
-
- QScriptString name() const
- {
- return object().engine()->toStringHandle(QString::number(m_last));
- }
-
- uint id() const
- {
- return m_last;
- }
-
-private:
- int m_index;
- int m_last;
-};
-
-QScriptClassPropertyIterator *QmlSqlQueryScriptClass::newIterator(const QScriptValue &object)
-{
- return new QmlSqlQueryScriptClassPropertyIterator(object);
-}
enum SqlException {
UNKNOWN_ERR,
@@ -323,11 +240,10 @@ static QScriptValue qmlsqldatabase_executeSql_readonly(QScriptContext *context,
{
QString sql = context->argument(0).toString();
if (sql.startsWith(QLatin1String("SELECT"),Qt::CaseInsensitive)) {
- qmlsqldatabase_executeSql(context,engine);
+ return qmlsqldatabase_executeSql(context,engine);
} else {
THROW_SQL(SYNTAX_ERR,QmlEngine::tr("Read-only Transaction"))
}
- return engine->undefinedValue();
}
static QScriptValue qmlsqldatabase_change_version(QScriptContext *context, QScriptEngine *engine)
@@ -349,7 +265,9 @@ static QScriptValue qmlsqldatabase_change_version(QScriptContext *context, QScri
return engine->undefinedValue();
}
+ bool ok = true;
if (callback.isFunction()) {
+ ok = false;
db.transaction();
callback.call(QScriptValue(), QScriptValueList() << tx);
if (engine->hasUncaughtException()) {
@@ -359,17 +277,21 @@ static QScriptValue qmlsqldatabase_change_version(QScriptContext *context, QScri
db.rollback();
THROW_SQL(0,QmlEngine::tr("SQL transaction failed"));
} else {
- context->thisObject().setProperty(QLatin1String("version"), to_version, QScriptValue::ReadOnly);
- QSettings ini(databaseFile(db.connectionName(),engine)+QLatin1String(".ini"),QSettings::IniFormat);
- ini.setValue(QLatin1String("Version"), to_version);
+ ok = true;
}
}
}
+ if (ok) {
+ context->thisObject().setProperty(QLatin1String("version"), to_version, QScriptValue::ReadOnly);
+ QSettings ini(databaseFile(db.connectionName(),engine)+QLatin1String(".ini"),QSettings::IniFormat);
+ ini.setValue(QLatin1String("Version"), to_version);
+ }
+
return engine->undefinedValue();
}
-static QScriptValue qmlsqldatabase_transaction(QScriptContext *context, QScriptEngine *engine)
+static QScriptValue qmlsqldatabase_transaction_shared(QScriptContext *context, QScriptEngine *engine, bool readOnly)
{
QSqlDatabase db = qscriptvalue_cast<QSqlDatabase>(context->thisObject());
QScriptValue callback = context->argument(0);
@@ -377,7 +299,8 @@ static QScriptValue qmlsqldatabase_transaction(QScriptContext *context, QScriptE
THROW_SQL(UNKNOWN_ERR,QmlEngine::tr("transaction: missing callback"));
QScriptValue instance = engine->newObject();
- instance.setProperty(QLatin1String("executeSql"), engine->newFunction(qmlsqldatabase_executeSql,1));
+ instance.setProperty(QLatin1String("executeSql"),
+ engine->newFunction(readOnly ? qmlsqldatabase_executeSql_readonly : qmlsqldatabase_executeSql,1));
QScriptValue tx = engine->newVariant(instance,qVariantFromValue(db));
db.transaction();
@@ -391,26 +314,13 @@ static QScriptValue qmlsqldatabase_transaction(QScriptContext *context, QScriptE
return engine->undefinedValue();
}
+static QScriptValue qmlsqldatabase_transaction(QScriptContext *context, QScriptEngine *engine)
+{
+ return qmlsqldatabase_transaction_shared(context,engine,false);
+}
static QScriptValue qmlsqldatabase_read_transaction(QScriptContext *context, QScriptEngine *engine)
{
- QSqlDatabase db = qscriptvalue_cast<QSqlDatabase>(context->thisObject());
- QScriptValue callback = context->argument(0);
- if (!callback.isFunction())
- return engine->undefinedValue();
-
- QScriptValue instance = engine->newObject();
- instance.setProperty(QLatin1String("executeSql"), engine->newFunction(qmlsqldatabase_executeSql_readonly,1));
- QScriptValue tx = engine->newVariant(instance,qVariantFromValue(db));
-
- db.transaction();
- callback.call(QScriptValue(), QScriptValueList() << tx);
- if (engine->hasUncaughtException()) {
- db.rollback();
- } else {
- if (!db.commit())
- db.rollback();
- }
- return engine->undefinedValue();
+ return qmlsqldatabase_transaction_shared(context,engine,true);
}
/*
@@ -439,6 +349,9 @@ static QScriptValue qmlsqldatabase_open_sync(QScriptContext *context, QScriptEng
if (QSqlDatabase::connectionNames().contains(dbid)) {
database = QSqlDatabase::database(dbid);
+ version = ini.value(QLatin1String("Version")).toString();
+ if (version != dbversion && !dbversion.isEmpty() && !version.isEmpty())
+ THROW_SQL(VERSION_ERR,QmlEngine::tr("SQL: database version mismatch"));
} else {
created = !QFile::exists(basename+QLatin1String(".sqlite"));
database = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), dbid);
@@ -456,6 +369,7 @@ static QScriptValue qmlsqldatabase_open_sync(QScriptContext *context, QScriptEng
// Incompatible
THROW_SQL(VERSION_ERR,QmlEngine::tr("SQL: database version mismatch"));
}
+ version = ini.value("Version").toString();
}
database.setDatabaseName(basename+QLatin1String(".sqlite"));
}