Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to seperate the ODBC connector and SQL Query #474

Closed
MartynKeigher opened this issue Jan 3, 2014 · 3 comments
Closed

Trying to seperate the ODBC connector and SQL Query #474

MartynKeigher opened this issue Jan 3, 2014 · 3 comments

Comments

@MartynKeigher
Copy link

Hey all,

So I have been working at this for a little while now and I'm struggling to see where I am going wrong, so I'm going to jump straight in... This is my current rb file to pull data from a MySQL db.

require 'mysql2'

SCHEDULER.every '10m', :first_in => 0 do |job|

        db = Mysql2::Client.new(:host => "192.168.10.1", :username => "username", :password => "password", :port => 1234, :database => "database name" )

        sql = "MY SQL QUERY WILL GO HERE"

        results = db.query(sql)

        current_agentcount = results.count

        send_event('agentcount', { value: current_agentcount } )
db.close
end

And it works fine! So far so good.

What I am looking to do is SPLIT the function of this RB so i have 2 files. One file is the SQL Query, and the other is the ODBC conenctor, basically the

db = Mysql2::Client.new(:host => "192.168.10.1", :username => "username", :password => "password", :port => 1234, :database => "database name" )

part, and then I want to be able to call the "odbc connector file' from my rb job file that is actually doing the querying of the db.

Make sense? I hope it does, and i'm pretty sure its something simple... but I'm just not getting it to work for me and I would really appreciate someone's response to this.

Many Thanks!

Martyn T. Keigher

@indirect
Copy link

indirect commented Jan 3, 2014

# my_db.rb
class MyDb
  def self.conn
    Mysql2::Client.new(:host => "192.168.10.1", :username => "username", :password => "password", :port => 1234, :database => "database name" )
  end
end

# work_file.rb
require 'my_db'
db = MyDb.conn
db.query('stuff')
db.close

@brianmario
Copy link
Owner

Hi @MartynKeigher!

If I'm understanding you correctly, @indirect's solution should work for what you need. Have you given that a try?

@MartynKeigher
Copy link
Author

Hey,
@indirect's response did put me on the right path yes! So thank you very much! I needed a little more help from the #ruby irc room, but we got it. Turns out it was just the require piece that needed to be changed.

Just so its recorded and here for reference for anyone else that needs to know.

Note: Both of the RB files are in the SAME folder.

./my_db.rb

require 'mysql2'

class MyDb
  def self.conn
    Mysql2::Client.new( :host => "192.168.10.1", :username => "username", :password => "password", :port => 1234, :database => "database name" )
  end
end

./agentcount.rb

require_relative 'my_db'

SCHEDULER.every '10m', :first_in => 0 do |job|

        db = MyDb.conn

        sql = "SELECT COUNT(*) FROM computers"

        results = db.query(sql)

        current_agentcount = object.each

        send_event('agentcount', { value: current_agentcount } )
db.close
end

Thank you again!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants