forked from binux/pyspider
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove duplicate code, fix resultdb InterfaceError bug
- Loading branch information
Showing
4 changed files
with
53 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python | ||
# -*- encoding: utf-8 -*- | ||
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8: | ||
# Author: Binux<[email protected]> | ||
# http://binux.me | ||
# Created on 2014-11-05 10:42:24 | ||
|
||
class MySQLMixin(object): | ||
@property | ||
def dbcur(self): | ||
try: | ||
if self.conn.unread_result: | ||
self.conn.get_rows() | ||
return self.conn.cursor() | ||
except (mysql.connector.OperationalError, mysql.connector.InterfaceError) as e: | ||
self.conn.ping(reconnect=True) | ||
self.conn.database = self.database_name | ||
return self.conn.cursor() | ||
|
||
class SplitTableMixin(object): | ||
|
||
def _tablename(self, project): | ||
if self.__tablename__: | ||
return '%s_%s' % (self.__tablename__, project) | ||
else: | ||
return project | ||
|
||
def _list_project(self): | ||
self.projects = set() | ||
if self.__tablename__: | ||
prefix = '%s_' % self.__tablename__ | ||
else: | ||
prefix = '' | ||
for project, in self._execute('show tables;'): | ||
if project.startswith(prefix): | ||
project = project[len(prefix):] | ||
self.projects.add(project) | ||
|
||
def drop(self, project): | ||
if project not in self.projects: | ||
self._list_project() | ||
if project not in self.projects: | ||
return | ||
tablename = self._tablename(project) | ||
self._execute("DROP TABLE %s" % self.escape(tablename)) | ||
self._list_project() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters