[Dive4elements-commits] [PATCH 2 of 3] Revert 5301:7d29c277000e which introduced inconsitencies in the sequences

Wald Commits scm-commit at wald.intevation.org
Wed Mar 20 17:20:24 CET 2013


# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1363795672 -3600
# Node ID 292f003370154bd9cc7c80931d5db7b1a4ffeb6c
# Parent  2f6e6ae2fc56fd51cfc7e0377d59974a579ac631
Revert 5301:7d29c277000e which introduced inconsitencies in the sequences

diff -r 2f6e6ae2fc56 -r 292f00337015 flys-backend/contrib/shpimporter/dgm.py
--- 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:


More information about the Dive4elements-commits mailing list