+ SELECT cn.id,visible,latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags
+ FROM way_nodes wn,current_nodes cn
+ WHERE wn.version=#{version}
+ AND wn.id=#{id}
+ AND wn.node_id=cn.id
+ ORDER BY sequence_id
+ EOF
+ rows=ActiveRecord::Base.connection.select_all(sql)
+
+ # if historic (full revert), get the old version of each node,
+ # and use this (though with a new id) if it differs from the current one
+ if historic then
+ rows.each_index do |i|
+ sql=<<-EOF
+ SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags
+ FROM nodes
+ WHERE id=#{rows[i]['id']}
+ AND timestamp<="#{waytime}"
+ ORDER BY timestamp DESC
+ LIMIT 1
+ EOF
+ row=ActiveRecord::Base.connection.select_one(sql)
+ unless row.nil? then
+ nx=row['longitude'].to_f
+ ny=row['latitude'].to_f
+ if (nx!=rows[i]['longitude'].to_f or ny!=rows[i]['latitude'].to_f or row['tags']!=rows[i]['tags']) then
+ rows[i]['id']=-1
+ # This generates a new node id if x/y/tags differ from current node.
+ # Strictly speaking, it need only do this for uniquenodes, but we're
+ # not generating uniquenodes for historic ways (yet!).
+ end
+ rows[i]['longitude']=nx
+ rows[i]['latitude' ]=ny
+ rows[i]['tags' ]=row['tags']
+ end
+ end
+ end
+ rows
+end
+
+def createuniquenodes(way,uqn_name,nodelist)
+ # Find nodes which appear in this way but no others
+ sql=<<-EOF
+ CREATE TEMPORARY TABLE #{uqn_name}
+ SELECT a.node_id
+ FROM (SELECT DISTINCT node_id FROM current_way_nodes
+ WHERE id=#{way}) a
+ LEFT JOIN current_way_nodes b
+ ON b.node_id=a.node_id
+ AND b.id!=#{way}
+ WHERE b.node_id IS NULL
+ EOF
+ unless nodelist.empty? then
+ sql+="AND a.node_id NOT IN ("+nodelist.join(',')+")"
+ end
+ ActiveRecord::Base.connection.execute(sql)
+end
+
+
+
+# ====================================================================
+# 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}")