LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
pgimpl.h
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#pragma once
10
11#include <util/sll/visitor.h>
12#include "oraltypes.h"
13#include "oraldetailfwd.h"
14#include "impldefs.h"
15
17{
18 using QSqlQuery_ptr = std::shared_ptr<QSqlQuery>;
19
21 {
22 const QSqlDatabase DB_;
23
24 QSqlQuery_ptr Default_;
25 QSqlQuery_ptr Ignore_;
26
27 const QString InsertBase_;
28 const QString Updater_;
29 public:
30 InsertQueryBuilder (const QSqlDatabase& db, const CachedFieldsData& data)
31 : DB_ { db }
32 , InsertBase_ { "INSERT INTO " + data.Table_ +
33 " (" + data.Fields_.join (", ") + ") VALUES (" +
34 data.BoundFields_.join (", ") + ") " }
35 , Updater_ { Map (data.Fields_, [] (auto&& str) { return str + " = EXCLUDED." + str; }).join (", ") }
36 {
37 }
38
40 {
41 return Visit (action.Selector_,
42 [this] (InsertAction::DefaultTag) { return GetDefaultQuery (); },
43 [this] (InsertAction::IgnoreTag) { return GetIgnoreQuery (); },
44 [this] (InsertAction::Replace ct) { return MakeReplaceQuery (ct.Fields_); });
45 }
46 private:
47 QSqlQuery_ptr GetDefaultQuery ()
48 {
49 if (!Default_)
50 {
51 Default_ = std::make_shared<QSqlQuery> (DB_);
52 Default_->prepare (InsertBase_);
53 }
54 return Default_;
55 }
56
57 QSqlQuery_ptr GetIgnoreQuery ()
58 {
59 if (!Default_)
60 {
61 Default_ = std::make_shared<QSqlQuery> (DB_);
62 Default_->prepare (InsertBase_ + "ON CONFLICT DO NOTHING");
63 }
64 return Default_;
65 }
66
67 QSqlQuery_ptr MakeReplaceQuery (const QStringList& constraining)
68 {
69 auto query = std::make_shared<QSqlQuery> (DB_);
70 query->prepare (InsertBase_ + GetReplacer (constraining));
71 return query;
72 }
73
74 QString GetReplacer (const QStringList& constraining) const
75 {
76 return "ON CONFLICT (" +
77 constraining.join (", ") +
78 ") DO UPDATE SET " +
79 Updater_;
80 }
81 };
82
84 {
85 public:
86 struct TypeLits
87 {
88 inline static const QString IntAutoincrement { "SERIAL PRIMARY KEY" };
89 inline static const QString Binary { "BYTEA" };
90 };
91
92 inline static const QString LimitNone { "ALL" };
93
94 auto MakeInsertQueryBuilder (const QSqlDatabase& db, const CachedFieldsData& data) const
95 {
96 return std::make_unique<InsertQueryBuilder> (db, data);
97 }
98 };
99}
100
101namespace LC::Util::oral
102{
104}
auto MakeInsertQueryBuilder(const QSqlDatabase &db, const CachedFieldsData &data) const
Definition: pgimpl.h:94
InsertQueryBuilder(const QSqlDatabase &db, const CachedFieldsData &data)
Definition: pgimpl.h:30
QSqlQuery_ptr GetQuery(InsertAction action) override
Definition: pgimpl.h:39
Fields_t Fields_
std::shared_ptr< QSqlQuery > QSqlQuery_ptr
Definition: pgimpl.h:18
std::shared_ptr< QSqlQuery > QSqlQuery_ptr
Definition: oral.h:52
auto Visit(const Either< Left, Right > &either, Args &&... args)
Definition: either.h:200
auto Map(Container &&c, F f)
Definition: prelude.h:143
ActionSelector_t Selector_
Definition: oraltypes.h:200