Class: Rho::RhoUtils

Inherits:
Object show all
Defined in:
lib/framework/rho/rhoutils.rb

Constant Summary collapse

@@mapSrc =
nil
@@mapSrcByIdx =
nil

Class Method Summary collapse

Class Method Details

.load_offline_data(tables = [], dir_prefix = nil, source_map = nil) ⇒ Object



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
# File 'lib/framework/rho/rhoutils.rb', line 33

def self.load_offline_data(tables=[], dir_prefix=nil, source_map=nil)
  columns = []
  tables.each do |filename|

    if @@mapSrc.nil?
        @@mapSrc = {}
        @@mapSrcByIdx = {}
        arSrcs = Rhom::RhomSource.find(:all)
        arSrcs.each do |src|
            @@mapSrc[src.name()] = src
            @@mapSrcByIdx[src.source_id()] = src
        end
    end
    mapDeleted = {}
    mapPartDeleted = {}
    #db = ::Rho::RHO.get_user_db()
    #db.delete_all_from_table(filename)
    #db.start_transaction
    db = nil
    
    first_row=true
    prefix = dir_prefix.nil? ? "" : dir_prefix
    cur_src = nil
    hashItem = {}
    cur_objid = nil
    f = File.open(File.join(Rho::RhoFSConnector.get_base_app_path(),'app',prefix,'fixtures',filename+'.txt'))
    f.each do |line|
      if first_row
        columns = line.chomp.split('|'); first_row = false; next;
      end
      parts = line.chomp.split('|')

      row = {}
      columns.each_with_index do |col,idx|
        col.strip! 
        if col == 'source_name'
          src_name = parts[idx]
          src_name = source_map[src_name] if source_map
          row['source_id'] = @@mapSrc[src_name].source_id()
        else
          row[col] = parts[idx]
        end
      end
      
      src = @@mapSrcByIdx[ row['source_id'].to_i ]
      src_db = src ? ::Rho::RHO.get_src_db(src.name() ) :  ::Rho::RHO.get_user_db()
      if src_db != db
        db.commit if db
        db = src_db
        db.start_transaction 
        
        if !mapPartDeleted[db]
            #puts  "delete_all_from_table: #{row}"
            db.delete_all_from_table(filename)
            mapPartDeleted[db] = 1
        end
      end
      
      if ( cur_objid != row['object'] )
        if cur_src && hashItem && hashItem.length() > 0            
        
            if !mapDeleted[cur_src.name()]
                db.delete_all_from_table(cur_src.name())
                mapDeleted[cur_src.name()] = 1
            end
            
            hashItem['object'] = cur_objid
            db.insert_into_table(cur_src.name(),hashItem) 
        end
        
        hashItem = {}
        cur_objid = row['object']            
      end
      
      cur_src = @@mapSrcByIdx[ row['source_id'].to_i ]
      #puts "cur_src: #{cur_src.schema()}" if cur_src
      if ( cur_src && cur_src.schema() )  
        hashItem[ row['attrib'] ] = row['value'] if row['value']
      else
        db.insert_into_table(filename,row)
      end  
    end                
    f.close
    
    if cur_src && hashItem && hashItem.length() > 0            
    
        if !mapDeleted[cur_src.name()]
            db.delete_all_from_table(cur_src.name())
            mapDeleted[cur_src.name()] = 1
        end
        
        hashItem['object'] = cur_objid
        db.insert_into_table(cur_src.name(),hashItem) 
    end

    db.commit if db
    columns = []
  end
end

.load_offline_data_ex(tables = [], dir_prefix = nil, commit_count = 100000) ⇒ Object



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
# File 'lib/framework/rho/rhoutils.rb', line 133

def self.load_offline_data_ex(tables=[], dir_prefix=nil, commit_count=100000)
  columns = []
  tables.each do |filename|

    db = ::Rho::RHO.get_user_db()    
    db.destroy_table(filename)
    db.start_transaction
    
    row_index=0
    prefix = dir_prefix.nil? ? "" : dir_prefix
    query = ""
    
    f = File.open(File.join(Rho::RhoFSConnector.get_base_app_path(),'app',prefix,'fixtures',filename+'.txt'))
    f.each do |line|
      if row_index == 0
        columns = line.chomp.split('|')
        quests = ""
        columns.each do |col|             
            quests += ',' if quests.length() > 0
            quests += '?'
        end
        query = "insert into #{filename} (#{columns.join(',')}) values (#{quests})"
        row_index += 1
        next
      end
      
      parts = line.chomp.split('|')
      
      begin
          db.execute_sql query, parts
      rescue Exception => e
        #puts "load_offline_data : exception insert data: #{e}; data : #{parts}; line : #{row_index}"
      end
      
      if row_index%commit_count == 0          
        puts "commit start"
        db.commit
        db.start_transaction
        puts "commit : #{row_index}"
      end  
      
      row_index += 1
    end                  
    f.close
    
    db.commit
    puts "commit : #{row_index}"
    columns = []
  end
end