1+ ; SQLite Simple Studio
2+ ; Alex, 2017/09/02
3+
4+ ; Variables
5+ Global db = 0
6+
7+ ; Procedures
8+ Declare OpenSQLiteDB(File$ = "")
9+ Declare ExecuteAndShow(handle)
10+ Declare AddToGadgetOnce(Gadget, Text$)
11+ Declare ShowResult(Handle)
12+ Declare Connect(Database$)
13+ Declare Disconnect(Database)
14+ Declare ExecuteQuery(Handle, Query$)
15+ Declare StartWindow(Width, Height, Maximized)
16+ Declare ResizeColumns(Gadget)
17+ Declare MenuShow()
18+ Declare MenuCopySelection()
19+
20+ ; GUI
21+ XIncludeFile "simple-sqlite-studio-window.pbf"
22+
23+ ; Window
24+ StartWindow(600, 400, 0)
25+
26+ ; Wait for event
27+ Repeat
28+ Event = WaitWindowEvent()
29+ If EventWindow() = WindowMain
30+ WindowMain_Events(Event)
31+ EndIf
32+ ; Gadgets
33+ If Event = #PB_Event_Gadget And EventType() = #PB_EventType_LeftClick
34+ Select EventGadget()
35+ ; Open database
36+ Case ButtonOpen
37+ db = OpenSQLiteDB()
38+ ; Execute query
39+ Case ButtonExecute
40+ ExecuteAndShow(db)
41+ EndSelect
42+ EndIf
43+ ; Enter in Query-field
44+ If Event = #PB_Event_Menu And EventMenu() = 10000
45+ ExecuteAndShow(db)
46+ EndIf
47+ ; Right click in output
48+ If Event = #PB_Event_Gadget And EventType() = #PB_EventType_RightClick
49+ MenuShow()
50+ EndIf
51+ If Event = #PB_Event_Menu And EventMenu() = 1
52+ MenuCopySelection()
53+ EndIf
54+ ; Resize columns
55+ If Event = #PB_Event_SizeWindow
56+ ResizeColumns(Output)
57+ EndIf
58+ ; Get file
59+ If Event = #PB_Event_WindowDrop
60+ db = OpenSQLiteDB(EventDropFiles())
61+ EndIf
62+ Until Event = #PB_Event_CloseWindow
63+
64+ ; Prolog
65+ If db <> 0
66+ Disconnect(db)
67+ EndIf
68+ End
69+
70+ ; ------------------------------------------------------------------------------------------------
71+
72+ Procedure OpenSQLiteDB(File$ = "")
73+ ; Choose file
74+ If File$ = ""
75+ File$ = OpenFileRequester("Please choose SQLite database", "", "SQLite database (*.db,*.sqlite)|*.db;*.sqlite|All files (*.*)|*.*", 0)
76+ If File$ = ""
77+ ProcedureReturn db ; return old handle
78+ EndIf
79+ EndIf
80+
81+ ; Close old database
82+ If db <> 0
83+ Disconnect(db)
84+ EndIf
85+
86+ ; Open database
87+ handle = Connect(File$)
88+ If handle = 0
89+ ProcedureReturn 0
90+ EndIf
91+
92+ ; Enable gadgets
93+ DisableGadget(Query, 0)
94+ DisableGadget(ButtonExecute, 0)
95+ DisableGadget(Output, 0)
96+
97+ ; Initial Query
98+ SetGadgetText(Query,"SELECT name AS tables, sql FROM sqlite_master WHERE type=" + Chr(34) + "table" + Chr(34))
99+ ExecuteAndShow(handle)
100+
101+ ProcedureReturn handle
102+ EndProcedure
103+
104+ Procedure ExecuteAndShow(handle)
105+ If ExecuteQuery(handle,GetGadgetText(Query))
106+ AddToGadgetOnce(Query, GetGadgetText(Query))
107+ ShowResult(handle)
108+ EndIf
109+ EndProcedure
110+
111+ Procedure AddToGadgetOnce(Gadget, Text$)
112+ ; Remove double entries
113+ For i=0 To CountGadgetItems(Gadget)-1
114+ If GetGadgetItemText(Gadget,i) = Text$
115+ RemoveGadgetItem(Gadget,i)
116+ EndIf
117+ Next
118+ ; Add
119+ AddGadgetItem(Query, 0, Text$)
120+ SetGadgetText(Query, Text$)
121+ EndProcedure
122+
123+ Procedure ShowResult(Handle)
124+ ; Get columns
125+ RemoveGadgetColumn(Output,#PB_All)
126+ For i=0 To DatabaseColumns(Handle)-1
127+ AddGadgetColumn(Output, i, DatabaseColumnName(Handle, i), GadgetWidth(Output)/DatabaseColumns(Handle))
128+ Next
129+
130+ ; Get result
131+ ClearGadgetItems(Output)
132+ While NextDatabaseRow(Handle)
133+ ; Get each column
134+ content$ = GetDatabaseString(Handle,0)
135+ For i=1 To DatabaseColumns(Handle)-1
136+ content$ = content$ + Chr(10) + GetDatabaseString(Handle,i)
137+ Next
138+ AddGadgetItem(Output, -1, content$)
139+ Wend
140+ FinishDatabaseQuery(Handle)
141+ EndProcedure
142+
143+ Procedure Connect(Database$)
144+ UseSQLiteDatabase()
145+ handle = OpenDatabase(#PB_Any, Database$, "", "")
146+ If (handle = 0)
147+ MessageRequester("Error", "Can not open database: " + DatabaseError())
148+ EndIf
149+ ProcedureReturn handle
150+ EndProcedure
151+
152+ Procedure Disconnect(Database)
153+ CloseDatabase(Database)
154+ EndProcedure
155+
156+ Procedure ExecuteQuery(Handle, Query$)
157+ 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
158+ result=DatabaseQuery(Handle, Query$)
159+ Else
160+ result=DatabaseUpdate(Handle, Query$)
161+ EndIf
162+ If result=0
163+ MessageRequester("Error", "Can not execute: "+DatabaseError())
164+ EndIf
165+ ProcedureReturn result
166+ EndProcedure
167+
168+ Procedure StartWindow(Width, Height, Maximized)
169+ OpenWindowMain(0, 0, Width, Height)
170+ If Maximized = 1
171+ ShowWindow_(WindowID(WindowMain),#SW_MAXIMIZE)
172+ EndIf
173+ ResizeGadgetsWindowMain()
174+ AddKeyboardShortcut(WindowMain, #PB_Shortcut_Return, 10000)
175+ EnableWindowDrop(WindowMain, #PB_Drop_Files, #PB_Drag_Copy)
176+ EndProcedure
177+
178+ Procedure ResizeColumns(Gadget)
179+ For i=0 To GetGadgetAttribute(Gadget,#PB_ListIcon_ColumnCount)-1
180+ SetGadgetItemAttribute(Output, 0, #PB_ListIcon_ColumnWidth, GadgetWidth(Gadget)/GetGadgetAttribute(Gadget,#PB_ListIcon_ColumnCount), i)
181+ Next
182+ EndProcedure
183+
184+ Procedure MenuShow()
185+ If CreatePopupMenu(0)
186+ MenuItem(1, "Copy")
187+ EndIf
188+ DisplayPopupMenu(0, WindowID(WindowMain))
189+ EndProcedure
190+
191+ Procedure MenuCopySelection()
192+ content$ = ""
193+ For i=0 To GetGadgetAttribute(Output,#PB_ListIcon_ColumnCount)-1
194+ content$ = content$ + ", " + GetGadgetItemText(Output,GetGadgetState(Output),i)
195+ Next
196+ SetClipboardText(Trim(Trim(content$,",")))
197+ EndProcedure
198+
199+ ; IDE Options = PureBasic 5.60 (Windows - x86)
200+ ; CursorPosition = 91
201+ ; FirstLine = 57
202+ ; Folding = --
203+ ; EnableXP
204+ ; UseIcon = icon.ico
205+ ; Executable = ..\simple-sqlite-studio.exe
206+ ; CPU = 1
207+ ; DisableDebugger
0 commit comments