-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_formatter_tester.rb
executable file
·62 lines (53 loc) · 1.32 KB
/
string_formatter_tester.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
#!/home/faims/.rbenv/shims/ruby
require 'sqlite3'
require 'antlr3'
require_relative 'string_formatter'
def spatialite_library
return 'libspatialite.dylib' if (/darwin/ =~ RUBY_PLATFORM) != nil
return 'libspatialite.so'
end
db_file = ARGV.shift
sql_file = ARGV.shift
db = SQLite3::Database.new(db_file)
db.enable_load_extension(true)
db.execute("select load_extension('#{spatialite_library}')")
db.create_function('debug_format', -1) do |func, *args|
$debug = true
func.result = 'Format debugging on'
end
db.create_function('format', -1) do |func, *args|
if args.empty? or args.size < 1
func.result = nil
else
if args[0].nil?
func.result = args[1..-1].select { |arg| !arg.nil? }.join(', ')
else
func.result = StringFormatter.new(args[0]).pre_compute.evaluate(args[1..-1])
end
end
end
File.open(sql_file, 'r') do |file|
sql = ""
file.readlines.each do |line|
sql += line.gsub(/--.*/, '')
if line =~ /;/
begin
sql = sql.gsub(/(\n|\s|\t)+/, ' ').strip
unless sql.nil? or sql == ''
result = db.execute(sql)
result.each do |r|
puts r.join("\t")
end
puts ""
end
sql = ""
rescue Exception => e
$stderr.puts sql
$stderr.puts e
puts sql
puts e
sql = ""
end
end
end
end