sqlitestatement.h 15 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25
**
****************************************************************************/

hjk's avatar
hjk committed
26
#pragma once
27

Marco Bubke's avatar
Marco Bubke committed
28 29 30
#include "sqliteglobal.h"

#include "sqliteexception.h"
31

32
#include <utils/smallstringvector.h>
33

Marco Bubke's avatar
Marco Bubke committed
34
#include <cstdint>
Marco Bubke's avatar
Marco Bubke committed
35
#include <memory>
Marco Bubke's avatar
Marco Bubke committed
36 37 38 39
#include <type_traits>
#include <tuple>

using std::int64_t;
40 41 42 43

struct sqlite3_stmt;
struct sqlite3;

Marco Bubke's avatar
Marco Bubke committed
44 45
namespace Sqlite {

46 47 48
class SqliteDatabase;
class SqliteDatabaseBackend;

49 50 51
class SQLITE_EXPORT SqliteStatement
{
protected:
52
    explicit SqliteStatement(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
53

Marco Bubke's avatar
Marco Bubke committed
54
    static void deleteCompiledStatement(sqlite3_stmt *m_compiledStatement);
55 56 57

    bool next() const;
    void step() const;
58
    void execute() const;
59 60 61 62
    void reset() const;

    template<typename Type>
    Type value(int column) const;
63
    Utils::SmallString text(int column) const;
64
    int columnCount() const;
65
    Utils::SmallStringVector columnNames() const;
66 67

    void bind(int index, int value);
Marco Bubke's avatar
Marco Bubke committed
68
    void bind(int index, long long value);
69
    void bind(int index, double value);
70
    void bind(int index, Utils::SmallStringView value);
71

Marco Bubke's avatar
Marco Bubke committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    void bind(int index, uint value)
    {
        bind(index, static_cast<long long>(value));
    }

    void bind(int index, long value)
    {
        bind(index, static_cast<long long>(value));
    }

    void bindValues()
    {
    }

    template<typename... Values>
    void bindValues(Values... values)
88 89 90 91
    {
        bindValuesByIndex(1, values...);
    }

Marco Bubke's avatar
Marco Bubke committed
92 93
    template<typename... Values>
    void write(Values... values)
94 95 96 97 98
    {
        bindValuesByIndex(1, values...);
        execute();
    }

Marco Bubke's avatar
Marco Bubke committed
99 100
    template<typename... Values>
    void bindNameValues(Values... values)
101 102 103 104
    {
        bindValuesByName(values...);
    }

Marco Bubke's avatar
Marco Bubke committed
105 106
    template<typename... Values>
    void writeNamed(Values... values)
107 108 109 110 111
    {
        bindValuesByName(values...);
        execute();
    }

112
    template <typename Type>
113
    void bind(Utils::SmallStringView name, Type value);
114

115
    int bindingIndexForName(Utils::SmallStringView name) const;
116

117 118
    void setBindingColumnNames(const Utils::SmallStringVector &bindingColumnNames);
    const Utils::SmallStringVector &bindingColumnNames() const;
119

Marco Bubke's avatar
Marco Bubke committed
120 121 122 123 124 125
    template <typename... ResultType>
    std::vector<std::tuple<ResultType...>> tupleValues(std::size_t reserveSize)
    {
        using Container = std::vector<std::tuple<ResultType...>>;
        Container resultValues;
        resultValues.reserve(reserveSize);
126

Marco Bubke's avatar
Marco Bubke committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
        while (next())
           emplaceTupleValues<Container, ResultType...>(resultValues);

        reset();

        return resultValues;
    }

    template <typename... ResultType,
              typename... QueryType>
    std::vector<std::tuple<ResultType...>> tupleValues(std::size_t reserveSize, const QueryType&... queryValues)
    {
        using Container = std::vector<std::tuple<ResultType...>>;
        Container resultValues;
        resultValues.reserve(reserveSize);

        bindValues(queryValues...);

        while (next())
           emplaceTupleValues<Container, ResultType...>(resultValues);

        reset();

        return resultValues;
    }

    template <typename... ResultType,
              typename... ElementType>
    std::vector<std::tuple<ResultType...>> tupleValues(std::size_t reserveSize,
                                                       const std::vector<std::tuple<ElementType...>> &queryTuples)
    {
        using Container = std::vector<std::tuple<ResultType...>>;
        Container resultValues;
        resultValues.reserve(reserveSize);

        for (const auto &queryTuple : queryTuples) {
            bindTupleValues(queryTuple);

            while (next())
                emplaceTupleValues<Container, ResultType...>(resultValues);

            reset();
        }

        return resultValues;
    }

    template <typename... ResultType,
              typename QueryElementType>
    std::vector<std::tuple<ResultType...>> tupleValues(std::size_t reserveSize,
                                                       const std::vector<QueryElementType> &queryValues)
    {
        using Container = std::vector<std::tuple<ResultType...>>;
        Container resultValues;
        resultValues.reserve(reserveSize);

        for (const QueryElementType &queryValue : queryValues) {
            bindValues(queryValue);

            while (next())
                emplaceTupleValues<Container, ResultType...>(resultValues);

            reset();
        }

        return resultValues;
    }

    template <typename ResultType,
              typename... ResultEntryType>
    std::vector<ResultType> structValues(std::size_t reserveSize)
    {
        using Container = std::vector<ResultType>;
        Container resultValues;
        resultValues.reserve(reserveSize);

        while (next())
           pushBackStructValues<Container, ResultEntryType...>(resultValues);

        reset();

        return resultValues;
    }

    template <typename ResultType,
              typename... ResultEntryType,
              typename... QueryType>
    std::vector<ResultType> structValues(std::size_t reserveSize, const QueryType&... queryValues)
    {
        using Container = std::vector<ResultType>;
        Container resultValues;
        resultValues.reserve(reserveSize);

        bindValues(queryValues...);

        while (next())
           pushBackStructValues<Container, ResultEntryType...>(resultValues);

        reset();

        return resultValues;
    }

    template <typename ResultType,
              typename... ResultEntryType,
              typename QueryElementType>
    std::vector<ResultType> structValues(std::size_t reserveSize,
                                         const std::vector<QueryElementType> &queryValues)
    {
        using Container = std::vector<ResultType>;
        Container resultValues;
        resultValues.reserve(reserveSize);

        for (const QueryElementType &queryValue : queryValues) {
            bindValues(queryValue);

            while (next())
                pushBackStructValues<Container, ResultEntryType...>(resultValues);

            reset();
        }

        return resultValues;
    }

    template <typename ResultType,
              typename... ResultEntryType,
              typename... QueryElementType>
    std::vector<ResultType> structValues(std::size_t reserveSize,
                                         const std::vector<std::tuple<QueryElementType...>> &queryTuples)
    {
        using Container = std::vector<ResultType>;
        Container resultValues;
        resultValues.reserve(reserveSize);

        for (const auto &queryTuple : queryTuples) {
            bindTupleValues(queryTuple);

            while (next())
                pushBackStructValues<Container, ResultEntryType...>(resultValues);

            reset();
        }

        return resultValues;
    }

    template <typename ResultType,
              typename... ElementType>
    std::vector<ResultType> values(std::size_t reserveSize)
    {
        std::vector<ResultType> resultValues;
        resultValues.reserve(reserveSize);

        while (next())
            resultValues.push_back(value<ResultType>(0));

        reset();

        return resultValues;
    }

    template <typename ResultType,
              typename... ElementType>
    std::vector<ResultType> values(std::size_t reserveSize,
                                   const std::vector<std::tuple<ElementType...>> &queryTuples)
    {
        std::vector<ResultType> resultValues;
        resultValues.reserve(reserveSize);

        for (const auto &queryTuple : queryTuples) {
            bindTupleValues(queryTuple);

            while (next())
                resultValues.push_back(value<ResultType>(0));

            reset();
        }

        return resultValues;
    }

    template <typename ResultType,
              typename ElementType>
    std::vector<ResultType> values(std::size_t reserveSize,
                                   const std::vector<ElementType> &queryValues)
    {
        std::vector<ResultType> resultValues;
        resultValues.reserve(reserveSize);

        for (const ElementType &queryValue : queryValues) {
            bindValues(queryValue);

            while (next())
                resultValues.push_back(value<ResultType>(0));

            reset();
        }

        return resultValues;
    }

    template <typename ResultType,
              typename... QueryType>
    std::vector<ResultType> values(std::size_t reserveSize, const QueryType&... queryValues)
    {
        std::vector<ResultType> resultValues;
        resultValues.reserve(reserveSize);

        bindValues(queryValues...);

        while (next())
            resultValues.push_back(value<ResultType>(0));

        reset();

        return resultValues;
    }
345 346

    template <typename Type>
347
    static Type toValue(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
348

349
    void prepare(Utils::SmallStringView sqlStatement);
350 351
    void waitForUnlockNotify() const;

352 353
    sqlite3 *sqliteDatabaseHandle() const;
    TextEncoding databaseTextEncoding();
354 355 356 357 358 359


    bool checkForStepError(int resultCode) const;
    void checkForPrepareError(int resultCode) const;
    void setIfIsReadyToFetchValues(int resultCode) const;
    void checkIfIsReadyToFetchValues() const;
360
    void checkColumnsAreValid(const std::vector<int> &columns) const;
361 362 363 364 365 366 367
    void checkColumnIsValid(int column) const;
    void checkBindingIndex(int index) const;
    void checkBindingName(int index) const;
    void setBindingParameterCount();
    void setBindingColumnNamesFromStatement();
    void setColumnCount();
    bool isReadOnlyStatement() const;
368
    Q_NORETURN void throwException(const char *whatHasHappened) const;
369 370

    template <typename ContainerType>
371
    ContainerType columnValues(const std::vector<int> &columnIndices) const;
372 373 374

    QString columnName(int column) const;

375 376
    SqliteDatabase &database() const;

377
protected:
378
    explicit SqliteStatement(Utils::SmallStringView sqlStatement,
379 380
                             SqliteDatabaseBackend &databaseBackend);

381
private:
Marco Bubke's avatar
Marco Bubke committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
    template <typename Container,
              typename... ResultType,
              int... Index>
    void emplaceTupleValues(Container &container, std::integer_sequence<int, Index...>)
    {
        container.emplace_back(value<ResultType>(Index)...);
    }

    template <typename Container,
              typename... ResultType>
    void emplaceTupleValues(Container &container)
    {
        emplaceTupleValues<Container, ResultType...>(container, std::make_integer_sequence<int, sizeof...(ResultType)>{});
    }

    template <typename Container,
              typename... ResultEntryType,
              int... Index>
    void pushBackStructValues(Container &container, std::integer_sequence<int, Index...>)
    {
        using ResultType = typename Container::value_type;
        container.push_back(ResultType{value<ResultEntryType>(Index)...});
    }

    template <typename Container,
              typename... ResultEntryType>
    void pushBackStructValues(Container &container)
    {
        pushBackStructValues<Container, ResultEntryType...>(container, std::make_integer_sequence<int, sizeof...(ResultEntryType)>{});
    }

413 414 415 416 417 418
    template<typename Type>
    void bindValuesByIndex(int index, Type value)
    {
        bind(index, value);
    }

Marco Bubke's avatar
Marco Bubke committed
419 420
    template<typename Type, typename... Value>
    void bindValuesByIndex(int index, Type value, Value... values)
421 422 423 424 425 426 427 428 429 430 431
    {
        bind(index, value);
        bindValuesByIndex(index + 1, values...);
    }

    template<typename Type>
    void bindValuesByName(Utils::SmallStringView name, Type value)
    {
       bind(bindingIndexForName(name), value);
    }

Marco Bubke's avatar
Marco Bubke committed
432 433
    template<typename Type, typename... Values>
    void bindValuesByName(Utils::SmallStringView name, Type value, Values... values)
434 435 436 437 438
    {
       bind(bindingIndexForName(name), value);
       bindValuesByName(values...);
    }

Marco Bubke's avatar
Marco Bubke committed
439 440 441 442 443 444 445 446 447 448 449 450 451
    template <typename TupleType, std::size_t... Index>
    void bindTupleValuesElement(const TupleType &tuple, std::index_sequence<Index...>)
    {
        bindValues(std::get<Index>(tuple)...);
    }

    template <typename TupleType,
              typename Indices = std::make_index_sequence<std::tuple_size<TupleType>::value>>
    void bindTupleValues(const TupleType &element)
    {
        bindTupleValuesElement(element, Indices());
    }

452
private:
Marco Bubke's avatar
Marco Bubke committed
453
    std::unique_ptr<sqlite3_stmt, void (*)(sqlite3_stmt*)> m_compiledStatement;
454
    Utils::SmallStringVector m_bindingColumnNames;
455
    SqliteDatabase &m_database;
Marco Bubke's avatar
Marco Bubke committed
456 457 458
    int m_bindingParameterCount;
    int m_columnCount;
    mutable bool m_isReadyToFetchValues;
459
};
Marco Bubke's avatar
Marco Bubke committed
460

Marco Bubke's avatar
Marco Bubke committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
extern template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, int value);
extern template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, long value);
extern template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, long long value);
extern template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, double value);
extern template SQLITE_EXPORT void SqliteStatement::bind(Utils::SmallStringView name, Utils::SmallStringView text);

extern template SQLITE_EXPORT int SqliteStatement::toValue<int>(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
extern template SQLITE_EXPORT long long SqliteStatement::toValue<long long>(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
extern template SQLITE_EXPORT double SqliteStatement::toValue<double>(Utils::SmallStringView sqlStatement, SqliteDatabase &database);
extern template SQLITE_EXPORT Utils::SmallString SqliteStatement::toValue<Utils::SmallString>(Utils::SmallStringView sqlStatement, SqliteDatabase &database);

template <> SQLITE_EXPORT int SqliteStatement::value<int>(int column) const;
template <> SQLITE_EXPORT long SqliteStatement::value<long>(int column) const;
template <> SQLITE_EXPORT long long SqliteStatement::value<long long>(int column) const;
template <> SQLITE_EXPORT double SqliteStatement::value<double>(int column) const;
extern template SQLITE_EXPORT Utils::SmallString SqliteStatement::value<Utils::SmallString>(int column) const;
extern template SQLITE_EXPORT Utils::PathString SqliteStatement::value<Utils::PathString>(int column) const;
Marco Bubke's avatar
Marco Bubke committed
478
} // namespace Sqlite