Class: Myq::Commands

Inherits:
Thor
  • Object
show all
Defined in:
lib/myq/commands.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = [], options = {}, config = {}) ⇒ Commands

Returns a new instance of Commands.



27
28
29
30
31
# 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

Instance Method Details

#bulk_insert_json(table) ⇒ Object



45
46
47
48
49
# 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

#consoleObject



100
101
102
# File 'lib/myq/commands.rb', line 100

def console
  @core.console
end

#count(table) ⇒ Object



68
69
70
# File 'lib/myq/commands.rb', line 68

def count(table)
  puts_json(@core.count(table, options['keys']))
end

#create_db(database) ⇒ Object



115
116
117
# File 'lib/myq/commands.rb', line 115

def create_db(database)
  @core.create_database_utf8(database)
end

#dump(filepath = "#{@profile['database']}.dump") ⇒ Object



105
106
107
# File 'lib/myq/commands.rb', line 105

def dump(filepath = "#{@profile['database']}.dump")
  @core.dump(filepath)
end

#processlistObject



83
84
85
# File 'lib/myq/commands.rb', line 83

def processlist
  puts_json(@core.processlist)
end

#query_file(file) ⇒ Object



52
53
54
# File 'lib/myq/commands.rb', line 52

def query_file(file)
  puts_json(@core.query(File.read(file)))
end

#query_inline(query) ⇒ Object



35
36
37
38
39
40
41
# 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 = "#{@profile['database']}.dump") ⇒ Object



110
111
112
# File 'lib/myq/commands.rb', line 110

def restore(filepath = "#{@profile['database']}.dump")
  @core.restore(filepath)
end

#sample(table) ⇒ Object



60
61
62
63
64
# 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) ⇒ Object



121
122
123
124
125
# File 'lib/myq/commands.rb', line 121

def set_variable(key = nil, value = nil)
  options['variables'].each do |k, v|
    @core.query("SET GLOBAL #{k}=#{v}")
  end
end

#show_databasesObject



78
79
80
# File 'lib/myq/commands.rb', line 78

def show_databases
  puts_json(@core.databases)
end

#show_table(table_name = nil) ⇒ Object



73
74
75
# File 'lib/myq/commands.rb', line 73

def show_table(table_name = nil)
  puts_json(@core.tables(table_name))
end

#show_variables(like = nil) ⇒ Object



88
89
90
# File 'lib/myq/commands.rb', line 88

def show_variables(like = nil)
  puts_json(@core.variables(like))
end

#template(template_path) ⇒ Object



95
96
97
# File 'lib/myq/commands.rb', line 95

def template(template_path)
  @core.render_template(template_path, options['output_template'], options['format'])
end

#versionObject



128
129
130
131
# File 'lib/myq/commands.rb', line 128

def version
  puts VERSION
  puts `mysql -V`
end