Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
sqlitebrowser/src/ColumnDisplayFormatDialog.cpp at master · jiangdon2007/sqlitebrowser · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
jiangdon2007
sqlitebrowser
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
sqlitebrowser
/
src
/
ColumnDisplayFormatDialog.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
137 lines (123 loc) · 7.13 KB
master
Breadcrumbs
sqlitebrowser
/
src
/
ColumnDisplayFormatDialog.cpp
Copy path
Top
File metadata and controls
Code
Blame
137 lines (123 loc) · 7.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
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
(
"
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
(
"
Custom
"
),
"
custom
"
);
ui->
labelDisplayFormat
->
setText
(ui->
labelDisplayFormat
->
text
().
arg
(column_name));
formatFunctions[
"
decimal
"
] =
"
printf('%d',
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
exponent
"
] =
"
printf('%e',
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
hexblob
"
] =
"
hex(
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
hex
"
] =
"
printf('0x%x',
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
octal
"
] =
"
printf('%o',
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
round
"
] =
"
round(
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
appleDate
"
] =
"
datetime('2001-01-01',
"
+
sqlb::escapeIdentifier
(column_name) +
"
|| ' seconds')
"
;
formatFunctions[
"
javaEpoch
"
] =
"
strftime('%Y-%m-%d %H:%M:%S.',
"
+
sqlb::escapeIdentifier
(column_name) +
"
/1000, 'unixepoch') || (
"
+
sqlb::escapeIdentifier
(column_name) +
"
%1000)
"
;
formatFunctions[
"
julian
"
] =
"
datetime(
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
epoch
"
] =
"
datetime(
"
+
sqlb::escapeIdentifier
(column_name) +
"
, 'unixepoch')
"
;
formatFunctions[
"
epochLocalTime
"
] =
"
datetime(
"
+
sqlb::escapeIdentifier
(column_name) +
"
, 'unixepoch', 'localtime')
"
;
formatFunctions[
"
winDate
"
] =
"
datetime('1899-12-30',
"
+
sqlb::escapeIdentifier
(column_name) +
"
|| ' days')
"
;
formatFunctions[
"
ddmmyyyyDate
"
] =
"
strftime('%d/%m/%Y',
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
lower
"
] =
"
lower(
"
+
sqlb::escapeIdentifier
(column_name) +
"
)
"
;
formatFunctions[
"
upper
"
] =
"
upper(
"
+
sqlb::escapeIdentifier
(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;
}
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
(
QRegExp
(
"
[a-z]+[a-z_0-9]* *
\\
(.*
"
+
QRegExp::escape
(
sqlb::escapeIdentifier
(column_name)) +
"
.*
\\
)
"
, Qt::CaseInsensitive))))
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, QStringList, QStringList) ->
bool
{
customNumberColumns = numberColumns;
//
Return false so the query is not aborted and no error is reported.
return
false
;
};
if
(!pdb.
executeSQL
(
QString
(
"
SELECT %1 FROM %2 LIMIT 1
"
).
arg
(ui->
editDisplayFormat
->
text
(),
QString::fromStdString
(curTable.
toString
())),
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
"
));
}
You can’t perform that action at this time.