+
+
+# ====================================================================
+# Relations handling
+# deleteuniquenoderelations(uqn_name,uid,db_now)
+# deleteitemrelations(way|node,'way'|'node',uid,db_now)
+
+def deleteuniquenoderelations(uqn_name,uid,db_now)
+ sql=<<-EOF
+ SELECT node_id,cr.id FROM #{uqn_name},current_relation_members crm,current_relations cr
+ WHERE crm.member_id=node_id
+ AND crm.member_type='node'
+ AND crm.id=cr.id
+ AND cr.visible=1
+ EOF
+
+ relnodes=ActiveRecord::Base.connection.select_all(sql)
+ relnodes.each do |a|
+ removefromrelation(a['node_id'],'node',a['id'],uid,db_now)
+ end
+end
+
+def deleteitemrelations(objid,type,uid,db_now)
+ sql=<<-EOF
+ SELECT cr.id FROM current_relation_members crm,current_relations cr
+ WHERE crm.member_id=#{objid}
+ AND crm.member_type='#{type}'
+ AND crm.id=cr.id
+ AND cr.visible=1
+ EOF
+
+ relways=ActiveRecord::Base.connection.select_all(sql)
+ relways.each do |a|
+ removefromrelation(objid,type,a['id'],uid,db_now)
+ end
+end
+
+def removefromrelation(objid,type,relation,uid,db_now)
+ rver=ActiveRecord::Base.connection.insert("INSERT INTO relations (id,user_id,timestamp,visible) VALUES (#{relation},#{uid},#{db_now},1)")
+
+ tagsql=<<-EOF
+ INSERT INTO relation_tags (id,k,v,version)
+ SELECT id,k,v,#{rver} FROM current_relation_tags
+ WHERE id=#{relation}
+ EOF
+ ActiveRecord::Base.connection.insert(tagsql)
+
+ membersql=<<-EOF
+ INSERT INTO relation_members (id,member_type,member_id,member_role,version)
+ SELECT id,member_type,member_id,member_role,#{rver} FROM current_relation_members
+ WHERE id=#{relation}
+ AND (member_id!=#{objid} OR member_type!='#{type}')
+ EOF
+ ActiveRecord::Base.connection.insert(membersql)
+
+ ActiveRecord::Base.connection.update("UPDATE current_relations SET user_id=#{uid},timestamp=#{db_now} WHERE id=#{relation}")
+ ActiveRecord::Base.connection.execute("DELETE FROM current_relation_members WHERE id=#{relation} AND member_type='#{type}' AND member_id=#{objid}")
+end
+
+