examples/sqlc.rb in rbcurse-0.1.2 vs examples/sqlc.rb in rbcurse-0.1.3

- old
+ new

@@ -1,5 +1,9 @@ +## rahul kumar, 2009 +# to demonstrate usage of rbcurse +# Use C-q to quit +# require 'rubygems' require 'ncurses' require 'logger' require 'sqlite3' require 'rbcurse' @@ -141,11 +145,11 @@ @db.connect "testd.db" end def run title = "rbcurse" @header = ApplicationHeader.new @form, title, {:text2=>"Demo", :text_center=>"SQL Client"} - status_row = RubyCurses::Label.new @form, {'text' => "", :row => Ncurses.LINES-4, :col => 0, :display_length=>60} + status_row = RubyCurses::Label.new @form, {'text' => "", :row => Ncurses.LINES-4, :col => 0, :display_length=>70} @status_row = status_row # setting ENTER across all objects on a form @form.bind(:ENTER) {|f| status_row.text = f.help_text unless f.help_text.nil? } r = 1; c = 1; @data = [ ["No data"] ] @@ -188,27 +192,24 @@ sqlarea.remove_all sqlarea.focus } # using ampersand to set mnemonic -=begin - b_delrow = Button.new @form do - text "&Delete" + + b_construct = Button.new @form do + text "Constr&uct" row buttrow col c+25 #bind(:ENTER) { status_row.text "Deletes focussed row" } - help_text "" + help_text "Select a table, select columns and press this to construct an SQL" end - b_delrow.command { - #@del_cmd.call - } -=end - Button.button_layout [b_run, b_clear], buttrow, startcol=5, cols=Ncurses.COLS-1, gap=5 + Button.button_layout [b_run, b_clear, b_construct], buttrow, startcol=5, cols=Ncurses.COLS-1, gap=5 + table_ht = 15 atable = Table.new @form do - name "tasktable" + name "sqltable" row buttrow+1 col c width t_width height table_ht #title "A Table" @@ -281,10 +282,11 @@ list_variable $listdata #selection_mode :SINGLE #show_selector true title "Tables" title_attrib 'reverse' + help_text "Press ENTER to run * query, Space to select columns" end #tablelist.bind(:PRESS) { |alist| @status_row.text = "Selected #{alist.current_index}" } tablelist.list_selection_model().bind(:LIST_SELECTION_EVENT,tablelist) { |lsm, alist| @status_row.text = "Selected #{alist.current_index}" } collist = [] @@ -299,10 +301,11 @@ list_variable $coldata #selection_mode :SINGLE #show_selector true title "Columns" title_attrib 'reverse' + help_text "Press ENTER to append columns to sqlarea, Space to select" end tablelist.bind_key(32) { @status_row.text = "Selected #{tablelist.get_content()[tablelist.current_index]}" table = "#{tablelist.get_content()[tablelist.current_index]}" columnlist.list_data_model.remove_all @@ -311,9 +314,29 @@ tablelist.bind_key(13) { @status_row.text = "Selected #{tablelist.get_content()[tablelist.current_index]}" table = "#{tablelist.get_content()[tablelist.current_index]}" run_query "select * from #{table}" } + columnlist.bind_key(13) { + # append column name to sqlarea if ENTER pressed + column = "#{columnlist.get_content()[columnlist.current_index]}" + sqlarea << "#{column}," + } + columnlist.bind_key(32) { + # select row + columnlist.toggle_row_selection + column = "#{columnlist.get_content()[columnlist.current_index]}" + } + b_construct.command { + table = "#{tablelist.get_content()[tablelist.current_index]}" + indexes = columnlist.selected_rows() + columns=[] + indexes.each do |i| + columns << columnlist.get_content()[i] + end + sql = "select #{columns.join(',')} from #{table}" + sqlarea << sql + } @form.repaint @window.wrefresh Ncurses::Panel.update_panels