Qt 4.8
Classes | Macros | Enumerations | Functions | Variables
qdeclarativesqldatabase.cpp File Reference
#include "private/qdeclarativesqldatabase_p.h"
#include "qdeclarativeengine.h"
#include "private/qdeclarativeengine_p.h"
#include "private/qdeclarativerefcount_p.h"
#include <QtCore/qobject.h>
#include <QtScript/qscriptvalue.h>
#include <QtScript/qscriptvalueiterator.h>
#include <QtScript/qscriptcontext.h>
#include <QtScript/qscriptengine.h>
#include <QtScript/qscriptclasspropertyiterator.h>
#include <QtSql/qsqldatabase.h>
#include <QtSql/qsqlquery.h>
#include <QtSql/qsqlerror.h>
#include <QtSql/qsqlrecord.h>
#include <QtCore/qstack.h>
#include <QtCore/qcryptographichash.h>
#include <QtCore/qsettings.h>
#include <QtCore/qdir.h>
#include <QtCore/qdebug.h>

Go to the source code of this file.

Classes

class  QDeclarativeSqlQueryScriptClass
 

Macros

#define THROW_SQL(error, desc)
 

Enumerations

enum  SqlException {
  UNKNOWN_ERR, DATABASE_ERR, VERSION_ERR, TOO_LARGE_ERR,
  QUOTA_ERR, SYNTAX_ERR, CONSTRAINT_ERR, TIMEOUT_ERR
}
 

Functions

static QScriptValue qmlsqldatabase_change_version (QScriptContext *context, QScriptEngine *engine)
 
static QString qmlsqldatabase_databaseFile (const QString &connectionName, QScriptEngine *engine)
 
static QString qmlsqldatabase_databasesPath (QScriptEngine *engine)
 
static QScriptValue qmlsqldatabase_executeSql (QScriptContext *context, QScriptEngine *engine)
 
static QScriptValue qmlsqldatabase_executeSql_outsidetransaction (QScriptContext *context, QScriptEngine *)
 
static QScriptValue qmlsqldatabase_executeSql_readonly (QScriptContext *context, QScriptEngine *engine)
 
static void qmlsqldatabase_initDatabasesPath (QScriptEngine *engine)
 
static QScriptValue qmlsqldatabase_item (QScriptContext *context, QScriptEngine *engine)
 
static QScriptValue qmlsqldatabase_open_sync (QScriptContext *context, QScriptEngine *engine)
 
static QScriptValue qmlsqldatabase_read_transaction (QScriptContext *context, QScriptEngine *engine)
 
static QScriptValue qmlsqldatabase_transaction (QScriptContext *context, QScriptEngine *engine)
 
static QScriptValue qmlsqldatabase_transaction_shared (QScriptContext *context, QScriptEngine *engine, bool readOnly)
 
void qt_add_qmlsqldatabase (QScriptEngine *engine)
 

Variables

static const char * sqlerror []
 

Macro Definition Documentation

◆ THROW_SQL

#define THROW_SQL (   error,
  desc 
)
Value:
{ \
QScriptValue errorValue = context->throwError(desc); \
errorValue.setProperty(QLatin1String("code"), error); \
return errorValue; \
}
#define error(msg)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString

Definition at line 168 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_change_version(), qmlsqldatabase_executeSql(), qmlsqldatabase_executeSql_outsidetransaction(), qmlsqldatabase_executeSql_readonly(), qmlsqldatabase_open_sync(), and qmlsqldatabase_transaction_shared().

Enumeration Type Documentation

◆ SqlException

Enumerator
UNKNOWN_ERR 
DATABASE_ERR 
VERSION_ERR 
TOO_LARGE_ERR 
QUOTA_ERR 
SYNTAX_ERR 
CONSTRAINT_ERR 
TIMEOUT_ERR 

Definition at line 145 of file qdeclarativesqldatabase.cpp.

Function Documentation

◆ qmlsqldatabase_change_version()

static QScriptValue qmlsqldatabase_change_version ( QScriptContext context,
QScriptEngine engine 
)
static

