Class: Sqldump::Options

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

Constant Summary collapse

SUPPORTED_DATABASES =
['postgresql', 'mysql', 'sqlite3']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Options

Returns a new instance of Options.



22
23
24
25
26
# File 'lib/sqldump/options.rb', line 22

def initialize(argv)
  parse_options(argv)

  set_derived_options(argv)
end

Instance Attribute Details

#columns_to_selectObject

Returns the value of attribute columns_to_select.



20
21
22
# File 'lib/sqldump/options.rb', line 20

def columns_to_select
  @columns_to_select
end

#csv_headerObject

Returns the value of attribute csv_header.



16
17
18
# File 'lib/sqldump/options.rb', line 16

def csv_header
  @csv_header
end

#databaseObject

Returns the value of attribute database.



9
10
11
# File 'lib/sqldump/options.rb', line 9

def database
  @database
end

#database_typeObject

Returns the value of attribute database_type.



13
14
15
# File 'lib/sqldump/options.rb', line 13

def database_type
  @database_type
end

#dump_modeObject

Returns the value of attribute dump_mode.



17
18
19
# File 'lib/sqldump/options.rb', line 17

def dump_mode
  @dump_mode
end

#hostObject

Returns the value of attribute host.



10
11
12
# File 'lib/sqldump/options.rb', line 10

def host
  @host
end

#passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/sqldump/options.rb', line 12

def password
  @password
end

#prettyObject

Returns the value of attribute pretty.



18
19
20
# File 'lib/sqldump/options.rb', line 18

def pretty
  @pretty
end

#sqlObject

Returns the value of attribute sql.



15
16
17
# File 'lib/sqldump/options.rb', line 15

def sql
  @sql
end

#suppress_nullsObject

Returns the value of attribute suppress_nulls.



19
20
21
# File 'lib/sqldump/options.rb', line 19

def suppress_nulls
  @suppress_nulls
end

#tableObject

Returns the value of attribute table.



14
15
16
# File 'lib/sqldump/options.rb', line 14

def table
  @table
end

#usernameObject

Returns the value of attribute username.



11
12
13
# File 'lib/sqldump/options.rb', line 11

def username
  @username
end

Instance Method Details

#define_optionsObject



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sqldump/options.rb', line 50

def define_options
  optparse = OptionParser.new do |opts|
    setup_defaults()

    opts.banner = "Usage: sqldump [options] table [extra sql]"

    opts.on_head('-h', '--help', 'Display this help.') do
      puts opts
      exit
    end

    # TODO: Handle line wrapping in description
    opts.on('-d', '--database DATABASE', "Specify the database to dump data from. In the case of file-based databases, this should be the full path of the database file.") do |database|
      self.database = database
    end

    opts.on('-S', '--host USER', 'Specifies the host where the database is located, if applicable. If not specified, the default host is localhost.') do |host|
      self.host = host
    end

    opts.on('-U', '--username USER', 'Specifies the username to use') do |username|
      self.username = username
    end

    opts.on('-P', '--password PASSWORD', 'Specifies the password to use') do |password|
      self.password = password
    end

    opts.on('-T', '--dbtype TYPE', 'Specify the type of database to connect to. Supported types are sqlite3, postgresql/pg.') do |type|
      type.downcase!
      type = 'postgresql' if type == 'pg'
      unless SUPPORTED_DATABASES.include?(type)
        puts "Unsupported database type #{type}"
        exit
      end
      self.database_type = type.to_sym
    end

    opts.on('-i', '--insert', 'Dump data as INSERT statements.') do
      self.dump_mode = :insert
    end

    opts.on('-t', '--pretty', 'Pretty-print SQL output (i.e. columns on separate lines, indentation).') do
      self.pretty = true
    end

    opts.on('-l', '--suppress-nulls', 'Suppresses null columns in insert mode.') do
      self.suppress_nulls = true
    end

    opts.on('-s', '--select-columns COLUMN[,COLUMN...]', 'Specifies which columns to select.') do |columns|
      self.columns_to_select = columns
    end

    opts.on('-H', '--header', 'Include column names in csv mode.') do
      self.csv_header = true
    end
  end
  optparse
end

#parse_options(argv) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/sqldump/options.rb', line 34

def parse_options(argv)
  optparse = define_options()
  optparse.parse!(argv)
  if argv.size == 0
    print optparse
    exit
  end
end

#set_derived_options(argv) ⇒ Object



28
29
30
31
32
# File 'lib/sqldump/options.rb', line 28

def set_derived_options(argv)
  self.table = argv[0]

  self.sql = "select #{columns_to_select} from " + argv.join(" ")
end

#setup_defaultsObject



43
44
45
46
47
48
# File 'lib/sqldump/options.rb', line 43

def setup_defaults
  self.dump_mode = :csv
  self.database_type = :sqlite3
  self.host = 'localhost'
  self.columns_to_select = '*'
end