-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-sqlite-studio.pb
More file actions
207 lines (182 loc) · 5.41 KB
/
simple-sqlite-studio.pb
File metadata and controls
207 lines (182 loc) · 5.41 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
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
; SQLite Simple Studio
; Alex, 2017/09/02
; Variables
Global db = 0
; Procedures
Declare OpenSQLiteDB(File$ = "")
Declare ExecuteAndShow(handle)
Declare AddToGadgetOnce(Gadget, Text$)
Declare ShowResult(Handle)
Declare Connect(Database$)
Declare Disconnect(Database)
Declare ExecuteQuery(Handle, Query$)
Declare StartWindow(Width, Height, Maximized)
Declare ResizeColumns(Gadget)
Declare MenuShow()
Declare MenuCopySelection()
; GUI
XIncludeFile "simple-sqlite-studio-window.pbf"
; Window
StartWindow(600, 400, 0)
; Wait for event
Repeat
Event = WaitWindowEvent()
If EventWindow() = WindowMain
WindowMain_Events(Event)
EndIf
; Gadgets
If Event = #PB_Event_Gadget And EventType() = #PB_EventType_LeftClick
Select EventGadget()
; Open database
Case ButtonOpen
db = OpenSQLiteDB()
; Execute query
Case ButtonExecute
ExecuteAndShow(db)
EndSelect
EndIf
; Enter in Query-field
If Event = #PB_Event_Menu And EventMenu() = 10000
ExecuteAndShow(db)
EndIf
; Right click in output
If Event = #PB_Event_Gadget And EventType() = #PB_EventType_RightClick
MenuShow()
EndIf
If Event = #PB_Event_Menu And EventMenu() = 1
MenuCopySelection()
EndIf
; Resize columns
If Event = #PB_Event_SizeWindow
ResizeColumns(Output)
EndIf
; Get file
If Event = #PB_Event_WindowDrop
db = OpenSQLiteDB(EventDropFiles())
EndIf
Until Event = #PB_Event_CloseWindow
; Prolog
If db <> 0
Disconnect(db)
EndIf
End
; ------------------------------------------------------------------------------------------------
Procedure OpenSQLiteDB(File$ = "")
; Choose file
If File$ = ""
File$ = OpenFileRequester("Please choose SQLite database", "", "SQLite database (*.db,*.sqlite)|*.db;*.sqlite|All files (*.*)|*.*", 0)
If File$ = ""
ProcedureReturn db ; return old handle
EndIf
EndIf
; Close old database
If db <> 0
Disconnect(db)
EndIf
; Open database
handle = Connect(File$)
If handle = 0
ProcedureReturn 0
EndIf
; Enable gadgets
DisableGadget(Query, 0)
DisableGadget(ButtonExecute, 0)
DisableGadget(Output, 0)
; Initial Query
SetGadgetText(Query,"SELECT name AS tables, sql FROM sqlite_master WHERE type=" + Chr(34) + "table" + Chr(34))
ExecuteAndShow(handle)
ProcedureReturn handle
EndProcedure
Procedure ExecuteAndShow(handle)
If ExecuteQuery(handle,GetGadgetText(Query))
AddToGadgetOnce(Query, GetGadgetText(Query))
ShowResult(handle)
EndIf
EndProcedure
Procedure AddToGadgetOnce(Gadget, Text$)
; Remove double entries
For i=0 To CountGadgetItems(Gadget)-1
If GetGadgetItemText(Gadget,i) = Text$
RemoveGadgetItem(Gadget,i)
EndIf
Next
; Add
AddGadgetItem(Query, 0, Text$)
SetGadgetText(Query, Text$)
EndProcedure
Procedure ShowResult(Handle)
; Get columns
RemoveGadgetColumn(Output,#PB_All)
For i=0 To DatabaseColumns(Handle)-1
AddGadgetColumn(Output, i, DatabaseColumnName(Handle, i), GadgetWidth(Output)/DatabaseColumns(Handle))
Next
; Get result
ClearGadgetItems(Output)
While NextDatabaseRow(Handle)
; Get each column
content$ = GetDatabaseString(Handle,0)
For i=1 To DatabaseColumns(Handle)-1
content$ = content$ + Chr(10) + GetDatabaseString(Handle,i)
Next
AddGadgetItem(Output, -1, content$)
Wend
FinishDatabaseQuery(Handle)
EndProcedure
Procedure Connect(Database$)
UseSQLiteDatabase()
handle = OpenDatabase(#PB_Any, Database$, "", "")
If (handle = 0)
MessageRequester("Error", "Can not open database: " + DatabaseError())
EndIf
ProcedureReturn handle
EndProcedure
Procedure Disconnect(Database)
CloseDatabase(Database)
EndProcedure
Procedure ExecuteQuery(Handle, Query$)
If FindString(Query$,"SELECT ",0,#PB_String_NoCase)=1 Or FindString(Query$,"EXPLAIN ",0,#PB_String_NoCase)=1 Or FindString(Query$,"PRAGMA ",0,#PB_String_NoCase)=1
result=DatabaseQuery(Handle, Query$)
Else
result=DatabaseUpdate(Handle, Query$)
EndIf
If result=0
MessageRequester("Error", "Can not execute: "+DatabaseError())
EndIf
ProcedureReturn result
EndProcedure
Procedure StartWindow(Width, Height, Maximized)
OpenWindowMain(0, 0, Width, Height)
If Maximized = 1
ShowWindow_(WindowID(WindowMain),#SW_MAXIMIZE)
EndIf
ResizeGadgetsWindowMain()
AddKeyboardShortcut(WindowMain, #PB_Shortcut_Return, 10000)
EnableWindowDrop(WindowMain, #PB_Drop_Files, #PB_Drag_Copy)
EndProcedure
Procedure ResizeColumns(Gadget)
For i=0 To GetGadgetAttribute(Gadget,#PB_ListIcon_ColumnCount)-1
SetGadgetItemAttribute(Output, 0, #PB_ListIcon_ColumnWidth, GadgetWidth(Gadget)/GetGadgetAttribute(Gadget,#PB_ListIcon_ColumnCount), i)
Next
EndProcedure
Procedure MenuShow()
If CreatePopupMenu(0)
MenuItem(1, "Copy")
EndIf
DisplayPopupMenu(0, WindowID(WindowMain))
EndProcedure
Procedure MenuCopySelection()
content$ = ""
For i=0 To GetGadgetAttribute(Output,#PB_ListIcon_ColumnCount)-1
content$ = content$ + ", " + GetGadgetItemText(Output,GetGadgetState(Output),i)
Next
SetClipboardText(Trim(Trim(content$,",")))
EndProcedure
; IDE Options = PureBasic 5.60 (Windows - x86)
; CursorPosition = 91
; FirstLine = 57
; Folding = --
; EnableXP
; UseIcon = icon.ico
; Executable = ..\simple-sqlite-studio.exe
; CPU = 1
; DisableDebugger