Class: MakumbaImport::Importer

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

Class Method Summary collapse

Class Method Details

.generate_models(schema) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/makumba_import/importer.rb', line 191

def self.generate_models(schema)
  txt = '';
  newOnes = {}

  schema.clone.each do |key, table|
    table.each do |name, field|
      if ["ptr"].include? field['type']
        newOnes[field['ptr']] = {} if newOnes[field['ptr']].blank?
        newOnes[field['ptr']][key+"_"+name] = {"type" => "has_many", "primary_key" => name, "link_from" => key}
      end
    end
  end
  
  newOnes.each do |k, v|
    schema[k]['ref'] = v 
  end
  
  schema.each do |key, table|
    tablename = key.gsub(".", "_")
    lastpart = key.split(".").last
    filename = tablename.classify.tableize.singularize + '.rb';

    txt = ''
    txt << "# " + tablename.classify.tableize.singularize + ".rb\n\n"
    
    txt << "class "+tablename.classify+" < ActiveRecord::Base\n"
    txt << "  self.table_name   = \""+tablename+"_\"\n"
    txt << "  self.primary_key  = \""+lastpart+"_\"\n"
    txt << "  self.pointer_type = \""+key+"\"\n\n"
    
    table.each do |name, field|
      if name == 'ref'
        field.each do |i, f|
          txt << "  has_many :"+f['link_from'].split('.').last.downcase.pluralize+", :foreign_key => '"+f['primary_key']+"_', :class_name => \""+f['link_from'].gsub(".","_").classify+"\"\n"
        end
      else
        #puts key
        if ["ptr"].include? field['type']
          txt << "  belongs_to :"+name.downcase+", :foreign_key => '"+name+"_', :class_name => '"+field['ptr'].gsub(".", "_").classify+"'\n"
          #puts "  belongs_to :"+name.downcase+", :foreign_key => '"+name+"_', :class_name => '"+field['ptr'].gsub(".", "_").classify+"'\n"
        end
      end
    end

    txt << "\n  fix_makumba_columns\n"
    
    txt << "end\n\n"

    File.open(@output_path+"app/models/"+filename, "w+") do |f|
      f.write(txt)
    end

  end
  
  txt    
end

.generate_ruby_schema(schema) ⇒ Object



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/makumba_import/importer.rb', line 132

def self.generate_ruby_schema(schema)
  
  txt = "# encoding: UTF-8\n\n";
  
  txt << "ActiveRecord::Schema.define(:version => "+Time.now.strftime("%Y%m%d%H%M%S")+") do\n\n"
  
  schema.each do |key, table|
    tablename = key.gsub(".", "_")
    lastpart = key.split(".").last
    
    txt << "  create_table \""+tablename+"_\", :force => true do |t|\n"
    
    table.each do |name, field|
      #type = "integer "
      type = field['type']
      type = "integer " if ["int", "primary", "ptr", "enum", "set enum"].include? field['type']
      type = "string  " if ["char"].include? field['type']
      type = "boolean " if ["boolean"].include? field['type']
      type = "datetime" if ["date"].include? field['type']
      type = "text    " if ["text"].include? field['type']
      type = "float   " if ["real"].include? field['type']
      
      enum = "";
      
      if field['type'] == "enum"
        enum = "\t\t # "
        field['fields'].each {|id,f| enum << id+" = \""+f+"\", "}
        enum = enum[0...-2]
      end

      txt << "    t."+type+" \""+name+"_\""+(field['not_null'].blank? ? "" : ",  :null => false")+enum+"\n"
      
      if field['type'] == 'primary'
        txt << "    t.datetime \"TS_modify_\",  :null => false\n"
        txt << "    t.datetime \"TS_create_\",  :null => false\n"
      end
      
    end
    
    txt << "  end\n\n"
    
    txt << "  add_index \""+tablename+"_\", [\""+lastpart+"_\"], :name => \""+lastpart+"_\", :unique => true\n"
    table.each do |name, field|
      unless field['unique'].blank?
        txt << "  add_index \""+tablename+"_\", [\""+name+"_\"], :name => \""+name+"_\", :unique => true\n"
      end
    end
    
    txt << "  \n\n"
    
  end
  
  txt << "end\n\n"
  
  File.open(@output_path+"db/schema.rb", "w+") do |f|
    f.write(txt)
  end
end

.get_data(lines, mdd) ⇒ Object



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
# File 'lib/makumba_import/importer.rb', line 15

