Qt 4.8
Classes | Macros | Functions | Variables
qsql_mysql.cpp File Reference
#include "qsql_mysql.h"
#include <qcoreapplication.h>
#include <qvariant.h>
#include <qdatetime.h>
#include <qsqlerror.h>
#include <qsqlfield.h>
#include <qsqlindex.h>
#include <qsqlquery.h>
#include <qsqlrecord.h>
#include <qstringlist.h>
#include <qtextcodec.h>
#include <qvector.h>
#include <qdebug.h>
#include "qsql_mysql.moc"

Go to the source code of this file.

Classes

class  QMYSQLDriverPrivate
 
class  QMYSQLResultPrivate
 
struct  QMYSQLResultPrivate::QMyField
 

Macros

#define Q_CLIENT_MULTI_STATEMENTS   0
 

Functions

static QTextCodeccodec (MYSQL *mysql)
 
static QByteArray fromUnicode (QTextCodec *tc, const QString &str)
 
static QVariant qDateFromString (const QString &val)
 
static QVariant qDateTimeFromString (QString &val)
 
static QVariant::Type qDecodeMYSQLType (int mysqltype, uint flags)
 
static void qLibraryEnd ()
 
static void qLibraryInit ()
 
static QSqlError qMakeError (const QString &err, QSqlError::ErrorType type, const QMYSQLDriverPrivate *p)
 
static QVariant qTimeFromString (const QString &val)
 
static QSqlField qToField (MYSQL_FIELD *field, QTextCodec *tc)
 
static void setOptionFlag (uint &optionFlags, const QString &opt)
 
static QString toUnicode (QTextCodec *tc, const char *str)
 
static QString toUnicode (QTextCodec *tc, const char *str, int length)
 

Variables

static int qMySqlConnectionCount = 0
 
static bool qMySqlInitHandledByUser = false
 

Macro Definition Documentation

◆ Q_CLIENT_MULTI_STATEMENTS

#define Q_CLIENT_MULTI_STATEMENTS   0

Definition at line 74 of file qsql_mysql.cpp.

Referenced by QMYSQLDriver::open().

Function Documentation

◆ codec()

static QTextCodec* codec ( MYSQL *  mysql)
static

Definition at line 220 of file qsql_mysql.cpp.

Referenced by QTextCodec::codecForMib(), QTextCodec::codecForName(), encodeText(), QDeclarativeXMLHttpRequest::findTextCodec(), QString::fromLocal8Bit(), QXmlInputSource::fromRawData(), QSettingsPrivate::iniEscapedStringList(), QXlibMime::mimeConvertToFormat(), QMYSQLDriver::open(), QMYSQLDriver::QMYSQLDriver(), QApplicationPrivate::qt_mac_apply_settings(), qt_x11_set_fallback_font_family(), QTextDecoder::QTextDecoder(), QTextEncoder::QTextEncoder(), QMimeDataPrivate::retrieveTypedData(), QPatternist::AccelTreeResourceLoader::retrieveUnparsedText(), QDomDocumentPrivate::saveDocument(), QTextOdfWriter::setCodec(), QSettings::setIniCodec(), QTextBrowserPrivate::setSource(), QClipboard::text(), QApplicationPrivate::x11_apply_settings(), and QX11Data::xdndMimeConvertToFormat().

221 {
222 #if MYSQL_VERSION_ID >= 32321
223  QTextCodec* heuristicCodec = QTextCodec::codecForName(mysql_character_set_name(mysql));
224  if (heuristicCodec)
225  return heuristicCodec;
226 #endif
228 }
static QTextCodec * codecForLocale()
Returns a pointer to the codec most suitable for this locale.
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62

◆ fromUnicode()

static QByteArray fromUnicode ( QTextCodec tc,
const QString str 
)
inlinestatic

Definition at line 115 of file qsql_mysql.cpp.

Referenced by QTextCodec::codecForName(), QTextCodec::fromUnicode(), QTextEncoder::QTextEncoder(), QMYSQLResult::reset(), and QMYSQLResult::virtual_hook().