Definition at line 273 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_open_sync().

274 {
275  if (context->argumentCount() < 2)
276  return engine->undefinedValue();
277 
279  QString from_version = context->argument(0).toString();
280  QString to_version = context->argument(1).toString();
281  QScriptValue callback = context->argument(2);
282 
283  QScriptValue instance = engine->newObject();
284  instance.setProperty(QLatin1String("executeSql"), engine->newFunction(qmlsqldatabase_executeSql,1));
285  QScriptValue tx = engine->newVariant(instance,QVariant::fromValue(db));
286 
287  QString foundvers = context->thisObject().property(QLatin1String("version")).toString();
288  if (from_version!=foundvers) {
289  THROW_SQL(VERSION_ERR,QDeclarativeEngine::tr("Version mismatch: expected %1, found %2").arg(from_version).arg(foundvers));
290  return engine->undefinedValue();
291  }
292 
293  bool ok = true;
294  if (callback.isFunction()) {
295  ok = false;
296  db.transaction();
297  callback.call(QScriptValue(), QScriptValueList() << tx);
298  if (engine->hasUncaughtException()) {
299  db.rollback();
300  } else {
301  if (!db.commit()) {
302  db.rollback();
303  THROW_SQL(UNKNOWN_ERR,QDeclarativeEngine::tr("SQL transaction failed"));
304  } else {
305  ok = true;
306  }
307  }
308  }
309 
310  if (ok) {
311  context->thisObject().setProperty(QLatin1String("version"), to_version, QScriptValue::ReadOnly);
312 #ifndef QT_NO_SETTINGS
314  ini.setValue(QLatin1String("Version"), to_version);
315 #endif
316  }
317 
318  return engine->undefinedValue();
319 }
QScriptValue newFunction(FunctionSignature signature, int length=0)
Creates a QScriptValue that wraps a native (C++) function.
void setValue(const QString &key, const QVariant &value)
Sets the value of setting key to value.
Definition: qsettings.cpp:3328
static void callback(AuServer *, AuEventHandlerRec *, AuEvent *e, AuPointer p)
Definition: qsound_x11.cpp:170
QScriptValue property(const QString &name, const ResolveFlags &mode=ResolvePrototype) const
Returns the value of this QScriptValue&#39;s property with the given name, using the given mode to resolv...
QList< QScriptValue > QScriptValueList
Definition: qscriptvalue.h:47
QString connectionName() const
Returns the connection name, which may be empty.
bool commit()
Commits a transaction to the database if the driver supports transactions and a transaction() has bee...
bool transaction()
Begins a transaction on the database if the driver supports transactions.
bool isFunction() const
Returns true if this QScriptValue is a function; otherwise returns false.
The QSettings class provides persistent platform-independent application settings.
Definition: qsettings.h:73
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
The QSqlDatabase class represents a connection to a database.
Definition: qsqldatabase.h:78
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
T qscriptvalue_cast(const QScriptValue &value)
Returns the given value converted to the template type T.
#define THROW_SQL(error, desc)
QScriptValue newObject()
Creates a QtScript object of class Object.
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
int argumentCount() const
Returns the number of arguments passed to the function in this invocation.
bool rollback()
Rolls back a transaction on the database, if the driver supports transactions and a transaction() has...
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
static QString qmlsqldatabase_databaseFile(const QString &connectionName, QScriptEngine *engine)
static QScriptValue qmlsqldatabase_executeSql(QScriptContext *context, QScriptEngine *engine)
QScriptValue undefinedValue()
Returns a QScriptValue of the primitive type Undefined.
QScriptValue thisObject() const
Returns the `this&#39; object associated with this QScriptContext.
bool hasUncaughtException() const
Returns true if the last script evaluation resulted in an uncaught exception; otherwise returns false...
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QScriptValue call(const QScriptValue &thisObject=QScriptValue(), const QScriptValueList &args=QScriptValueList())
Calls this QScriptValue as a function, using thisObject as the `this&#39; object in the function call...
QScriptValue argument(int index) const
Returns the function argument at the given index.
QScriptValue newVariant(const QVariant &value)
Creates a QtScript object holding the given variant value.

