FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlitebrowser/src/DbStructureModel.cpp at sqlb-3.2.x · sqlitebrowser/sqlitebrowser · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
sqlitebrowser
/
sqlitebrowser
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
2.4k
Star
24.5k
Code
Issues
825
Pull requests
17
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlitebrowser
/
src
/
DbStructureModel.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
255 lines (216 loc) · 8.37 KB
Breadcrumbs
sqlitebrowser
/
src
/
DbStructureModel.cpp
Copy path
File metadata and controls
255 lines (216 loc) · 8.37 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#
include
"
DbStructureModel.h
"
#
include
"
sqlitedb.h
"
#
include
"
sqlitetablemodel.h
"
#
include
<
QTreeWidgetItem
>
#
include
<
QMimeData
>
#
include
<
QMessageBox
>
#
include
<
QApplication
>
DbStructureModel::DbStructureModel
(QObject* parent)
: QAbstractItemModel(parent)
{
//
Create root item and use its columns to store the header strings
QStringList header;
header <<
tr
(
"
Name
"
) <<
tr
(
"
Object
"
) <<
tr
(
"
Type
"
) <<
tr
(
"
Schema
"
);
rootItem =
new
QTreeWidgetItem
(header);
}
DbStructureModel::~DbStructureModel
()
{
delete
rootItem;
}
int
DbStructureModel::columnCount
(
const
QModelIndex&)
const
{
return
rootItem->
columnCount
();
}
QVariant
DbStructureModel::data
(
const
QModelIndex& index,
int
role)
const
{
if
(!index.
isValid
())
return
QVariant
();
//
Get the item the index points at
QTreeWidgetItem* item =
static_cast
<QTreeWidgetItem*>(index.
internalPointer
());
//
Depending on the role either return the text or the icon
if
(role == Qt::DisplayRole)
return
item->
text
(index.
column
());
else
if
(role == Qt::DecorationRole)
return
item->
icon
(index.
column
());
else
return
QVariant
();
}
Qt::ItemFlags
DbStructureModel::flags
(
const
QModelIndex &index)
const
{
if
(!index.
isValid
())
return
Qt::ItemIsDropEnabled;
//
All items are enabled and selectable
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
//
Only enable dragging for entire table objects
QString type =
data
(index.
sibling
(index.
row
(),
1
), Qt::DisplayRole).
toString
();
if
(type ==
"
table
"
|| type ==
"
view
"
|| type ==
"
index
"
|| type ==
"
trigger
"
)
flags |= Qt::ItemIsDragEnabled;
return
flags;
}
QVariant
DbStructureModel::headerData
(
int
section, Qt::Orientation orientation,
int
role)
const
{
//
Get the header string from the root item
if
(orientation == Qt::Horizontal && role == Qt::DisplayRole)
return
rootItem->
data
(section, role);
return
QVariant
();
}
QModelIndex
DbStructureModel::index
(
int
row,
int
column,
const
QModelIndex& parent)
const
{
if
(!
hasIndex
(row, column, parent))
return
QModelIndex
();
QTreeWidgetItem *parentItem;
if
(!parent.
isValid
())
parentItem = rootItem;
else
parentItem =
static_cast
<QTreeWidgetItem*>(parent.
internalPointer
());
QTreeWidgetItem* childItem = parentItem->
child
(row);
if
(childItem)
return
createIndex
(row, column, childItem);
else
return
QModelIndex
();
}
QModelIndex
DbStructureModel::parent
(
const
QModelIndex& index)
const
{
if
(!index.
isValid
())
return
QModelIndex
();
QTreeWidgetItem* childItem =
static_cast
<QTreeWidgetItem*>(index.
internalPointer
());
QTreeWidgetItem* parentItem = childItem->
parent
();
if
(parentItem == rootItem)
return
QModelIndex
();
else
return
createIndex
(
0
,
0
, parentItem);
}
int
DbStructureModel::rowCount
(
const
QModelIndex& parent)
const
{
if
(parent.
column
() >
0
)
return
0
;
if
(!parent.
isValid
())
return
rootItem->
childCount
();
else
return
static_cast
<QTreeWidgetItem*>(parent.
internalPointer
())->
childCount
();
}
void
DbStructureModel::reloadData
(DBBrowserDB* db)
{
//
Save pointer to DB object
m_db = db;
beginResetModel
();
//
Remove all data except for the root item
while
(rootItem->
childCount
())
{
delete
rootItem->
child
(
0
);
rootItem->
removeChild
(rootItem->
child
(
0
));
}
//
Return here if no DB is opened
if
(!db->
isOpen
())
{
endResetModel
();
return
;
}
//
Create the nodes for tables, indices, views and triggers
QMap<QString, QTreeWidgetItem*> typeToParentItem;
QTreeWidgetItem* itemTables =
new
QTreeWidgetItem
(rootItem);
itemTables->
setIcon
(
0
,
QIcon
(
QString
(
"
:/icons/table
"
)));
itemTables->
setText
(
0
,
tr
(
"
Tables (%1)
"
).
arg
(db->
objMap
.
values
(
"
table
"
).
count
()));
typeToParentItem.
insert
(
"
table
"
, itemTables);
QTreeWidgetItem* itemIndices =
new
QTreeWidgetItem
(rootItem);
itemIndices->
setIcon
(
0
,
QIcon
(
QString
(
"
:/icons/index
"
)));
itemIndices->
setText
(
0
,
tr
(
"
Indices (%1)
"
).
arg
(db->
objMap
.
values
(
"
index
"
).
count
()));
typeToParentItem.
insert
(
"
index
"
, itemIndices);
QTreeWidgetItem* itemViews =
new
QTreeWidgetItem
(rootItem);
itemViews->
setIcon
(
0
,
QIcon
(
QString
(
"
:/icons/view
"
)));
itemViews->
setText
(
0
,
tr
(
"
Views (%1)
"
).
arg
(db->
objMap
.
values
(
"
view
"
).
count
()));
typeToParentItem.
insert
(
"
view
"
, itemViews);
QTreeWidgetItem* itemTriggers =
new
QTreeWidgetItem
(rootItem);
itemTriggers->
setIcon
(
0
,
QIcon
(
QString
(
"
:/icons/trigger
"
)));
itemTriggers->
setText
(
0
,
tr
(
"
Triggers (%1)
"
).
arg
(db->
objMap
.
values
(
"
trigger
"
).
count
()));
typeToParentItem.
insert
(
"
trigger
"
, itemTriggers);
//
Get all database objects and sort them by their name
QMultiMap<QString, DBBrowserObject> dbobjs;
for
(objectMap::ConstIterator it=db->
objMap
.
begin
(); it != db->
objMap
.
end
(); ++it)
dbobjs.
insert
((*it).
getname
(), (*it));
//
Add the actual table objects
for
(QMultiMap<QString, DBBrowserObject>::ConstIterator it=dbobjs.
begin
(); it != dbobjs.
end
(); ++it)
{
//
Object node
QTreeWidgetItem *tableItem =
new
QTreeWidgetItem
(typeToParentItem.
value
((*it).
gettype
()));
tableItem->
setIcon
(
0
,
QIcon
(
QString
(
"
:/icons/%1
"
).
arg
((*it).
gettype
())));
tableItem->
setText
(
0
, (*it).
getname
());
tableItem->
setText
(
1
, (*it).
gettype
());
tableItem->
setText
(
3
, (*it).
getsql
());
//
If it is a table or view add the field Nodes
if
((*it).
gettype
() ==
"
table
"
|| (*it).
gettype
() ==
"
view
"
)
{
for
(
int
i=
0
; i < it->
fldmap
.
size
(); ++i)
{
QTreeWidgetItem *fldItem =
new
QTreeWidgetItem
(tableItem);
fldItem->
setText
(
0
, (*it).
fldmap
.
at
(i)->
name
());
fldItem->
setText
(
1
,
"
field
"
);
fldItem->
setText
(
2
, (*it).
fldmap
.
at
(i)->
type
());
fldItem->
setIcon
(
0
,
QIcon
(
"
:/icons/field
"
));
}
}
}
//
Refresh the view
endResetModel
();
}
QStringList
DbStructureModel::mimeTypes
()
const
{
QStringList types;
types <<
"
text/plain
"
;
return
types;
}
QMimeData*
DbStructureModel::mimeData
(
const
QModelIndexList& indices)
const
{
//
Loop through selected indices
QByteArray d;
foreach
(QModelIndex index, indices)
{
//
Only export data for valid indices and only for the SQL column, i.e. only once per row
if
(index.
isValid
() && index.
column
() ==
3
)
{
//
Add the SQL code used to create the object
d = d.
append
(
data
(index, Qt::DisplayRole).
toString
() +
"
;
\n
"
);
//
If it is a table also add the content
if
(
data
(index.
sibling
(index.
row
(),
1
), Qt::DisplayRole).
toString
() ==
"
table
"
)
{
SqliteTableModel
tableModel
(
0
, m_db);
tableModel.
setTable
(
data
(index.
sibling
(index.
row
(),
0
), Qt::DisplayRole).
toString
());
for
(
int
i=
0
; i < tableModel.
rowCount
(); ++i)
{
QString insertStatement =
"
INSERT INTO `
"
+
data
(index.
sibling
(index.
row
(),
0
), Qt::DisplayRole).
toString
() +
"
` VALUES(
"
;
for
(
int
j=
1
; j < tableModel.
columnCount
(); ++j)
insertStatement +=
QString
(
"
'%1',
"
).
arg
(tableModel.
data
(tableModel.
index
(i, j)).
toString
());
insertStatement.
chop
(
1
);
insertStatement +=
"
);
\n
"
;
d = d.
append
(insertStatement);
}
}
}
}
//
Create the MIME data object
QMimeData* mime =
new
QMimeData
();
mime->
setProperty
(
"
db_file
"
, m_db->
curDBFilename
);
//
Also save the file name to avoid dropping an object on the same database as it comes from
mime->
setData
(
"
text/plain
"
, d);
return
mime;
}
bool
DbStructureModel::dropMimeData
(
const
QMimeData* data, Qt::DropAction action,
int
,
int
,
const
QModelIndex&)
{
if
(action == Qt::IgnoreAction)
return
true
;
if
(!data->
hasFormat
(
"
text/plain
"
))
return
false
;
if
(data->
property
(
"
db_file
"
) == m_db->
curDBFilename
)
return
false
;
//
Get data
QByteArray d = data->
data
(
"
text/plain
"
);
//
Try to execute the SQL statement
if
(m_db->
executeMultiSQL
(d,
true
,
true
))
{
m_db->
updateSchema
();
reloadData
(m_db);
return
true
;
}
else
{
QMessageBox::warning
(
0
,
QApplication::applicationName
(), m_db->
lastErrorMessage
);
return
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL