1515import time
1616import re
1717
18+ from .. import clientparameters as pg_param
1819from .. import driver as pg_driver
1920from .. import types as pg_type
2021from .. import string as pg_str
22+ from .pq3 import Connection
2123
2224##
2325# Basically, is it a mapping, or is it a sequence?
@@ -82,6 +84,9 @@ def dbapi_type(typid):
8284 return ROWID
8385
8486class Portal (object ):
87+ """
88+ Manages read() interfaces to a chunks iterator.
89+ """
8590 def __init__ (self , chunks ):
8691 self .chunks = chunks
8792 self .buf = []
@@ -93,6 +98,10 @@ def __next__(self):
9398 self .pos += 1
9499 return r
95100 except IndexError :
101+ # Any alledged infinite recursion will stop on the StopIteration
102+ # thrown by this next(). Recursion is unlikely to occur more than
103+ # once; specifically, empty chunks would need to be returned
104+ # by this invocation of next().
96105 self .buf = next (self .chunks )
97106 self .pos = 0
98107 return self .__next__ ()
@@ -125,8 +134,7 @@ class Cursor(object):
125134 description = None
126135
127136 def __init__ (self , C ):
128- self .connection = C
129- self .database = C .database
137+ self .database = self .connection = C
130138 self .description = ()
131139 self .__portals = []
132140
@@ -173,6 +181,7 @@ def fetchone(self):
173181 def __next__ (self ):
174182 return next (self ._portal )
175183 next = __next__
184+
176185 def __iter__ (self ):
177186 return self
178187
@@ -275,7 +284,7 @@ def close(self):
275284 self .__portals = None
276285 for p in ps : p .close ()
277286
278- class Connection (object ):
287+ class Connection (Connection ):
279288 """
280289 DB-API 2.0 connection implementation for PG-API connection objects.
281290 """
@@ -299,7 +308,7 @@ def autocommit_set(self, val):
299308 else :
300309 if self ._xact is not None :
301310 return
302- self ._xact = self .database . xact ()
311+ self ._xact = self .xact ()
303312 self ._xact .start ()
304313
305314 def autocommit_get (self ):
@@ -315,19 +324,19 @@ def autocommit_del(self):
315324 )
316325 del autocommit_set , autocommit_get , autocommit_del
317326
318- def __init__ (self , connection ):
319- self . database = connection
320- self ._xact = self .database . xact ()
327+ def connect (self , * args , ** kw ):
328+ super (). connect ( * args , ** kw )
329+ self ._xact = self .xact ()
321330 self ._xact .start ()
322331
323332 def close (self ):
324- if self .database . closed :
333+ if self .closed :
325334 raise Error (
326335 "connection already closed" ,
327336 source = 'CLIENT' ,
328- creator = self . database
337+ creator = self
329338 )
330- self . database .close ()
339+ super () .close ()
331340
332341 def cursor (self ):
333342 return Cursor (self )
@@ -340,10 +349,10 @@ def commit(self):
340349 details = {
341350 'hint' : 'The "autocommit" property on the connection was set to True.'
342351 },
343- creator = self . database
352+ creator = self
344353 )
345354 self ._xact .commit ()
346- self ._xact = self .database . xact ()
355+ self ._xact = self .xact ()
347356 self ._xact .start ()
348357
349358 def rollback (self ):
@@ -354,16 +363,22 @@ def rollback(self):
354363 details = {
355364 'hint' : 'The "autocommit" property on the connection was set to True.'
356365 },
357- creator = self . database
366+ creator = self
358367 )
359368 self ._xact .rollback ()
360- self ._xact = self .database . xact ()
369+ self ._xact = self .xact ()
361370 self ._xact .start ()
362371
372+ driver = pg_driver .Driver (connection = Connection )
363373def connect (** kw ):
364374 """
365375 Create a DB-API connection using the given parameters.
366376 """
367- db = pg_driver .connect (** kw )
368- dbapi = Connection (db )
369- return dbapi
377+ std_params = pg_param .collect (prompt_title = None )
378+ params = pg_param .normalize (
379+ list (pg_param .denormalize_parameters (std_params )) + \
380+ list (pg_param .denormalize_parameters (kw ))
381+ )
382+ # Resolve the password, but never prompt.
383+ pg_param .resolve_password (params , prompt_title = None )
384+ return driver .connect (** params )
0 commit comments