Search code examples
sqlrubyregexsqlitecinch

feed user regex match into database query


class Database
  include Cinch::Plugin

  DB = SQLite3::Database.new('development.sqlite3')
  match /(select .* from gears where .* like .*)/i 

  def execute(m)
    @db = SQLite3::Database.new('development.sqlite3')
    #m.reply @db.execute("select * from gears where lab like 'Primary'") 
  end
end

This part of an IRC bot. I am trying to have the matched regex that the user inputs directly inputed to the @db.execute to be able perform the query. Any help or suggestions for a different way to go would be appreciated.


Solution

  • Something along these lines should work:

    def execute(m)
      @db = SQLite3::Database.new('development.sqlite3')
      input = m.input # or however you're getting the input into this function
      regex = /(select .* from gears where .* like .*)/i 
      db_query_string = regex.match(input).to_s
      m.reply @db.execute(db_query_string) 
    end