File indexing completed on 2024-05-05 16:47:14

0001 /* This file is part of the KDE project
0002    Copyright (C) 2005 Adam Pigg <adam@piggz.co.uk>
0003    Copyright (C) 2010 Jarosław Staniek <staniek@kde.org>
0004 
0005    This program is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This program is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this program; see the file COPYING.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "PostgresqlConnection_p.h"
0022 
0023 PostgresqlConnectionInternal::PostgresqlConnectionInternal(KDbConnection *_conn)
0024         : KDbConnectionInternal(_conn)
0025         , conn(nullptr)
0026         , unicode(true) // will be set in PostgresqlConnection::drv_useDatabase()
0027 {
0028     escapingBuffer.reserve(0x8000);
0029 }
0030 
0031 PostgresqlConnectionInternal::~PostgresqlConnectionInternal()
0032 {
0033 }
0034 
0035 //static
0036 QString PostgresqlConnectionInternal::serverResultName(int resultCode)
0037 {
0038     return QString::fromLatin1(PQresStatus(static_cast<ExecStatusType>(resultCode)));
0039 }
0040 
0041 void PostgresqlConnectionInternal::storeResultAndClear(KDbResult *result, PGresult **pgResult,
0042                                                        ExecStatusType execStatus)
0043 {
0044     QByteArray msg(PQresultErrorMessage(*pgResult));
0045     if (msg.endsWith('\n')) {
0046         msg.chop(1);
0047     }
0048     result->setServerMessage(QString::fromLatin1(msg));
0049     if (*pgResult) {
0050         result->setServerErrorCode(execStatus);
0051         PQclear(*pgResult);
0052         *pgResult = nullptr;
0053     }
0054 }
0055 
0056 void PostgresqlConnectionInternal::storeResult(KDbResult *result)
0057 {
0058     QByteArray msg(PQerrorMessage(conn));
0059     if (msg.endsWith('\n')) {
0060         msg.chop(1);
0061     }
0062     result->setServerMessage(QString::fromLatin1(msg));
0063 }
0064 
0065 PGresult* PostgresqlConnectionInternal::executeSql(const KDbEscapedString& sql)
0066 {
0067 //! @todo consider using binary mode with PQexecParams()
0068     return PQexec(conn, sql.toByteArray().constData());
0069 }
0070 
0071 //--------------------------------------
0072 
0073 PostgresqlCursorData::PostgresqlCursorData(KDbConnection* connection)
0074         : PostgresqlConnectionInternal(connection), res(nullptr), resultStatus(PGRES_FATAL_ERROR)
0075 {
0076     conn = static_cast<PostgresqlConnection*>(connection)->d->conn;
0077 }
0078 
0079 PostgresqlCursorData::~PostgresqlCursorData()
0080 {
0081 }
0082 
0083 KDbField* PostgresqlSqlResult::createField(const QString &tableName, int index)
0084 {
0085     Q_UNUSED(tableName)
0086     QScopedPointer<PostgresqlSqlField> f(static_cast<PostgresqlSqlField*>(field(index)));
0087     if (!f) {
0088         return nullptr;
0089     }
0090     const QString caption(f->name());
0091     QString realFieldName(KDb::stringToIdentifier(caption.toLower()));
0092     const PostgresqlDriver *pgdriver = static_cast<const PostgresqlDriver*>(conn->driver());
0093     const KDbField::Type kdbType = pgdriver->pgsqlToKDbType(
0094                 PQftype(result, index), PQfmod(result, index), nullptr);
0095     KDbField *kdbField = new KDbField(realFieldName, kdbType);
0096     kdbField->setCaption(caption);
0097     if (KDbField::isTextType(kdbType)) {
0098         const int len = f->length();
0099         if (len != -1) {
0100             kdbField->setMaxLength(len);
0101         }
0102     }
0103     //! @todo use information_schema.table_constraints to get constraints
0104     //copyConstraints(...);
0105     //! @todo use information_schema.columns to get options
0106     //copyOptions(...);
0107     return kdbField;
0108 }