Mercurial > dive4elements > river
comparison flys-backend/contrib/shpimporter/dgm.py @ 5352:f1009aa7dcd1
Fix id handling in dgm import
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Wed, 20 Mar 2013 17:20:19 +0100 |
parents | 292f00337015 |
children | 5b50247d4d99 |
comparison
equal
deleted
inserted
replaced
5351:292f00337015 | 5352:f1009aa7dcd1 |
---|---|
52 SELECT id FROM ranges WHERE river_id = :s AND a = :s AND b = :s | 52 SELECT id FROM ranges WHERE river_id = :s AND a = :s AND b = :s |
53 """ | 53 """ |
54 SQL_INSERT_RANGE_ID_ORA = """ | 54 SQL_INSERT_RANGE_ID_ORA = """ |
55 INSERT INTO ranges (id, river_id, a, b) VALUES (:s, :s, :s, :s) | 55 INSERT INTO ranges (id, river_id, a, b) VALUES (:s, :s, :s, :s) |
56 """ | 56 """ |
57 SQL_NEXT_ID = "SELECT coalesce(max(id), -1) + 1 FROM %s" | 57 SQL_NEXT_ID = "select nextval('%s_ID_SEQ')" |
58 SQL_NEXT_ID_ORA = "select %s_ID_SEQ.nextval FROM dual" | |
58 | 59 |
59 | 60 def next_id(cur, relation, oracle): |
60 NEXT_IDS = {} | 61 if oracle: |
61 def next_id(cur, relation): | 62 cur.execute(SQL_NEXT_ID_ORA % relation.upper()) |
62 idx = NEXT_IDS.get(relation) | 63 else: |
63 if idx is None: | 64 cur.execute(SQL_NEXT_ID % relation.upper()) |
64 cur.execute(SQL_NEXT_ID % relation) | 65 idx = cur.fetchone()[0] |
65 idx = cur.fetchone()[0] | |
66 NEXT_IDS[relation] = idx + 1 | |
67 return idx | 66 return idx |
68 | 67 |
69 def get_range_id(cur, river_id, a, b, oracle): | 68 def get_range_id(cur, river_id, a, b, oracle): |
70 if oracle: | 69 if oracle: |
71 cur.execute(SQL_SELECT_RANGE_ID_ORA, (river_id, a, b)) | 70 cur.execute(SQL_SELECT_RANGE_ID_ORA, (river_id, a, b)) |
72 else: | 71 else: |
73 cur.execute(SQL_SELECT_RANGE_ID, (river_id, a, b)) | 72 cur.execute(SQL_SELECT_RANGE_ID, (river_id, a, b)) |
74 row = cur.fetchone() | 73 row = cur.fetchone() |
75 if row: return row[0] | 74 if row: return row[0] |
76 idx = next_id(cur, "ranges") | 75 idx = next_id(cur, "ranges", oracle) |
77 if oracle: | 76 if oracle: |
78 cur.execute(SQL_INSERT_RANGE_ID_ORA, (idx, river_id, a, b)) | 77 cur.execute(SQL_INSERT_RANGE_ID_ORA, (idx, river_id, a, b)) |
79 else: | 78 else: |
80 cur.execute(SQL_INSERT_RANGE_ID, (idx, river_id, a, b)) | 79 cur.execute(SQL_INSERT_RANGE_ID, (idx, river_id, a, b)) |
81 cur.connection.commit() | 80 cur.connection.commit() |
88 cur.execute(SQL_SELECT_TIME_ID_ORA, (a, b)) | 87 cur.execute(SQL_SELECT_TIME_ID_ORA, (a, b)) |
89 else: | 88 else: |
90 cur.execute(SQL_SELECT_TIME_ID, (a, b)) | 89 cur.execute(SQL_SELECT_TIME_ID, (a, b)) |
91 row = cur.fetchone() | 90 row = cur.fetchone() |
92 if row: return row[0] | 91 if row: return row[0] |
93 idx = next_id(cur, "time_intervals") | 92 idx = next_id(cur, "time_intervals", oracle) |
94 if oracle: | 93 if oracle: |
95 cur.execute(SQL_INSERT_TIME_ID_ORA, (idx, a, b)) | 94 cur.execute(SQL_INSERT_TIME_ID_ORA, (idx, a, b)) |
96 else: | 95 else: |
97 cur.execute(SQL_INSERT_TIME_ID, (idx, a, b)) | 96 cur.execute(SQL_INSERT_TIME_ID, (idx, a, b)) |
98 cur.connection.commit() | 97 cur.connection.commit() |