2 Specialised connection and cursor functions.
8 import psycopg2.extensions
11 from ..errors import UsageError
13 class _Cursor(psycopg2.extras.DictCursor):
14 """ A cursor returning dict-like objects and providing specialised
18 def execute(self, query, args=None): # pylint: disable=W0221
19 """ Query execution that logs the SQL query when debugging is enabled.
21 logger = logging.getLogger()
22 logger.debug(self.mogrify(query, args).decode('utf-8'))
24 super().execute(query, args)
26 def scalar(self, sql, args=None):
27 """ Execute query that returns a single value. The value is returned.
28 If the query yields more than one row, a ValueError is raised.
30 self.execute(sql, args)
32 if self.rowcount != 1:
33 raise RuntimeError("Query did not return a single row.")
35 return self.fetchone()[0]
38 class _Connection(psycopg2.extensions.connection):
39 """ A connection that provides the specialised cursor by default and
40 adds convenience functions for administrating the database.
43 def cursor(self, cursor_factory=_Cursor, **kwargs):
44 """ Return a new cursor. By default the specialised cursor is returned.
46 return super().cursor(cursor_factory=cursor_factory, **kwargs)
49 def table_exists(self, table):
50 """ Check that a table with the given name exists in the database.
52 with self.cursor() as cur:
53 num = cur.scalar("""SELECT count(*) FROM pg_tables
54 WHERE tablename = %s and schemaname = 'public'""", (table, ))
58 def index_exists(self, index, table=None):
59 """ Check that an index with the given name exists in the database.
60 If table is not None then the index must relate to the given
63 with self.cursor() as cur:
64 cur.execute("""SELECT tablename FROM pg_indexes
65 WHERE indexname = %s and schemaname = 'public'""", (index, ))
71 return row[0] == table
76 def server_version_tuple(self):
77 """ Return the server version as a tuple of (major, minor).
78 Converts correctly for pre-10 and post-10 PostgreSQL versions.
80 version = self.server_version
82 return (version / 10000, (version % 10000) / 100)
84 return (version / 10000, version % 10000)
87 """ Open a connection to the database using the specialised connection
88 factory. The returned object may be used in conjunction with 'with'.
89 When used outside a context manager, use the `connection` attribute
90 to get the connection.
93 conn = psycopg2.connect(dsn, connection_factory=_Connection)
94 ctxmgr = contextlib.closing(conn)
95 ctxmgr.connection = conn
97 except psycopg2.OperationalError as err:
98 raise UsageError("Cannot connect to database: {}".format(err)) from err