This file is indexed.

/usr/share/pyshared/kiwi/db/sqlobj.py is in python-kiwi 1.9.22-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4

##
## Copyright (C) 2007 Async Open Source
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., or visit: http://www.gnu.org/.
##
##
## Author(s):    Johan Dahlin            <jdahlin@async.com.br>
##

"""
SQLObject integration for Kiwi
"""

from sqlobject.sqlbuilder import func, AND, OR, LIKE, SQLExpression

from kiwi.db.query import NumberQueryState, StringQueryState, \
     DateQueryState, DateIntervalQueryState, QueryExecuter
from kiwi.interfaces import ISearchFilter

class _FTI(SQLExpression):
    def __init__(self, q):
        self.q = q
    def __sqlrepr__(self, db):
        return self.q

class SQLObjectQueryExecuter(QueryExecuter):
    def __init__(self, conn=None):
        QueryExecuter.__init__(self)
        self.conn = conn
        self.table = None
        self._query_callbacks = []
        self._filter_query_callbacks = {}
        self._query = self._default_query
        self._full_text_indexes = {}

    #
    # Public API
    #

    def set_table(self, table):
        """
        Sets the SQLObject table/object for this executer
        @param table: a SQLObject subclass
        """
        self.table = table

    def add_query_callback(self, callback):
        """
        Adds a generic query callback

        @param callback: a callable
        """
        if not callable(callback):
            raise TypeError
        self._query_callbacks.append(callback)

    def add_filter_query_callback(self, search_filter, callback):
        """
        Adds a query callback for the filter search_filter

        @param search_filter: a search filter
        @param callback: a callable
        """
        if not ISearchFilter.providedBy(search_filter):
            raise TypeError
        if not callable(callback):
            raise TypeError
        l = self._filter_query_callbacks.setdefault(search_filter, [])
        l.append(callback)

    def set_query(self, callback):
        """
        Overrides the default query mechanism.
        @param callback: a callable which till take two arguments:
          (query, connection)
        """
        if callback is None:
            callback = self._default_query
        elif not callable(callback):
            raise TypeError

        self._query = callback

    #
    # QueryBuilder
    #

    def search(self, states):
        """
        Execute a search.
        @param states:
        """
        if self.table is None:
            raise ValueError("table cannot be None")
        table = self.table
        queries = []
        for state in states:
            search_filter = state.filter
            assert state.filter

            # Column query
            if search_filter in self._columns:
                query = self._construct_state_query(
                    table, state, self._columns[search_filter])
                if query:
                    queries.append(query)
            # Custom per filter/state query.
            elif search_filter in self._filter_query_callbacks:
                for callback in self._filter_query_callbacks[search_filter]:
                    query = callback(state)
                    if query:
                        queries.append(query)
            else:
                if (self._query == self._default_query and
                    not self._query_callbacks):
                    raise ValueError(
                        "You need to add a search column or a query callback "
                        "for filter %s" % (search_filter))

        for callback in self._query_callbacks:
            query = callback(states)
            if query:
                queries.append(query)

        if queries:
            query = AND(*queries)
        else:
            query = None
        result = self._query(query, self.conn)
        return result.limit(self.get_limit())

    #
    # Private
    #

    def _default_query(self, query, conn):
        return self.table.select(query, connection=conn)

    def _construct_state_query(self, table, state, columns):
        queries = []
        for column in columns:
            query = None
            table_field = getattr(table.q, column)
            if isinstance(state, NumberQueryState):
                query = self._parse_number_state(state, table_field)
            elif isinstance(state, StringQueryState):
                query = self._parse_string_state(state, table_field)
            elif isinstance(state, DateQueryState):
                query = self._parse_date_state(state, table_field)
            elif isinstance(state, DateIntervalQueryState):
                query = self._parse_date_interval_state(state, table_field)
            else:
                raise NotImplementedError(state.__class__.__name__)

            if query:
                queries.append(query)

        if queries:
            return OR(*queries)

    def _postgres_has_fti_index(self, table_name, column_name):
        # Assume that the PostgreSQL full text index columns are
        # named xxx_fti where xxx is the name of the column
        res = self.conn.queryOne(
            """SELECT 1
            FROM information_schema.columns
            WHERE table_name = %s AND
                  column_name = %s AND
                  udt_name = 'tsvector';""" % (
            self.conn.sqlrepr(table_name),
            self.conn.sqlrepr(column_name)))
        return bool(res)

    def _check_has_fulltext_index(self, table_name, field_name):
        fullname = table_name + field_name
        if fullname in self._full_text_indexes:
            return self._full_text_indexes[fullname]
        else:
            value = False
            if 'postgres' in self.conn.__class__.__module__:
                value = self._postgres_has_fti_index(table_name,
                                                     field_name + '_fti')
            self._full_text_indexes[fullname] = value
        return value

    def _parse_number_state(self, state, table_field):
        if state.value is not None:
            return table_field == state.value

    def _parse_string_state(self, state, table_field):
        if not state.text:
            return

        if self._check_has_fulltext_index(table_field.tableName,
                                          table_field.fieldName):
            value = state.text.lower()
            # FTI operators:
            #  & = AND
            #  | = OR
            value = value.replace(' ', ' & ')
            return _FTI("%s.%s_fti @@ %s::tsquery" % (
                table_field.tableName,
                table_field.fieldName,
                self.conn.sqlrepr(value)))
        else:
            text = '%%%s%%' % state.text.lower()
            return LIKE(func.LOWER(table_field), text)

    def _parse_date_state(self, state, table_field):
        if state.date:
            return func.DATE(table_field) == state.date

    def _parse_date_interval_state(self, state, table_field):
        queries = []
        if state.start:
            queries.append(table_field >= state.start)
        if state.end:
            queries.append(func.DATE(table_field) <= state.end)
        if queries:
            return AND(*queries)