1 | #!/usr/bin/env python |
---|
2 | # |
---|
3 | # Orginal from Andre van der Vlies <andre@vandervlies.xs4all.nl> for MySQL. Changed |
---|
4 | # and added some more functions for postgres. |
---|
5 | # |
---|
6 | # |
---|
7 | # Changed by: Bas van der Vlies <basv@sara.nl> |
---|
8 | # |
---|
9 | # SARA API for Postgres Database |
---|
10 | # |
---|
11 | # Changed by: Ramon Bastiaans for Elsa |
---|
12 | # SVN info: |
---|
13 | # $Id: DBClass.py 541 2004-06-29 09:19:40Z bas $ |
---|
14 | """ |
---|
15 | This is an generic SARA python class that connects to any postgres database. |
---|
16 | THe default one is uva_cluster_db, but that can specified as argument. |
---|
17 | |
---|
18 | The basic usage is: |
---|
19 | import DBClass |
---|
20 | |
---|
21 | db_vars = DBClass.InitVars(DataBaseName='rc_cluster_db', |
---|
22 | User='root', |
---|
23 | Host='localhost', |
---|
24 | Password='', |
---|
25 | Dictionary='true') |
---|
26 | db = DBClass.DB(db_vars) |
---|
27 | |
---|
28 | # Get valuese from database |
---|
29 | print db.Get("SELECT * FROM clusters") |
---|
30 | |
---|
31 | # Set values into database |
---|
32 | bla=db.Set("DELETE FROM clusters WHERE clustername = 'bas'") |
---|
33 | """ |
---|
34 | from pyPgSQL import PgSQL |
---|
35 | |
---|
36 | # |
---|
37 | # Class to 'simplify' the declaration of 'global' |
---|
38 | # variables.... |
---|
39 | # |
---|
40 | class InitVars: |
---|
41 | Vars = {} |
---|
42 | |
---|
43 | def __init__(self, **key_arg): |
---|
44 | for (key, value) in key_arg.items(): |
---|
45 | if value: |
---|
46 | self.Vars[key] = value |
---|
47 | else: |
---|
48 | self.Vars[key] = None |
---|
49 | |
---|
50 | def __call__(self, *key): |
---|
51 | key = "%s" % key |
---|
52 | return self.Vars[key] |
---|
53 | |
---|
54 | def __getitem__(self, key): |
---|
55 | return self.Vars[key] |
---|
56 | |
---|
57 | def __repr__(self): |
---|
58 | return repr(self.Vars) |
---|
59 | |
---|
60 | def keys(self): |
---|
61 | barf = map(None, self.Vars.keys()) |
---|
62 | return barf |
---|
63 | |
---|
64 | def values(self): |
---|
65 | barf = map(None, self.Vars.values()) |
---|
66 | return barf |
---|
67 | |
---|
68 | def has_key(self, key): |
---|
69 | if self.Vars.has_key(key): |
---|
70 | return 1 |
---|
71 | else: |
---|
72 | return 0 |
---|
73 | |
---|
74 | class DBError(Exception): |
---|
75 | def __init__(self, msg=''): |
---|
76 | self.msg = msg |
---|
77 | Exception.__init__(self, msg) |
---|
78 | def __repr__(self): |
---|
79 | return self.msg |
---|
80 | __str__ = __repr__ |
---|
81 | |
---|
82 | # |
---|
83 | # Class to connect to a database |
---|
84 | # and return the queury in a list or dictionairy. |
---|
85 | # |
---|
86 | class DB: |
---|
87 | def __init__(self, db_vars): |
---|
88 | |
---|
89 | self.dict = db_vars |
---|
90 | |
---|
91 | if self.dict.has_key('User'): |
---|
92 | self.user = self.dict['User'] |
---|
93 | else: |
---|
94 | self.user = 'postgres' |
---|
95 | |
---|
96 | if self.dict.has_key('Host'): |
---|
97 | self.host = self.dict['Host'] |
---|
98 | else: |
---|
99 | self.host = 'localhost' |
---|
100 | |
---|
101 | if self.dict.has_key('Password'): |
---|
102 | self.passwd = self.dict['Password'] |
---|
103 | else: |
---|
104 | self.passwd = '' |
---|
105 | |
---|
106 | if self.dict.has_key('DataBaseName'): |
---|
107 | self.db = self.dict['DataBaseName'] |
---|
108 | else: |
---|
109 | self.db = 'uva_cluster_db' |
---|
110 | |
---|
111 | # connect_string = 'host:port:database:user:password: |
---|
112 | dsn = "%s::%s:%s:%s" %(self.host, self.db, self.user, self.passwd) |
---|
113 | |
---|
114 | try: |
---|
115 | self.SQL = PgSQL.connect(dsn) |
---|
116 | except PgSQL.Error, details: |
---|
117 | str = "%s" %details |
---|
118 | raise DBError(str) |
---|
119 | |
---|
120 | def __repr__(self): |
---|
121 | return repr(self.result) |
---|
122 | |
---|
123 | def __nonzero__(self): |
---|
124 | return not(self.result == None) |
---|
125 | |
---|
126 | def __len__(self): |
---|
127 | return len(self.result) |
---|
128 | |
---|
129 | def __getitem__(self,i): |
---|
130 | return self.result[i] |
---|
131 | |
---|
132 | def __getslice__(self,i,j): |
---|
133 | return self.result[i:j] |
---|
134 | |
---|
135 | def Get(self, q_str): |
---|
136 | c = self.SQL.cursor() |
---|
137 | try: |
---|
138 | c.execute(q_str) |
---|
139 | result = c.fetchall() |
---|
140 | except PgSQL.Error, details: |
---|
141 | c.close() |
---|
142 | str = "%s" %details |
---|
143 | raise DBError(str) |
---|
144 | |
---|
145 | c.close() |
---|
146 | return result |
---|
147 | |
---|
148 | def Set(self, q_str): |
---|
149 | c = self.SQL.cursor() |
---|
150 | try: |
---|
151 | c.execute(q_str) |
---|
152 | result = c.oidValue |
---|
153 | |
---|
154 | except PgSQL.Error, details: |
---|
155 | c.close() |
---|
156 | str = "%s" %details |
---|
157 | raise DBError(str) |
---|
158 | |
---|
159 | c.close() |
---|
160 | return result |
---|
161 | |
---|
162 | def Commit(self): |
---|
163 | self.SQL.commit() |
---|
164 | |
---|
165 | # |
---|
166 | # Some tests.... |
---|
167 | # |
---|
168 | def main(): |
---|
169 | db_vars = InitVars(DataBaseName='rc_cluster_db', |
---|
170 | User='root', |
---|
171 | Host='localhost', |
---|
172 | Password='', |
---|
173 | Dictionary='true') |
---|
174 | print db_vars; |
---|
175 | |
---|
176 | db = DB(db_vars) |
---|
177 | print db.Get("""SELECT * FROM clusters""") |
---|
178 | |
---|
179 | if __name__ == '__main__': |
---|
180 | main() |
---|