Potlatch 1/Remote calls/putway SQL

From OpenStreetMap Wiki
Jump to: navigation, search

One of the most complex parts of the code within amf_controller.rb is the set of SQL statements (and supporting code) to write a way to the database ('putway'). 'deleteway', to delete a way, works similarly.

Here is how it works. 'Stage' numbers refer to comments in the code.

Stage 3: read original way into memory

  • If it's an existing way (i.e. way id>0):
    • Read the way's segments and nodes from the database (using the same query as 'getway')
    • Record the start/end nodes for each segment in a hash
    • Record x, y, tags for each node in further hashes
    • If we're writing a reverted/undeleted way, also record visible for each node
  • If it's a new way (way id<0)
    • Create a new way in the database:
      • Insert new row into meta_ways
      • Insert new row into current_ways
    • Get its ID

Stage 4: get version

  • Get version number by inserting new row into ways

Stage 5: compare new nodes against old

For each point in the array of points:

  • Update bounding box
  • If it's a new node (i.e. id<0) and not already created:
    • Create a new node in the db with these x/y/tags:
      • Insert new row into nodes
      • Insert new row into current_nodes
      • Record new id in renumberednodes so we can send it back to client
  • Else if it already existed in this way:
    • Update if x, y or tags have changed:
      • Insert new row into nodes
      • Update current_nodes
  • Otherwise ignore

Stage 6a: delete any nodes not in modified way

  • Create temporary table containing all nodes that are:
    • in the previous version of this way
    • not in the new version of this way
    • not in any other way
  • Delete each node in this table that isn't in the current way:
    • Insert new rows into nodes
    • Update current_nodes
    • Remove from any relations
  • Drop temporary table

Stage 6b: insert new version of route into way_nodes

  • Delete old version from current_way_nodes
  • Insert rows into way_nodes
  • Insert rows into current_way_nodes

Stage 7: insert new way tags

  • Delete old version from current_way_tags
  • Insert rows into way_tags
  • Insert rows into current_way_tags

Finally

  • Return 0 (success), original way ID, new way ID, hash of renumbered nodes, bounding box