1 /*
2  * SPDX-FileCopyrightText: (C) 2020 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #ifndef APREPAREDQUERY_H
7 #define APREPAREDQUERY_H
8 
9 #include <QString>
10 
11 #include <aqsqlexports.h>
12 
13 #define APreparedQueryLiteral(str) \
14     ([]() Q_DECL_NOEXCEPT -> APreparedQuery { \
15         static const APreparedQuery aprepared_literal_temp(str); \
16         return aprepared_literal_temp; \
17     }()) \
18     /**/
19 
20 /*!
21  * \brief The APreparedQuery class
22  *
23  * This class holds an identification to a prepared query, there
24  * are 3 ways for correct usage:
25  * * Creating it as a member of some object class.
26  * * Creating it as a static variable before using it with a database.
27  * * Using \sa APreparedQueryLiteral() macro which creates a static object.
28  *
29  * This class doesn't prepare the query on the database so it's thread-safe,
30  * once a database executes this prepared query it will check if the query was
31  * prepared, if not it will prepare, on successful preparation it will store
32  * the indication internally to know this was prepared already.
33  *
34  * This way this object can be seem as a simple generator of unique prepared
35  * statements identifiers (always prefixed with asql_), you can however for debugging
36  * purposes manually set the identification.
37  */
38 class ASQL_EXPORT APreparedQuery
39 {
40 public:
41     /*!
42      * \brief APreparedQuery constructs an empty and invalid prepared query object
43      */
44     APreparedQuery();
45 
46     /*!
47      * \brief APreparedQuery constructs a prepared \p query with an automatic unique identification
48      * \param query
49      */
50     APreparedQuery(const QString &query);
51 
52     /*!
53      * \brief APreparedQuery constructs a prepared \p query with an automatic unique identification
54      * \param query
55      */
56     APreparedQuery(QStringView query);
57 
58     /*!
59      * \brief APreparedQuery constructs a prepared \p query with a manual \p identification
60      * \param query
61      */
62     APreparedQuery(const QString &query, const QString &identification);
63 
64     QByteArray query() const;
65     QByteArray identification() const;
66 
67 private:
68     QByteArray m_query;
69     QByteArray m_identification;
70 };
71 
72 #endif // APREPAREDQUERY_H
73