lib/framework/rhom/rhom_object_factory.rb in rhodes-2.3.2 vs lib/framework/rhom/rhom_object_factory.rb in rhodes-2.4.0.beta.1

- old
+ new

@@ -162,16 +162,16 @@ def get_values_table_name is_schema_source() ? get_schema_table_name() : 'object_values' end def get_source_id - Rho::RhoConfig.sources[get_source_name]['source_id'].to_s + Rho::RhoConfig.sources[get_source_name]['source_id'].to_i end def convertOpToStr(val_op, value) res = "" - if val_op == 'IN' or val_op == 'in' + if val_op.upcase == 'IN' if value.is_a?(String) value = value.split(",") value.each do |item| item.strip! @@ -331,15 +331,21 @@ val_func = key[:func] if key[:func] attrib_name = key[:name] if key[:name] else attrib_name = key end - - sql << "attrib=" + ::Rhom::RhomDbAdapter.get_value_for_sql_stmt(attrib_name) + + sql << "attrib=" + ::Rhom::RhomDbAdapter.get_value_for_sql_stmt( + val_func.upcase == 'CAST' ? attrib_name.split(' ')[0] : attrib_name) sql << " AND source_id=" + srcid_value - sql << " AND " + (val_func.length > 0 ? val_func + "(value)" : "value") + ' ' + if val_func.upcase == 'CAST' + sql << " AND " + val_func + "(value " + attrib_name.split(' ')[1] + ' ' + attrib_name.split(' ')[2] + " ) " + else + sql << " AND " + (val_func.length > 0 ? val_func + "(value)" : "value") + ' ' + end + sql << convertOpToStr(val_op, value) sql end @@ -360,15 +366,21 @@ end if srcid_value.nil? sql << (val_func.length > 0 ? val_func + "(#{attrib_name})" : "#{attrib_name}") + ' ' else - sql << "attrib=?" - vals << attrib_name + sql << "attrib=?" + vals << (val_func.upcase == 'CAST' ? attrib_name.split(' ')[0] : attrib_name) + sql << " AND source_id=?" - vals << srcid_value - sql << " AND " + (val_func.length > 0 ? val_func + "(value)" : "value") + ' ' + vals << srcid_value + + if val_func.upcase == 'CAST' + sql << " AND " + val_func + "(value " + attrib_name.split(' ')[1] + ' ' + attrib_name.split(' ')[2] + " ) " + else + sql << " AND " + (val_func.length > 0 ? val_func + "(value)" : "value") + ' ' + end end if val_op == 'IN' or val_op == 'in' if value.is_a?(String) @@ -1047,10 +1059,30 @@ end ret_list end + def find_by_sql(sql_query) + raise ArgumentError, 'find_by_sql only works with FixedSchema models' if !is_schema_source + + db = ::Rho::RHO.get_src_db(get_source_name) + list = db.execute_sql(sql_query) + + ret_list = [] + list.each do |rowhash| + new_obj = self.new({:object=>"#{rowhash['object']}"}) + + rowhash.each do |attrName, attrVal| + new_obj.vars.merge!( { attrName.to_sym()=>attrVal } ) if attrVal + end + + ret_list << new_obj + end + + ret_list + end + def search(args) args[:source_names] = [self.name.to_s] SyncEngine.search(args) end @@ -1087,15 +1119,86 @@ def clear_notification SyncEngine.clear_notification(get_source_id.to_i) end + def on_sync_delete_error( objects, action ) + raise ArgumentError, 'on_sync_delete_error action should be :retry' unless action == :retry + return unless is_sync_source() + + nSrcID = get_source_id() + db_partition = Rho::RhoConfig.sources[get_source_name]['partition'].to_s + + db = ::Rho::RHO.get_src_db(get_source_name) + db.start_transaction + + begin + objects.each do |obj, values| + values['attributes'].each do |attrib, value| + + resUpdateType = db.select_from_table('changed_values', 'update_type', + {"object"=>obj, "source_id"=>nSrcID, "attrib"=>attrib, 'sent'=>0}) + next if resUpdateType && resUpdateType.length > 0 + + attrib_type = SyncEngine.is_blob_attr(db_partition, nSrcID,attrib) ? "blob.file" : "" + db.insert_into_table('changed_values', {"source_id"=>nSrcID, "object"=>obj, "attrib"=>attrib, + "value"=>value, "update_type"=>'delete', "attrib_type"=>attrib_type }) + end + end + db.commit + rescue Exception => e + puts 'on_sync_delete_error Exception: ' + e.inspect + db.rollback + + raise + end + + end + + def on_sync_update_error( objects, action ) + raise ArgumentError, 'on_sync_update_error action should be :retry' unless action == :retry + return unless is_sync_source() + + nSrcID = get_source_id() + db_partition = Rho::RhoConfig.sources[get_source_name]['partition'].to_s + + db = ::Rho::RHO.get_src_db(get_source_name) + db.start_transaction + + begin + objects.each do |obj, values| + values['attributes'].each do |attrib, value| + + resUpdateType = db.select_from_table('changed_values', 'update_type', + {"object"=>obj, "source_id"=>nSrcID, "attrib"=>attrib, 'sent'=>0}) + next if resUpdateType && resUpdateType.length > 0 + + attrib_type = SyncEngine.is_blob_attr(db_partition, nSrcID,attrib) ? "blob.file" : "" + db.insert_into_table('changed_values', {"source_id"=>nSrcID, "object"=>obj, "attrib"=>attrib, + "value"=>value, "update_type"=>'update', "attrib_type"=>attrib_type }) + end + end + db.commit + rescue Exception => e + puts 'on_sync_update_error Exception: ' + e.inspect + db.rollback + + raise + end + end + def on_sync_create_error( objects, action ) - raise ArgumentError, 'on_create_error action should be :delete or :recreate' unless action == :delete || action == :recreate + raise ArgumentError, 'on_sync_create_error action should be :delete or :recreate' unless action == :delete || action == :recreate return unless is_sync_source() - ar_objs = objects.is_a?(Array) ? objects : [objects] + ar_objs = objects + if objects.is_a?(Hash) + ar_objs = objects.keys() + elsif !objects.is_a?(Array) + ar_objs = [objects] + end + puts "ar_objs : #{ar_objs}" tableName = is_schema_source() ? get_schema_table_name : 'object_values' nSrcID = get_source_id() db = ::Rho::RHO.get_src_db(get_source_name) @@ -1608,10 +1711,10 @@ def get_inst_source_name self.class.name.to_s end def get_inst_source_id - Rho::RhoConfig.sources[get_inst_source_name]['source_id'].to_s + Rho::RhoConfig.sources[get_inst_source_name]['source_id'].to_i end def is_inst_sync_source Rho::RhoConfig.sources[get_inst_source_name]['sync_type'] != 'none' end \ No newline at end of file