Class: Myq::Commands
- Inherits:
-
Thor
- Object
- Thor
- Myq::Commands
- Defined in:
- lib/myq/commands.rb
Instance Method Summary collapse
- #bulk_insert_json(table) ⇒ Object
- #console ⇒ Object
- #count(table) ⇒ Object
- #create_db(database) ⇒ Object
- #dump(filepath = "#{@profile['database']}.dump") ⇒ Object
-
#initialize(args = [], options = {}, config = {}) ⇒ Commands
constructor
A new instance of Commands.
- #processlist ⇒ Object
- #query_file(file) ⇒ Object
- #query_inline(query) ⇒ Object
- #restore(filepath = "#{@profile['database']}.dump") ⇒ Object
- #sample(table) ⇒ Object
- #set_variable(key = nil, value = nil) ⇒ Object
- #show_databases ⇒ Object
- #show_table(table_name = nil) ⇒ Object
- #show_variables(like = nil) ⇒ Object
- #template(template_path) ⇒ Object
- #version ⇒ Object
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 = [], = {}, config = {}) super(args, , config) @global_options = config[:shell].base. @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, ['update_columns']) @core.query_single(sql) end |
#console ⇒ Object
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, ['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 |
#processlist ⇒ Object
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 ['interval'] == 0 sleep ['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 = ['all'] ? "" : "limit #{['limit']}" where = ['where'].nil? ? "" : "where #{['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) ['variables'].each do |k, v| @core.query("SET GLOBAL #{k}=#{v}") end end |
#show_databases ⇒ Object
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, ['output_template'], ['format']) end |
#version ⇒ Object
128 129 130 131 |
# File 'lib/myq/commands.rb', line 128 def version puts VERSION puts `mysql -V` end |