-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbload.rb
executable file
·164 lines (148 loc) · 4.35 KB
/
dbload.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env ruby
#
# file:: dbload.rb
# author:: Jon A. Lambert
# version:: 2.9.0
# date:: 03/16/2006
#
# This source code copyright (C) 2005, 2006 by Jon A. Lambert
# All rights reserved.
#
# Released under the terms of the TeensyMUD Public License
# See LICENSE file for additional information.
#
$:.unshift "lib" if !$:.include? "lib"
$:.unshift "vendor" if !$:.include? "vendor"
require 'yaml'
require 'optparse'
require 'ostruct'
require 'pp'
require 'utility/utility'
require 'storage/properties'
require 'core/character'
require 'core/room'
require 'core/world'
require 'core/script'
require 'core/account'
# This utility program loads a yaml file to a database
#
class Loader
VERSION = "0.2.0"
attr_accessor :opts
DATABASES = [:dbm, :gdbm, :sdbm, :sqlite, :sqlite3]
def initialize
@opts = get_options
case @opts.dbtype
when :dbm
require 'dbm'
when :gdbm
require 'gdbm'
when :sdbm
require 'sdbm'
when :sqlite
require 'sqlite'
require 'storage/sqlitehash'
when :sqlite3
require 'sqlite3'
require 'storage/sqlite3hash'
end
@dbtop = 0
@db = {}
@count = 0
end
#
# Processes command line arguments
#
def get_options
# The myopts specified on the command line will be collected in *myopts*.
# We set default values here.
myopts = OpenStruct.new
myopts.ifile = nil
myopts.ofile = nil
myopts.dbtype = nil
opts = OptionParser.new do |opts|
opts.banner = "Database Loader #{VERSION}"
opts.separator ""
opts.separator "Usage: ruby #{$0} [options]"
opts.separator ""
opts.separator "Options:"
opts.on("-i", "--ifile FILE", String,
"Select the yaml file to read",
" defaults to same as database") {|myopts.ifile|}
opts.on("-o", "--ofile FILE", String,
"Select the database file to write",
" extension determined automatically") {|myopts.ofile|}
opts.on("-t", "--type DBTYPE", DATABASES,
"Select the database type - required (no default)",
" One of: #{DATABASES.join(", ")}",
" Example: -t gdbm") {|myopts.dbtype|}
opts.on_tail("-h", "--help", "Show this message") do
puts opts.help
exit
end
opts.on_tail("-v", "--version", "Show version") do
puts "Database Loader #{VERSION}"
exit
end
end
opts.parse!(ARGV)
raise(OptionParser::MissingArgument.new("-t")) if myopts.dbtype == nil
raise(OptionParser::ParseError, "Must specify input file!") if myopts.ifile.nil?
myopts.ofile = myopts.ifile.dup if myopts.ofile.nil?
myopts.ofile << ".gdbm" if myopts.dbtype == :gdbm
myopts.ofile << ".sqlite" if myopts.dbtype == :sqlite
myopts.ofile << ".sqlite3" if myopts.dbtype == :sqlite3
myopts.ifile << ".yaml"
return myopts
rescue OptionParser::ParseError
puts "ERROR - #{$!}"
puts "For help..."
puts " ruby #{$0} --help"
exit
end
#
# Launches the loader
#
def run
YAML::load_file(@opts.ifile).each do |o|
@dbtop = o.id if o.id > @dbtop
@db[o.id]=o
@count += 1
end
case @opts.dbtype
when :sdbm
SDBM.open(@opts.ofile, 0666) do |db|
@db.each {|k,v| db[k.to_s] = Utility.encode(v)}
end
when :gdbm
GDBM.open(@opts.ofile, 0666) do |db|
@db.each {|k,v| db[k.to_s] = Utility.encode(v)}
end
when :dbm
DBM.open(@opts.ofile, 0666) do |db|
@db.each {|k,v| db[k.to_s] = Utility.encode(v)}
end
when :sqlite
db = SQLite::Database.open(@opts.ofile)
begin
db.execute("drop table tmud;")
rescue
end
db.execute("create table tmud (id integer primary key, data text);")
@db.each {|k,v| db[k] = Utility.encode(v)}
db.close
when :sqlite3
db = SQLite3::Database.open(@opts.ofile)
begin
db.execute("drop table tmud;")
rescue Exception
end
db.execute("create table tmud (id integer primary key, data text);")
@db.each {|k,v| db[k] = Utility.encode(v)}
db.close
end
puts "Highest object in use : #{@dbtop}"
puts "Count of objects dumped : #{@count}"
end
end
app = Loader.new.run