PageRenderTime 76ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/sqlite3manager.rb

https://bitbucket.org/jjhop/sqlite3manager
Ruby | 215 lines | 163 code | 24 blank | 28 comment | 8 complexity | 3e339abe142cf099c45686bc4fbe6598 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, version 3 of the License.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. #
  15. # Copyright: (C) 2004-2010 Rafal Kotusiewicz aka 'jjhop'. All rights reserved.
  16. #
  17. # http://bitbucket.org/jjhop/sqlite3manager/overview/
  18. # http://bitbucket.org/jjhop/sqlite3manager/wiki/Home
  19. #
  20. require 'rubygems'
  21. require 'fox16'
  22. require 'sqlite3'
  23. include Fox
  24. class SQLite3ManagerApp < FXApp
  25. def initialize
  26. super("sqlite3manager", "sqlite3manager")
  27. main_window = MainWindow.new(self)
  28. self.addSignal("SIGINT", main_window.method(:cmd_quit))
  29. self.create()
  30. main_window.create()
  31. end
  32. end
  33. class SchemaUtils
  34. def self.show_database_table(dbh)
  35. table_names = []
  36. dbh.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name ASC") do |r|
  37. table_names<< r[0]
  38. end
  39. table_names
  40. end
  41. end
  42. class MainWindow < FXMainWindow
  43. def initialize(app)
  44. super(app, "SQLite3Manager 0.1", nil, nil, DECOR_ALL, 0, 0, 800, 600)
  45. FXToolTip.new(getApp())
  46. menubar = FXMenuBar.new(self, LAYOUT_SIDE_TOP|LAYOUT_FILL_X|FRAME_RAISED)
  47. @status_bar = FXStatusBar.new(self, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|STATUSBAR_WITH_DRAGCORNER)
  48. FXHorizontalSeparator.new(self, LAYOUT_SIDE_TOP, LAYOUT_FILL_X, SEPARATOR_GROOVE)
  49. FXVerticalFrame.new(self, LAYOUT_SIDE_TOP|FRAME_NONE|LAYOUT_FILL_X|LAYOUT_FILL_Y) do |contents|
  50. FXSplitter.new(contents, LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y|SPLITTER_TRACKING|SPLITTER_HORIZONTAL) do |splitter|
  51. FXGroupBox.new(splitter, "Relacje", LAYOUT_FILL_Y|FRAME_GROOVE, 0, 0, 200) do |groupbox|
  52. FXHorizontalFrame.new(groupbox, LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK,0, 0, 0, 0, 0, 0, 0, 0) do |frame|
  53. FXList.new(frame, nil, 0, LIST_SINGLESELECT|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_TOP|LAYOUT_RIGHT) do |list|
  54. list.appendItem("Hello")
  55. end
  56. end
  57. end
  58. FXGroupBox.new(splitter,nil) do |group_right|
  59. @table = FXTable.new(group_right, nil, 0, TABLE_COL_SIZABLE|TABLE_NO_COLSELECT|LAYOUT_FILL, 0,0,0,0, 2,2,2,2) do |t|
  60. t.setTableSize(0, 50)
  61. t.setColumnText( 0, "Pierwsze pole")
  62. t.setColumnText( 1, "Drugie pole")
  63. t.setColumnText( 2, "Trzecie pole")
  64. t.setColumnText( 3, "Czwarte pole")
  65. t.setColumnText( 4, "Piate pole")
  66. t.rowHeaderMode = LAYOUT_FIX_WIDTH
  67. t.rowHeaderWidth = 0
  68. t.editable = false
  69. t.setColumnWidth(0, 100)
  70. t.setColumnWidth(1, 100)
  71. t.setColumnWidth(2, 100)
  72. t.setColumnWidth(3, 100)
  73. t.setColumnWidth(4, 100)
  74. 50.times do |idx|
  75. t.insertRows(t.numRows, 1, true)
  76. t.setRowText(idx, "#{idx+1}")
  77. t.setItemText(idx, 0, " Linux. Programowanie.");
  78. t.setItemText(idx, 1, " 83-7243-020-9");
  79. t.setItemText(idx, 2, " ReadMe");
  80. t.setItemText(idx, 3, " 1999");
  81. t.setItemText(idx, 4, " DOM");
  82. end
  83. t.connect(SEL_RIGHTBUTTONRELEASE) do |sender, sel, evt|
  84. col = sender.colAtX(evt.click_x)
  85. row = sender.rowAtY(evt.click_y)
  86. begin
  87. #selectedItem = sender.getItem(col, row)
  88. #puts "#{selectedItem.class}\n"
  89. #unless selectedItem.nil?
  90. FXMenuPane.new(self) do |menuPane|
  91. FXMenuCommand.new(menuPane, "Pokaz szczegoly")
  92. FXMenuCommand.new(menuPane, "usun w diably")
  93. menuPane.create()
  94. t.selectRow(t.rowAtY(evt.click_y))
  95. menuPane.popup(nil, evt.root_x, evt.root_y)
  96. end
  97. #else
  98. # puts "nil?"
  99. #end
  100. rescue IndexError => ex
  101. puts ex
  102. end
  103. end
  104. end
  105. end
  106. end
  107. end
  108. filemenu = FXMenuPane.new(self) do |menu|
  109. FXMenuCommand.new(menu, "Nowa baza...\tCtrl-N\tNowa baza...", self.get_icon('db_new.gif')) do |c|
  110. #c.connect(SEL_COMMAND, method(:not_yet_implemented))
  111. end
  112. FXMenuCommand.new(menu, "Otworz baze...\tCtrl-O\tOtwiera baze...", self.get_icon('db_open.gif')) do |c|
  113. c.connect(SEL_COMMAND, method(:cmd_open_database))
  114. end
  115. FXMenuCommand.new(menu, "Zamknij baze\tCtrl-W\tZamyka baze", self.get_icon('db_close.gif')) do |c|
  116. #c.connect(SEL_COMMAND, method(:not_yet_implemented))
  117. c.disable
  118. end
  119. FXMenuSeparator.new(menu)
  120. FXMenuCommand.new(menu, "Wyjscie\tAlt-F4\t\tZamykam program", self.get_icon('app_close.gif')) do |c|
  121. c.connect(SEL_COMMAND, method(:cmd_quit))
  122. end
  123. end
  124. helpmenu = FXMenuPane.new(self) do |menu|
  125. FXMenuCommand.new(menu, "Pomoc\tF1\tShowing help.", nil).connect(SEL_COMMAND, method(:cmd_help))
  126. FXMenuCommand.new(menu, "O programie\tCtrl-F1\tPokazuje okno z informacjami o programie.", nil).connect(SEL_COMMAND, method(:cmd_about))
  127. if RUBY_PLATFORM == 'i386-mswin32'
  128. FXMenuSeparator.new(menu)
  129. FXMenuCommand.new(menu, "Strona domowa\tCtrl-Shift-F1\tOtwiera w przegladarce Internet Explorer strone domowa programu.", nil) do |cmd|
  130. cmd.connect(SEL_COMMAND) do
  131. begin
  132. Thread.new do
  133. require 'win32ole'
  134. ie = WIN32OLE.new('InternetExplorer.Application')
  135. ie.visible = true
  136. ie.navigate('http://bitbucket.org/jjhop/sqlite3manager/')
  137. end
  138. rescue; end
  139. end
  140. end
  141. end
  142. end
  143. FXMenuTitle.new(menubar, "&Plik", nil, filemenu)
  144. FXMenuTitle.new(menubar, "Pomoc", nil, helpmenu, LAYOUT_RIGHT)
  145. end
  146. def create
  147. super()
  148. show(PLACEMENT_SCREEN)
  149. end
  150. def cmd_open_database(sender, sel, ptr)
  151. dialog = FXFileDialog.new(self, "Otworz baze danych...")
  152. dialog.patternList = [
  153. "All files (*)",
  154. "SQLite3 .db (.db)",
  155. "SQLite3 .sqlite (.sqlite)"
  156. ]
  157. dialog.selectMode = SELECTFILE_EXISTING
  158. if dialog.execute != 0
  159. self.load_database(dialog.filename)
  160. end
  161. end
  162. def cmd_quit(sender, sel, ptr)
  163. if MBOX_CLICKED_YES == FXMessageBox.question(self, MBOX_YES_NO, "Potwierdzenie zamkniecia", "Czy jestes pewien, ze chcesz zamknac program?")
  164. # jeszcze trzeba zamkna? ewentualnie otwarta baze danych...
  165. unless @dbh.nil?
  166. @dbh.close()
  167. end
  168. getApp().exit(0)
  169. end
  170. end
  171. def cmd_help(sender, sel, ptr)
  172. end
  173. def cmd_about(sender, sel, ptr)
  174. end
  175. def get_icon(file)
  176. begin
  177. FXGIFIcon.new(getApp(),File.open(File.join(File.dirname(__FILE__), "./ico/#{file}"),"rb").read)
  178. rescue
  179. nil
  180. end
  181. end
  182. def load_database(db_filename)
  183. @dbh = SQLite3::Database.new(db_filename)
  184. SchemaUtils.show_database_table(@dbh).each do |n|
  185. puts "#{n}"
  186. end
  187. end
  188. end
  189. if __FILE__ == $0
  190. SQLite3ManagerApp.new.run
  191. end