changeset 5351:292f00337015

Revert 5301:7d29c277000e which introduced inconsitencies in the sequences
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 20 Mar 2013 17:07:52 +0100
parents 2f6e6ae2fc56
children f1009aa7dcd1
files flys-backend/contrib/shpimporter/dgm.py
diffstat 1 files changed, 14 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/contrib/shpimporter/dgm.py	Wed Mar 20 16:42:52 2013 +0100
+++ b/flys-backend/contrib/shpimporter/dgm.py	Wed Mar 20 17:07:52 2013 +0100
@@ -34,34 +34,34 @@
 SELECT id FROM time_intervals WHERE start_time = %s AND stop_time = %s
 """
 SQL_INSERT_TIME_ID = """
-INSERT INTO time_intervals (start_time, stop_time) VALUES (%s, %s)
+INSERT INTO time_intervals (id, start_time, stop_time) VALUES (%s, %s, %s)
 """
 SQL_SELECT_TIME_ID_ORA = """
 SELECT id FROM time_intervals WHERE start_time = :s AND stop_time = :s
 """
 SQL_INSERT_TIME_ID_ORA = """
-INSERT INTO time_intervals (start_time, stop_time) VALUES (:s, :s)
+INSERT INTO time_intervals (id, start_time, stop_time) VALUES (:s, :s, :s)
 """
 SQL_SELECT_RANGE_ID = """
 SELECT id FROM ranges WHERE river_id = %s AND a = %s AND b = %s
 """
 SQL_INSERT_RANGE_ID = """
-INSERT INTO ranges (river_id, a, b) VALUES (%s, %s, %s)
+INSERT INTO ranges (id, river_id, a, b) VALUES (%s, %s, %s, %s)
 """
 SQL_SELECT_RANGE_ID_ORA = """
 SELECT id FROM ranges WHERE river_id = :s AND a = :s AND b = :s
 """
 SQL_INSERT_RANGE_ID_ORA = """
-INSERT INTO ranges (river_id, a, b) VALUES (:s, :s, :s)
+INSERT INTO ranges (id, river_id, a, b) VALUES (:s, :s, :s, :s)
 """
-SQL_NEW_ID     = "SELECT max(id) FROM %s"
+SQL_NEXT_ID     = "SELECT coalesce(max(id), -1) + 1 FROM %s"
 
 
 NEXT_IDS = {}
 def next_id(cur, relation):
     idx = NEXT_IDS.get(relation)
     if idx is None:
-        cur.execute(SQL_NEW_ID % relation)
+        cur.execute(SQL_NEXT_ID % relation)
         idx = cur.fetchone()[0]
     NEXT_IDS[relation] = idx + 1
     return idx
@@ -73,12 +73,13 @@
         cur.execute(SQL_SELECT_RANGE_ID, (river_id, a, b))
     row = cur.fetchone()
     if row: return row[0]
+    idx = next_id(cur, "ranges")
     if oracle:
-        cur.execute(SQL_INSERT_RANGE_ID_ORA, (river_id, a, b))
+        cur.execute(SQL_INSERT_RANGE_ID_ORA, (idx, river_id, a, b))
     else:
-        cur.execute(SQL_INSERT_RANGE_ID, (river_id, a, b))
+        cur.execute(SQL_INSERT_RANGE_ID, (idx, river_id, a, b))
     cur.connection.commit()
-    return next_id(cur, "ranges")
+    return idx
 
 def get_time_interval_id(cur, a, b, oracle):
     if not a or not b:
@@ -89,12 +90,13 @@
         cur.execute(SQL_SELECT_TIME_ID, (a, b))
     row = cur.fetchone()
     if row: return row[0]
+    idx = next_id(cur, "time_intervals")
     if oracle:
-        cur.execute(SQL_INSERT_TIME_ID_ORA, (a, b))
+        cur.execute(SQL_INSERT_TIME_ID_ORA, (idx, a, b))
     else:
-        cur.execute(SQL_INSERT_TIME_ID, (a, b))
+        cur.execute(SQL_INSERT_TIME_ID, (idx, a, b))
     cur.connection.commit()
-    return next_id(cur, "time_intervals")
+    return idx
 
 def insertRiverDgm(dbconn, dgmfile, river_name, dry_run, oracle):
     with codecs.open(dgmfile, "r", "latin1") as csvfile:

http://dive4elements.wald.intevation.org