Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.swp
*.pyc
2 changes: 1 addition & 1 deletion django_sphinx_db/backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def using(self, alias):
class SphinxManager(models.Manager):
use_for_related_fields = True

def get_query_set(self):
def get_queryset(self):
# Determine which fields are sphinx fields (full-text data) and
# defer loading them. Sphinx won't return them.
# TODO: we probably need a way to keep these from being loaded
Expand Down
33 changes: 29 additions & 4 deletions django_sphinx_db/backend/sphinx/base.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
from django.db.backends.mysql.base import *
from django.db.backends.mysql.base import DatabaseWrapper as MySQLDatabaseWrapper
from django.db.backends.mysql.base import DatabaseOperations as MySQLDatabaseOperations
from django.db.backends.mysql.creation import DatabaseCreation as MySQLDatabaseCreation


# import features class from sphinx backend module:
from .features import DatabaseFeatures # isort:skip
from .introspection import DatabaseIntrospection
from .validation import DatabaseValidation


class SphinxOperations(MySQLDatabaseOperations):
compiler_module = "django_sphinx_db.backend.sphinx.compiler"

def quote_name(self, name):
# TODO: remove this function altogether when no longer needed.
# In one of the releases Sphinx fails with "SELECT `id`" - alike clauses
# http://sphinxsearch.com/bugs/view.php?id=1487
if name == 'id':
return name
if name.startswith("`") and name.endswith("`"):
return name # Quoting once is enough.
return "`%s`" % name

def fulltext_search_sql(self, field_name):
return 'MATCH (%s)'


class SphinxCreation(MySQLDatabaseCreation):
def create_test_db(self, verbosity=1, autoclobber=False):
def create_test_db(self, verbosity=1, autoclobber=False, serialize=False, keepdb=False):
# NOOP, test using regular sphinx database.
if self.connection.settings_dict['TEST_NAME']:
if 'TEST_NAME' in self.connection.settings_dict:
test_name = self.connection.settings_dict['TEST_NAME']
self.connection.close()
self.connection.settings_dict['NAME'] = test_name
cursor = self.connection.cursor()
return test_name
return self.connection.settings_dict['NAME']

def destroy_test_db(self, old_database_name, verbosity=1):
def destroy_test_db(self, old_database_name, verbosity=1, keepdb=False):
# NOOP, we created nothing, nothing to destroy.
return


class DatabaseWrapper(MySQLDatabaseWrapper):
vendor = 'sphinx'
display_name = 'sphinx'
features_class = DatabaseFeatures
def __init__(self, *args, **kwargs):
super(DatabaseWrapper, self).__init__(*args, **kwargs)
self.ops = SphinxOperations(self)
Expand All @@ -39,4 +59,9 @@ def __init__(self, *args, **kwargs):
# transactions ARE. Therefore, we can just set this to True, and Django will
# use transactions for clearing data between tests when all OTHER backends
# support it.
self.features.supports_transactions = True
#
# deprecated in Django 1.10
#self.features.supports_transactions = True
self.features = DatabaseFeatures(self)
self.introspection = DatabaseIntrospection(self)
self.validation = DatabaseValidation(self)
22 changes: 13 additions & 9 deletions django_sphinx_db/backend/sphinx/compiler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db.models.sql import compiler
from django.db.models.sql.where import WhereNode
from django.db.models.sql.where import EmptyShortCircuit, EmptyResultSet
from django.db.models.sql.expressions import SQLEvaluator
from django.db.models.sql.where import EmptyResultSet
#from django.db.models.sql.expressions import SQLEvaluator


class SphinxWhereNode(WhereNode):
Expand Down Expand Up @@ -30,8 +30,11 @@ def make_atom(self, child, qn, connection):
if hasattr(lvalue, 'process'):
try:
lvalue, params = lvalue.process(lookup_type, params_or_value, connection)
except EmptyShortCircuit:
raise EmptyResultSet
except:
raise
# note EmptyShortCircuit was removed in 1.9, will leave a raise:
# except EmptyShortCircuit:
# raise EmptyResultSet
if isinstance(lvalue, tuple):
# A direct database column lookup.
field_sql = self.sql_for_columns(lvalue, qn, connection)
Expand Down Expand Up @@ -102,8 +105,9 @@ def as_sql(self):
else:
placeholder = '%s'

if hasattr(val, 'evaluate'):
val = SQLEvaluator(val, self.query, allow_joins=False)
# deprecated #14030
#if hasattr(val, 'evaluate'):
# val = SQLEvaluator(val, self.query, allow_joins=False)
name = field.column
if hasattr(val, 'as_sql'):
sql, params = val.as_sql(qn, self.connection)
Expand All @@ -123,6 +127,6 @@ def as_sql(self):
class SQLAggregateCompiler(compiler.SQLAggregateCompiler, SphinxQLCompiler):
pass


class SQLDateCompiler(compiler.SQLDateCompiler, SphinxQLCompiler):
pass
# not in Django 1.8.6:
#class SQLDateCompiler(compiler.SQLDateCompiler, SphinxQLCompiler):
#pass
8 changes: 8 additions & 0 deletions django_sphinx_db/backend/sphinx/features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.db.backends.mysql.features import DatabaseFeatures as MYSQLDatabaseFeatures
from django.utils.functional import cached_property

class DatabaseFeatures(MYSQLDatabaseFeatures):

@cached_property
def is_sql_auto_is_null_enabled(self):
return 0
16 changes: 16 additions & 0 deletions django_sphinx_db/backend/sphinx/introspection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db.backends.mysql.introspection import *
from django.db.backends.mysql.introspection import DatabaseIntrospection as MYSQLDatabaseIntrospection
from django.utils.functional import cached_property


class DatabaseIntrospection(MYSQLDatabaseIntrospection):

def get_table_list(self, cursor):
"""
Returns a list of table and view names in the current database.
"""
cursor.execute("SHOW TABLES")
return [TableInfo(row[0], {'BASE TABLE': 't', 'VIEW': 'v'}.get(row[1]))
for row in cursor.fetchall()]


7 changes: 7 additions & 0 deletions django_sphinx_db/backend/sphinx/validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db.backends.mysql.validation import *
from django.db.backends.mysql.validation import DatabaseValidation as MYSQLDatabaseIntrospection

class DatabaseValidation(MYSQLDatabaseIntrospection):
def _check_sql_mode(self, **kwargs):
'''sphinx does not appear to support this sql_mode validation, skipped'''
return []
5 changes: 5 additions & 0 deletions django_sphinx_db/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ def db_for_write(self, model, **kwargs):
def allow_relation(self, obj1, obj2, **kwargs):
# Allow all relations...
return True

def allow_migrate(self, db, app_label, model_name=None, model=None):
dbname = getattr(settings, 'SPHINX_DATABASE_NAME', 'sphinx')
if db == dbname:
return False
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
readme = os.path.join(os.path.dirname(__file__), 'README.rst')
download_url = 'https://github.com/downloads/smartfile/django-sphinx-db' \
'/' + name + '-' + versrel + '.tar.gz'
long_description = file(readme).read()
long_description = open(readme).read()

setup(
name = name,
Expand Down