Allow multiple mysql connections
This commit is contained in:
parent
b17611f2db
commit
7f68cb5bb0
|
@ -54,8 +54,7 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
options.password = HighLine.new.ask('') {|q| q.echo = false }
|
options.password = HighLine.new.ask('') {|q| q.echo = false }
|
||||||
end
|
end
|
||||||
|
|
||||||
@db = Mysql2::Client.new(host: options.host, username: options.username,
|
@default_db_connection = create_db_connection
|
||||||
password: options.password, database: options.database)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
|
@ -284,15 +283,21 @@ class ImportScripts::Smf2 < ImportScripts::Base
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def query(sql, **opts, &block)
|
def create_db_connection
|
||||||
return __query(sql).to_a if opts[:as] == :array
|
Mysql2::Client.new(host: options.host, username: options.username,
|
||||||
return __query(sql, as: :array).first[0] if opts[:as] == :single
|
password: options.password, database: options.database)
|
||||||
return __query(sql, stream: true).each(&block) if block_given?
|
|
||||||
return __query(sql, stream: true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def __query(sql, **opts)
|
def query(sql, **opts, &block)
|
||||||
@db.query(sql.gsub('{prefix}', options.prefix),
|
db = opts[:connection] || @default_db_connection
|
||||||
|
return __query(db, sql).to_a if opts[:as] == :array
|
||||||
|
return __query(db, sql, as: :array).first[0] if opts[:as] == :single
|
||||||
|
return __query(db, sql, stream: true).each(&block) if block_given?
|
||||||
|
return __query(db, sql, stream: true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def __query(db, sql, **opts)
|
||||||
|
db.query(sql.gsub('{prefix}', options.prefix),
|
||||||
{symbolize_keys: true, cache_rows: false}.merge(opts))
|
{symbolize_keys: true, cache_rows: false}.merge(opts))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue