Sha256: 3b11656ea66b637702176605d14b0c469b479af22557610ec7214bf779483df4

Contents?: true

Size: 1.48 KB

Versions: 6

Compression:

Stored size: 1.48 KB

Contents

require_relative 'BaseAction.rb'

#noinspection RubyResolve
module PPCurses

  class InsertSQLDataAction < GetDataAction

    def initialize( actions, sql, db )
      super(actions)
      @sql = sql
      @db = db
    end

    def win_height
      8 + @actions.length
    end

    def after_actions
      prepared_sql = @sql
      data_array = []

      @actions.each do |action|
        prepared_sql = prepared_sql.sub('?', action.data)
        data_array.push(action.data)
      end

      self.prompt_to_change_data(prepared_sql, data_array)
      
    end

    #
    # returns true if data was inserted
    #
    def prompt_to_change_data(user_display_sql, data_array)
      self.print_line(user_display_sql)

      proceed = GetBooleanAction.new('Proceed? ')
      proceed.set_parent_action(self)
      proceed.set_window(@win)
      proceed.execute

      did_insert = false

      if proceed.data == '1'
        self.print_line('')
        begin
          prep_statement = @db.prepare(@sql)
          prep_statement.bind_params(data_array)
          prep_statement.execute
          prep_statement.close
          did_insert = true
          self.print_success_line('Execution successful')
        rescue SQLite3::Exception => e
          self.print_error_line('Exception occurred')
          self.print_error_line(e.message)
        ensure
          self.print_line('')
          self.print_line('< Press any key to continue > ')
          @win.getch
        end

      end

      did_insert
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ppcurses-0.1.1 lib/ppcurses/actions/InsertSQLDataAction.rb
ppcurses-0.1.0 lib/ppcurses/actions/InsertSQLDataAction.rb
ppcurses-0.0.25 lib/ppcurses/actions/InsertSQLDataAction.rb
ppcurses-0.0.24 lib/ppcurses/actions/InsertSQLDataAction.rb
ppcurses-0.0.23 lib/ppcurses/actions/InsertSQLDataAction.rb
ppcurses-0.0.22 lib/ppcurses/actions/InsertSQLDataAction.rb