Class: Command
- Inherits:
-
Object
- Object
- Command
- Defined in:
- lib/command.rb
Instance Attribute Summary collapse
-
#processed ⇒ Object
readonly
Returns the value of attribute processed.
Class Method Summary collapse
Instance Method Summary collapse
- #exec ⇒ Object
- #exit? ⇒ Boolean
- #explain_object(schema, object) ⇒ Object
- #find(types, name) ⇒ Object
- #go? ⇒ Boolean
-
#initialize(command, connection) ⇒ Command
constructor
A new instance of Command.
- #parse ⇒ Object
- #show_tables ⇒ Object
Constructor Details
#initialize(command, connection) ⇒ Command
Returns a new instance of Command.
3 4 5 6 7 |
# File 'lib/command.rb', line 3 def initialize(command, connection) @connection = connection @command = command.strip.downcase @processed = false end |
Instance Attribute Details
#processed ⇒ Object (readonly)
Returns the value of attribute processed.
9 10 11 |
# File 'lib/command.rb', line 9 def processed @processed end |
Class Method Details
.go?(command) ⇒ Boolean
11 12 13 |
# File 'lib/command.rb', line 11 def self.go?(command) command.strip.downcase == "go" end |
Instance Method Details
#exec ⇒ Object
23 24 25 26 |
# File 'lib/command.rb', line 23 def exec parse @processed || go? || exit? end |
#exit? ⇒ Boolean
19 20 21 |
# File 'lib/command.rb', line 19 def exit? @command == ".exit" || @command == ".quit" end |
#explain_object(schema, object) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/command.rb', line 102 def explain_object(schema, object) sql = <<-EOS declare @object_id int, @type varchar(2), @name varchar(255) select @object_id = o.object_id, @type = o.type, @name = s.name + '.' + o.name from sys.objects o inner join sys.schemas s on o.schema_id = s.schema_id where s.name = '#{schema}' and o.name = '#{object}' --finding object with like in name, possibly confusing, so comment out --if @object_id is null -- select top 1 @object_id = o.object_id, @type = o.type, @name = s.name + '.' + o.name -- from sys.objects o -- inner join sys.schemas s on o.schema_id = s.schema_id -- where s.name = '#{schema}' and o.name like '#{object}%' -- order by s.schema_id, o.name if @object_id is not null if @type = 'U' exec sp_help @name else select text from syscomments where id = @object_id else select 'object not found' EOS QueryOutput.new(@connection, sql).show_text_or_table @processed = true end |
#find(types, name) ⇒ Object
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 89 |
# File 'lib/command.rb', line 57 def find(types, name) types = "'TF', 'IF', 'FN', 'P', 'V', 'U'" if types.nil? sql = <<-EOS select --db_name() [database], case when type in ('TF', 'IF', 'FN') then 'function' when type in ('P') then 'procedure' when type in ('V') then 'view' when type in ('U') then 'table' else '???' end type, s.name [schema], o.name from sys.objects o inner join sys.schemas s on o.schema_id = s.schema_id where type in ( #{types} ) and is_ms_shipped = 0 and s.name + "." + o.name like '%#{name}%' order by case when o.schema_id = 1 then 0 else 1 end, --prvo dbo schema case when type in ('TF', 'IF', 'FN') then 4 when type in ('P') then 3 when type in ('V') then 2 when type in ('U') then 1 else 100 end, s.name, o.name EOS QueryOutput.new(@connection, sql).show @processed = true end |
#go? ⇒ Boolean
15 16 17 |
# File 'lib/command.rb', line 15 def go? @command == "go" || @command.start_with?(".use") end |
#parse ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/command.rb', line 28 def parse cp = CommandParser.new(@command) @processed = cp.is_command? return unless cp.is_command? case cp.command when :find types = nil p = cp.params[0] if p types = "'U'" if p.start_with?("table") types = "'V'" if p.start_with?("view") types = "'P'" if p.start_with?("procedure") types = "'TF', 'IF', 'FN'" if p.start_with?("function") end find(types, types.nil? ? cp.params[0].to_s + cp.params[1].to_s : cp.params[1]) when :explain if cp.params[1].nil? || cp.params[1].empty? schema = 'dbo' object = cp.params[0] else schema = cp.params[0] object = cp.params[1] end explain_object schema, object when :use @connection.use cp.params[0] end end |
#show_tables ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/command.rb', line 91 def show_tables sql = <<-EOS select schemas.name + '.' + tables.name table_name from sys.tables inner join sys.schemas on schemas.schema_id = tables.schema_id order by 1 EOS QueryOutput.new(@connection, sql).show @processed = true end |