summaryrefslogtreecommitdiff
path: root/python/skytools
diff options
context:
space:
mode:
authorMarko Kreen2007-07-23 14:16:22 +0000
committerMarko Kreen2007-07-23 14:16:22 +0000
commita03667304da8e8b3a28d8f76fdb6cac14d0e967f (patch)
tree10e424fe6c81467d8882b5d0e57dcd002a1e07cf /python/skytools
parent868c7b200a9fa6ea9ac1fb4dcf2beb16422d6d37 (diff)
skytools: separate psycopg specific code
Diffstat (limited to 'python/skytools')
-rw-r--r--python/skytools/__init__.py1
-rw-r--r--python/skytools/psycopgwrapper.py68
-rw-r--r--python/skytools/quoting.py5
-rw-r--r--python/skytools/scripting.py2
-rw-r--r--python/skytools/skylog.py2
-rw-r--r--python/skytools/sqltools.py50
6 files changed, 73 insertions, 55 deletions
diff --git a/python/skytools/__init__.py b/python/skytools/__init__.py
index f1ef7197..06018721 100644
--- a/python/skytools/__init__.py
+++ b/python/skytools/__init__.py
@@ -1,6 +1,7 @@
"""Tools for Python database scripts."""
+from psycopgwrapper import *
from config import *
from dbstruct import *
from gzlog import *
diff --git a/python/skytools/psycopgwrapper.py b/python/skytools/psycopgwrapper.py
new file mode 100644
index 00000000..36b41124
--- /dev/null
+++ b/python/skytools/psycopgwrapper.py
@@ -0,0 +1,68 @@
+
+"""Wrapper around psycopg1/2.
+
+Preferred is psycopg2, fallback to psycopg1.
+
+Interface provided is psycopg1:
+ - dict* methods.
+ - new columns can be assigned to row.
+
+"""
+
+__all__ = ["QuotedString", "connect_database"]
+
+try:
+ ##from psycopg2.psycopg1 import connect as _pgconnect
+ # psycopg2.psycopg1.cursor is too backwards compatible,
+ # to the point of avoiding optimized access.
+ # only backwards compat thing we need is dict* methods
+
+ import psycopg2.extensions, psycopg2.extras
+ from psycopg2.extensions import QuotedString
+
+ class _CompatRow(psycopg2.extras.DictRow):
+ """Allow setting fields by name."""
+ def __setitem__(self, k, v):
+ if type(k) != int:
+ if k not in self._index:
+ self._index[k] = len(self._index)
+ k = self._index[k]
+ while k >= len(self):
+ self.append(None)
+ return list.__setitem__(self, k, v)
+
+ class _CompatCursor(psycopg2.extras.DictCursor):
+ """Regular psycopg2 DictCursor with dict* methods."""
+ def __init__(self, *args, **kwargs):
+ psycopg2.extras.DictCursor.__init__(self, *args, **kwargs)
+ self.row_factory = _CompatRow
+ dictfetchone = psycopg2.extras.DictCursor.fetchone
+ dictfetchall = psycopg2.extras.DictCursor.fetchall
+ dictfetchmany = psycopg2.extras.DictCursor.fetchmany
+
+ class _CompatConnection(psycopg2.extensions.connection):
+ """Connection object that uses _CompatCursor."""
+ def cursor(self):
+ return psycopg2.extensions.connection.cursor(self, cursor_factory = _CompatCursor)
+
+ def _pgconnect(cstr):
+ """Create a psycopg2 connection."""
+ return _CompatConnection(cstr)
+
+except ImportError:
+ # use psycopg 1
+ from psycopg import connect as _pgconnect
+
+def connect_database(connstr):
+ """Create a db connection with connect_timeout option.
+
+ Default connect_timeout is 15, to change put it directly into dsn.
+ """
+
+ # allow override
+ if connstr.find("connect_timeout") < 0:
+ connstr += " connect_timeout=15"
+
+ # create connection
+ return _pgconnect(connstr)
+
diff --git a/python/skytools/quoting.py b/python/skytools/quoting.py
index 7df56fee..51e91905 100644
--- a/python/skytools/quoting.py
+++ b/python/skytools/quoting.py
@@ -4,10 +4,7 @@
import urllib, re
-try:
- from psycopg2.extensions import QuotedString
-except:
- from psycopg import QuotedString
+from skytools.psycopgwrapper import QuotedString
__all__ = [
"quote_literal", "quote_copy", "quote_bytea_raw",
diff --git a/python/skytools/scripting.py b/python/skytools/scripting.py
index 136c89ef..058ff523 100644
--- a/python/skytools/scripting.py
+++ b/python/skytools/scripting.py
@@ -5,7 +5,7 @@ import sys, os, signal, optparse, traceback, time
import logging, logging.handlers, logging.config
from skytools.config import *
-from skytools.sqltools import connect_database
+from skytools.psycopgwrapper import connect_database
import skytools.skylog
__all__ = ['daemonize', 'run_single_process', 'DBScript',
diff --git a/python/skytools/skylog.py b/python/skytools/skylog.py
index bde6f153..852d645b 100644
--- a/python/skytools/skylog.py
+++ b/python/skytools/skylog.py
@@ -4,8 +4,8 @@
import sys, os, time, socket
import logging, logging.handlers
+from skytools.psycopgwrapper import connect_database
from skytools.quoting import quote_json
-from skytools.sqltools import connect_database
# configurable file logger
diff --git a/python/skytools/sqltools.py b/python/skytools/sqltools.py
index 11e259af..ba1df74d 100644
--- a/python/skytools/sqltools.py
+++ b/python/skytools/sqltools.py
@@ -12,45 +12,10 @@ __all__ = [
"exists_function", "exists_language", "Snapshot", "magic_insert",
"db_copy_from_dict", "db_copy_from_list", "CopyPipe", "full_copy",
"DBObject", "DBSchema", "DBTable", "DBFunction", "DBLanguage",
- "db_install", "connect_database"
+ "db_install",
]
-try:
- ##from psycopg2.psycopg1 import connect as _pgconnect
- # psycopg2.psycopg1.cursor is too backwards compatible,
- # to the point of avoiding optimized access.
-
- ## only backwards compat thing we need is dict* methods
- import psycopg2.extensions, psycopg2.extras
- class _CompatRow(psycopg2.extras.DictRow):
- def __setitem__(self, k, v):
- if type(k) != int:
- if k not in self._index:
- self._index[k] = len(self._index)
- k = self._index[k]
- while k >= len(self):
- self.append(None)
- return list.__setitem__(self, k, v)
- class _CompatCursor(psycopg2.extras.DictCursor):
- """Regular psycopg2 DictCursor with dict* methods."""
- def __init__(self, *args, **kwargs):
- psycopg2.extras.DictCursor.__init__(self, *args, **kwargs)
- self.row_factory = _CompatRow
- dictfetchone = psycopg2.extras.DictCursor.fetchone
- dictfetchall = psycopg2.extras.DictCursor.fetchall
- dictfetchmany = psycopg2.extras.DictCursor.fetchmany
- class _CompatConnection(psycopg2.extensions.connection):
- """Connection object that uses _CompatCursor."""
- def cursor(self):
- return psycopg2.extensions.connection.cursor(self, cursor_factory = _CompatCursor)
- def _pgconnect(cstr):
- """Create a psycopg2 connection."""
- return _CompatConnection(cstr)
-except ImportError:
- # use psycopg 1
- from psycopg import connect as _pgconnect
-
#
# Fully qualified table name
#
@@ -448,16 +413,3 @@ def db_install(curs, list, log = None):
if log:
log.info('%s is installed' % obj.name)
-def connect_database(connstr):
- """Create a db connection with connect_timeout option.
-
- Default connect_timeout is 15, to change put it directly into dsn.
- """
-
- # allow override
- if connstr.find("connect_timeout") < 0:
- connstr += " connect_timeout=15"
-
- # create connection
- return _pgconnect(connstr)
-