[Thuban-commits] r2768 - in trunk/thuban: . Thuban/Model test

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu May 10 00:06:31 CEST 2007


Author: dpinte
Date: 2007-05-10 00:06:29 +0200 (Thu, 10 May 2007)
New Revision: 2768

Modified:
   trunk/thuban/ChangeLog
   trunk/thuban/Thuban/Model/postgisdb.py
   trunk/thuban/test/postgissupport.py
   trunk/thuban/test/test_postgis_db.py
Log:
2007-05-10  Didrik Pinte <dpinte at itae.be>

    Migration to psycopg2 to support UTF8 environments

    * Thuban/Model/postgisdb.py : psycopg2 migration
    * test/postgissupport.py, test_postgis_db.py : psycopg2 migration



Modified: trunk/thuban/ChangeLog
===================================================================
--- trunk/thuban/ChangeLog	2007-05-08 18:11:35 UTC (rev 2767)
+++ trunk/thuban/ChangeLog	2007-05-09 22:06:29 UTC (rev 2768)
@@ -1,4 +1,10 @@
-<<<<<<< .mine
+2007-05-10  Didrik Pinte <dpinte at itae.be>
+
+	Migration to psycopg2 to support UTF8 environments
+
+	* Thuban/Model/postgisdb.py : psycopg2 migration
+	* test/postgissupport.py, test_postgis_db.py : psycopg2 migration
+
 2007-05-07  Bernhard Reiter <bernhard at intevation.de>
 
 	* Releasenotes.txt: Improved the language on the announcement,

Modified: trunk/thuban/Thuban/Model/postgisdb.py
===================================================================
--- trunk/thuban/Thuban/Model/postgisdb.py	2007-05-08 18:11:35 UTC (rev 2767)
+++ trunk/thuban/Thuban/Model/postgisdb.py	2007-05-09 22:06:29 UTC (rev 2768)
@@ -11,9 +11,10 @@
 from __future__ import generators
 
 try:
-    import psycopg
+    import psycopg2
+    from psycopg2 import extensions
 except ImportError:
-    psycopg = None
+    psycopg2 = None
 
 import table
 import wellknowntext
