]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
initial 3.0-only driver for py-postgresql. tests are maybe 60/40 at the moment.
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 31 May 2009 22:47:50 +0000 (22:47 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 31 May 2009 22:47:50 +0000 (22:47 +0000)
lib/sqlalchemy/dialects/postgres/base.py
lib/sqlalchemy/dialects/postgres/pypostgresql.py [new file with mode: 0644]

index 90af08867b4c0bd2feda40b61e5f7cd74b241282..30a1877807c2dc97ea2c3eda2c9c2b9a2f7121c8 100644 (file)
@@ -514,7 +514,7 @@ class PGDialect(default.DefaultDialect):
                         sql.bindparam('schema', unicode(schema), type_=sqltypes.Unicode)] 
                 )
             )
-        return bool( cursor.rowcount )
+        return bool(cursor.fetchone())
 
     def has_sequence(self, connection, sequence_name):
         cursor = connection.execute(
@@ -523,7 +523,7 @@ class PGDialect(default.DefaultDialect):
                         "AND nspname != 'information_schema' AND relname = :seqname)", 
                         bindparams=[sql.bindparam('seqname', unicode(sequence_name), type_=sqltypes.Unicode)]
                     ))
-        return bool(cursor.rowcount)
+        return bool(cursor.fetchone())
 
     def table_names(self, connection, schema):
         result = connection.execute(
diff --git a/lib/sqlalchemy/dialects/postgres/pypostgresql.py b/lib/sqlalchemy/dialects/postgres/pypostgresql.py
new file mode 100644 (file)
index 0000000..b032aa6
--- /dev/null
@@ -0,0 +1,80 @@
+"""Support for the PostgreSQL database via py-postgresql.
+
+Connecting
+----------
+
+URLs are of the form `postgres+pypostgresql://user@password@host:port/dbname[?key=value&key=value...]`.
+
+
+"""
+from sqlalchemy.engine import default
+import decimal
+from sqlalchemy import util
+from sqlalchemy import types as sqltypes
+from sqlalchemy.dialects.postgres.base import PGDialect, PGDefaultRunner
+
+class PGNumeric(sqltypes.Numeric):
+    def bind_processor(self, dialect):
+        return None
+
+    def result_processor(self, dialect):
+        if self.asdecimal:
+            return None
+        else:
+            def process(value):
+                if isinstance(value, decimal.Decimal):
+                    return float(value)
+                else:
+                    return value
+            return process
+
+class Postgres_pypostgresqlExecutionContext(default.DefaultExecutionContext):
+    pass
+
+class Postgres_pypostgresqlDefaultRunner(PGDefaultRunner):
+    def execute_string(self, stmt, params=None):
+        return PGDefaultRunner.execute_string(self, stmt, params or ())
+        
+class Postgres_pypostgresql(PGDialect):
+    driver = 'pypostgresql'
+
+    supports_unicode_statements = True
+    
+    supports_unicode_binds = True
+    description_encoding = None
+    
+    defaultrunner = Postgres_pypostgresqlDefaultRunner
+    
+    default_paramstyle = 'format'
+    
+    supports_sane_rowcount = False  # alas....posting a bug now
+    
+    supports_sane_multi_rowcount = False
+    
+    execution_ctx_cls = Postgres_pypostgresqlExecutionContext
+    colspecs = util.update_copy(
+        PGDialect.colspecs,
+        {
+            sqltypes.Numeric : PGNumeric,
+            sqltypes.Float: sqltypes.Float,  # prevents PGNumeric from being used
+        }
+    )
+    
+    @classmethod
+    def dbapi(cls):
+        from postgresql.driver import dbapi20
+        return dbapi20
+
+    def create_connect_args(self, url):
+        opts = url.translate_connect_args(username='user')
+        if 'port' in opts:
+            opts['port'] = int(opts['port'])
+        else:
+            opts['port'] = 5432
+        opts.update(url.query)
+        return ([], opts)
+
+    def is_disconnect(self, e):
+        return "connection is closed" in str(e)
+
+dialect = Postgres_pypostgresql