File: query.py

package info (click to toggle)
python-irodsclient 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,352 kB
  • sloc: python: 16,650; xml: 525; sh: 104; awk: 5; sql: 3; makefile: 3
file content (380 lines) | stat: -rw-r--r-- 12,063 bytes parent folder | download
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
from collections import OrderedDict

from irods import MAX_SQL_ROWS
from irods.models import Model
from irods.column import Column, Keyword
from irods.message import (
    IntegerIntegerMap,
    IntegerStringMap,
    StringStringMap,
    _OrderedMultiMapping,
    GenQueryRequest,
    GenQueryResponse,
    empty_gen_query_out,
    iRODSMessage,
    SpecificQueryRequest,
    GeneralAdminRequest,
)
from irods.api_number import api_number
from irods.exception import CAT_NO_ROWS_FOUND, MultipleResultsFound, NoResultFound
from irods.results import ResultSet, SpecificQueryResultSet

query_number = {
    "ORDER_BY": 0x400,
    "ORDER_BY_DESC": 0x800,
    "RETURN_TOTAL_ROW_COUNT": 0x20,
    "NO_DISTINCT": 0x40,
    "QUOTA_QUERY": 0x80,
    "AUTO_CLOSE": 0x100,
    "UPPER_CASE_WHERE": 0x200,
    "SELECT_MIN": 2,
    "SELECT_MAX": 3,
    "SELECT_SUM": 4,
    "SELECT_AVG": 5,
    "SELECT_COUNT": 6,
}


class Query:

    def __init__(self, sess, *args, **kwargs):
        self.sess = sess
        self.columns = OrderedDict()
        self.criteria = []
        self.case_sensitive = kwargs.pop("case_sensitive", True)
        self._limit = -1
        self._offset = 0
        self._continue_index = 0
        self._keywords = {}

        for arg in args:
            if isinstance(arg, type) and issubclass(arg, Model):
                for col in arg._columns:
                    if self.sess.server_version >= col.min_version:
                        self.columns[col] = 1
            elif isinstance(arg, Column):
                self.columns[arg] = 1
            else:
                raise TypeError("Arguments must be models or columns")

    def _clone(self):
        new_q = Query(self.sess)
        new_q.columns = self.columns
        new_q.criteria = self.criteria
        new_q.case_sensitive = self.case_sensitive
        new_q._limit = self._limit
        new_q._offset = self._offset
        new_q._continue_index = self._continue_index
        new_q._keywords = self._keywords
        return new_q

    def add_keyword(self, keyword, value=""):
        new_q = self._clone()
        new_q._keywords[keyword] = value
        return new_q

    def filter(self, *criteria):
        new_q = self._clone()

        if self.case_sensitive:
            new_q.criteria += list(criteria)
        else:
            # In case-insensitive mode, all criterion values are converted
            # to uppercase here, and the UPPER_CASE_WHERE option is enabled
            # when creating a new GenQueryRequest in the _message function.
            # Converting both keys and values to uppercase results in
            # case-insensitive queries.
            for criterion in criteria:
                if type(criterion.value) == str:
                    criterion.value = str.upper(criterion.value)
                elif type(criterion.value) == list:
                    criterion.value = [
                        str.upper(c) if type(c) == str else c for c in criterion.value
                    ]
                elif type(criterion.value) == tuple:
                    criterion.value = tuple(
                        [
                            str.upper(c) if type(c) == str else c
                            for c in list(criterion.value)
                        ]
                    )
                new_q.criteria.append(criterion)

        return new_q

    def order_by(self, column, order="asc"):
        new_q = self._clone()
        new_q.columns.pop(column, None)
        if order == "asc":
            new_q.columns[column] = query_number["ORDER_BY"]
        elif order == "desc":
            new_q.columns[column] = query_number["ORDER_BY_DESC"]
        else:
            raise ValueError("Ordering must be 'asc' or 'desc'")
        return new_q

    def limit(self, limit):
        new_q = self._clone()
        new_q._limit = limit
        return new_q

    def offset(self, offset):
        new_q = self._clone()
        new_q._offset = offset
        return new_q

    def continue_index(self, continue_index):
        new_q = self._clone()
        new_q._continue_index = continue_index
        return new_q

    def _aggregate(self, func, *args):
        new_q = self._clone()
        # NOTE(wtakase): Override existing column by aggregation.
        for arg in args:
            if isinstance(arg, type) and issubclass(arg, Model):
                for col in arg._columns:
                    self.columns[col] = func
            elif isinstance(arg, Column):
                self.columns[arg] = func
            else:
                raise TypeError("Arguments must be models or columns")
        new_q.columns = self.columns
        return new_q

    def min(self, *args):
        return self._aggregate(query_number["SELECT_MIN"], *args)

    def max(self, *args):
        return self._aggregate(query_number["SELECT_MAX"], *args)

    def sum(self, *args):
        return self._aggregate(query_number["SELECT_SUM"], *args)

    def avg(self, *args):
        return self._aggregate(query_number["SELECT_AVG"], *args)

    def count(self, *args):
        return self._aggregate(query_number["SELECT_COUNT"], *args)

    def _select_message(self):
        dct = OrderedDict(
            [(column.icat_id, value) for (column, value) in self.columns.items()]
        )
        return IntegerIntegerMap(dct)

    # todo store criterion for columns and criterion for keywords in seaparate
    # lists
    def _conds_message(self):
        dct = _OrderedMultiMapping(
            [
                (
                    criterion.query_key.icat_id,
                    criterion.op + " " + criterion.irods_value,
                )
                for criterion in self.criteria
                if isinstance(criterion.query_key, Column)
            ]
        )
        return IntegerStringMap(dct)

    def _kw_message(self):
        dct = dict(
            [
                (
                    criterion.query_key.icat_key,
                    criterion.op + " " + criterion.irods_value,
                )
                for criterion in self.criteria
                if isinstance(criterion.query_key, Keyword)
            ]
        )
        for key in self._keywords:
            dct[key] = self._keywords[key]
        return StringStringMap(dct)

    def _message(self):
        max_rows = 500 if self._limit == -1 else self._limit
        args = {
            "maxRows": max_rows,
            "continueInx": self._continue_index,
            "partialStartIndex": self._offset,
            "options": 0 if self.case_sensitive else query_number["UPPER_CASE_WHERE"],
            "KeyValPair_PI": self._kw_message(),
            "InxIvalPair_PI": self._select_message(),
            "InxValPair_PI": self._conds_message(),
        }
        return GenQueryRequest(**args)

    def execute(self):
        with self.sess.pool.get_connection() as conn:

            message_body = self._message()
            message = iRODSMessage(
                "RODS_API_REQ", msg=message_body, int_info=api_number["GEN_QUERY_AN"]
            )

            conn.send(message)
            try:
                result_message = conn.recv()
                results = result_message.get_main_message(GenQueryResponse)
                result_set = ResultSet(results)
            except CAT_NO_ROWS_FOUND:
                result_set = ResultSet(empty_gen_query_out(list(self.columns.keys())))
        return result_set

    def close(self):
        """Closes an open query on the server side.
        self._continue_index must be set to a valid value (returned by a previous query API call).
        """
        self.limit(0).execute()

    def all(self):
        result_set = self.execute()
        if result_set.continue_index > 0:
            self.continue_index(result_set.continue_index).close()
        return result_set

    def get_batches(self):
        result_set = self.execute()

        try:
            yield result_set

            while result_set.continue_index > 0:
                try:
                    result_set = self.continue_index(
                        result_set.continue_index
                    ).execute()
                    yield result_set
                except CAT_NO_ROWS_FOUND:
                    break
        except GeneratorExit:
            if result_set.continue_index > 0:
                self.continue_index(result_set.continue_index).close()

    def get_results(self):
        for result_set in self.get_batches():
            for result in result_set:
                yield result

    def __iter__(self):
        return self.get_results()

    def one(self):
        results = self.execute()
        if results.continue_index > 0:
            self.continue_index(results.continue_index).close()
        if not len(results):
            raise NoResultFound()
        if len(results) > 1:
            raise MultipleResultsFound()
        return results[0]

    def first(self):
        query = self.limit(1)
        results = query.execute()
        if results.continue_index > 0:
            query.continue_index(results.continue_index).close()
        if not len(results):
            return None
        else:
            return results[0]


