SqlQueryUtils

Since: BlackBerry 10.2.0

#include <bb/cascades/datamanager/SqlQueryUtils>

To link against this class, add the following line to your .pro file: LIBS += -lbbcascadesdatamanager

Utility methods for performing common operations in SQL data queries.


Overview

Public Functions Index

SqlQueryUtils ()
virtual ~SqlQueryUtils ()
QSqlDatabaseconnection (const QUrl &source, QSqlError *error)
QStringextendQuery (const QString &query, int offset, int limit)
boolgetQueryData (QSqlDatabase &connection, const QString &query, int offset, int limit, const QVariantMap &bindValues, const QString &keyColumn, const QString &revisionColumn, QList< DataItem > *results, QSqlError *error)
boolgetSingleQueryValue (QSqlDatabase &connection, const QString &query, const QVariantMap &bindValues, const QString &resultName, QVariant *resultValue, QSqlError *error)
QList< HeaderDataItem >normalizeHeaderData (const QList< DataItem > &data)
voidprepareQuery (const QString &query, const QVariantMap &bindValues, QSqlQuery *sqlQuery)

Public Functions

SqlQueryUtils ()

Constructs an SqlQueryUtils.

Since:

BlackBerry 10.2.0

virtual~SqlQueryUtils ()

Destructor.

Since:

BlackBerry 10.2.0

QSqlDatabase connection (

Return an existing or new database connection for the database at the source URL.

Parameters
source

The path to the local database.

error

The error object to update with the status. This pointer must not be null. If the method is successful, QSqlError::type() will be QSqlError::NoError.

Return:

The QSqlDatabase object to use as an open connection to the database.

Since:

BlackBerry 10.2.0

QString extendQuery (
  • const QString &query,
  • intoffset,
  • intlimit )

Extend the given query string with an offset and/or limit and return the modified string.

The offset cannot be used without a limit.

For example, this query:
extendQuery("select * from contact", 100, 200);
returns:
"select * from contact limit 200 offset 100"
Parameters
query

The SQL query to extend.

offset

The index offset within the view. A negative offset is ignored.

limit

The number of items to retrieve. A negative limit means both limit and offset are ignored.

Return:

The query string with offset and limit parameters added.

Since:

BlackBerry 10.2.0

bool getQueryData (

Execute the supplied SQL data query after applying a limit and an offset and binding any values.

Parameters
connection

The open database connection.

query

The SQL data query.

offset

The index offset within the view. A negative offset is ignored.

limit

The number of items to retrieve. A negative limit means both limit and offset are ignored.

bindValues

A map used to replace any named placeholders in the query with values.

keyColumn

The key column name used in the query. This can be empty.

revisionColumn

The revision column name used in the query. This can be empty.

results

The list of data items which is the return data. This pointer must not be null.

error

The error object to update with the status. This pointer must not be null.

Return:

True if the data is successfully retrieved, false otherwise.

Since:

BlackBerry 10.2.0

bool getSingleQueryValue (

Execute the supplied SQL query after binding any values and return a single result value.

If there are multiple rows and/or multiple columns returned by the query, the first column value from the first row is returned.

Parameters
connection

The open database connection.

query

The SQL query.

bindValues

A map used to replace any named placeholders in the query with values.

resultName

The name of the result column. If not provided, the first value is returned.

resultValue

The query result value being returned. This pointer must not be null.

error

The error object to update with the status. This pointer must not be null.

Return:

True if the data is successfully retrieved, false otherwise.

Since:

BlackBerry 10.2.0

QList< HeaderDataItem > normalizeHeaderData (

Use this method to normalize DataItem into HeaderDataItem.

If the header child count is zero, the item is removed.

Parameters
data

The list of DataItem objects to normalize.

Return:

A new list of HeaderDataItem objects.

Since:

BlackBerry 10.2.0

void prepareQuery (

Prepare the given QSqlQuery object using the query string and bindValues map.

Note:
This method does a better job of preparing the query and binding values than using the equivalent Qt code. Unlike the Qt code, this method works correctly:
  1. When a query has no parameters but bindValues is supplied.

  2. When a query uses the same named parameter more than once in query string.

Parameters
query

The SQL query string using a named placeholder such as :name.

bindValues

A map of names to values for supplying values to the query.

sqlQuery

The SQL query object to prepare.

Since:

BlackBerry 10.2.0

Last modified: 2014-06-24



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus