Qx v0.7
Qt Extensions Library
Loading...
Searching...
No Matches
qx-sqlquery.h File Reference

The qx-sql header file offers a straightforward interface for querying an SQL database. More...

#include "qx/sql/qx_sql_export.h"
#include <QSqlDatabase>
#include <QUuid>
#include <QSqlQuery>
#include "qx/sql/qx-sqlconcepts.h"
#include "qx/sql/qx-sqlerror.h"
#include "qx/sql/qx-sqlresult.h"
#include "qx/sql/qx-sqlstring.h"
#include "qx/sql/__private/qx-sqlquery_p.h"
#include "qx/sql/__private/qx-sqlstring_helpers.h"
#include "qx/utility/qx-macros.h"
Include dependency graph for qx-sqlquery.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Qx::AbstractSqlQuery< Derived >
 AbstractSqlQuery is a common base class from which all query types are derived, and provides common SQL keywords. More...
 
class  Qx::SqlDmlQuery
 The SqlDmlQuery class represents SQL queries that fit into the data manipulation language sub-language. More...
 
class  Qx::SqlDqlQuery
 The SqlDqlQuery class represents SQL queries that fit into the data query language sub-language. More...
 
class  Qx::SqlQuery
 SqlQuery is a base class from which all query types derive. More...
 

Namespaces

namespace  Qx
 The Qx namespace is the main namespace through which all non-global functionality of the Qx library is accessed.
 

Macros

#define QX_SQL_MEMBER(member)
 
#define QX_SQL_MEMBER_ALIASED(member, field)
 
#define QX_SQL_MEMBER_OVERRIDE(Struct, member, ...)
 
#define QX_SQL_QUERY_STRUCT(struct_name, id, ...)
 
#define QX_SQL_STRUCT(id, ...)
 
#define QX_SQL_STRUCT_FULL(id, query_struct, ...)
 
#define QX_SQL_STRUCT_OUTSIDE(Struct, id, ...)
 
#define QX_SQL_STRUCT_OUTSIDE_FRIEND(struct_type)
 
#define QX_SQL_STRUCT_OUTSIDE_FULL(Struct, id, query_struct, ...)
 
#define QX_SQL_STRUCT_OUTSIDE_X(Struct, id, ...)
 
#define QX_SQL_STRUCT_X(id, ...)
 

Detailed Description

Most significantly this file provides access to Qx Declarative SQL.

See also
QX_JSQL_STRUCT(), and QxSql.

Macro Definition Documentation

◆ QX_SQL_MEMBER

#define QX_SQL_MEMBER ( member)
Value:
QxSqlPrivate::makeMemberMetadata<#member>(&StructT::member)

Used to denote a SQL struct member whose name matches its field when using QX_SQL_STRUCT_X().

See also
QX_SQL_STRUCT_X()

◆ QX_SQL_MEMBER_ALIASED

#define QX_SQL_MEMBER_ALIASED ( member,
field )
Value:
QxSqlPrivate::makeMemberMetadata<field>(&StructT::member)

Used to denote a SQL struct member with a field that is different from its name when using QX_SQL_STRUCT_X().

field is to be a string literal.

See also
QX_SQL_STRUCT_X()

◆ QX_SQL_MEMBER_OVERRIDE

#define QX_SQL_MEMBER_OVERRIDE ( Struct,
member,
... )
Value:
namespace QxSql \
{ \
template<> \
struct MemberOverrideConverter<Struct, #member> \
{ \
__VA_ARGS__\
}; \
}
The QxSql namespace contains several utilities that are specific to the SQL module and grouped togeth...
Definition qx-sqlconcepts.dox:10

Used to define a member/field specific value parsing/serialzing override for a SQL-tried struct. The specified member will be processed using the provided functions instead of potentially available generic ones for that type.

Must be used in namespace scope, outside of the struct definition.

struct MySpecialStruct
{
int number;
QString name;
QX_SQL_STRUCT(number, name);
}
QX_SQL_MEMBER_OVERRIDE(MySpecialStruct, name,
static Qx::SqlError fromSql(QString& member, const QVariant& vv)
{
// Add prefix when parsing
member = "Prefix" + vv.toString();
return Qx::JsonError();
}
static QVariant toSql(const QString& member)
{
// Remove prefix when serializing
return member.remove("Prefix");
}
)

◆ QX_SQL_QUERY_STRUCT

