Package Peach :: Package Publishers :: Module sql
[hide private]

Source Code for Module Peach.Publishers.sql

  1   
  2  ''' 
  3  SQL publisher objects.  Includes a default ODBC publisher. 
  4   
  5  @author: Michael Eddington 
  6  @version: $Id: Peach.Publishers.sql-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
  7  ''' 
  8   
  9  # 
 10  # Copyright (c) 2005-2008 Michael Eddington 
 11  # Copyright (c) 2004-2005 IOActive Inc. 
 12  # 
 13  # Permission is hereby granted, free of charge, to any person obtaining a copy  
 14  # of this software and associated documentation files (the "Software"), to deal 
 15  # in the Software without restriction, including without limitation the rights  
 16  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 17  # copies of the Software, and to permit persons to whom the Software is  
 18  # furnished to do so, subject to the following conditions: 
 19  # 
 20  # The above copyright notice and this permission notice shall be included in     
 21  # all copies or substantial portions of the Software. 
 22  # 
 23  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 24  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 25  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 26  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 27  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 28  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 29  # SOFTWARE. 
 30  # 
 31   
 32  # Authors: 
 33  #   Michael Eddington (mike@phed.org) 
 34   
 35  # $Id: Peach.Publishers.sql-pysrc.html 1138 2008-08-16 19:39:03Z meddingt $ 
 36   
 37  from types import * 
 38   
 39  try: 
 40          import dbi, odbc 
 41  except: 
 42          pass 
 43   
 44  from Peach.publisher import Publisher 
 45   
 46  #__all__ = ["Odbc"] 
 47   
48 -class Odbc(Publisher):
49 ''' 50 Publisher for ODBC connections. Generated data sent as a SQL query via 51 execute. Calling receave will return a string of all row data concatenated 52 together with \t as field separator. 53 54 Currently this Publisher makes use of the odbc package which is some what 55 broken in that you must create an actual ODBC DSN via the ODBC manager. 56 Check out mxODBC which is not open source for another alterative. 57 58 Note: Each call to start/stop will create and close the SQL connection and 59 cursor. 60 ''' 61
62 - def __init__(self, dsn):
63 ''' 64 @type dsn: string 65 @param dsn: DSN must be in format of "dsn/user/password" where DSN is a DSN name. 66 ''' 67 self._dsn = dsn 68 self._sql = None 69 self._cursor = None 70 self._sql = None
71
72 - def start(self):
73 ''' 74 Create connection to server. 75 ''' 76 self._sql = odbc.odbc(self._dsn)
77
78 - def stop(self):
79 ''' 80 Close any open cursors, and close connection to server. 81 ''' 82 self._cursor.close() 83 self._cursor = None 84 self._sql.close() 85 self._sql = None
86
87 - def call(self, method, args):
88 ''' 89 Create cursor and execute data. 90 ''' 91 self._cursor = self._sql.cursor() 92 self._cursor.execute(method, args) 93 94 ret = '' 95 try: 96 row = self._cursor.fetchone() 97 for i in range(len(row)): 98 retType = type(row[i]) 99 if retType is IntType: 100 ret += "\t%d" % row[i] 101 elif retType is FloatType: 102 ret += "\t%f" % row[i] 103 elif retType is LongType: 104 ret += "\t%d" % row[i] 105 elif retType is StringType: 106 ret += "\t%s" % row[i] 107 108 except: 109 pass 110 111 return ret
112 113 # end 114