@@ -26,18 +27,18 @@
     Having PostGIS support means that the psycopg module can be
     imported.
     """
-    return psycopg is not None
+    return psycopg2 is not None
 
 def psycopg_version():
-    return psycopg.__version__
+    return psycopg2.__version__
 
-if psycopg is not None:
+if psycopg2 is not None:
     # type_map maps psycopg type objects.  It's a list of pairs since
     # the psycopg type objects are unhashable.
-    type_map = [(psycopg.STRING, table.FIELDTYPE_STRING),
-                (psycopg.INTEGER, table.FIELDTYPE_INT),
-                (psycopg.ROWID, table.FIELDTYPE_INT),
-                (psycopg.FLOAT, table.FIELDTYPE_DOUBLE)]
+    type_map = [(psycopg2.STRING, table.FIELDTYPE_STRING),
+                (extensions.INTEGER, table.FIELDTYPE_INT),
+                (psycopg2.ROWID, table.FIELDTYPE_INT),
+                (extensions.FLOAT, table.FIELDTYPE_DOUBLE)]
 
     # _raw_type_map maps the postgresql type constants to Thuban type
     # constants.  This is very low level and postgresql specific and
@@ -103,8 +104,8 @@
             if val:
                 params.append("%s=%s" % (name, val))
         try:
-            self.connection = psycopg.connect(" ".join(params))
-        except psycopg.OperationalError, val:
+            self.connection = psycopg2.connect(" ".join(params))
+        except psycopg2.OperationalError, val:
             raise ConnectionError(str(val))
 
         # Use autocommit mode.  For simple reading of the database it's
@@ -115,7 +116,7 @@
         # the same connection will lead to further errors ("ERROR:
         # current transaction is aborted, commands ignored until end of
         # transaction block")
-        self.connection.autocommit()
+        self.connection.set_isolation_level(0)
 
         # determine the OID for the geometry type. This is PostGIS
         # specific.
@@ -170,7 +171,7 @@
                              # Omit the special PostGIS tables
                              " AND c.relname NOT IN ('geometry_columns',"
                                                    " 'spatial_ref_sys')"
-                            " AND %d in (SELECT a.atttypid FROM pg_attribute a"
+                            " AND %s in (SELECT a.atttypid FROM pg_attribute a"
                                        " WHERE a.attrelid = c.oid)"
                        " ORDER BY c.relname;", (self.geometry_type,))
         result = [row[0] for row in cursor.fetchall()]
@@ -377,23 +378,23 @@
     def RowIdToOrdinal(self, gid):
         """Return the row ordinal given its id"""
         cursor = self.db.cursor()
-        cursor.execute("SELECT count(*) FROM %s WHERE %s < %d;"
+        cursor.execute("SELECT count(*) FROM %s WHERE %s < %s;"
                        % (self.quoted_tablename, self.quoted_id_column, gid))
         return cursor.fetchone()[0]
 
     def RowOrdinalToId(self, num):
         """Return the rowid for given its ordinal"""
         cursor = self.db.cursor()
-        cursor.execute("SELECT %s FROM %s LIMIT 1 OFFSET %d;"
+        cursor.execute("SELECT %s FROM %s LIMIT 1 OFFSET %s;"
                        % (self.quoted_id_column, self.quoted_tablename, num))
         return cursor.fetchone()[0]
 
     def ReadRowAsDict(self, row, row_is_ordinal = 0):
         cursor = self.db.cursor()
         if row_is_ordinal:
-            stmt = self.query_stmt + " LIMIT 1 OFFSET %d" % row
+            stmt = self.query_stmt + " LIMIT 1 OFFSET %s" % row
         else:
-            stmt = self.query_stmt + " WHERE %s = %d" % (self.quoted_id_column,
+            stmt = self.query_stmt + " WHERE %s = %s" % (self.quoted_id_column,
                                                          row)
         cursor.execute(stmt)
         result = {}
@@ -404,11 +405,11 @@
     def ReadValue(self, row, col, row_is_ordinal = 0):
         cursor = self.db.cursor()
         if row_is_ordinal:
-            stmt = ("SELECT %s FROM %s LIMIT 1 OFFSET %d" %
+            stmt = ("SELECT %s FROM %s LIMIT 1 OFFSET %s" %
                     (self.column_map[col].quoted_name, self.quoted_tablename,
                      row))
         else:
-            stmt = ("SELECT %s FROM %s WHERE %s = %d" %
+            stmt = ("SELECT %s FROM %s WHERE %s = %s" %
                     (self.column_map[col].quoted_name, self.quoted_tablename,
                      self.quoted_id_column, row))
         cursor.execute(stmt)
@@ -531,7 +532,7 @@
         # First, try to get it from the geometry_columns table.
         cursor = self.db.cursor()
         cursor.execute("SELECT srid, type FROM geometry_columns"
-                       " WHERE f_table_name = %s AND f_geometry_column=%s",
+                       " WHERE f_table_name = %s AND f_geometry_column = %s",
                        (self.tablename, self.geometry_column))
         row = cursor.fetchone()
         if row is not None:
@@ -658,7 +659,7 @@
 
     def Shape(self, shapeid):
         cursor = self.db.cursor()
-        cursor.execute("SELECT AsText(%s) FROM %s WHERE %s=%d"
+        cursor.execute("SELECT AsText(%s) FROM %s WHERE %s = %s"
                        % (self.quoted_geo_col, self.quoted_tablename,
                           self.quoted_id_column, shapeid))
         wkt = cursor.fetchone()[0]

Modified: trunk/thuban/test/postgissupport.py
===================================================================
--- trunk/thuban/test/postgissupport.py	2007-05-08 18:11:35 UTC (rev 2767)
+++ trunk/thuban/test/postgissupport.py	2007-05-09 22:06:29 UTC (rev 2768)
@@ -22,9 +22,9 @@
 import support
 
 try:
-    import psycopg
+    import psycopg2
 except ImportError:
-    psycopg = None
+    psycopg2 = None
 
 #
 #       Helper code
@@ -326,7 +326,7 @@
         first row of the result will be returned.  Otherwise the return
         value is None.
         """
