Package osh :: Package command :: Package sqladapter :: Module postgres
[frames] | no frames]

Source Code for Module osh.command.sqladapter.postgres

 1  # osh 
 2  # Copyright (C) Jack Orenstein <jao@geophile.com> 
 3  # 
 4  # This program is free software; you can redistribute it and/or modify 
 5  # it under the terms of the GNU General Public License as published by 
 6  # the Free Software Foundation; either version 2 of the License, or 
 7  # (at your option) any later version. 
 8  # 
 9  # This program is distributed in the hope that it will be useful, 
10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12  # GNU General Public License for more details. 
13  # 
14  # You should have received a copy of the GNU General Public License 
15  # along with this program; if not, write to the Free Software 
16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
17   
18  from osh.external.pg8000 import dbapi 
19   
20  import osh.command.sql 
21   
22  CURSOR_ARRAYSIZE = 20 
23   
24 -def _postgres():
25 return _Postgres()
26
27 -class _Postgres(osh.command.sql._DBType):
28
29 - def connect(self, db, host, user, password):
30 try: 31 return _Connection(dbapi.connect(host = host, database = db, user = user, password = password)) 32 except dbapi.Error, e: 33 raise Exception('Unable to connect to %s: %s' % (db, str(e)))
34
35 - def run_query(self, connection, query, inputs):
36 cursor = connection.cursor 37 try: 38 cursor.execute(query, inputs) 39 rows = cursor.fetchmany() 40 while len(rows) > 0: 41 for row in rows: 42 yield row 43 rows = cursor.fetchmany() 44 except dbapi.Error, e: 45 raise Exception('Error during execution of query %s: %s' % (query, str(e)))
46
47 - def run_update(self, connection, query, inputs):
48 try: 49 cursor = connection.cursor 50 cursor.execute(query, inputs) 51 rowcount = cursor.rowcount 52 connection.commit() 53 return rowcount 54 except dbapi.Error, e: 55 raise Exception('Error during execution of update %s: %s' % (query, str(e)))
56 57
58 -class _Connection(object):
59 60 connection = None 61 cursor = None 62
63 - def __init__(self, connection):
64 self.connection = connection 65 self.cursor = connection.cursor() 66 self.cursor.arraysize = CURSOR_ARRAYSIZE
67
68 - def commit(self):
69 self.connection.commit()
70