forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumnDisplayFormatDialog.cpp
More file actions
169 lines (153 loc) · 8.57 KB
/
ColumnDisplayFormatDialog.cpp
File metadata and controls
169 lines (153 loc) · 8.57 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <QMessageBox>
#include "ColumnDisplayFormatDialog.h"
#include "ui_ColumnDisplayFormatDialog.h"
#include "sql/sqlitetypes.h"
#include "sqlitedb.h"
ColumnDisplayFormatDialog::ColumnDisplayFormatDialog(DBBrowserDB& db, const sqlb::ObjectIdentifier& tableName, const QString& colname, const QString& current_format, QWidget* parent)
: QDialog(parent),
ui(new Ui::ColumnDisplayFormatDialog),
column_name(colname),
pdb(db),
curTable(tableName)
{
// Create UI
ui->setupUi(this);
ui->comboDisplayFormat->addItem(tr("Default"), "default");
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Decimal number"), "decimal");
ui->comboDisplayFormat->addItem(tr("Exponent notation"), "exponent");
ui->comboDisplayFormat->addItem(tr("Hex blob"), "hexblob");
ui->comboDisplayFormat->addItem(tr("Hex number"), "hex");
ui->comboDisplayFormat->addItem(tr("Octal number"), "octal");
ui->comboDisplayFormat->addItem(tr("Round number"), "round");
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Apple NSDate to date"), "appleDate");
ui->comboDisplayFormat->addItem(tr("Java epoch (milliseconds) to date"), "javaEpoch");
ui->comboDisplayFormat->addItem(tr(".NET DateTime.Ticks to date"), "dotNetTicks");
ui->comboDisplayFormat->addItem(tr("Julian day to date"), "julian");
ui->comboDisplayFormat->addItem(tr("Unix epoch to date"), "epoch");
ui->comboDisplayFormat->addItem(tr("Unix epoch to local time"), "epochLocalTime");
ui->comboDisplayFormat->addItem(tr("Windows DATE to date"), "winDate");
ui->comboDisplayFormat->addItem(tr("Date as dd/mm/yyyy"), "ddmmyyyyDate");
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Lower case"), "lower");
ui->comboDisplayFormat->addItem(tr("Upper case"), "upper");
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Binary GUID to text"), "guid");
ui->comboDisplayFormat->addItem(tr("SpatiaLite Geometry to SVG"), "geomToSVG");
ui->comboDisplayFormat->insertSeparator(ui->comboDisplayFormat->count());
ui->comboDisplayFormat->addItem(tr("Custom"), "custom");
ui->labelDisplayFormat->setText(ui->labelDisplayFormat->text().arg(column_name));
const QString e_column_name = sqlb::escapeIdentifier(column_name);
formatFunctions["decimal"] = "printf('%d', " + e_column_name + ")";
formatFunctions["exponent"] = "printf('%e', " + e_column_name + ")";
formatFunctions["hexblob"] = "hex(" + e_column_name + ")";
formatFunctions["hex"] = "printf('0x%x', " + e_column_name + ")";
formatFunctions["octal"] = "printf('%o', " + e_column_name + ")";
formatFunctions["round"] = "round(" + e_column_name + ")";
formatFunctions["appleDate"] = "datetime('2001-01-01', " + e_column_name + " || ' seconds')";
formatFunctions["javaEpoch"] = "strftime('%Y-%m-%d %H:%M:%S.', " + e_column_name +
"/1000, 'unixepoch') || (" + e_column_name + "%1000)";
formatFunctions["dotNetTicks"] = "datetime(" + e_column_name + " / 10000000 - 62135596800, 'unixepoch')";
formatFunctions["julian"] = "datetime(" + e_column_name + ")";
formatFunctions["epoch"] = "datetime(" + e_column_name + ", 'unixepoch')";
formatFunctions["epochLocalTime"] = "datetime(" + e_column_name + ", 'unixepoch', 'localtime')";
formatFunctions["winDate"] = "datetime('1899-12-30', " + e_column_name + " || ' days')";
formatFunctions["ddmmyyyyDate"] = "strftime('%d/%m/%Y', " + e_column_name + ")";
formatFunctions["lower"] = "lower(" + e_column_name + ")";
formatFunctions["upper"] = "upper(" + e_column_name + ")";
formatFunctions["guid"] = "substr(hex(" + e_column_name + "), 7, 2) || " +
"substr(hex(" + e_column_name + "), 5, 2) || " +
"substr(hex(" + e_column_name + "), 3, 2) || " +
"substr(hex(" + e_column_name + "), 1, 2) || '-' || " +
"substr(hex(" + e_column_name + "), 11, 2) || " +
"substr(hex(" + e_column_name + "), 9, 2) || '-' || " +
"substr(hex(" + e_column_name + "), 15, 2) || " +
"substr(hex(" + e_column_name + "), 13, 2) || '-' || " +
"substr(hex(" + e_column_name + "), 17, 4) || '-' || " +
"substr(hex(" + e_column_name + "), 21, 12)";
formatFunctions["geomToSVG"] = QString(
R"('<svg xmlns="http://www.w3.org/2000/svg">'
|| '<path d="'
|| AsSVG(
ScaleCoords(
ST_Translate(%1, -St_MinX(%1), -St_MaxY(%1), 0)
, 639 / (MAX(St_MaxX(%1) - St_MinX(%1), St_MaxY(%1) - St_MinY(%1))))
, 1, 5)
|| '" stroke="darkblue" fill="#b5cfed" stroke-width="1"/>'
|| '</svg>')").arg(e_column_name);
// Set the current format, if it's empty set the default format
if(current_format.isEmpty())
{
ui->comboDisplayFormat->setCurrentIndex(0);
updateSqlCode();
} else {
// When it doesn't match any predefined format, it is considered custom
QString formatName = "custom";
auto it = std::find_if(formatFunctions.begin(), formatFunctions.end(), [current_format](const std::pair<std::string, QString>& s) {
return s.second == current_format;
});
if(it != formatFunctions.end())
formatName = QString::fromStdString(it->first);
ui->comboDisplayFormat->setCurrentIndex(ui->comboDisplayFormat->findData(formatName));
ui->editDisplayFormat->setText(current_format);
}
}
ColumnDisplayFormatDialog::~ColumnDisplayFormatDialog()
{
delete ui;
}
void ColumnDisplayFormatDialog::showEvent(QShowEvent *)
{
adjustSize();
}
QString ColumnDisplayFormatDialog::selectedDisplayFormat() const
{
if(ui->comboDisplayFormat->currentData().toString() == "default")
return QString();
else
return ui->editDisplayFormat->text();
}
void ColumnDisplayFormatDialog::updateSqlCode()
{
std::string format = ui->comboDisplayFormat->currentData().toString().toStdString();
if(format == "default")
ui->editDisplayFormat->setText(sqlb::escapeIdentifier(column_name));
else if(format != "custom")
ui->editDisplayFormat->setText(formatFunctions.at(format));
}
void ColumnDisplayFormatDialog::accept()
{
QString errorMessage;
// Accept the SQL code if it's the column name (default), it contains a function invocation applied to the column name and it can be
// executed without errors returning only one column.
// Users could still devise a way to break this, but this is considered good enough for letting them know about simple incorrect
// cases.
if(!(ui->editDisplayFormat->text() == sqlb::escapeIdentifier(column_name) ||
ui->editDisplayFormat->text().contains(QRegularExpression("[a-z]+[a-z_0-9]* *\\(.*" + QRegularExpression::escape(sqlb::escapeIdentifier(column_name)) + ".*\\)", QRegularExpression::CaseInsensitiveOption))))
errorMessage = tr("Custom display format must contain a function call applied to %1").arg(sqlb::escapeIdentifier(column_name));
else {
// Execute a query using the display format and check that it only returns one column.
int customNumberColumns = 0;
DBBrowserDB::execCallback callback = [&customNumberColumns](int numberColumns, std::vector<QByteArray>, std::vector<QByteArray>) -> bool {
customNumberColumns = numberColumns;
// Return false so the query is not aborted and no error is reported.
return false;
};
if(!pdb.executeSQL("SELECT " + ui->editDisplayFormat->text().toStdString() + " FROM " + curTable.toString() + " LIMIT 1",
false, true, callback))
errorMessage = tr("Error in custom display format. Message from database engine:\n\n%1").arg(pdb.lastError());
else if(customNumberColumns != 1)
errorMessage = tr("Custom display format must return only one column but it returned %1.").arg(customNumberColumns);
}
if(!errorMessage.isEmpty())
QMessageBox::warning(this, QApplication::applicationName(), errorMessage);
else
QDialog::accept();
}
void ColumnDisplayFormatDialog::setCustom(bool modified)
{
// If the SQL code is modified by user, select the custom value in the combo-box
if(modified && ui->editDisplayFormat->hasFocus())
ui->comboDisplayFormat->setCurrentIndex(ui->comboDisplayFormat->findData("custom"));
}