diff options
author | Saku Ytti <saku@ytti.fi> | 2014-07-20 12:06:38 +0300 |
---|---|---|
committer | Saku Ytti <saku@ytti.fi> | 2014-07-20 12:06:38 +0300 |
commit | a39c93f861f554648bbc0938173e64474d689dad (patch) | |
tree | 99d648290b6e2b159e232a4b155c4b12198f353e /lib | |
parent | 2245a452788337338e67319fad6da1931c6c5be1 (diff) |
Return useful error message on missing SQL adapter
SQL file in configuration renamed to database.
Should the generated example file have host+user+password+query?
Diffstat (limited to 'lib')
-rw-r--r-- | lib/oxidized/source/sql.rb | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/lib/oxidized/source/sql.rb b/lib/oxidized/source/sql.rb index c65d335..cd9ef4a 100644 --- a/lib/oxidized/source/sql.rb +++ b/lib/oxidized/source/sql.rb @@ -6,15 +6,10 @@ class SQL < Source raise OxidizedError, 'sequel not found: sudo gem install sequel' end - def initialize - super - @cfg = CFG.source.sql - end - def setup if @cfg.empty? CFGS.user.source.sql.adapter = 'sqlite' - CFGS.user.source.sql.file = File.join(Config::Root, 'sqlite.db') + CFGS.user.source.sql.database = File.join(Config::Root, 'sqlite.db') CFGS.user.source.sql.table = 'devices' CFGS.user.source.sql.map.name = 'name' CFGS.user.source.sql.map.model = 'rancid' @@ -23,27 +18,11 @@ class SQL < Source end end - def connect - #begin - #require @cfg.adapter - #rescue LoadError - #raise OxidizedError, "@{cfg.adapter} not found: install gem" - #end - Sequel.connect(:adapter => @cfg.adapter, - :host => @cfg.host, - :user => @cfg.user, - :password => @cfg.password, - :database => @cfg.database) - end - def load nodes = [] db = connect - if @cfg.query? - query = db[@cfg.table.to_sym].with_sql(@cfg.query) - else - query = db[@cfg.table.to_sym] - end + query = db[@cfg.table.to_sym] + query = query.with_sql(@cfg.query) if @cfg.query? query.each do |node| # map node parameters keys = {} @@ -57,8 +36,26 @@ class SQL < Source nodes << keys end + db.disconnect nodes end + private + + def initialize + super + @cfg = CFG.source.sql + end + + def connect + Sequel.connect(:adapter => @cfg.adapter, + :host => @cfg.host?, + :user => @cfg.user?, + :password => @cfg.password?, + :database => @cfg.database) + rescue Sequel::AdapterNotFound => error + raise OxidizedError, "SQL adapter gem not installed: " + error.message + end + end end |