Created
April 23, 2012 18:41
-
-
Save w0rm/2472971 to your computer and use it in GitHub Desktop.
Sphinx search (SphinxQL) database wrapper for web.py
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
from web.db import DB, SQLQuery, register_database | |
class SphinxDB(DB): | |
def __init__(self, **keywords): | |
import MySQLdb as db | |
if 'pw' in keywords: | |
keywords['passwd'] = keywords['pw'] | |
del keywords['pw'] | |
if 'charset' not in keywords: | |
keywords['charset'] = 'utf8' | |
elif keywords['charset'] is None: | |
del keywords['charset'] | |
self.paramstyle = db.paramstyle = 'pyformat' # it's both, like psycopg | |
self.dbname = "sphinx" | |
DB.__init__(self, db, keywords) | |
def _load_context(self, ctx): | |
ctx.dbq_count = 0 | |
ctx.transactions = [] # stack of transactions | |
if self.has_pooling: | |
ctx.db = self._connect_with_pooling(self.keywords) | |
else: | |
ctx.db = self._connect(self.keywords) | |
ctx.db_execute = self._db_execute | |
ctx.db.commit = lambda: None | |
ctx.db.rollback = lambda: None | |
def commit(unload=True): | |
# do db commit and release the connection if pooling is enabled. | |
ctx.db.commit() | |
if unload and self.has_pooling: | |
self._unload_context(self._ctx) | |
def rollback(): | |
# do db rollback and release the connection if pooling is enabled. | |
ctx.db.rollback() | |
if self.has_pooling: | |
self._unload_context(self._ctx) | |
ctx.commit = commit | |
ctx.rollback = rollback | |
register_database('sphinx', SphinxDB) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment