47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/spectre/mysql.rb', line 47
def mysql(name = nil, &)
query = {}
if !name.nil? and @config.key? name
query.merge! @config[name]
unless query['host']
raise "No `host' set for MySQL client '#{name}'. Check your MySQL config in your environment."
end
elsif !name.nil?
query['host'] = name
elsif @last_conn.nil?
raise 'No name given and there was no previous MySQL connection to use'
end
MySqlQuery.new(query).instance_eval(&) if block_given?
unless name.nil?
@last_conn = {
host: query['host'],
username: query['username'],
password: query['password'],
database: query['database'],
}
end
@logger.info "Connecting to database #{query['username']}@#{query['host']}:#{query['database']}"
client = ::Mysql2::Client.new(**@last_conn)
res = []
query['query']&.each do |statement|
@logger.info("Executing statement '#{statement}'")
res = client.query(statement, cast_booleans: true)
end
@result = res.map { |row| OpenStruct.new row } if res
client.close
end
|