Class: PeekDB

Inherits:
Object
  • Object
show all
Defined in:
lib/peekdb.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ PeekDB

Returns a new instance of PeekDB.



14
15
16
17
18
19
20
21
22
23
# File 'lib/peekdb.rb', line 14

def initialize(argv)
  begin
    puts "PeekDB:"
    parse_options(argv)
  rescue Exception => e
    puts "... Error FATAL: #{e}"
    puts usage
    exit(1)
  end
end

Instance Method Details

#parse_options(args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/peekdb.rb', line 25

def parse_options(args)
  @options = OpenStruct.new

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: peekdb [options]"
    opts.program_name = "PeekDB"
    opts.separator ""
    opts.separator "Specific options:"

    opts.on("-n", "--name NAME", "Name of database - required") do |name|
      @options.name = name
    end

    opts.on("-t", "--type [TYPE]", [:psql, :mysql, :sqlite], "Type of database (psql, mysql, sqlite) - required") do |type|
      @options.type = type
    end

    opts.on("-f", "--format [FORMAT]", [:pdf, :dot], "Format of output file (pdf, dot) - default pdf") do |format|
      @options.format = format
    end
  end
  opts.parse!(args)
end

#runObject



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
# File 'lib/peekdb.rb', line 49

def run
  if @options.name && @options.name
    case @options.type
    when :psql
      db = DatabasePSQL.new @options.name
    when :mysql
      db = DatabaseMySQL.new @options.name
    when :sqlite
      db = DatabaseSQLite.new @options.name
    else
      puts "... Error FATAL: Unknown database type #{@options.type}"
      puts usage
      exit(1)
    end

    begin
      graph = Graph.new
      graph.build(db.name, db.tables, db.relations)
      graph.output(db.name, @options.format)
    rescue
      puts '... Error FATAL: missing arguments'
      puts usage
      exit(1)
    else
      exit(0)
    end
  end
end

#usageObject



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

def usage
  "Usage: peekdb -n DATABASE-NAME -t DATABASE-TYPE [psql | mysql | sqlite] -f FORMAT [pdf | dot]"
end