Qt 4.8
Classes | Functions
qsql_sqlite.cpp File Reference
#include "qsql_sqlite.h"
#include <qcoreapplication.h>
#include <qvariant.h>
#include <qsqlerror.h>
#include <qsqlfield.h>
#include <qsqlindex.h>
#include <qsqlquery.h>
#include <qstringlist.h>
#include <qvector.h>
#include <qdebug.h>
#include <qt_windows.h>
#include <sqlite3.h>

Go to the source code of this file.

Classes

class  QSQLiteDriverPrivate
 
class  QSQLiteResultPrivate
 

Functions

static QString _q_escapeIdentifier (const QString &identifier)
 
static QVariant::Type qGetColumnType (const QString &tpName)
 
static QSqlIndex qGetTableInfo (QSqlQuery &q, const QString &tableName, bool onlyPIndex=false)
 
static QSqlError qMakeError (sqlite3 *access, const QString &descr, QSqlError::ErrorType type, int errorCode=-1)
 

Function Documentation

◆ _q_escapeIdentifier()

static QString _q_escapeIdentifier ( const QString identifier)
static

Definition at line 67 of file qsql_sqlite.cpp.

Referenced by QSQLiteDriver::escapeIdentifier(), and qGetTableInfo().

68 {
69  QString res = identifier;
70  if(!identifier.isEmpty() && identifier.left(1) != QString(QLatin1Char('"')) && identifier.right(1) != QString(QLatin1Char('"')) ) {
71  res.replace(QLatin1Char('"'), QLatin1String("\"\""));
72  res.prepend(QLatin1Char('"')).append(QLatin1Char('"'));
73  res.replace(QLatin1Char('.'), QLatin1String("\".\""));
74  }
75  return res;
76 }
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
QString & prepend(QChar c)
Definition: qstring.h:261
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
QString right(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n rightmost characters of the string.
Definition: qstring.cpp:3682
QString & append(QChar c)
Definition: qstring.cpp:1777
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qGetColumnType()

static QVariant::Type qGetColumnType ( const QString tpName)
static

Definition at line 78 of file qsql_sqlite.cpp.

Referenced by QSQLiteResultPrivate::initColumns(), and qGetTableInfo().

79 {
80  const QString typeName = tpName.toLower();
81 
82  if (typeName == QLatin1String("integer")
83  || typeName == QLatin1String("int"))
84  return QVariant::Int;
85  if (typeName == QLatin1String("double")
86  || typeName == QLatin1String("float")
87  || typeName == QLatin1String("real")
88  || typeName.startsWith(QLatin1String("numeric")))
89  return QVariant::Double;
90  if (typeName == QLatin1String("blob"))
91  return QVariant::ByteArray;
92  return QVariant::String;
93 }
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
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
const char * typeName
Definition: qmetatype.cpp:239
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389

◆ qGetTableInfo()

static QSqlIndex qGetTableInfo ( QSqlQuery q,
const QString tableName,
bool  onlyPIndex = false 
)
static

Definition at line 681 of file qsql_sqlite.cpp.

Referenced by QSQLiteDriver::primaryIndex(), qExtractSecurityPolicyFromString(), and QSQLiteDriver::record().

682 {
683  QString schema;
684  QString table(tableName);
685  int indexOfSeparator = tableName.indexOf(QLatin1Char('.'));
686  if (indexOfSeparator > -1) {
687  schema = tableName.left(indexOfSeparator).append(QLatin1Char('.'));
688  table = tableName.mid(indexOfSeparator + 1);
689  }
690  q.exec(QLatin1String("PRAGMA ") + schema + QLatin1String("table_info (") + _q_escapeIdentifier(table) + QLatin1String(")"));
691 
692  QSqlIndex ind;
693  while (q.next()) {
694  bool isPk = q.value(5).toInt();
695  if (onlyPIndex && !isPk)
696  continue;
698  QSqlField fld(q.value(1).toString(), qGetColumnType(typeName));
699  if (isPk && (typeName == QLatin1String("integer")))
700  // INTEGER PRIMARY KEY fields are auto-generated in sqlite
701  // INT PRIMARY KEY is not the same as INTEGER PRIMARY KEY!
702  fld.setAutoValue(true);
703  fld.setRequired(q.value(3).toInt() != 0);
704  fld.setDefaultValue(q.value(4));
705  ind.append(fld);
706  }
707  return ind;
708 }
The QSqlIndex class provides functions to manipulate and describe database indexes.
Definition: qsqlindex.h:55
static QVariant::Type qGetColumnType(const QString &tpName)
Definition: qsql_sqlite.cpp:78
void append(const QSqlField &field)
Appends the field field to the list of indexed fields.
Definition: qsqlindex.cpp:129
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QString _q_escapeIdentifier(const QString &identifier)
Definition: qsql_sqlite.cpp:67
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
const char * typeName
Definition: qmetatype.cpp:239
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
QString & append(QChar c)
Definition: qstring.cpp:1777
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56
bool exec(const QString &query)
Executes the SQL in query.
Definition: qsqlquery.cpp:355
void setAutoValue(bool autoVal)
Marks the field as an auto-generated value if autoVal is true.
Definition: qsqlfield.cpp:575
QVariant value(int i) const
Returns the value of field index in the current record.
Definition: qsqlquery.cpp:403
bool next()
Retrieves the next record in the result, if available, and positions the query on the retrieved recor...
Definition: qsqlquery.cpp:594
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qMakeError()

static QSqlError qMakeError ( sqlite3 *  access,
const QString descr,
QSqlError::ErrorType  type,
int  errorCode = -1 
)
static

Definition at line 95 of file qsql_sqlite.cpp.

Referenced by QSQLiteDriver::close(), QSQLiteResult::exec(), QSQLiteResultPrivate::fetchNext(), QSQLiteDriver::open(), and QSQLiteResult::prepare().

97 {
98  return QSqlError(descr,
99  QString(reinterpret_cast<const QChar *>(sqlite3_errmsg16(access))),
100  type, errorCode);
101 }
The QSqlError class provides SQL database error information.
Definition: qsqlerror.h:53
int type
Definition: qmetatype.cpp:239
The QString class provides a Unicode character string.
Definition: qstring.h:83
int access(const char *, int)