#     def __getitem__(self, val):
#         pass


class SpecificQuery:

    def __init__(self, sess, sql=None, alias=None, columns=None, args=None):
        if not sql and not alias:
            raise ValueError("A query or alias must be provided")

        self.session = sess
        self._sql = sql
        self._alias = alias
        self._continue_index = 0
        self._columns = columns
        self._args = args or []

    def register(self):
        if not self._sql:
            raise ValueError("Empty query")

        message_body = GeneralAdminRequest(
            "add", "specificQuery", self._sql, self._alias
        )
        request = iRODSMessage(
            "RODS_API_REQ", msg=message_body, int_info=api_number["GENERAL_ADMIN_AN"]
        )

        with self.session.pool.get_connection() as conn:
            conn.send(request)
            response = conn.recv()
        return response

    def remove(self):
        target = self._alias or self._sql

        message_body = GeneralAdminRequest("rm", "specificQuery", target)
        request = iRODSMessage(
            "RODS_API_REQ", msg=message_body, int_info=api_number["GENERAL_ADMIN_AN"]
        )

        with self.session.pool.get_connection() as conn:
            conn.send(request)
            response = conn.recv()
        return response

    def execute(self, limit=MAX_SQL_ROWS, offset=0, options=0, conditions=None):
        target = self._alias or self._sql

        if conditions is None:
            conditions = StringStringMap({})

        sql_args = {}
        for i, arg in enumerate(self._args[:10], start=1):
            sql_args["arg{}".format(i)] = arg

        message_body = SpecificQueryRequest(
            sql=target,
            maxRows=limit,
            continueInx=self._continue_index,
            rowOffset=offset,
            options=0,
            KeyValPair_PI=conditions,
            **sql_args
        )

        request = iRODSMessage(
            "RODS_API_REQ", msg=message_body, int_info=api_number["SPECIFIC_QUERY_AN"]
        )

        with self.session.pool.get_connection() as conn:
            conn.send(request)
            response = conn.recv()

        results = response.get_main_message(GenQueryResponse)
        return SpecificQueryResultSet(results, self._columns)

    def __iter__(self):
        return self.get_results()

    def get_batches(self):
        result_set = self.execute()
        yield result_set

        while result_set.continue_index > 0:
            self._continue_index = result_set.continue_index
            try:
                result_set = self.execute()
                yield result_set
            except CAT_NO_ROWS_FOUND:
                break

    def get_results(self):
        for result_set in self.get_batches():
            for result in result_set:
                yield result