summaryrefslogtreecommitdiffstats
path: root/pg2rb.rb
blob: 764c94eec6b927b7a5033887c4c78685ab2c9ebd (plain)
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
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-

#----------------------------------------------------------------------------
#
# File     : dia2pg.rb
# Author   : Jérémy Zurcher <jeremy@asynk.ch>
# Date     : 24/08/10
# License  :
#
#  Permission is hereby granted, free of charge, to any person obtaining
#  a copy of this software and associated documentation files (the
#  "Software"), to deal in the Software without restriction, including
#  without limitation the rights to use, copy, modify, merge, publish,
#  distribute, sublicense, and/or sell copies of the Software, and to
#  permit persons to whom the Software is furnished to do so, subject to
#  the following conditions:
#
#  The above copyright notice and this permission notice shall be
#  included in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
#  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
#  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#----------------------------------------------------------------------------
#
require 'ostruct'
require 'optparse'
#
class OpenStruct
    def to_h; @table end
end
#
options = OpenStruct.new
#
options.verbose = false
options.input = nil
options.database = nil
options.indent=4
#
opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} [options]"
    opts.on( "-i",  "--input FILENAME",             "input file" )                  { |n| options.input = n }
    opts.on( "-V",  "--verbose",                    "produce verbose output" )      { options.verbose = true }
    opts.on( "-d",  "--database DATABASE",          "set default database" )        { |n| options.database = n }
    opts.on( "-n",  "--indent N",                   "set number of indentation spaces" )        { |n| options.indent = n.to_i }
end
#
options.tbl_prefix+='_' if not options.tbl_prefix.nil? and options.tbl_prefix[-1]!='_'
#
opts.parse!(ARGV)
#
if options.input.nil?
    $stderr << "missing input file argument\n"
    exit 1
end
#
INDENT="\n"+" "*options.indent
#
class Table
    #
    def initialize n, t, a, d
        @name = n
        @tbl_prefix = t
        @attr_prefix = a
        @db = d
        @ids = []
        @bools = []
        @floats = []
        @integers = []
        @texts = []
        @times = []
    end
    attr_reader :ids, :bools, :floats, :integers, :texts, :times
    #
    def name
        (@tbl_prefix.nil? ? @name : @name[@tbl_prefix.length..-1]).capitalize   # TODO replace _(.) by $1.capitalize
    end
    #
    def to_s
        r="class #{name}"
        r << INDENT+'#'
        r << INDENT+'include Hmsa_lib::PgsqlModel'
        r << INDENT+'#'
        r << INDENT+"db '#{@db}'.freeze" unless @db.nil?
        r << INDENT+"prefix '#{@attr_prefix}'.freeze" unless @attr_prefix.nil?
        r << INDENT+'#' unless @db.nil? and @attr_prefix.nil?
        r << INDENT+"ids " + @ids.collect { |i| ":#{i}" }.join(', ') unless @ids.empty?
        [ :bools, :floats, :integers, :texts, :times].each do |sym|
            attrs = send sym
            next if attrs.empty?
            r << INDENT+"#{sym} " + attrs.collect { |a| ":#{@attr_prefix.nil? ? a : a[@attr_prefix.length..-1]}" }.join(', ')
        end
        r << INDENT+'#'
        r << INDENT+'sql_stmts( {'
        r << INDENT+" # :exists => [ 'select count(*) from #{@name} where #{@ids[0]}=$1::int', :#{@ids[0]} ]," unless @ids.empty?
        r << INDENT+'} )'
        r << INDENT+'#'
        r << "\nend"
    end
end
#
TABLES=[]
#
tbl=nil
tbl_prefix=nil
attr_prefix=nil
File.open(options.input).readlines.each do |l|
#    puts l
    if l=~/dia2pg/
        l.split.each do |s|
            k,v = s.split '='
            if k=~/tbl_prefix/
                tbl_prefix=v
            elsif k=~/attr_prefix/
                attr_prefix=v
            end
        end
    elsif l=~/CREATE TABLE (\S+)/
        tbl = Table.new $1, tbl_prefix, attr_prefix, options.database
        tbl_prefix=nil
        attr_prefix=nil
        TABLES << tbl
    elsif l=~/PRIMARY\s+KEY\s+\((\w*)\)/
        $1.split(/,/).reverse.each do |i|
            tbl.ids.insert 0, i
        end
    elsif l=~/(\w+)\s+integer\s+REFERENCES/
        tbl.ids << $1
    elsif l=~/(\w+)\s+boolean/
        tbl.bools << $1
    elsif l=~/(\w+)\s+double/
        tbl.floats << $1
    elsif l=~/(\w+)\s+integer/
        tbl.integers << $1
    elsif l=~/(\w+)\s+text/
        tbl.texts << $1
    elsif l=~/(\w+)\s+char/
        tbl.texts << $1
    elsif l=~/(\w+)\s+timestamp/
        tbl.times << $1
    end
end
#
TABLES.each do |t|
    puts "#"
    puts t
end