[Dive4elements-commits] [PATCH 3 of 5] Pull information about the backend into dgm

Wald Commits scm-commit at wald.intevation.org
Wed Feb 20 10:47:14 CET 2013


# HG changeset patch
# User Andre Heinecke <aheinecke at intevation.de>
# Date 1361353207 -3600
# Branch dami
# Node ID d6860fca89e4787b7c20e81191626431f43142c8
# Parent  1f33012cc02a8dcf3b1696634f52102b0843e163
Pull information about the backend into dgm.

    While the api of cx_oracle and psycopg2 is compatible enough,
    the query syntax is not :(

diff -r 1f33012cc02a -r d6860fca89e4 flys-backend/contrib/shpimporter/dgm.py
--- a/flys-backend/contrib/shpimporter/dgm.py	Wed Feb 20 10:38:17 2013 +0100
+++ b/flys-backend/contrib/shpimporter/dgm.py	Wed Feb 20 10:40:07 2013 +0100
@@ -26,8 +26,10 @@
 
 SQL_INSERT_DGT = "INSERT INTO dem (river_id, " + ", ".join(DGM_MAP.keys()) + \
         ") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
+SQL_INSERT_DGT_ORA = "INSERT INTO dem (river_id, " + ", ".join(DGM_MAP.keys()) + \
+        ") VALUES (:s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s, :s)"
 
-def insertRiverDgm(dbconn, dgmfile, river_name, dry_run):
+def insertRiverDgm(dbconn, dgmfile, river_name, dry_run, oracle):
     with codecs.open(dgmfile, "r", "latin1") as csvfile:
         firstline = csvfile.readline()
         names = firstline.split(";")
@@ -37,7 +39,7 @@
             namedict[name] = field_nr
             field_nr += 1
 
-        river_id = utils.getRiverId(dbconn, river_name)
+        river_id = utils.getRiverId(dbconn, river_name, oracle)
         for line in csvfile:
             fields = line.split(";")
             if not fields: continue
@@ -52,7 +54,12 @@
                         values.append(unicode.encode(
                             fields[namedict[val]], "UTF-8"))
                 cur = dbconn.cursor()
-                cur.execute(SQL_INSERT_DGT, [river_id] + values)
+                if oracle:
+                    stmt = SQL_INSERT_DGT_ORA
+                else:
+                    stmt = SQL_INSERT_DGT
+
+                cur.execute(stmt, [river_id] + values)
 
         if not dry_run:
             dbconn.commit()
diff -r 1f33012cc02a -r d6860fca89e4 flys-backend/contrib/shpimporter/shpimporter.py
--- a/flys-backend/contrib/shpimporter/shpimporter.py	Wed Feb 20 10:38:17 2013 +0100
+++ b/flys-backend/contrib/shpimporter/shpimporter.py	Wed Feb 20 10:40:07 2013 +0100
@@ -145,7 +145,9 @@
     else:
         connstr = 'OCI:%s/%s@%s' % (config.user, config.password, config.host)
 
+    oracle = False # Marker if oracle is used.
     if 'OCI:' in connstr:
+        oracle = True
         try:
             import cx_Oracle as dbapi
             raw_connstr=connstr.replace("OCI:", "")
@@ -154,6 +156,7 @@
                   "Neccessary to connect to a Oracle Database.\n"
                   "Please refer to the installation "
                   "documentation." % sys.path)
+            return -1
 
     else: # Currently only support for oracle and postgres
         try:
@@ -164,6 +167,7 @@
                   "Neccessary to connect to a Posgresql Database.\n"
                   "Please refer to the installation "
                   "documentation." % sys.path)
+            return -1
 
     dbconn_raw = dbapi.connect(raw_connstr)
     dbconn = ogr.Open(connstr)
@@ -189,7 +193,7 @@
                     os.path.basename(os.path.normpath(directory)))
         else:
             river_name = config.river_name
-        river_id = utils.getRiverId(dbconn_raw, river_name)
+        river_id = utils.getRiverId(dbconn_raw, river_name, oracle)
 
         if not river_id:
             logger.info("Could not find river in database. Skipping: %s"
@@ -229,7 +233,8 @@
                      "Skipping DGM import." % dgmfilename)
             else:
                 logger.info("Inserting DGM meta information in 'dem' table.")
-                insertRiverDgm(dbconn_raw, dgmfilename, river_name, config.dry_run)
+                insertRiverDgm(dbconn_raw, dgmfilename, river_name,
+                        config.dry_run, oracle)
         else:
             logger.info("Skip import of DGM.")
 
diff -r 1f33012cc02a -r d6860fca89e4 flys-backend/contrib/shpimporter/utils.py
--- a/flys-backend/contrib/shpimporter/utils.py	Wed Feb 20 10:38:17 2013 +0100
+++ b/flys-backend/contrib/shpimporter/utils.py	Wed Feb 20 10:40:07 2013 +0100
@@ -11,6 +11,7 @@
 
 SHP='.shp'
 SQL_SELECT_RIVER_ID="SELECT id FROM rivers WHERE name = %s"
+SQL_SELECT_RIVER_ID_ORA="SELECT id FROM rivers WHERE name = :s"
 
 def findShapefiles(path):
     shapes = []
@@ -28,13 +29,19 @@
 
     return shapes
 
-def getRiverId(dbconn, name):
+def getRiverId(dbconn, name, oracle):
     """
     Returns the id of the river "name"
     Dbconn must be a python database connection api compliant object
     """
     cur = dbconn.cursor()
-    cur.execute(SQL_SELECT_RIVER_ID, (name,))
+    if oracle:
+        # This is stupid and shoudl not be neccessary. But I don't
+        # know how to make it work both ways. aheinecke - 02/2013
+        stmt = SQL_SELECT_RIVER_ID_ORA
+    else:
+        stmt = SQL_SELECT_RIVER_ID
+    cur.execute(stmt, (name,))
     row = cur.fetchone()
     if row:
         return row[0]


More information about the Dive4elements-commits mailing list