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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
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
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
73 '''
74 Create connection to server.
75 '''
76 self._sql = odbc.odbc(self._dsn)
77
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
114