1 """psycopg 1.1.x compatibility module
3 This module uses the new style connection and cursor types to build a psycopg
4 1.1.1.x compatibility layer. It should be considered a temporary hack to run
5 old code while porting to psycopg 2. Import it as follows::
7 from psycopg2 import psycopg1 as psycopg
9 # psycopg/psycopg1.py - psycopg 1.1.x compatibility module
11 # Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by the
15 # Free Software Foundation; either version 2, or (at your option) any later
18 # This program is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
20 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 import _psycopg as _2psycopg
24 from psycopg2.extensions import cursor as _2cursor
25 from psycopg2.extensions import connection as _2connection
27 from psycopg2 import *
31 def connect(*args, **kwargs):
32 """connect(dsn, ...) -> new psycopg 1.1.x compatible connection object"""
33 kwargs['connection_factory'] = connection
34 conn = _2psycopg.connect(*args, **kwargs)
35 conn.set_isolation_level(2)
38 class connection(_2connection):
39 """psycopg 1.1.x connection."""
42 """cursor() -> new psycopg 1.1.x compatible cursor object"""
43 return _2connection.cursor(self, cursor_factory=cursor)
45 def autocommit(self, on_off=1):
46 """autocommit(on_off=1) -> switch autocommit on (1) or off (0)"""
48 self.set_isolation_level(0)
50 self.set_isolation_level(2)
53 class cursor(_2cursor):
54 """psycopg 1.1.x cursor.
56 Note that this cursor implements the exact procedure used by psycopg 1 to
57 build dictionaries out of result rows. The DictCursor in the
58 psycopg.extras modules implements a much better and faster algorithm.
61 def __build_dict(self, row):
63 for i in range(len(self.description)):
64 res[self.description[i][0]] = row[i]
67 def dictfetchone(self):
68 row = _2cursor.fetchone(self)
70 return self.__build_dict(row)
74 def dictfetchmany(self, size):
76 rows = _2cursor.fetchmany(self, size)
78 res.append(self.__build_dict(row))
81 def dictfetchall(self):
83 rows = _2cursor.fetchall(self)
85 res.append(self.__build_dict(row))