◆ qmlsqldatabase_databaseFile()

static QString qmlsqldatabase_databaseFile ( const QString connectionName,
QScriptEngine engine 
)
static

Definition at line 187 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_change_version(), and qmlsqldatabase_open_sync().

188 {
190  + connectionName;
191 }
static QString qmlsqldatabase_databasesPath(QScriptEngine *engine)
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831

◆ qmlsqldatabase_databasesPath()

static QString qmlsqldatabase_databasesPath ( QScriptEngine engine)
static

Definition at line 175 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_databaseFile(), and qmlsqldatabase_initDatabasesPath().

176 {
177  QDeclarativeScriptEngine *qmlengine = static_cast<QDeclarativeScriptEngine*>(engine);
179  + QDir::separator() + QLatin1String("Databases");
180 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static QChar separator()
Returns the native directory separator: "/" under Unix (including Mac OS X) and "\\" under Windows...
Definition: qdir.cpp:1831
static QString toNativeSeparators(const QString &pathName)
Returns pathName with the &#39;/&#39; separators converted to separators that are appropriate for the underly...
Definition: qdir.cpp:812

◆ qmlsqldatabase_executeSql()

static QScriptValue qmlsqldatabase_executeSql ( QScriptContext context,
QScriptEngine engine 
)
static

Definition at line 214 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_change_version(), qmlsqldatabase_executeSql_readonly(), and qmlsqldatabase_transaction_shared().

215 {
217  QString sql = context->argument(0).toString();
218  QSqlQuery query(db);
219  bool err = false;
220 
221  QScriptValue result;
222 
223  if (query.prepare(sql)) {
224  if (context->argumentCount() > 1) {
225  QScriptValue values = context->argument(1);
226  if (values.isObject()) {
227  if (values.isArray()) {
228  int size = values.property(QLatin1String("length")).toInt32();
229  for (int i = 0; i < size; ++i)
230  query.bindValue(i, values.property(i).toVariant());
231  } else {
232  for (QScriptValueIterator it(values); it.hasNext();) {
233  it.next();
234  query.bindValue(it.name(),it.value().toVariant());
235  }
236  }
237  } else {
238  query.bindValue(0,values.toVariant());
239  }
240  }
241  if (query.exec()) {
242  result = engine->newObject();
243  QDeclarativeScriptEngine *qmlengine = static_cast<QDeclarativeScriptEngine*>(engine);
244  if (!qmlengine->sqlQueryClass)
245  qmlengine->sqlQueryClass = new QDeclarativeSqlQueryScriptClass(engine);
246  QScriptValue rows = engine->newObject(qmlengine->sqlQueryClass);
247  rows.setData(engine->newVariant(QVariant::fromValue(query)));
249  result.setProperty(QLatin1String("rows"),rows);
250  result.setProperty(QLatin1String("rowsAffected"),query.numRowsAffected());
251  result.setProperty(QLatin1String("insertId"),query.lastInsertId().toString());
252  } else {
253  err = true;
254  }
255  } else {
256  err = true;
257  }
258  if (err)
259  THROW_SQL(DATABASE_ERR,query.lastError().text());
260  return result;
261 }
QScriptValue newFunction(FunctionSignature signature, int length=0)
Creates a QScriptValue that wraps a native (C++) function.
QScriptValue property(const QString &name, const ResolveFlags &mode=ResolvePrototype) const
Returns the value of this QScriptValue&#39;s property with the given name, using the given mode to resolv...
#define it(className, varName)
qint32 toInt32() const
Returns the signed 32-bit integer value of this QScriptValue, using the conversion rules described in...
The QSqlQuery class provides a means of executing and manipulating SQL statements.
Definition: qsqlquery.h:63
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
The QSqlDatabase class represents a connection to a database.
Definition: qsqldatabase.h:78
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
T qscriptvalue_cast(const QScriptValue &value)
Returns the given value converted to the template type T.
#define THROW_SQL(error, desc)
QVariant toVariant() const
Returns the QVariant value of this QScriptValue, if it can be converted to a QVariant; otherwise retu...
QScriptValue newObject()
Creates a QtScript object of class Object.
quint16 values[128]
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
int argumentCount() const
Returns the number of arguments passed to the function in this invocation.
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
static QScriptValue qmlsqldatabase_item(QScriptContext *context, QScriptEngine *engine)
bool isArray() const
Returns true if this QScriptValue is an object of the Array class; otherwise returns false...
QScriptValue thisObject() const
Returns the `this&#39; object associated with this QScriptContext.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QScriptValue argument(int index) const
Returns the function argument at the given index.
QScriptValue newVariant(const QVariant &value)
Creates a QtScript object holding the given variant value.
bool isObject() const
Returns true if this QScriptValue is of the Object type; otherwise returns false. ...
void setData(const QScriptValue &data)
Sets the internal data of this QScriptValue object.

◆ qmlsqldatabase_executeSql_outsidetransaction()

static QScriptValue qmlsqldatabase_executeSql_outsidetransaction ( QScriptContext context,
QScriptEngine  
)
static

Definition at line 209 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_transaction_shared().

210 {
211  THROW_SQL(DATABASE_ERR,QDeclarativeEngine::tr("executeSql called outside transaction()"));
212 }
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
#define THROW_SQL(error, desc)

◆ qmlsqldatabase_executeSql_readonly()

static QScriptValue qmlsqldatabase_executeSql_readonly ( QScriptContext context,
QScriptEngine engine 
)
static

Definition at line 263 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_transaction_shared().

264 {
265  QString sql = context->argument(0).toString();
266  if (sql.startsWith(QLatin1String("SELECT"),Qt::CaseInsensitive)) {
267  return qmlsqldatabase_executeSql(context,engine);
268  } else {
269  THROW_SQL(SYNTAX_ERR,QDeclarativeEngine::tr("Read-only Transaction"))
270  }
271 }
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define THROW_SQL(error, desc)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static QScriptValue qmlsqldatabase_executeSql(QScriptContext *context, QScriptEngine *engine)
QScriptValue argument(int index) const
Returns the function argument at the given index.

◆ qmlsqldatabase_initDatabasesPath()

static void qmlsqldatabase_initDatabasesPath ( QScriptEngine engine)
static

Definition at line 182 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_open_sync().

183 {
185 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
bool mkpath(const QString &dirPath) const
Creates the directory path dirPath.
Definition: qdir.cpp:1477
static QString qmlsqldatabase_databasesPath(QScriptEngine *engine)

◆ qmlsqldatabase_item()

static QScriptValue qmlsqldatabase_item ( QScriptContext context,
QScriptEngine engine 
)
static

Definition at line 194 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_executeSql().

195 {
196  QSqlQuery query = qscriptvalue_cast<QSqlQuery>(context->thisObject().data());
197  int i = context->argument(0).toNumber();
198  if (query.at() == i || query.seek(i)) { // Qt 4.6 doesn't optimize seek(at())
199  QSqlRecord r = query.record();
200  QScriptValue row = engine->newObject();
201  for (int j=0; j<r.count(); ++j) {
202  row.setProperty(r.fieldName(j), QScriptValue(engine,r.value(j).toString()));
203  }
204  return row;
205  }
206  return engine->undefinedValue();
207 }
QSqlRecord record() const
Returns a QSqlRecord containing the field information for the current query.
Definition: qsqlquery.cpp:858
The QSqlQuery class provides a means of executing and manipulating SQL statements.
Definition: qsqlquery.h:63
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
T qscriptvalue_cast(const QScriptValue &value)
Returns the given value converted to the template type T.
QScriptValue newObject()
Creates a QtScript object of class Object.
qsreal toNumber() const
Returns the number value of this QScriptValue, as defined in ECMA-262 section 9.3, "ToNumber".
QScriptValue data() const
Returns the internal data of this QScriptValue object.
int count() const
Returns the number of fields in the record.
Definition: qsqlrecord.cpp:573
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
bool seek(int i, bool relative=false)
Retrieves the record at position index, if available, and positions the query on the retrieved record...
Definition: qsqlquery.cpp:502
QScriptValue undefinedValue()
Returns a QScriptValue of the primitive type Undefined.
QScriptValue thisObject() const
Returns the `this&#39; object associated with this QScriptContext.
int at() const
Returns the current internal position of the query.
Definition: qsqlquery.cpp:420
QString fieldName(int i) const
Returns the name of the field at position index.
Definition: qsqlrecord.cpp:232
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QScriptValue argument(int index) const
Returns the function argument at the given index.
QVariant value(int i) const
Returns the value of the field located at position index in the record.
Definition: qsqlrecord.cpp:203

◆ qmlsqldatabase_open_sync()

static QScriptValue qmlsqldatabase_open_sync ( QScriptContext context,
QScriptEngine engine 
)
static

Definition at line 358 of file qdeclarativesqldatabase.cpp.

Referenced by qt_add_qmlsqldatabase().

359 {
360 #ifndef QT_NO_SETTINGS
362 
363  QSqlDatabase database;
364 
365  QString dbname = context->argument(0).toString();
366  QString dbversion = context->argument(1).toString();
367  QString dbdescription = context->argument(2).toString();
368  int dbestimatedsize = context->argument(3).toNumber();
369  QScriptValue dbcreationCallback = context->argument(4);
370 
372  md5.addData(dbname.toUtf8());
373  QString dbid(QLatin1String(md5.result().toHex()));
374 
375  QString basename = qmlsqldatabase_databaseFile(dbid, engine);
376  bool created = false;
377  QString version = dbversion;
378 
379  {
380  QSettings ini(basename+QLatin1String(".ini"),QSettings::IniFormat);
381 
382  if (QSqlDatabase::connectionNames().contains(dbid)) {
383  database = QSqlDatabase::database(dbid);
384  version = ini.value(QLatin1String("Version")).toString();
385  if (version != dbversion && !dbversion.isEmpty() && !version.isEmpty())
386  THROW_SQL(VERSION_ERR,QDeclarativeEngine::tr("SQL: database version mismatch"));
387  } else {
388  created = !QFile::exists(basename+QLatin1String(".sqlite"));
389  database = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), dbid);
390  if (created) {
391  ini.setValue(QLatin1String("Name"), dbname);
392  if (dbcreationCallback.isFunction())
393  version = QString();
394  ini.setValue(QLatin1String("Version"), version);
395  ini.setValue(QLatin1String("Description"), dbdescription);
396  ini.setValue(QLatin1String("EstimatedSize"), dbestimatedsize);
397  ini.setValue(QLatin1String("Driver"), QLatin1String("QSQLITE"));
398  } else {
399  if (!dbversion.isEmpty() && ini.value(QLatin1String("Version")) != dbversion) {
400  // Incompatible
401  THROW_SQL(VERSION_ERR,QDeclarativeEngine::tr("SQL: database version mismatch"));
402  }
403  version = ini.value(QLatin1String("Version")).toString();
404  }
405  database.setDatabaseName(basename+QLatin1String(".sqlite"));
406  }
407  if (!database.isOpen())
408  database.open();
409  }
410 
411  QScriptValue instance = engine->newObject();
412  instance.setProperty(QLatin1String("transaction"), engine->newFunction(qmlsqldatabase_transaction,1));
413  instance.setProperty(QLatin1String("readTransaction"), engine->newFunction(qmlsqldatabase_read_transaction,1));
414  instance.setProperty(QLatin1String("version"), version, QScriptValue::ReadOnly);
415  instance.setProperty(QLatin1String("changeVersion"), engine->newFunction(qmlsqldatabase_change_version,3));
416 
417  QScriptValue result = engine->newVariant(instance,QVariant::fromValue(database));
418 
419  if (created && dbcreationCallback.isFunction()) {
420  dbcreationCallback.call(QScriptValue(), QScriptValueList() << result);
421  }
422 
423  return result;
424 #else
425  return engine->undefinedValue();
426 #endif // QT_NO_SETTINGS
427 }
QScriptValue newFunction(FunctionSignature signature, int length=0)
Creates a QScriptValue that wraps a native (C++) function.
static QScriptValue qmlsqldatabase_transaction(QScriptContext *context, QScriptEngine *engine)
bool isOpen() const
Returns true if the database connection is currently open; otherwise returns false.
QList< QScriptValue > QScriptValueList
Definition: qscriptvalue.h:47
bool isFunction() const
Returns true if this QScriptValue is a function; otherwise returns false.
static QScriptValue qmlsqldatabase_change_version(QScriptContext *context, QScriptEngine *engine)
static QStringList connectionNames()
Returns a list containing the names of all connections.
The QSettings class provides persistent platform-independent application settings.
Definition: qsettings.h:73
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
The QSqlDatabase class represents a connection to a database.
Definition: qsqldatabase.h:78
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static QScriptValue qmlsqldatabase_read_transaction(QScriptContext *context, QScriptEngine *engine)
The QString class provides a Unicode character string.
Definition: qstring.h:83
static void qmlsqldatabase_initDatabasesPath(QScriptEngine *engine)
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
#define THROW_SQL(error, desc)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QSqlDatabase database(const QString &connectionName=QLatin1String(defaultConnection), bool open=true)
Returns the database connection called connectionName.
QScriptValue newObject()
Creates a QtScript object of class Object.
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
qsreal toNumber() const
Returns the number value of this QScriptValue, as defined in ECMA-262 section 9.3, "ToNumber".
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
static QString qmlsqldatabase_databaseFile(const QString &connectionName, QScriptEngine *engine)
static QSqlDatabase addDatabase(const QString &type, const QString &connectionName=QLatin1String(defaultConnection))
Adds a database to the list of database connections using the driver type and the connection name con...
QScriptValue undefinedValue()
Returns a QScriptValue of the primitive type Undefined.
void setDatabaseName(const QString &name)
Sets the connection&#39;s database name to name.
bool open()
Opens the database connection using the current connection values.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QScriptValue call(const QScriptValue &thisObject=QScriptValue(), const QScriptValueList &args=QScriptValueList())
Calls this QScriptValue as a function, using thisObject as the `this&#39; object in the function call...
QScriptValue argument(int index) const
Returns the function argument at the given index.
QScriptValue newVariant(const QVariant &value)
Creates a QtScript object holding the given variant value.
The QCryptographicHash class provides a way to generate cryptographic hashes.

◆ qmlsqldatabase_read_transaction()

static QScriptValue qmlsqldatabase_read_transaction ( QScriptContext context,
QScriptEngine engine 
)
static

Definition at line 350 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_open_sync().

351 {
352  return qmlsqldatabase_transaction_shared(context,engine,true);
353 }
static QScriptValue qmlsqldatabase_transaction_shared(QScriptContext *context, QScriptEngine *engine, bool readOnly)

◆ qmlsqldatabase_transaction()

static QScriptValue qmlsqldatabase_transaction ( QScriptContext context,
QScriptEngine engine 
)
static

Definition at line 346 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_open_sync().

347 {
348  return qmlsqldatabase_transaction_shared(context,engine,false);
349 }
static QScriptValue qmlsqldatabase_transaction_shared(QScriptContext *context, QScriptEngine *engine, bool readOnly)

◆ qmlsqldatabase_transaction_shared()

static QScriptValue qmlsqldatabase_transaction_shared ( QScriptContext context,
QScriptEngine engine,
bool  readOnly 
)
static

Definition at line 321 of file qdeclarativesqldatabase.cpp.

Referenced by qmlsqldatabase_read_transaction(), and qmlsqldatabase_transaction().

322 {
324  QScriptValue callback = context->argument(0);
325  if (!callback.isFunction())
326  THROW_SQL(UNKNOWN_ERR,QDeclarativeEngine::tr("transaction: missing callback"));
327 
328  QScriptValue instance = engine->newObject();
329  instance.setProperty(QLatin1String("executeSql"),
331  QScriptValue tx = engine->newVariant(instance,QVariant::fromValue(db));
332 
333  db.transaction();
334  callback.call(QScriptValue(), QScriptValueList() << tx);
335  instance.setProperty(QLatin1String("executeSql"),
337  if (engine->hasUncaughtException()) {
338  db.rollback();
339  } else {
340  if (!db.commit())
341  db.rollback();
342  }
343  return engine->undefinedValue();
344 }
QScriptValue newFunction(FunctionSignature signature, int length=0)
Creates a QScriptValue that wraps a native (C++) function.
static void callback(AuServer *, AuEventHandlerRec *, AuEvent *e, AuPointer p)
Definition: qsound_x11.cpp:170
QList< QScriptValue > QScriptValueList
Definition: qscriptvalue.h:47
bool commit()
Commits a transaction to the database if the driver supports transactions and a transaction() has bee...
bool transaction()
Begins a transaction on the database if the driver supports transactions.
The QSqlDatabase class represents a connection to a database.
Definition: qsqldatabase.h:78
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
T qscriptvalue_cast(const QScriptValue &value)
Returns the given value converted to the template type T.
#define THROW_SQL(error, desc)
QScriptValue newObject()
Creates a QtScript object of class Object.
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
static QScriptValue qmlsqldatabase_executeSql_outsidetransaction(QScriptContext *context, QScriptEngine *)
bool rollback()
Rolls back a transaction on the database, if the driver supports transactions and a transaction() has...
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
static QScriptValue qmlsqldatabase_executeSql(QScriptContext *context, QScriptEngine *engine)
QScriptValue undefinedValue()
Returns a QScriptValue of the primitive type Undefined.
QScriptValue thisObject() const
Returns the `this&#39; object associated with this QScriptContext.
bool hasUncaughtException() const
Returns true if the last script evaluation resulted in an uncaught exception; otherwise returns false...
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
QScriptValue argument(int index) const
Returns the function argument at the given index.
QScriptValue newVariant(const QVariant &value)
Creates a QtScript object holding the given variant value.
static QScriptValue qmlsqldatabase_executeSql_readonly(QScriptContext *context, QScriptEngine *engine)

