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

Source Code for Module osh.command.sqladapter.dbapi

 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  import osh.command.sql 
19   
20  CURSOR_ARRAYSIZE = 20 
21   
22 -class DBAPI(osh.command.sql._DBType):
23
24 - def connect(self, connect_info):
25 connect_args = [] 26 driver = None 27 for key, value in connect_info.iteritems(): 28 if key == 'driver': 29 driver = value 30 else: 31 connect_args.append("%s = '%s'" % (key, value)) 32 connect_statement = '%s.connect(%s)' % (driver, ', '.join(connect_args)) 33 exec('import %s' % driver) 34 connection = _Connection(eval(connect_statement)) 35 return connection
36
37 - def run_query(self, connection, query, inputs):
38 cursor = connection.cursor 39 cursor.execute(query, inputs) 40 rows = cursor.fetchmany() 41 while len(rows) > 0: 42 for row in rows: 43 yield row 44 rows = cursor.fetchmany()
45
46 - def run_update(self, connection, query, inputs):
47 cursor = connection.cursor 48 cursor.execute(query, inputs) 49 rowcount = cursor.rowcount 50 connection.commit() 51 return rowcount
52
53 -class _Connection(object):
54 55 connection = None 56 cursor = None 57
58 - def __init__(self, connection):
59 self.connection = connection 60 self.cursor = connection.cursor() 61 self.cursor.arraysize = CURSOR_ARRAYSIZE
62
63 - def commit(self):
64 self.connection.commit()
65