forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.cpp
More file actions
148 lines (124 loc) · 4.24 KB
/
Query.cpp
File metadata and controls
148 lines (124 loc) · 4.24 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
#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(m_column_names.at(i->first));
std::string column = sqlb::escapeIdentifier(m_column_names.at(i->first));
if(it != m_selected_columns.cend() && it->selector != column)
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)
{
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)
{
if(sorted_column.column < m_column_names.size())
order_by += sqlb::escapeIdentifier(m_column_names.at(sorted_column.column)) + " "
+ (sorted_column.direction == sqlb::Ascending ? "ASC" : "DESC") + ",";
}
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;
});
}
}