Class: Coopy

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

Defined Under Namespace

Classes: DbiSqlWrapper, DiffApplySql, DiffOutputStats, Flavor, OpenStruct

Class Method Summary collapse

Class Method Details

.core(flavor, argv) ⇒ Object



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/coopy.rb', line 70

def self.core(flavor,argv)
  options = self.parse(flavor,argv)

  if argv.length < flavor.min_length
    self.parse(flavor,["--help"])
    exit(1)
  end

  if flavor.sql_subject?
    db = SQLite3::Database.new(argv[0])
    sql = SqliteSqlWrapper.new(db)
  end

  if flavor.sql_object?
    name1 = nil
    name2 = nil
    case argv.length
    when 2
      name0 = sql.get_table_names[0]
      db.execute("ATTACH ? AS `__peer_ - _`",argv[1])
      name1 = "main.#{name0}"
      name2 = "__peer_ - _.#{name0}"
    when 3
      name1 = argv[1]
      name2 = argv[2]
    when 4
      name1 = "main.#{argv[1]}"
      db.execute("ATTACH ? AS __peer__",argv[2])
      name2 = "__peer__.#{argv[3]}"
    end
    cmp = SqlCompare.new(sql,name1,name2)
  else
    cmp = DiffParser.new(argv[flavor.min_length-1])
  end

  patches = DiffOutputGroup.new
  # patches << DiffOutputRaw.new
  case options.format
  when :html
    patches << DiffRenderHtml.new
  when :tdiff
    patches << DiffOutputTdiff.new
  when :csv
    patches << DiffRenderCsv.new
  when :apply
    patches << DiffApplySql.new(sql,name1)
  when :raw
    patches << DiffOutputRaw.new
  when :stats
    patches << DiffOutputStats.new
  else
    patches << DiffRenderCsv.new
  end

  cmp.set_output(patches)
  
  cmp.apply
  result = patches.to_string
  if result != ""
    if options.output.nil?
      print result
    else
      File.open(options.output,"w") do |f|
        f << result
      end
    end
  end
  0
end

.diff(argv) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/coopy.rb', line 140

def self.diff(argv)
  flavor = Flavor.new
  flavor.key = :diff
  flavor.banner = "Usage: sqlite_diff [options] ver1.sqlite ver2.sqlite"
  flavor.min_length = 2
  self.core(flavor,argv)
end

.parse(flavor, args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/coopy.rb', line 36

def self.parse(flavor,args)
  options = OpenStruct.new
  options.format = flavor.default_format
  options.output = nil
  OptionParser.new do |opts|
    begin
      opts.banner = flavor.banner
      opts.separator ""
      opts.separator "Specific options"
      if flavor.can_choose_format?
        opts.on("-f","--format [FORMAT]", [:csv, :html, :tdiff, :apply, :stats],
                "select format (csv,html,tdiff,apply,stats)") do |fmt|
          options.format = fmt
        end
      end
      if flavor.can_set_output?
        opts.on("-o", "--output [FILENAME]",
                "direct output to a file") do |fname|
          options.output = fname
        end
      end
      opts.on_tail("-h", "--help", "Show this message") do
        puts opts
        exit
      end
      opts.parse!(args)
      return options
    rescue
      puts "#{$!} (--help for help)"
      exit 1
    end
  end
end

.patch(argv) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/coopy.rb', line 148

def self.patch(argv)
  flavor = Flavor.new
  flavor.key = :patch
  flavor.banner = "Usage: sqlite_patch [options] db.sqlite patch.csv"
  flavor.min_length = 2
  self.core(flavor,argv)
end

.rediff(argv) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/coopy.rb', line 156

def self.rediff(argv)
  flavor = Flavor.new
  flavor.key = :rediff
  flavor.banner = "Usage: sqlite_rediff [options] patch.csv"
  flavor.min_length = 1
  self.core(flavor,argv)
end