@@ -76,9 +76,45 @@ def __enter__(self):
7676 def __exit__ (self , type , value , tb ):
7777 self .connection .close ()
7878
79- def execute (self , query ):
80- self .cursor .execute (query )
81- return self .cursor .fetchall ()
79+ def begin (self , isolation_level = 0 ):
80+ levels = [ 'read uncommitted' ,
81+ 'read committed' ,
82+ 'repeatable read' ,
83+ 'serializable' ]
84+
85+ # Check if level is int [0..3]
86+ if isinstance (isolation_level , int ) and \
87+ isolation_level in range (0 , 4 ):
88+
89+ # Replace index with isolation level type
90+ isolation_level = levels [isolation_level ]
91+
92+ # Or it might be a string
93+ elif isinstance (isolation_level , str ) and \
94+ str .lower (isolation_level ) in levels :
95+
96+ # Nothing to do here
97+ pass
98+
99+ # Something is wrong, emit exception
100+ else :
101+ raise QueryException ('Invalid isolation level "%s"'
102+ % isolation_level )
103+
104+ self .cursor .execute ('SET TRANSACTION ISOLATION LEVEL %s'
105+ % isolation_level )
106+
107+ def commit (self ):
108+ self .connection .commit ()
109+
110+ def rollback (self ):
111+ self .connection .rollback ()
112+
113+ def execute (self , query , * args ):
114+ self .cursor .execute (query , args )
115+
116+ if self .cursor .rowcount > 0 :
117+ return self .cursor .fetchall ()
82118
83119 def close (self ):
84120 self .connection .close ()
@@ -359,7 +395,7 @@ def backup(self, name):
359395
360396 return backup_path
361397
362- def connect (self , dbname ):
398+ def connect (self , dbname = 'postgres' ):
363399 return NodeConnection (parent_node = self , dbname = dbname )
364400
365401
0 commit comments