class PeekDB

Public Class Methods

new(argv) click to toggle source
# 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

Public Instance Methods

parse_options(args) click to toggle source
# 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
run() click to toggle source
# 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
usage() click to toggle source
# File lib/peekdb.rb, line 78
def usage
  "Usage: peekdb -n DATABASE-NAME -t DATABASE-TYPE [psql | mysql | sqlite] -f FORMAT [pdf | dot]"
end