Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / trunk / psycopg2 / lib / psycopg1.py
1 """psycopg 1.1.x compatibility module
2
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::
6
7     from psycopg2 import psycopg1 as psycopg
8 """
9 # psycopg/psycopg1.py - psycopg 1.1.x compatibility module
10 #
11 # Copyright (C) 2003-2004 Federico Di Gregorio  <fog@debian.org>
12 #
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
16 # version.
17 #
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
21 # for more details.
22
23 import _psycopg as _2psycopg
24 from psycopg2.extensions import cursor as _2cursor
25 from psycopg2.extensions import connection as _2connection
26
27 from psycopg2 import *
28 del connect
29
30
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)
36     return conn
37     
38 class connection(_2connection):
39     """psycopg 1.1.x connection."""
40     
41     def cursor(self):
42         """cursor() -> new psycopg 1.1.x compatible cursor object"""
43         return _2connection.cursor(self, cursor_factory=cursor)
44
45     def autocommit(self, on_off=1):
46         """autocommit(on_off=1) -> switch autocommit on (1) or off (0)"""
47         if on_off > 0:
48             self.set_isolation_level(0)
49         else:
50             self.set_isolation_level(2)
51             
52
53 class cursor(_2cursor):
54     """psycopg 1.1.x cursor.
55
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.
59     """
60
61     def __build_dict(self, row):
62         res = {}
63         for i in range(len(self.description)):
64             res[self.description[i][0]] = row[i]
65         return res
66     
67     def dictfetchone(self):
68         row = _2cursor.fetchone(self)
69         if row:
70             return self.__build_dict(row)
71         else:
72             return row
73             
74     def dictfetchmany(self, size):
75         res = []
76         rows = _2cursor.fetchmany(self, size)
77         for row in rows:
78             res.append(self.__build_dict(row))
79         return res
80     
81     def dictfetchall(self):
82         res = []
83         rows = _2cursor.fetchall(self)
84         for row in rows:
85             res.append(self.__build_dict(row))
86         return res
87