Line data Source code
1 : /**
2 : Copyright (c) 2018-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023-2024 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef STAPPLER_DB_SQL_SPSQLQUERY_H_
25 : #define STAPPLER_DB_SQL_SPSQLQUERY_H_
26 :
27 : #include "SPDbBackendInterface.h"
28 : #include "SPDbWorker.h"
29 :
30 : namespace STAPPLER_VERSIONIZED stappler::db::sql {
31 :
32 : class Driver;
33 :
34 : class SqlQuery : public stappler::sql::Query<db::Binder, Interface> {
35 : public:
36 : struct Context : FieldResolver {
37 : Context(SqlQuery &, const Scheme &scheme, const Worker &w, const db::Query &q);
38 :
39 : SqlQuery *_this;
40 :
41 : bool hasAltLimit = false;
42 : bool softLimitIsFts = false;
43 : StringView softLimitField;
44 :
45 : StringView getAlt(StringView);
46 : };
47 :
48 : using TypeString = db::Binder::TypeString;
49 :
50 17650 : virtual ~SqlQuery() = default;
51 :
52 : SqlQuery(db::QueryInterface *, const Driver *);
53 :
54 : void clear();
55 :
56 : virtual bool writeQuery(Context &);
57 : virtual bool writeQuery(Context &, const db::Scheme &scheme, uint64_t, const db::Field &f);
58 :
59 : virtual void writeWhere(SqlQuery::SelectWhere &, db::Operator op, const db::Scheme &, const db::Query &);
60 : virtual void writeWhere(SqlQuery::WhereContinue &, db::Operator op, const db::Scheme &, const db::Query &);
61 :
62 : virtual void writeWhereItem(SqlQuery::WhereContinue &w, db::Operator op, const db::Scheme &scheme, const db::Query::Select &sel);
63 : virtual void writeWhereCond(SqlQuery::WhereContinue &w, db::Operator op, const db::Scheme &scheme, const db::Worker::ConditionData &sel);
64 :
65 : virtual void writeOrdering(SqlQuery::SelectFrom &, const db::Scheme &, const db::Query &, bool dropLimits = false);
66 :
67 : virtual SelectFrom writeSelectFrom(GenericQuery &q, const db::QueryList::Item &item, bool idOnly, const StringView &scheme, const StringView &field, bool isSimpleGet = false);
68 : virtual SelectFrom writeSelectFrom(Select &sel, Context &);
69 :
70 : virtual void writeQueryReqest(SqlQuery::SelectFrom &s, const db::QueryList::Item &item);
71 : virtual void writeQueryListItem(GenericQuery &sq, const db::QueryList &list, size_t idx, bool idOnly, const db::Field *field = nullptr, bool forSubquery = false);
72 : virtual void writeQueryList(const db::QueryList &query, bool idOnly, size_t count = stappler::maxOf<size_t>());
73 : virtual void writeQueryFile(const ApplicationInterface *app, const db::QueryList &query, const db::Field *field);
74 : virtual void writeQueryArray(const db::QueryList &query, const db::Field *field);
75 :
76 : virtual void writeQueryDelta(const db::Scheme &, const stappler::Time &, const Set<const db::Field *> &fields, bool idOnly);
77 : virtual void writeQueryViewDelta(const db::QueryList &list, const stappler::Time &, const Set<const db::Field *> &fields, bool idOnly);
78 :
79 : template <typename T>
80 375 : friend auto & operator << (SqlQuery &query, const T &value) {
81 375 : return query.stream << value;
82 : }
83 :
84 : const StringStream &getQuery() const;
85 : db::QueryInterface * getInterface() const;
86 :
87 : virtual void writeFullTextFrom(SelectFrom &sel, const Scheme &scheme, const db::Field *, const db::Query::Select &it);
88 : virtual void writeFullTextRank(Select &sel, const Scheme &scheme, const db::Query &q);
89 :
90 : virtual void writeFullTextWhere(WhereContinue &w, db::Operator op, const db::Scheme &scheme, const db::Query::Select &sel, StringView ftsQuery);
91 :
92 : virtual StringView getFullTextQuery(const Scheme &scheme, const db::Field &f, const db::Query::Select &it);
93 :
94 : protected:
95 : const Driver *_driver = nullptr;
96 : std::forward_list<FullTextQuery> _parsedQueries;
97 : Map<String, String> _fulltextQueries;
98 : };
99 :
100 : }
101 :
102 : #endif /* STAPPLER_DB_SQL_SPSQLQUERY_H_ */
|