-        conn = psycopg.connect("dbname=%s " % dbname
+        conn = psycopg2.connect("dbname=%s " % dbname
                                + self.connection_string(user))
         cursor = conn.cursor()
         cursor.execute(sql)
@@ -471,7 +471,7 @@
         for srid, params in self.reference_systems:
             self.server.execute_sql(self.dbname, "admin",
                                     "INSERT INTO spatial_ref_sys VALUES"
-                                    " (%d, '', %d, '', '%s');"
+                                    " (%s, '', %s, '', '%s');"
                                     % (srid, srid, params))
         if self.tables is not None:
             def unpack(item):
@@ -611,7 +611,7 @@
 
     # The test for psycopg was already done when this module was
     # imported so we only have to check whether it was successful
-    if psycopg is None:
+    if psycopg2 is None:
         return "Can't run PostGIS tests because psycopg can't be imported"
 
     return ""
@@ -729,7 +729,7 @@
 
     server = db.server
     dbname = db.dbname
-    conn = psycopg.connect("dbname=%s " % dbname
+    conn = psycopg2.connect("dbname=%s " % dbname
                            + db.server.connection_string("admin"))
     cursor = conn.cursor()
 
@@ -765,10 +765,10 @@
     convert = wkt_converter[wkttype]
 
     cursor.execute("select AddGeometryColumn('%(dbname)s',"
-                   "'%(tablename)s', 'the_geom', %(srid)d, '%(wkttype)s', 2);"
+                   "'%(tablename)s', 'the_geom', %(srid)s, '%(wkttype)s', 2);"
                    % locals())
     fields.append("the_geom")
-    insert_formats.append("GeometryFromText(%(the_geom)s, %(srid)d)")
+    insert_formats.append("GeometryFromText(%(the_geom)s, %(srid)s)")
 
     insert = ("INSERT INTO %s (%s) VALUES (%s)"
               % (tablename, ", ".join(fields), ", ".join(insert_formats)))

Modified: trunk/thuban/test/test_postgis_db.py
===================================================================
--- trunk/thuban/test/test_postgis_db.py	2007-05-08 18:11:35 UTC (rev 2767)
+++ trunk/thuban/test/test_postgis_db.py	2007-05-09 22:06:29 UTC (rev 2768)
@@ -12,7 +12,7 @@
 
 
 try:
-    import psycopg
+    import psycopg2
 except ImportError:
     # No psycopg available. Nothing to be done here because the
     # postgis.py support module determines this too and the tests will
@@ -102,7 +102,7 @@
         db = PostGISConnection(dbname = self.dbname,
                                **self.server.connection_params("user"))
 
-        conn = psycopg.connect("dbname=%s " % self.dbname
+        conn = psycopg2.connect("dbname=%s " % self.dbname
                                + self.server.connection_string("admin"))
         cursor = conn.cursor()
         # First a normal table, i.e. one without a geometry column
@@ -123,7 +123,6 @@
                        " (SELECT * FROM geo WHERE a > 100);"
                        "GRANT SELECT ON my_view TO PUBLIC;")
         conn.commit()
-
         self.assertEquals(db.GeometryTables(), ["geo", "my_view"])
         self.assertEquals(db.table_columns("geo"),
                           [("oid", FIELDTYPE_INT), ("a", FIELDTYPE_INT),
@@ -192,7 +191,7 @@
         # The bigint column will be ignored because it's not mapped to a
         # known integer type, so there are only two colunns
         self.assertEquals(table.NumColumns(), 2)
-        self.assertEquals(table.Column(0).name, "gid")
+        self.assertEquals(table.Column(0).name, "gid")        
         self.assertEquals(table.Column(1).name, "length")
 
     def test_table_name_quoting(self):
@@ -278,7 +277,7 @@
 
     def test_shapestore_empty_table(self):
         """Test PostGISShapeStore with emtpy table"""
-        conn = psycopg.connect("dbname=%s " % self.dbname
+        conn = psycopg2.connect("dbname=%s " % self.dbname
                                + self.server.connection_string("admin"))
         cursor = conn.cursor()
         cursor.execute("CREATE TABLE test (A INT);")
@@ -295,7 +294,7 @@
 
     def test_shapestore_two_geom_cols(self):
         """Test PostGISShapeStore with two geometry columns"""
-        conn = psycopg.connect("dbname=%s " % self.dbname
+        conn = psycopg2.connect("dbname=%s " % self.dbname
                                + self.server.connection_string("admin"))
         cursor = conn.cursor()
         cursor.execute("INSERT INTO spatial_ref_sys VALUES"
@@ -356,7 +355,7 @@
         If an error happens in, say the SimpleQuery method, a subsequent
         ReadValue call should still succeed.
         """
-        conn = psycopg.connect("dbname=%s " % self.dbname
+        conn = psycopg2.connect("dbname=%s " % self.dbname
                                + self.server.connection_string("admin"))
         cursor = conn.cursor()
         cursor.execute("CREATE TABLE some_table"
@@ -372,7 +371,7 @@
 
         # trying to compare the length to a string should fail with an
         # exception.
-        self.assertRaises(psycopg.ProgrammingError,
+        self.assertRaises(psycopg2.ProgrammingError,
                           table.SimpleQuery,
                           table.Column("length"), "<=", "abc")
 
@@ -385,7 +384,7 @@
         #     until end of transaction block
         try:
             self.assertEquals(table.ReadValue(1, "length"), 3.5)
-        except psycopg.ProgrammingError, val:
+        except psycopg2.ProgrammingError, val:
             self.fail("table didn't handle exception properly (%s)" % val)
 
 



More information about the Thuban-commits mailing list