File indexing completed on 2024-04-21 15:29:58

0001 /* This file is part of the KDE project
0002    Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KDB_SQLSTRING_H
0021 #define KDB_SQLSTRING_H
0022 
0023 #include "kdb_export.h"
0024 #include <QByteArray>
0025 
0026 //! The KDbSqlString class abstracts low-level information about a single string value returned by KDbSqlRecord
0027 /**
0028  * KDbSqlString exists for optimization purposes. KDbSqlRecord can return KDbSqlString
0029  * objects to avoid premature converting to QString or QByteArray. This way memory allocations
0030  * are not required.
0031  */
0032 class KDbSqlString
0033 {
0034 public:
0035     //! Creates an empty string object
0036     inline KDbSqlString() : string(nullptr), length(0) {}
0037 
0038     //! Creates string object from raw string @a s, of specified length
0039     inline KDbSqlString(const char *s, quint64 len) : string(s), length(len) {}
0040 
0041     //! @return true if this string value is empty. Here, NULL values are considered empty too.
0042     inline bool isEmpty() const { return !string || length == 0; }
0043 
0044     //! @return string value converted to bytea array
0045     //! For optimization, raw string data is used via, see QByteArray::fromRawData() for details.
0046     //! The caller must not delete data or modify the parent KDbSqlRecord object
0047     //! directly as long as the returned QByteArray exists.
0048     inline QByteArray rawDataToByteArray() const { return QByteArray::fromRawData(string, length); }
0049 
0050     const char *string;
0051     quint64 length;
0052 };
0053 
0054 #endif