◆ qt_add_qmlsqldatabase()

void qt_add_qmlsqldatabase ( QScriptEngine engine)

Definition at line 429 of file qdeclarativesqldatabase.cpp.

Referenced by QDeclarativeScriptEngine::QDeclarativeScriptEngine().

430 {
431  QScriptValue openDatabase = engine->newFunction(qmlsqldatabase_open_sync, 4);
432  engine->globalObject().setProperty(QLatin1String("openDatabaseSync"), openDatabase);
433 
434  QScriptValue sqlExceptionPrototype = engine->newObject();
435  for (int i=0; sqlerror[i]; ++i)
436  sqlExceptionPrototype.setProperty(QLatin1String(sqlerror[i]),
438 
439  engine->globalObject().setProperty(QLatin1String("SQLException"), sqlExceptionPrototype);
440 }
QScriptValue newFunction(FunctionSignature signature, int length=0)
Creates a QScriptValue that wraps a native (C++) function.
static QScriptValue qmlsqldatabase_open_sync(QScriptContext *context, QScriptEngine *engine)
QScriptValue globalObject() const
Returns this engine&#39;s Global Object.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QScriptValue newObject()
Creates a QtScript object of class Object.
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
static const char * sqlerror[]

Variable Documentation

◆ sqlerror

const char* sqlerror[]
static
Initial value:
= {
"UNKNOWN_ERR",
"DATABASE_ERR",
"VERSION_ERR",
"TOO_LARGE_ERR",
"QUOTA_ERR",
"SYNTAX_ERR",
"CONSTRAINT_ERR",
"TIMEOUT_ERR",
0
}

Definition at line 156 of file qdeclarativesqldatabase.cpp.

Referenced by qt_add_qmlsqldatabase().