def self.get_data(lines, mdd)
  fieldRegexp = /^([a-zA-Z\_]+)\s*=\s*([^;]+);?(.*)/
  fieldTypes = ["int", "text", "real", "date", "boolean", "binary", "file"]
  
  schema = {}
  schema[mdd] = {}
  primary = mdd.split(".").last
  schema[mdd][primary] = {'type' => 'primary'}
  
  lines.each do |line|
    unless line.match("^#") or line.strip == "" or line.match("->")
      if m = fieldRegexp.match(line)
        obj = {}
        field = m[1].strip
        attrs = m[2].strip
        comment = m[3] ? m[3].strip.gsub(/^;/,"").strip : ""
        
        attrs.split(" ").each do |attr|
          if fieldTypes.include?(attr)
            obj['type'] = attr
          end
          if attr.match(/fixed/)
            obj['not_null'] = true
          end
        end
        if m2 = attrs.match(/char\[(\d+)\]/)
          obj['type'] = "char"
          obj['length'] = m2[1]
        end
        if attrs.match(/set$/)
          newmdd = mdd+".."+field
          lines2 = []
          lines.each do |l|
            if l.start_with?(field+"->")
              lines2.push l.gsub(field+"->", "")
            end
          end
          data2 = get_data(lines2, newmdd)
          schema.merge!(data2)
          schema[newmdd][primary] = {'type' => "ptr", 'ptr' => mdd}
        end
        if m3 = attrs.match(/int\s?\{([^\}]+)\}/)
          obj['type'] = "enum"
          obj['fields'] = {}
          # assuming there's no comma in the enum strings, (this is true for cherry)
          m3[1].split(",").each do |part|
            m4 = part.strip.match(/"([^"]+)"\s*=\s*(-?[0-9]+)/)
            obj['fields'][m4[2]] = m4[1]
          end
        end
        if m3 = attrs.match(/ptr\s+([a-zA-Z\.\_]+)/)
          obj['type'] = "ptr"
          obj['ptr'] = m3[1]
        end
        if m3 = attrs.match(/set\s+([a-zA-Z\.\_]+)/)
          newmdd = mdd+".."+field
          fieldname = m3[1].split('.').last
          schema[newmdd] = {}
          schema[newmdd][field] = {'type' => 'primary'}
          schema[newmdd][primary] = {'type' => "ptr", 'ptr' => mdd}
          schema[newmdd][fieldname] = {'type' => "ptr", 'ptr' => m3[1]}
        end
        if m3 = attrs.match(/set int\s?\{([^\}]+)\}/)
          fields = {}
          # assuming there's no comma in the enum strings, (this is true for cherry)
          m3[1].split(",").each do |part|
            m4 = part.strip.match(/"([^"]+)"\s*=\s*(-?[0-9]+)/)
            fields[m4[2]] = m4[1]
          end

          newmdd = mdd+".."+field
          schema[newmdd] = {}
          schema[newmdd][field] = {'type' => 'primary'}
          schema[newmdd][primary] = {'type' => "ptr", 'ptr' => mdd}
          schema[newmdd]['enum'] = {'type' => "set enum", 'fields' => fields}
        end
        if attrs.match(/not null/)
          obj['not_null'] = true
        end
        if attrs.match(/unique/)
          obj['unique'] = true
        end
        
        unless obj['type'].blank?
          schema[mdd][field] = obj 
        end
      end
      
    end
  end
  #puts schema
  schema
  
end

.load_mddsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/makumba_import/importer.rb', line 110

def self.load_mdds
  dir = @mdd_path

  @files = Dir.glob(dir+"/*/*.mdd")
  @files |= Dir.glob(dir+"/*/*/*.mdd")
  @files |= Dir.glob(dir+"/*/*/*/*.mdd")
  
  schema = {}
  
  for file in @files
    unless File.directory?(file)
      filename = file.gsub(dir+"/", "").gsub(".mdd","")
      mdd = filename.gsub("/", ".")
      
      lines = open(file).map { |line| line }
      schema.merge!(get_data(lines, mdd))
    end
  end
  
  schema
end

.set_mdd_path(mdd_path) ⇒ Object



7
8
9
# File 'lib/makumba_import/importer.rb', line 7

def self.set_mdd_path(mdd_path)
  @mdd_path = mdd_path
end

.set_output_path(output_path) ⇒ Object



11
12
13
# File 'lib/makumba_import/importer.rb', line 11

def self.set_output_path(output_path)
  @output_path = output_path
end