KDb

PostgresqlDriver.h
1/* This file is part of the KDE project
2 Copyright (C) 2003 Adam Pigg <adam@piggz.co.uk>
3 Copyright (C) 2010-2016 Jarosław Staniek <staniek@kde.org>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#ifndef KDB_POSTGRESQLDRIVER_H
22#define KDB_POSTGRESQLDRIVER_H
23
24#include "KDbDriver.h"
25
26class KDbConnection;
27
28//! PostgreSQL database driver.
30{
32
33public:
34 PostgresqlDriver(QObject *parent, const QVariantList &args);
35
36 ~PostgresqlDriver() override;
37
38 //! @todo implement
39 bool isSystemObjectName(const QString& name) const override;
40
41 bool isSystemDatabaseName(const QString& name) const override;
42
43 //! Escape a string for use as a value
44 KDbEscapedString escapeString(const QString& str) const override;
45 KDbEscapedString escapeString(const QByteArray& str) const override;
46 //! Overrides the default implementation to allow for NUMERIC type natively
47 QString sqlTypeName(KDbField::Type type, const KDbField &field) const override;
48
49 //! Escape BLOB value @a array
50 KDbEscapedString escapeBLOB(const QByteArray& array) const override;
51
52 //! Converts information converted from PQfmod() to length. -1 if missing.
53 inline static int pqfmodToLength(int pqfmod) {
54 int len;
55 if (pqfmod > 0) {
56 const int PGSQL_VAR_HDRSZ = 4;
57 len = pqfmod - PGSQL_VAR_HDRSZ; //!< See e.g. postgis_get_char_length()
58 } else {
59 len = -1;
60 }
61 return len;
62 }
63
64 //! Uses information obtained from PQfmod() and adjust type @a t if possible.
65 //! Also sets @a maxTextLength.
66 //! @todo using pqfmod not tested
67 //! @todo more types such as decimal
68 inline static KDbField::Type typeForSize(KDbField::Type t, int pqfmod, int *maxTextLength) {
69 KDbField::Type newType = t;
70 if (maxTextLength) {
71 *maxTextLength = -1;
72 }
73 if (t == KDbField::Integer) {
74 if (pqfmod == 1) {
75 newType = KDbField::Byte;
76 } else if (pqfmod == 2) {
77 newType = KDbField::ShortInteger;
78 } else if (pqfmod == 8) {
79 newType = KDbField::BigInteger;
80 }
81 } else if (t == KDbField::LongText) {
82 const int len = pqfmodToLength(pqfmod);
83 if (len > 0 && len <= 255) {
84 newType = KDbField::Text;
85 if (maxTextLength) {
86 *maxTextLength = len;
87 }
88 }
89 }
90 return newType;
91 }
92
93 //! @return KDb field type for PostgreSQL type @a pqtype and modifier @a pqfmod.
94 //! If type cannot be found KDbField::InvalidType is returned.
95 //! Used in cursors to speed up data conversion.
96 inline KDbField::Type pgsqlToKDbType(int pqtype, int pqfmod, int *maxTextLength) const {
97 KDbField::Type t = m_pgsqlToKDbTypes.value(pqtype, KDbField::InvalidType);
98 return typeForSize(t, pqfmod, maxTextLength);
99 }
100
101 //! Generates native (driver-specific) HEX() function call.
102 //! Uses UPPER(ENCODE(val, 'hex')).
103 //! See https://www.postgresql.org/docs/9.3/static/functions-string.html#FUNCTIONS-STRING-OTHER */
106 KDb::ExpressionCallStack* callStack) const override;
107
108 //! Generates native (driver-specific) IFNULL() function call.
109 //! Uses COALESCE().
112 KDb::ExpressionCallStack* callStack) const override;
113
114 //! Generates native (driver-specific) LENGTH() function call.
115 //! For text types default LENGTH(val) is used, for BLOBs OCTET_LENGTH(val) is used because
116 //! LENGTH(val) for BLOB returns number of bits.
119 KDb::ExpressionCallStack* callStack) const override;
120
121 //! Generates native (driver-specific) GREATEST() and LEAST() function calls.
122 //! Since PostgreSQL's LEAST()/GREATEST() function ignores NULL values, it only returns NULL
123 //! if all the expressions evaluate to NULL. So this is used for F(v0,..,vN):
124 //! (CASE WHEN (v0) IS NULL OR .. OR (vN) IS NULL THEN NULL ELSE F(v0,..,vN) END)
125 //! where F == GREATEST or LEAST.
127 const KDbNArgExpression &args,
129 KDb::ExpressionCallStack* callStack) const override;
130
131 //! Generates native (driver-specific) UNICODE() function call.
132 //! Uses ASCII(X).
135 KDb::ExpressionCallStack* callStack) const override;
136
137protected:
138 QString drv_escapeIdentifier(const QString& str) const override;
139 QByteArray drv_escapeIdentifier(const QByteArray& str) const override;
141 const KDbConnectionOptions &options) override;
142 bool drv_isSystemFieldName(const QString& name)const override;
143
144private:
145 void initPgsqlToKDbMap();
146
147 static const char *m_keywords[];
148 QMap<int, KDbField::Type> m_pgsqlToKDbTypes;
149 Q_DISABLE_COPY(PostgresqlDriver)
150};
151
152#endif // KDB_DRIVER_POSTGRESQLDRIVER_H
Database specific connection data, e.g. host, port.
Generic options for a single connection. The options are accessible using key/value pairs....
Provides database connection, allowing queries and data modification.
Database driver's abstraction.
Definition KDbDriver.h:50
Specialized string for escaping.
Meta-data for a field.
Definition KDbField.h:72
@ Integer
Definition KDbField.h:90
@ ShortInteger
Definition KDbField.h:89
@ InvalidType
Definition KDbField.h:86
@ BigInteger
Definition KDbField.h:91
@ LongText
Definition KDbField.h:99
The KDbNArgExpression class represents a base class N-argument expression.
An iterator for a list of values of query schema parameters Allows to iterate over parameters and ret...
PostgreSQL database driver.
static KDbField::Type typeForSize(KDbField::Type t, int pqfmod, int *maxTextLength)
Uses information obtained from PQfmod() and adjust type t if possible.
bool isSystemObjectName(const QString &name) const override
KDbEscapedString ifnullFunctionToString(const KDbNArgExpression &args, KDbQuerySchemaParameterValueListIterator *params, KDb::ExpressionCallStack *callStack) const override
Generates native (driver-specific) IFNULL() function call.
QString drv_escapeIdentifier(const QString &str) const override
KDbEscapedString escapeString(const QString &str) const override
Escape a string for use as a value.
PostgresqlDriver(QObject *parent, const QVariantList &args)
KDbEscapedString unicodeFunctionToString(const KDbNArgExpression &args, KDbQuerySchemaParameterValueListIterator *params, KDb::ExpressionCallStack *callStack) const override
Generates native (driver-specific) UNICODE() function call.
KDbConnection * drv_createConnection(const KDbConnectionData &connData, const KDbConnectionOptions &options) override
KDbEscapedString hexFunctionToString(const KDbNArgExpression &args, KDbQuerySchemaParameterValueListIterator *params, KDb::ExpressionCallStack *callStack) const override
Generates native (driver-specific) HEX() function call.
bool drv_isSystemFieldName(const QString &name) const override
static int pqfmodToLength(int pqfmod)
Converts information converted from PQfmod() to length. -1 if missing.
QString sqlTypeName(KDbField::Type type, const KDbField &field) const override
Overrides the default implementation to allow for NUMERIC type natively.
KDbEscapedString lengthFunctionToString(const KDbNArgExpression &args, KDbQuerySchemaParameterValueListIterator *params, KDb::ExpressionCallStack *callStack) const override
Generates native (driver-specific) LENGTH() function call.
KDbEscapedString escapeBLOB(const QByteArray &array) const override
Escape BLOB value array.
KDbEscapedString greatestOrLeastFunctionToString(const QString &name, const KDbNArgExpression &args, KDbQuerySchemaParameterValueListIterator *params, KDb::ExpressionCallStack *callStack) const override
Generates native (driver-specific) GREATEST() and LEAST() function calls.
KDbField::Type pgsqlToKDbType(int pqtype, int pqfmod, int *maxTextLength) const
bool isSystemDatabaseName(const QString &name) const override
T value(const Key &key, const T &defaultValue) const const
Q_OBJECTQ_OBJECT
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.