-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathQuery.cpp
More file actions
150 lines (126 loc) · 4.45 KB
/
Query.cpp
File metadata and controls
150 lines (126 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "Query.h"
#include <algorithm>
namespace sqlb
{
void Query::clear()
{
m_table.clear();
m_rowid_columns = {"_rowid_"};
m_selected_columns.clear();
m_where.clear();
m_sort.clear();
}
std::string Query::buildWherePart() const
{
if(m_where.empty() && m_global_where.empty())
return std::string();
std::string where = "WHERE ";
if(m_where.size())
{
for(auto i=m_where.cbegin();i!=m_where.cend();++i)
{
const auto it = findSelectedColumnByName(i->first);
std::string column = sqlb::escapeIdentifier(i->first);
if(it != m_selected_columns.cend() && it->selector != i->first)
column = it->selector;
where += column + " " + i->second + " AND ";
}
// Remove last ' AND '
where.erase(where.size() - 5);
}
if(m_global_where.size())
{
// Connect to previous conditions if there are any
if(m_where.size())
where += " AND ";
// For each condition
for(const auto& w : m_global_where)
{
where += "(";
// For each selected column
for(const auto& c : m_column_names)
{
const auto it = findSelectedColumnByName(c);
std::string column = sqlb::escapeIdentifier(c);
if(it != m_selected_columns.cend() && it->selector != column)
column = it->selector;
where += column + " " + w + " OR ";
}
// Remove last ' OR '
where.erase(where.size() - 4);
where += ") AND ";
}
// Remove last ' AND '
where.erase(where.size() - 5);
}
return where;
}
std::string Query::buildQuery(bool withRowid) const
{
// Selector and display formats
std::string selector;
if (withRowid)
{
// We select the rowid data into a JSON array in case there are multiple rowid columns in order to have all values at hand.
// If there is only one rowid column, we leave it as is.
if(m_rowid_columns.size() == 1)
{
// As of SQLite 3.36 when selecting rowid from a view SQLite does not return NULL anymore but instead returns "rowid" and throws
// an error. To avoid that error (we don't actually care for the value of this column, so the value is not the issue here) we
// explicitly select NULL (without any quotes) here.
if(m_is_view && !hasCustomRowIdColumn())
selector = "NULL,";
else
selector = sqlb::escapeIdentifier(m_rowid_columns.at(0)) + ",";
} else {
selector += "sqlb_make_single_value(";
for(size_t i=0;i<m_rowid_columns.size();i++)
selector += sqlb::escapeIdentifier(m_rowid_columns.at(i)) + ",";
selector.pop_back(); // Remove the last comma
selector += "),";
}
}
if(m_selected_columns.empty())
{
selector += "*";
} else {
for(const auto& it : m_selected_columns)
{
if (it.original_column != it.selector)
selector += it.selector + " AS " + sqlb::escapeIdentifier(it.original_column) + ",";
else
selector += sqlb::escapeIdentifier(it.original_column) + ",";
}
selector.pop_back();
}
// Filter
std::string where = buildWherePart();
// Sorting
std::string order_by;
for(const auto& sorted_column : m_sort)
order_by += sorted_column.toSql() + ",";
if(order_by.size())
{
order_by.pop_back();
order_by = "ORDER BY " + order_by;
}
return "SELECT " + selector + " FROM " + m_table.toString() + " " + where + " " + order_by;
}
std::string Query::buildCountQuery() const
{
// Build simplest count query for this (filtered) table
return "SELECT COUNT(*) FROM " + m_table.toString() + " " + buildWherePart();
}
std::vector<SelectedColumn>::iterator Query::findSelectedColumnByName(const std::string& name)
{
return std::find_if(m_selected_columns.begin(), m_selected_columns.end(), [name](const SelectedColumn& c) {
return name == c.original_column;
});
}
std::vector<SelectedColumn>::const_iterator Query::findSelectedColumnByName(const std::string& name) const
{
return std::find_if(m_selected_columns.cbegin(), m_selected_columns.cend(), [name](const SelectedColumn& c) {
return name == c.original_column;
});
}
}