class Myq::Commands
Public Class Methods
new(args = [], options = {}, config = {})
click to toggle source
Calls superclass method
# File lib/myq/commands.rb, line 27 def initialize(args = [], options = {}, config = {}) super(args, options, config) @global_options = config[:shell].base.options @core = Myq::Core.new(get_profile(@global_options)) end
Public Instance Methods
bulk_insert_json(table)
click to toggle source
# File lib/myq/commands.rb, line 45 def bulk_insert_json(table) data = @core.parse_json(STDIN.read) sql = @core.make_bulk_insert_sql(table, data, options['update_columns']) @core.query_single(sql) end
console()
click to toggle source
# File lib/myq/commands.rb, line 101 def console @core.console end
count(table)
click to toggle source
# File lib/myq/commands.rb, line 68 def count(table) puts_json(@core.count(table, options['keys'])) end
create_db(database)
click to toggle source
# File lib/myq/commands.rb, line 116 def create_db(database) @core.create_database_utf8(database) end
dump(filepath = "
click to toggle source
# File lib/myq/commands.rb, line 106 def dump(filepath = "#{@profile['database']}.dump") @core.dump(filepath) end
processlist()
click to toggle source
# File lib/myq/commands.rb, line 83 def processlist puts_json(@core.processlist) end
query_file(file)
click to toggle source
# File lib/myq/commands.rb, line 52 def query_file(file) puts_json(@core.query(File.read(file))) end
query_inline(query)
click to toggle source
# File lib/myq/commands.rb, line 35 def query_inline(query) loop do puts_json(@core.query(query)) break if options['interval'] == 0 sleep options['interval'] end end
restore(filepath = "
click to toggle source
# File lib/myq/commands.rb, line 111 def restore(filepath = "#{@profile['database']}.dump") @core.restore(filepath) end
sample(table)
click to toggle source
# File lib/myq/commands.rb, line 60 def sample(table) limit = options['all'] ? "" : "limit #{options['limit']}" where = options['where'].nil? ? "" : "where #{options['where'].map{ |k, v| k + ' = ' + v }.join(' and ') }" puts_json(@core.query("select * from #{table} #{where} order by id desc #{limit}")) end
set_variable(key = nil, value = nil)
click to toggle source
# File lib/myq/commands.rb, line 122 def set_variable(key = nil, value = nil) options['variables'].each do |k, v| @core.query("SET GLOBAL #{k}=#{v}") end end
show_databases()
click to toggle source
# File lib/myq/commands.rb, line 78 def show_databases puts_json(@core.databases) end
show_table(table_name = nil)
click to toggle source
# File lib/myq/commands.rb, line 73 def show_table(table_name = nil) puts_json(@core.tables(table_name)) end
show_variables(like = nil)
click to toggle source
# File lib/myq/commands.rb, line 88 def show_variables(like = nil) puts_json(@core.variables(like)) end
template(template_path)
click to toggle source
# File lib/myq/commands.rb, line 96 def template(template_path) @core.render_template(template_path, options['output_template'], options['format'], options['params']) end
version()
click to toggle source
# File lib/myq/commands.rb, line 129 def version puts VERSION puts `mysql -V` end
Private Instance Methods
get_profile(options)
click to toggle source
# File lib/myq/commands.rb, line 136 def get_profile(options) if File.exist?(options['config']) profile = YAML.load_file(options['config'])[options['profile']] else puts "please create #{ENV['HOME']}/.database.yml" exit 1 end profile end
puts_json(object)
click to toggle source
# File lib/myq/commands.rb, line 146 def puts_json(object) puts Yajl::Encoder.encode(object, pretty: @global_options['pretty']) end