#define QX_SQL_QUERY_STRUCT ( struct_name,
id,
... )
Value:
struct struct_name \
{ \
static inline const Qx::SqlString _ = Qx::SqlString::makeIdentifier(u ## id); \
QX_FOR_EACH(__QX_SQL_QUERY_STRUCT_MEMBER, __VA_ARGS__) \
};
The SqlString class is a convenience class for more easily building SQL statements in a natural manne...
Definition qx-sqlstring.h:36
static SqlString makeIdentifier(const char16_t *str) noexcept
Definition qx-sqlstring.cpp:128

Creates a struct named struct_name that is intended to reflect the same members from the original struct as quoted strings, for conveniently using them as identifiers in an SQL statement.

Additionally, an identifier for the struct itself is created as the member _ using id.

Technically however, this struct isn't directly tied to the original, so it can bear any name and placed anywhere.

◆ QX_SQL_STRUCT

#define QX_SQL_STRUCT ( id,
... )
Value:
__QX_SQL_META_STRUCT_INSIDE(id, std::make_tuple(QX_FOR_EACH_DELIM(QX_SQL_MEMBER, __VA_ARGS__)))
#define QX_FOR_EACH_DELIM(macro,...)
Definition qx-macros.h:48
#define QX_SQL_MEMBER(member)
Definition qx-sqlquery.h:64

Specifies that the surrounding struct is a SQL-tied struct, which enables support for automatic parsing/serializing or binding of a corresponding SQL row.

The name of each included member (varargs) must match the name their corresponding SQL field.

id is the identifier of the table that the struct represent in the database.

struct MyStruct
{
int number;
QString name;
QX_SQL_STRUCT(number, name);
}
See also
QX_SQL_STRUCT_OUTSIDE() and QX_SQL_STRUCT_X()

◆ QX_SQL_STRUCT_FULL

#define QX_SQL_STRUCT_FULL ( id,
query_struct,
... )
Value:
QX_SQL_STRUCT(id, __VA_ARGS__) \
QX_SQL_QUERY_STRUCT(query_struct, id, __VA_ARGS__)
#define QX_SQL_STRUCT(id,...)
Definition qx-sqlquery.h:73

Same as QX_SQL_STRUCT(), but also invokes QX_SQL_QUERY_STRUCT() for you at the same time, using query_struct as the query struct name.

See also
QX_SQL_STRUCT_OUTSIDE_FULL()

◆ QX_SQL_STRUCT_OUTSIDE

#define QX_SQL_STRUCT_OUTSIDE ( Struct,
id,
... )
Value:
__QX_SQL_META_STRUCT_OUTSIDE(Struct, id, std::make_tuple(QX_FOR_EACH_DELIM(QX_SQL_MEMBER, __VA_ARGS__)))

Same as QX_SQL_STRUCT(), but is used outside of a struct instead of inside, with Struct used to identify the target struct.

struct MyStruct
{
int number;
QString name;
}
// At global scope
QX_SQL_STRUCT_OUTSIDE(number, name);

This is useful for hiding the SQL parsing implementation details of a public struct within a different source file.

See also
QX_SQL_STRUCT()

◆ QX_SQL_STRUCT_OUTSIDE_FRIEND

#define QX_SQL_STRUCT_OUTSIDE_FRIEND ( struct_type)
Value:
friend QxSql::QxSqlMetaStructOutside<struct_type, struct_type>;

Place this inside of a class to enable the use of QX_SQL_STRUCT_OUTSIDE() and QX_SQL_STRUCT_OUTSIDE_X() with a struct/class with private data members.

QX_SQL_STRUCT() and QX_SQL_STRUCT_X() have no need for this in either case.

◆ QX_SQL_STRUCT_OUTSIDE_FULL

#define QX_SQL_STRUCT_OUTSIDE_FULL ( Struct,
id,
query_struct,
... )
Value:
QX_SQL_STRUCT_OUTSIDE(Struct, id, __VA_ARGS__) \
QX_SQL_QUERY_STRUCT(query_struct, id, __VA_ARGS__)
#define QX_SQL_STRUCT_OUTSIDE(Struct, id,...)
Definition qx-sqlquery.h:80

Same as QX_SQL_STRUCT_OUTSIDE(), but also invokes QX_SQL_QUERY_STRUCT() for you at the same time, using query_struct as the query struct name.

See also
QX_SQL_STRUCT_FULL()

◆ QX_SQL_STRUCT_OUTSIDE_X

#define QX_SQL_STRUCT_OUTSIDE_X ( Struct,
id,
... )
Value:
__QX_SQL_META_STRUCT_OUTSIDE(Struct, id, std::make_tuple(__VA_ARGS__))

Same as QX_SQL_STRUCT_X(), but is used outside of a struct instead of inside.

Note
When using this macro, you must manually invoke QX_SQL_QUERY_STRUCT_OUTSIDE() if you want to generate the SQL identifier convenience struct.
See also
QX_SQL_STRUCT_OUTSIDE()

◆ QX_SQL_STRUCT_X

#define QX_SQL_STRUCT_X ( id,
... )
Value:
__QX_SQL_META_STRUCT_INSIDE(id, std::make_tuple(__VA_ARGS__))

Same as QX_SQL_STRUCT(), but allows for customization of member fields.

Each member must be wrapped in QX_SQL_MEMBER() or QX_SQL_MEMBER_ALIASED().

struct MyStruct
{
int number;
QString name;
QX_SQL_MEMBER(number),
QX_SQL_MEMBER_ALIASED(name, "aliasName")
);
}