Пример #1
0
    def _buildQuery(self, operator_value_dict, logical_operator, parsed,
                    group):
        """
      Create Queries from values, logical and comparison operators.

      operator_value_dict (dict)
        See _processSearchValue.
      logical_operator (string)
        See _processSearchValue.
      parsed (bool)
        See _processSearchValue.
      group (string)
        The gorup all queries will belong to.
    """
        column = self.getColumn()
        query_list = []
        append = query_list.append
        if logical_operator == 'or' and '=' in operator_value_dict:
            # Special case for equality with an 'or' logical operator: use SQL 'in'.
            append(
                SimpleQuery(search_key=self,
                            comparison_operator='in',
                            group=group,
                            **{column: operator_value_dict.pop('=')}))
        for comparison_operator, value_list in operator_value_dict.iteritems():
            for value in value_list:
                append(
                    SimpleQuery(search_key=self,
                                comparison_operator=comparison_operator,
                                group=group,
                                **{column: value}))
        return query_list
Пример #2
0
def wholePeriod(search_key, group, column, value_list, exclude=False):
    if exclude:
        first_operator = '<'
        second_operator = '>='
        logical_operator = 'or'
    else:
        first_operator = '>='
        second_operator = '<'
        logical_operator = 'and'
    query_list = []
    append = query_list.append
    for value in value_list:
        first_date, second_date = getPeriodBoundaries(value)
        append(
            ComplexQuery([
                SimpleQuery(search_key=search_key,
                            comparison_operator=first_operator,
                            group=group,
                            **{column: first_date}),
                SimpleQuery(search_key=search_key,
                            comparison_operator=second_operator,
                            group=group,
                            **{column: second_date})
            ],
                         operator=logical_operator))
    return query_list
Пример #3
0
def matchExact(search_key, group, column, value_list, comparison_operator, logical_operator):
  if comparison_operator is None:
    comparison_operator = '='
  value_list = [castDate(x) for x in value_list]
  if logical_operator == 'or' and comparison_operator == '=':
    query_list = [SimpleQuery(search_key=search_key, comparison_operator='in', group=group, **{column: value_list})]
  else:
    query_list = [SimpleQuery(search_key=search_key, comparison_operator=comparison_operator, group=group, **{column: x}) for x in value_list]
  return query_list
Пример #4
0
    def _buildQuery(self, operator_value_dict, logical_operator, parsed,
                    group):
        """
      Special Query builder for FullText queries: merge all values having the
      same operator into just one query, to save SQL server from the burden to
      do multiple fulltext lookups when one would suit the purpose.

      Here We wrap each phrase or word with double quotes, as the
      workaround of 1-gram search specification in current Sphinx.

      Example:
      'aaa bbb' : '"aaa" "bbb"'
      '"aaa bbb"' : '"aaa bbb"'
      '"aaa bbb" ccc' : '"aaa bbb" "ccc"'
    """
        column = self.getColumn()
        query_list = []
        append = query_list.append
        for comparison_operator, value_list in operator_value_dict.iteritems():
            if len(value_list) == 1:
                value_list = value_list[0].split()
            append(
                SimpleQuery(search_key=self,
                            comparison_operator=comparison_operator,
                            group=group,
                            **{
                                column:
                                ' '.join([
                                    '"%s"' % x.replace('"', '')
                                    for x in value_list
                                ])
                            }))
        return query_list
Пример #5
0
 def _buildQuery(self, operator_value_dict, logical_operator, parsed,
                 group):
     """
   Treat "!=" operator specialy:
    - if the value contains at least one "%", change operator into "not like"
    - otherwise, let it go untouched
 """
     result = []
     if '!=' in operator_value_dict:
         column = self.getColumn()
         original_different_list = operator_value_dict.pop('!=')
         different_list = []
         for value in original_different_list:
             if isinstance(value, basestring) and '%' in value:
                 result.append(
                     SimpleQuery(search_key=self,
                                 group=group,
                                 comparison_operator='not like',
                                 **{column: value}))
             else:
                 different_list.append(value)
             if len(different_list):
                 operator_value_dict['!='] = different_list
     return result + SearchKey._buildQuery(self, operator_value_dict,
                                           logical_operator, parsed, group)
Пример #6
0
 def _buildQuery(self, operator_value_dict, logical_operator, parsed,
                 group):
     """
   Special Query builder for FullText queries: merge all values having the
   same operator into just one query, to save SQL server from the burden to
   do multiple fulltext lookups when one would suit the purpose.
 """
     column = self.getColumn()
     query_list = []
     append = query_list.append
     for comparison_operator in ('match', 'match_boolean'):
         value_list = operator_value_dict.pop(comparison_operator, [])
         if not value_list:
             continue
         # XXX:
         # In MySQL FTS, no operator implies OR so that we should not merge
         # AND queries into one...
         append(
             SimpleQuery(search_key=self,
                         comparison_operator=comparison_operator,
                         group=group,
                         **{column: ' '.join(value_list)}))
     # Other comparison operators are handled by the super class.
     if operator_value_dict:
         query_list += super(FullTextKey,
                             self)._buildQuery(operator_value_dict,
                                               logical_operator, parsed,
                                               group)
     return query_list
Пример #7
0
 def _buildQuery(self, operator_value_dict, logical_operator, parsed, group):
   """
     Special Query builder for FullText queries: merge all values having the
     same operator into just one query, to save SQL server from the burden to
     do multiple fulltext lookups when one would suit the purpose.
   """
   column = self.getColumn()
   query_list = []
   append = query_list.append
   for comparison_operator, value_list in operator_value_dict.iteritems():
     append(SimpleQuery(search_key=self,
                        comparison_operator=comparison_operator,
                        group=group, **{column: ' '.join(value_list)}))
   return query_list
Пример #8
0
 def _buildQuery(self, operator_value_dict, logical_operator, parsed,
                 group):
     """
   Special Query builder for FullText queries: merge all values having the
   same operator into just one query, to save SQL server from the burden to
   do multiple fulltext lookups when one would suit the purpose.
 """
     column = self.getColumn()
     query_list = []
     append = query_list.append
     for comparison_operator in ('mroonga', 'mroonga_boolean'):
         value_list = operator_value_dict.pop(comparison_operator, [])
         if not value_list:
             continue
         if logical_operator == 'and':
             joined_value = ' '.join(value_list)
             append(
                 SimpleQuery(search_key=self,
                             comparison_operator=comparison_operator,
                             group=group,
                             **{column: joined_value}))
         else:
             # TODO : We can join to one query like 'aaa OR (bbb ccc) OR "ddd eee"'.
             for value in value_list:
                 append(
                     SimpleQuery(search_key=self,
                                 comparison_operator=comparison_operator,
                                 group=group,
                                 **{column: value}))
     # Other comparison operators are handled by the super class.
     if operator_value_dict:
         query_list += super(MroongaFullTextKey,
                             self)._buildQuery(operator_value_dict,
                                               logical_operator, parsed,
                                               group)
     return query_list