2005-01-20 08:49:40 +03:00
|
|
|
|
"""Miscellaneous goodies for psycopg
|
|
|
|
|
|
|
|
|
|
This module is a generic place used to hold little helper function
|
|
|
|
|
and classes untill a better place in the distribution is found.
|
|
|
|
|
"""
|
2004-10-19 07:17:12 +04:00
|
|
|
|
# psycopg/extras.py - miscellaneous extra goodies for psycopg
|
|
|
|
|
#
|
|
|
|
|
# Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>
|
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by the
|
|
|
|
|
# Free Software Foundation; either version 2, or (at your option) any later
|
|
|
|
|
# version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
|
|
|
|
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
|
# for more details.
|
|
|
|
|
|
|
|
|
|
from psycopg.extensions import cursor as _cursor
|
2005-02-27 18:03:53 +03:00
|
|
|
|
from psycopg.extensions import register_adapter as _RA
|
|
|
|
|
from psycopg.extensions import adapt as _A
|
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
|
|
class DictCursor(_cursor):
|
|
|
|
|
"""A cursor that keeps a list of column name -> index mappings."""
|
|
|
|
|
|
|
|
|
|
__query_executed = 0
|
|
|
|
|
|
|
|
|
|
def execute(self, query, vars=None, async=0):
|
2004-11-19 18:50:57 +03:00
|
|
|
|
self.row_factory = DictRow
|
2004-10-19 07:17:12 +04:00
|
|
|
|
self.index = {}
|
|
|
|
|
self.__query_executed = 1
|
|
|
|
|
return _cursor.execute(self, query, vars, async)
|
|
|
|
|
|
|
|
|
|
def _build_index(self):
|
2004-11-19 18:50:57 +03:00
|
|
|
|
if self.__query_executed == 1 and self.description:
|
2004-10-19 07:17:12 +04:00
|
|
|
|
for i in range(len(self.description)):
|
|
|
|
|
self.index[self.description[i][0]] = i
|
2004-11-19 18:50:57 +03:00
|
|
|
|
self.__query_executed = 0
|
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
|
def fetchone(self):
|
2004-11-19 18:50:57 +03:00
|
|
|
|
res = _cursor.fetchone(self)
|
2004-10-19 07:17:12 +04:00
|
|
|
|
if self.__query_executed:
|
|
|
|
|
self._build_index()
|
2004-11-19 18:50:57 +03:00
|
|
|
|
return res
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
|
|
def fetchmany(self, size=None):
|
2004-11-19 18:50:57 +03:00
|
|
|
|
res = _cursor.fetchmany(self, size)
|
2004-10-19 07:17:12 +04:00
|
|
|
|
if self.__query_executed:
|
|
|
|
|
self._build_index()
|
2004-11-19 18:50:57 +03:00
|
|
|
|
return res
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
|
|
def fetchall(self):
|
2004-11-19 18:50:57 +03:00
|
|
|
|
res = _cursor.fetchall(self)
|
2004-10-19 07:17:12 +04:00
|
|
|
|
if self.__query_executed:
|
|
|
|
|
self._build_index()
|
2004-11-19 18:50:57 +03:00
|
|
|
|
return res
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
|
|
class DictRow(list):
|
|
|
|
|
"""A row object that allow by-colun-name access to data."""
|
|
|
|
|
|
|
|
|
|
def __init__(self, cursor):
|
|
|
|
|
self._cursor = cursor
|
|
|
|
|
self[:] = [None] * len(cursor.description)
|
|
|
|
|
|
|
|
|
|
def __getitem__(self, x):
|
|
|
|
|
if type(x) != int:
|
|
|
|
|
x = self._cursor.index[x]
|
|
|
|
|
return list.__getitem__(self, x)
|
2005-02-27 18:03:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SQL_IN(object):
|
|
|
|
|
"""Adapt any iterable to an SQL quotable object."""
|
|
|
|
|
|
|
|
|
|
def __init__(self, seq):
|
|
|
|
|
self._seq = seq
|
|
|
|
|
|
|
|
|
|
def getquoted(self):
|
|
|
|
|
# this is the important line: note how every object in the
|
|
|
|
|
# list is adapted and then how getquoted() is called on it
|
|
|
|
|
qobjs = [str(_A(o).getquoted()) for o in self._seq]
|
|
|
|
|
|
|
|
|
|
return '(' + ', '.join(qobjs) + ')'
|
|
|
|
|
|
2005-02-28 18:50:55 +03:00
|
|
|
|
__str__ = getquoted
|
|
|
|
|
|
2005-02-27 18:03:53 +03:00
|
|
|
|
_RA(tuple, SQL_IN)
|