source: trunk/src/sql/drivers/mysql/qsql_mysql.cpp@ 134

Last change on this file since 134 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 41.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtSql module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qsql_mysql.h"
43
44#include <qcoreapplication.h>
45#include <qvariant.h>
46#include <qdatetime.h>
47#include <qsqlerror.h>
48#include <qsqlfield.h>
49#include <qsqlindex.h>
50#include <qsqlquery.h>
51#include <qsqlrecord.h>
52#include <qstringlist.h>
53#include <qtextcodec.h>
54#include <qvector.h>
55
56#include <qdebug.h>
57
58#ifdef Q_OS_WIN32
59// comment the next line out if you want to use MySQL/embedded on Win32 systems.
60// note that it will crash if you don't statically link to the mysql/e library!
61# define Q_NO_MYSQL_EMBEDDED
62#endif
63
64Q_DECLARE_METATYPE(MYSQL_RES*)
65Q_DECLARE_METATYPE(MYSQL*)
66
67#if MYSQL_VERSION_ID >= 40108
68Q_DECLARE_METATYPE(MYSQL_STMT*)
69#endif
70
71#if MYSQL_VERSION_ID >= 40100
72# define Q_CLIENT_MULTI_STATEMENTS CLIENT_MULTI_STATEMENTS
73#else
74# define Q_CLIENT_MULTI_STATEMENTS 0
75#endif
76
77QT_BEGIN_NAMESPACE
78
79class QMYSQLDriverPrivate
80{
81public:
82 QMYSQLDriverPrivate() : mysql(0),
83#ifndef QT_NO_TEXTCODEC
84 tc(QTextCodec::codecForLocale()),
85#else
86 tc(0),
87#endif
88 preparedQuerys(false), preparedQuerysEnabled(false) {}
89 MYSQL *mysql;
90 QTextCodec *tc;
91
92 bool preparedQuerys;
93 bool preparedQuerysEnabled;
94};
95
96static inline QString toUnicode(QTextCodec *tc, const char *str)
97{
98#ifdef QT_NO_TEXTCODEC
99 Q_UNUSED(tc);
100 return QString::fromLatin1(str);
101#else
102 return tc->toUnicode(str);
103#endif
104}
105
106static inline QString toUnicode(QTextCodec *tc, const char *str, int length)
107{
108#ifdef QT_NO_TEXTCODEC
109 Q_UNUSED(tc);
110 return QString::fromLatin1(str, length);
111#else
112 return tc->toUnicode(str, length);
113#endif
114}
115
116static inline QByteArray fromUnicode(QTextCodec *tc, const QString &str)
117{
118#ifdef QT_NO_TEXTCODEC
119 Q_UNUSED(tc);
120 return str.toLatin1();
121#else
122 return tc->fromUnicode(str);
123#endif
124}
125
126static inline QVariant qDateFromString(const QString &val)
127{
128#ifdef QT_NO_DATESTRING
129 Q_UNUSED(val);
130 return QVariant(val);
131#else
132 if (val.isEmpty())
133 return QVariant(QDate());
134 return QVariant(QDate::fromString(val, Qt::ISODate));
135#endif
136}
137
138static inline QVariant qTimeFromString(const QString &val)
139{
140#ifdef QT_NO_DATESTRING
141 Q_UNUSED(val);
142 return QVariant(val);
143#else
144 if (val.isEmpty())
145 return QVariant(QTime());
146 return QVariant(QTime::fromString(val, Qt::ISODate));
147#endif
148}
149
150static inline QVariant qDateTimeFromString(QString &val)
151{
152#ifdef QT_NO_DATESTRING
153 Q_UNUSED(val);
154 return QVariant(val);
155#else
156 if (val.isEmpty())
157 return QVariant(QDateTime());
158 if (val.length() == 14)
159 // TIMESTAMPS have the format yyyyMMddhhmmss
160 val.insert(4, QLatin1Char('-')).insert(7, QLatin1Char('-')).insert(10,
161 QLatin1Char('T')).insert(13, QLatin1Char(':')).insert(16, QLatin1Char(':'));
162 return QVariant(QDateTime::fromString(val, Qt::ISODate));
163#endif
164}
165
166class QMYSQLResultPrivate
167{
168public:
169 QMYSQLResultPrivate(QMYSQLDriverPrivate* dp) : d(dp), result(0),
170 rowsAffected(0), hasBlobs(false)
171#if MYSQL_VERSION_ID >= 40108
172 , stmt(0), meta(0), inBinds(0), outBinds(0)
173#endif
174 , precisionPolicy(QSql::HighPrecision)
175 {}
176
177 QMYSQLDriverPrivate* d;
178 MYSQL_RES *result;
179 MYSQL_ROW row;
180
181 int rowsAffected;
182
183 bool bindInValues();
184 void bindBlobs();
185
186 bool hasBlobs;
187 struct QMyField
188 {
189 QMyField()
190 : outField(0), nullIndicator(false), bufLength(0ul),
191 myField(0), type(QVariant::Invalid)
192 {}
193 char *outField;
194 my_bool nullIndicator;
195 ulong bufLength;
196 MYSQL_FIELD *myField;
197 QVariant::Type type;
198 };
199
200 QVector<QMyField> fields;
201
202#if MYSQL_VERSION_ID >= 40108
203 MYSQL_STMT* stmt;
204 MYSQL_RES* meta;