116 {
117 #ifdef QT_NO_TEXTCODEC
118  Q_UNUSED(tc);
119  return str.toLatin1();
120 #else
121  return tc->fromUnicode(str);
122 #endif
123 }
QByteArray fromUnicode(const QString &uc) const
Converts str from Unicode to the encoding of this codec, and returns the result in a QByteArray...
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ qDateFromString()

static QVariant qDateFromString ( const QString val)
inlinestatic

Definition at line 125 of file qsql_mysql.cpp.

Referenced by QMYSQLResult::data().

126 {
127 #ifdef QT_NO_DATESTRING
128  Q_UNUSED(val);
129  return QVariant(val);
130 #else
131  if (val.isEmpty())
132  return QVariant(QDate());
133  return QVariant(QDate::fromString(val, Qt::ISODate));
134 #endif
135 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDate class provides date functions.
Definition: qdatetime.h:55
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QDate fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDate represented by the string, using the format given, or an invalid date if the string...
Definition: qdatetime.cpp:1203
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ qDateTimeFromString()

static QVariant qDateTimeFromString ( QString val)
inlinestatic

Definition at line 149 of file qsql_mysql.cpp.

Referenced by QMYSQLResult::data().

150 {
151 #ifdef QT_NO_DATESTRING
152  Q_UNUSED(val);
153  return QVariant(val);
154 #else
155  if (val.isEmpty())
156  return QVariant(QDateTime());
157  if (val.length() == 14)
158  // TIMESTAMPS have the format yyyyMMddhhmmss
159  val.insert(4, QLatin1Char('-')).insert(7, QLatin1Char('-')).insert(10,
160  QLatin1Char('T')).insert(13, QLatin1Char(':')).insert(16, QLatin1Char(':'));
162 #endif
163 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
static QDateTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDateTime represented by the string, using the format given, or an invalid datetime if th...
Definition: qdatetime.cpp:3487
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216
QString & insert(int i, QChar c)
Definition: qstring.cpp:1671
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ qDecodeMYSQLType()

static QVariant::Type qDecodeMYSQLType ( int  mysqltype,
uint  flags 
)
static

Definition at line 241 of file qsql_mysql.cpp.

Referenced by QMYSQLResult::nextResult(), qToField(), and QMYSQLResult::reset().

242 {
244  switch (mysqltype) {
245  case FIELD_TYPE_TINY :
246  case FIELD_TYPE_SHORT :
247  case FIELD_TYPE_LONG :
248  case FIELD_TYPE_INT24 :
249  type = (flags & UNSIGNED_FLAG) ? QVariant::UInt : QVariant::Int;
250  break;
251  case FIELD_TYPE_YEAR :
252  type = QVariant::Int;
253  break;
254  case FIELD_TYPE_LONGLONG :
255  type = (flags & UNSIGNED_FLAG) ? QVariant::ULongLong : QVariant::LongLong;
256  break;
257  case FIELD_TYPE_FLOAT :
258  case FIELD_TYPE_DOUBLE :
259  case FIELD_TYPE_DECIMAL :
260 #if defined(FIELD_TYPE_NEWDECIMAL)
261  case FIELD_TYPE_NEWDECIMAL:
262 #endif
263  type = QVariant::Double;
264  break;
265  case FIELD_TYPE_DATE :
266  type = QVariant::Date;
267  break;
268  case FIELD_TYPE_TIME :
269  type = QVariant::Time;
270  break;
271  case FIELD_TYPE_DATETIME :
272  case FIELD_TYPE_TIMESTAMP :
273  type = QVariant::DateTime;
274  break;
275  case FIELD_TYPE_STRING :
276  case FIELD_TYPE_VAR_STRING :
277  case FIELD_TYPE_BLOB :
278  case FIELD_TYPE_TINY_BLOB :
279  case FIELD_TYPE_MEDIUM_BLOB :
280  case FIELD_TYPE_LONG_BLOB :
281  type = (flags & BINARY_FLAG) ? QVariant::ByteArray : QVariant::String;
282  break;
283  default:
284  case FIELD_TYPE_ENUM :
285  case FIELD_TYPE_SET :
286  type = QVariant::String;
287  break;
288  }
289  return type;
290 }
int type
Definition: qmetatype.cpp:239
Type
This enum type defines the types of variable that a QVariant can contain.
Definition: qvariant.h:95

◆ qLibraryEnd()

static void qLibraryEnd ( )
static

Definition at line 1110 of file qsql_mysql.cpp.

Referenced by QMYSQLDriver::~QMYSQLDriver().

1111 {
1112 #ifndef Q_NO_MYSQL_EMBEDDED
1113 # if MYSQL_VERSION_ID > 40000
1114 # if (MYSQL_VERSION_ID >= 40110 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50003
1115  mysql_library_end();
1116 # else
1117  mysql_server_end();
1118 # endif
1119 # endif
1120 #endif
1121 }

◆ qLibraryInit()

static void qLibraryInit ( )
static

Definition at line 1092 of file qsql_mysql.cpp.

Referenced by QMYSQLDriver::QMYSQLDriver().

1093 {
1094 #ifndef Q_NO_MYSQL_EMBEDDED
1095 # if MYSQL_VERSION_ID >= 40000
1097  return;
1098 
1099 # if (MYSQL_VERSION_ID >= 40110 && MYSQL_VERSION_ID < 50000) || MYSQL_VERSION_ID >= 50003
1100  if (mysql_library_init(0, 0, 0)) {
1101 # else
1102  if (mysql_server_init(0, 0, 0)) {
1103 # endif
1104  qWarning("QMYSQLDriver::qServerInit: unable to start server.");
1105  }
1106 # endif // MYSQL_VERSION_ID
1107 #endif // Q_NO_MYSQL_EMBEDDED
1108 }
static int qMySqlConnectionCount
Q_CORE_EXPORT void qWarning(const char *,...)
static bool qMySqlInitHandledByUser

◆ qMakeError()

static QSqlError qMakeError ( const QString err,
QSqlError::ErrorType  type,
const QMYSQLDriverPrivate p 
)
static

Definition at line 231 of file qsql_mysql.cpp.

Referenced by QMYSQLDriver::beginTransaction(), QMYSQLDriver::commitTransaction(), QMYSQLResult::nextResult(), QMYSQLDriver::open(), QMYSQLResult::reset(), QMYSQLDriver::rollbackTransaction(), and QMYSQLResult::virtual_hook().

233 {
234  const char *cerr = p->mysql ? mysql_error(p->mysql) : 0;
235  return QSqlError(QLatin1String("QMYSQL: ") + err,
236  p->tc ? toUnicode(p->tc, cerr) : QString::fromLatin1(cerr),
237  type, mysql_errno(p->mysql));
238 }
The QSqlError class provides SQL database error information.
Definition: qsqlerror.h:53
int type
Definition: qmetatype.cpp:239
QTextCodec * tc
Definition: qsql_mysql.cpp:90
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static QString toUnicode(QTextCodec *tc, const char *str)
Definition: qsql_mysql.cpp:95
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188

◆ qTimeFromString()

static QVariant qTimeFromString ( const QString val)
inlinestatic

Definition at line 137 of file qsql_mysql.cpp.

Referenced by QMYSQLResult::data().

138 {
139 #ifdef QT_NO_DATESTRING
140  Q_UNUSED(val);
141  return QVariant(val);
142 #else
143  if (val.isEmpty())
144  return QVariant(QTime());
145  return QVariant(QTime::fromString(val, Qt::ISODate));
146 #endif
147 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QTime class provides clock time functions.
Definition: qdatetime.h:148
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the time represented in the string as a QTime using the format given, or an invalid time if t...
Definition: qdatetime.cpp:1928
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ qToField()

static QSqlField qToField ( MYSQL_FIELD *  field,
QTextCodec tc 
)
static

Definition at line 292 of file qsql_mysql.cpp.

Referenced by QMYSQLResult::record(), and QMYSQLDriver::record().

293 {
294  QSqlField f(toUnicode(tc, field->name),
295  qDecodeMYSQLType(int(field->type), field->flags));
296  f.setRequired(IS_NOT_NULL(field->flags));
297  f.setLength(field->length);
298  f.setPrecision(field->decimals);
299  f.setSqlType(field->type);
300  f.setAutoValue(field->flags & AUTO_INCREMENT_FLAG);
301  return f;
302 }
static QString toUnicode(QTextCodec *tc, const char *str)
Definition: qsql_mysql.cpp:95
virtual QByteArray name() const =0
QTextCodec subclasses must reimplement this function.
static QVariant::Type qDecodeMYSQLType(int mysqltype, uint flags)
Definition: qsql_mysql.cpp:241
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56

◆ setOptionFlag()

static void setOptionFlag ( uint optionFlags,
const QString opt 
)
static

Definition at line 1209 of file qsql_mysql.cpp.

Referenced by QMYSQLDriver::open().

1210 {
1211  if (opt == QLatin1String("CLIENT_COMPRESS"))
1212  optionFlags |= CLIENT_COMPRESS;
1213  else if (opt == QLatin1String("CLIENT_FOUND_ROWS"))
1214  optionFlags |= CLIENT_FOUND_ROWS;
1215  else if (opt == QLatin1String("CLIENT_IGNORE_SPACE"))
1216  optionFlags |= CLIENT_IGNORE_SPACE;
1217  else if (opt == QLatin1String("CLIENT_INTERACTIVE"))
1218  optionFlags |= CLIENT_INTERACTIVE;
1219  else if (opt == QLatin1String("CLIENT_NO_SCHEMA"))
1220  optionFlags |= CLIENT_NO_SCHEMA;
1221  else if (opt == QLatin1String("CLIENT_ODBC"))
1222  optionFlags |= CLIENT_ODBC;
1223  else if (opt == QLatin1String("CLIENT_SSL"))
1224  optionFlags |= CLIENT_SSL;
1225  else
1226  qWarning("QMYSQLDriver::open: Unknown connect option '%s'", opt.toLocal8Bit().constData());
1227 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Q_CORE_EXPORT void qWarning(const char *,...)
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433

◆ toUnicode() [1/2]

static QString toUnicode ( QTextCodec tc,
const char *  str 
)
inlinestatic

Definition at line 95 of file qsql_mysql.cpp.

Referenced by QTextCodec::codecForName(), QMYSQLResult::data(), QPdfEnginePrivate::embedFont(), QFontEngineXLFD::fontStruct(), QMYSQLDriver::formatValue(), QTextCodec::fromUnicode(), QFontSubset::glyphName(), qMakeError(), QTextDecoder::QTextDecoder(), qToField(), and QMYSQLDriver::tables().

96 {
97 #ifdef QT_NO_TEXTCODEC
98  Q_UNUSED(tc);
99  return QString::fromLatin1(str);
100 #else
101  return tc->toUnicode(str);
102 #endif
103 }
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ toUnicode() [2/2]

static QString toUnicode ( QTextCodec tc,
const char *  str,
int  length 
)
inlinestatic

Definition at line 105 of file qsql_mysql.cpp.

106 {
107 #ifdef QT_NO_TEXTCODEC
108  Q_UNUSED(tc);
109  return QString::fromLatin1(str, length);
110 #else
111  return tc->toUnicode(str, length);
112 #endif
113 }
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

Variable Documentation

◆ qMySqlConnectionCount

int qMySqlConnectionCount = 0
static

Definition at line 1089 of file qsql_mysql.cpp.

◆ qMySqlInitHandledByUser

bool qMySqlInitHandledByUser = false
static

Definition at line 1090 of file qsql_mysql.cpp.