Qx v0.7
Qt Extensions Library
Loading...
Searching...
No Matches
qx-sqlstring.h
1#ifndef QX_SQLSTRING_H
2#define QX_SQLSTRING_H
3
4// Shared Lib Support
5#include "qx/sql/qx_sql_export.h"
6
7// Extra-component Includes
9
10using namespace Qt::StringLiterals;
11
12// Forwards
13class QUuid;
14namespace Qx
15{
16class SqlQuery;
17class SqlString;
18}
19
20namespace QxSql
21{
22inline Qx::SqlString operator""_sq(const char16_t* str, size_t size) noexcept;
23inline Qx::SqlString operator""_sqi(const char16_t* str, size_t size) noexcept;
24inline Qx::SqlString operator""_sqs(const char16_t* str, size_t size) noexcept;
25};
26
27namespace Qx
28{
29template<typename T>
30concept sql_string = std::same_as<T, SqlString>;
31
32template<typename T>
33concept sql_stringable = std::convertible_to<T, SqlString> || std::constructible_from<SqlString, T>;
34
35class QX_SQL_EXPORT SqlString
36{
37 friend SqlString QxSql::operator""_sq(const char16_t* str, size_t size) noexcept;
38 friend SqlString QxSql::operator""_sqi(const char16_t* str, size_t size) noexcept;
39 friend SqlString QxSql::operator""_sqs(const char16_t* str, size_t size) noexcept;
40 friend SqlString operator!(const SqlString& s);
41 friend SqlString operator&&(const SqlString& lhs, const SqlString& rhs);
42 friend SqlString operator||(const SqlString& lhs, const SqlString& rhs);
43 friend SqlString operator==(const SqlString& lhs, const SqlString& rhs);
44 friend SqlString operator!=(const SqlString& lhs, const SqlString& rhs);
45 friend SqlString operator<(const SqlString& lhs, const SqlString& rhs);
46 friend SqlString operator<=(const SqlString& lhs, const SqlString& rhs);
47 friend SqlString operator>(const SqlString& lhs, const SqlString& rhs);
48 friend SqlString operator>=(const SqlString& lhs, const SqlString& rhs);
49 friend SqlString operator==(const SqlString& a, const SqlQuery& b);
50 friend SqlString operator!=(const SqlString& a, const SqlQuery& b);
51 friend SqlString operator<(const SqlString& a, const SqlQuery& b);
52 friend SqlString operator<=(const SqlString& a, const SqlQuery& b);
53 friend SqlString operator>(const SqlString& a, const SqlQuery& b);
54 friend SqlString operator>=(const SqlString& a, const SqlQuery& b);
55 friend SqlString operator|=(const SqlString& a, const SqlString& b);
56 friend SqlString operator&=(const SqlString& a, const SqlString& b);
57
58//-Class Enums------------------------------------------------------------------------------------------------
59private:
60 enum Type { Default, Identifier, Literal };
61
62//-Instance Variables-----------------------------------------------------------------------------------------------
63private:
64 QString mStr;
65
66//-Constructor-------------------------------------------------------------------------------------------------
67private:
68 explicit SqlString(const char16_t* str, size_t size, Type type = Default);
69
70public:
71 explicit SqlString();
72 explicit SqlString(QString&& str); // Could possibly be implicit instead
73 explicit SqlString(const QString& str); // Could possibly be implicit instead
74
75 SqlString(bool b);
76 SqlString(const QUuid& id);
77
78 template<Qx::arithmetic N>
79 SqlString(N n) : mStr(QString::number(n)) {}
80
81 /* This is likely too broad, as it's better if we only allow a subquery to be inserted where it makes sense,
82 * though if we get stuck on how to enable use of a subquery somewhere, this can be enabled as a fallback, as
83 * the functions that explicitly take SqlQuery will be preferred over implicitly converting to SqlString anyway
84 */
85 //SqlString(const SqlQuery& q);
86
87//-Class Functions------------------------------------------------------------------------------------------------------
88public:
89 static SqlString makeRegular(const char16_t* str) noexcept;
90 static SqlString makeIdentifier(const char16_t* str) noexcept;
91 static SqlString makeLiteral(const char16_t* str) noexcept;
92
93//-Instance Functions------------------------------------------------------------------------------------------------------
94public:
95 QString toString() const;
96 bool isEmpty() const;
97
98//-Operators------------------------------------------------------------------------------------------------------
99public:
100 SqlString& operator+=(const SqlString& s) { mStr += s.mStr; return *this; }
101};
102
103//-Operators------------------------------------------------------------------------------------------------------
104// Standard
105inline SqlString operator!(const SqlString& s) { return SqlString(u"NOT "_s + s.mStr); }
106inline SqlString operator&&(const SqlString& a, const SqlString& b) { return SqlString(u"("_s + a.mStr + u" AND "_s + b.mStr + u")"_s); }
107inline SqlString operator||(const SqlString& a, const SqlString& b) { return SqlString(u"("_s + a.mStr + u" OR "_s + b.mStr + u")"_s); }
108inline SqlString operator==(const SqlString& a, const SqlString& b) { return SqlString(a.mStr + u" = "_s + b.mStr); }
109inline SqlString operator!=(const SqlString& a, const SqlString& b) { return SqlString(a.mStr + u" <> "_s + b.mStr); }
110inline SqlString operator<(const SqlString& a, const SqlString& b) { return SqlString(a.mStr + u" < "_s + b.mStr); }
111inline SqlString operator<=(const SqlString& a, const SqlString& b) { return SqlString(a.mStr + u" <= "_s + b.mStr); }
112inline SqlString operator>(const SqlString& a, const SqlString& b) { return SqlString(a.mStr + u" > "_s + b.mStr); }
113inline SqlString operator>=(const SqlString& a, const SqlString& b) { return SqlString(a.mStr + u" >= "_s + b.mStr); }
114
115// Sub-query
116SqlString operator==(const SqlString& s, const SqlQuery& q);
117SqlString operator!=(const SqlString& s, const SqlQuery& q);
118SqlString operator<(const SqlString& s, const SqlQuery& q);
119SqlString operator<=(const SqlString& s, const SqlQuery& q);
120SqlString operator>(const SqlString& s, const SqlQuery& q);
121SqlString operator>=(const SqlString& s, const SqlQuery& q);
122
123// Special (use of assignment operators for non-assignment here is because many operators are already used in SQL)
124// Concat (with space): Chosen cause + is taken and separating words with just a space is generally only use for
125// shorthand aliases and "or equal" kind of matches saying "this is an alias", like "otherwise known as"
126inline SqlString operator|=(const SqlString& a, const SqlString& b) { return SqlString(a.mStr + u" "_s + b.mStr); }
127// Concat: Chosen cause + is taken
128inline SqlString operator&=(const SqlString& a, const SqlString& b) { return SqlString(a.mStr + b.mStr); }
129
130} // namespace Qx
131
132namespace QxSql
133{
134
135inline Qx::SqlString operator""_sq(const char16_t* str, size_t size) noexcept { return Qx::SqlString(str, size); }
136inline Qx::SqlString operator""_sqi(const char16_t* str, size_t size) noexcept { return Qx::SqlString(str, size, Qx::SqlString::Identifier); }
137inline Qx::SqlString operator""_sqs(const char16_t* str, size_t size) noexcept { return Qx::SqlString(str, size, Qx::SqlString::Literal); }
138
139inline Qx::SqlString sq(const QString& s) noexcept { return Qx::SqlString(s); }
140inline Qx::SqlString sqi(const QString& s) noexcept { return Qx::SqlString(u'"' + s + u'"'); }
141inline Qx::SqlString sqs(const QString& s) noexcept { return Qx::SqlString(u'\'' + s + u'\''); }
142}
143
144#endif // QX_SQLSTRING_H
SqlQuery is a base class from which all query types derive.
Definition qx-sqlquery.h:171
The SqlString class is a convenience class for more easily building SQL statements in a natural manne...
Definition qx-sqlstring.h:36
SqlString & operator+=(const SqlString &s)
Definition qx-sqlstring.h:100
SqlString(N n)
Definition qx-sqlstring.h:79
Specifies that a type is the same as SqlString.
Definition qx-sqlstring.h:30
Specifies that a type can be converted to SqlString, or used to construct a SqlString.
Definition qx-sqlstring.h:33
The QxSql namespace contains several utilities that are specific to the SQL module and grouped togeth...
Definition qx-sqlconcepts.dox:10
Qx::SqlString sqs(const QString &s) noexcept
Definition qx-sqlstring.h:141
Qx::SqlString sq(const QString &s) noexcept
Definition qx-sqlstring.h:139
Qx::SqlString sqi(const QString &s) noexcept
Definition qx-sqlstring.h:140
The Qx namespace is the main namespace through which all non-global functionality of the Qx library i...
Definition qx-abstracterror.cpp:13
SqlString operator&&(const SqlString &a, const SqlString &b)
Definition qx-sqlstring.h:106
SqlString operator<=(const SqlString &s, const Qx::SqlQuery &q)
Definition qx-sqlstring.cpp:214
SqlString operator!(const SqlString &s)
Definition qx-sqlstring.h:105
SqlString operator<(const SqlString &s, const Qx::SqlQuery &q)
Definition qx-sqlstring.cpp:209
SqlString operator>=(const SqlString &s, const Qx::SqlQuery &q)
Definition qx-sqlstring.cpp:224
SqlString operator&=(const SqlString &a, const SqlString &b)
Definition qx-sqlstring.h:128
SqlString operator!=(const SqlString &s, const Qx::SqlQuery &q)
Definition qx-sqlstring.cpp:204
SqlString operator|=(const SqlString &a, const SqlString &b)
Definition qx-sqlstring.h:126
SqlString operator||(const SqlString &a, const SqlString &b)
Definition qx-sqlstring.h:107
SqlString operator>(const SqlString &s, const Qx::SqlQuery &q)
Definition qx-sqlstring.cpp:219
SqlString operator==(const SqlString &s, const Qx::SqlQuery &q)
Definition qx-sqlstring.cpp:199
The qx-concepts header file provides a library of general purpose concepts as an extension of the sta...