equal
deleted
inserted
replaced
1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
2 # |
2 # |
3 # MyManager - manage database connections (MySQL version) |
3 # MyManager - manage database connections (MySQL version) |
4 # |
4 # |
5 # Requires: Python 2.6, MySQLdb |
5 # Requires: Python 2.6 / 2.7, MySQLdb |
6 # |
6 # |
7 # Part of pgtoolkit |
7 # Part of pgtoolkit |
8 # http://hg.devl.cz/pgtoolkit |
8 # http://hg.devl.cz/pgtoolkit |
9 # |
9 # |
10 # Copyright (c) 2011 Radek Brich <radek.brich@devl.cz> |
10 # Copyright (c) 2011 Radek Brich <radek.brich@devl.cz> |
91 self.isolation_level = isolation_level |
91 self.isolation_level = isolation_level |
92 self.init_statement = init_statement |
92 self.init_statement = init_statement |
93 self.keep_open = keep_open |
93 self.keep_open = keep_open |
94 self.parameters = kw |
94 self.parameters = kw |
95 self.adjust_parameters() |
95 self.adjust_parameters() |
96 |
96 |
97 def adjust_parameters(self): |
97 def adjust_parameters(self): |
98 '''Rename Postgres parameters to proper value for MySQL.''' |
98 '''Rename Postgres parameters to proper value for MySQL.''' |
99 m = {'dbname' : 'db', 'password' : 'passwd'} |
99 m = {'dbname' : 'db', 'password' : 'passwd'} |
100 res = dict() |
100 res = dict() |
101 for k, v in list(self.parameters.items()): |
101 for k, v in list(self.parameters.items()): |
164 self.conn_known[name] = ci |
164 self.conn_known[name] = ci |
165 self.conn_pool[name] = [] |
165 self.conn_pool[name] = [] |
166 |
166 |
167 def close_conn(self, name='default'): |
167 def close_conn(self, name='default'): |
168 '''Close all connections of given name. |
168 '''Close all connections of given name. |
169 |
169 |
170 Connection credentials are still saved. |
170 Connection credentials are still saved. |
171 |
171 |
172 ''' |
172 ''' |
173 while len(self.conn_pool[name]): |
173 while len(self.conn_pool[name]): |
174 conn = self.conn_pool[name].pop() |
174 conn = self.conn_pool[name].pop() |
175 conn.close() |
175 conn.close() |
176 |
176 |
177 def destroy_conn(self, name='default'): |
177 def destroy_conn(self, name='default'): |
178 '''Destroy connection. |
178 '''Destroy connection. |
179 |
179 |
180 Counterpart of create_conn. |
180 Counterpart of create_conn. |
181 |
181 |
182 ''' |
182 ''' |
183 if not name in self.conn_known: |
183 if not name in self.conn_known: |
184 raise MyManagerError('Connection name "%s" not registered.' % name) |
184 raise MyManagerError('Connection name "%s" not registered.' % name) |
185 |
185 |
186 self.close_conn(name) |
186 self.close_conn(name) |
211 self.lock.release() |
211 self.lock.release() |
212 return conn |
212 return conn |
213 |
213 |
214 def put_conn(self, conn, name='default'): |
214 def put_conn(self, conn, name='default'): |
215 '''Put connection back to pool. |
215 '''Put connection back to pool. |
216 |
216 |
217 Name must be same as used for get_conn, |
217 Name must be same as used for get_conn, |
218 otherwise things become broken. |
218 otherwise things become broken. |
219 |
219 |
220 ''' |
220 ''' |
221 self.lock.acquire() |
221 self.lock.acquire() |
222 try: |
222 try: |
223 if not name in self.conn_known: |
223 if not name in self.conn_known: |
224 raise MyManagerError("Connection name '%s' not registered." % name) |
224 raise MyManagerError("Connection name '%s' not registered." % name) |
239 self.lock.release() |
239 self.lock.release() |
240 |
240 |
241 @contextmanager |
241 @contextmanager |
242 def cursor(self, name='default'): |
242 def cursor(self, name='default'): |
243 '''Cursor context. |
243 '''Cursor context. |
244 |
244 |
245 Uses any connection of name 'name' from pool |
245 Uses any connection of name 'name' from pool |
246 and returns cursor for that connection. |
246 and returns cursor for that connection. |
247 |
247 |
248 ''' |
248 ''' |
249 conn = self.get_conn(name) |
249 conn = self.get_conn(name) |
250 |
250 |
251 try: |
251 try: |
252 curs = conn.cursor() |
252 curs = conn.cursor() |