From a39c93f861f554648bbc0938173e64474d689dad Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Sun, 20 Jul 2014 12:06:38 +0300 Subject: 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? --- lib/oxidized/source/sql.rb | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) (limited to 'lib') 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 -- cgit v1.2.1