Skip to content

Instantly share code, notes, and snippets.

@w0rm
Created April 23, 2012 18:41
Show Gist options
  • Save w0rm/2472971 to your computer and use it in GitHub Desktop.
Save w0rm/2472971 to your computer and use it in GitHub Desktop.
Sphinx search (SphinxQL) database wrapper for web.py
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