Login

MYSQL Full Text Expression

Author:
Bidaya0
Posted:
May 12, 2022
Language:
Python
Version:
3.2
Score:
0 (after 0 ratings)

MYSQL Full Text Expression

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django.db import models
import copy
from django.db.models import Expression


class SearchLanguageMode(Expression):
    template = "MATCH( %(expressions)s ) AGAINST (+ '%(search_keyword)s' IN NATURAL LANGUAGE MODE )"

    def __init__(self, expressions, search_keyword):
        super().__init__(output_field=models.IntegerField())
        self.search_keyword = search_keyword
        for expression in expressions:
            if not hasattr(expression, 'resolve_expression'):
                raise TypeError('%r is not an Expression' % expression)
        self.expressions = expressions

    def resolve_expression(self,
                           query=None,
                           allow_joins=True,
                           reuse=None,
                           summarize=False,
                           for_save=False):
        c = self.copy()
        c.is_summary = summarize
        for pos, expression in enumerate(self.expressions):
            c.expressions[pos] = expression.resolve_expression(
                query, allow_joins, reuse, summarize, for_save)
        return c

    def as_sql(self, compiler, connection, template=None):
        sql_expressions, sql_params = [], []
        for expression in self.expressions:
            sql, params = compiler.compile(expression)
            sql_expressions.append(sql)
            sql_params.extend(params)
        template = template or self.template
        data = {
            'expressions': ','.join(sql_expressions),
            'search_keyword': self.search_keyword
        }
        return template % data, sql_params

    def get_source_expressions(self):
        return self.expressions

    def set_source_expressions(self, expressions):
        self.expressions = expressions

More like this

  1. Browser-native date input field by kytta 1 month ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 2 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 3 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 months ago

Comments

Please login first before commenting.