Skip to content

Commit 352ef9c

Browse files
committed
[535] Implement basic support for GenQuery2
1 parent 86a8f11 commit 352ef9c

File tree

6 files changed

+127
-0
lines changed

6 files changed

+127
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Currently supported:
1111
- iRODS connection over SSL
1212
- Implement basic GenQueries (select columns and filtering)
1313
- Support more advanced GenQueries with limits, offsets, and aggregations
14+
- Support for queries using the GenQuery2 interface
1415
- Query the collections and data objects within a collection
1516
- Execute direct SQL queries
1617
- Execute iRODS rules
@@ -1252,6 +1253,23 @@ As stated, this type of object discovery requires some extra study and
12521253
effort, but the ability to search arbitrary iRODS zones (to which we are
12531254
federated and have the user permissions) is powerful indeed.
12541255

1256+
1257+
GenQuery2 queries
1258+
-------
1259+
1260+
GenQuery2 is a successor to the regular GenQuery interface. It is available
1261+
by default on iRODS 4.3.2 and higher.
1262+
1263+
In order to use it, create and execute a Query2 object:
1264+
1265+
```
1266+
>>> q = session.query2("tempZone", "SELECT COLL_NAME WHERE COLL_NAME = '/tempZone/home' OR COLL_NAME LIKE '%/query2_dummy_doesnotexist'")
1267+
>>> q.execute()
1268+
[['/tempZone/home']]
1269+
```
1270+
1271+
The first argument is the zone name, and the second argument is the GenQuery2 query.
1272+
12551273
Tickets
12561274
-------
12571275

irods/api_number.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
"SSL_END_AN": 1101,
179179
"CLIENT_HINTS_AN": 10215,
180180
"GET_RESOURCE_INFO_FOR_OPERATION_AN": 10220,
181+
"GENQUERY2_AN": 10221,
181182
"ATOMIC_APPLY_METADATA_OPERATIONS_APN": 20002,
182183
"GET_FILE_DESCRIPTOR_INFO_APN": 20000,
183184
"REPLICA_CLOSE_APN": 20004,

irods/message/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,12 @@ class GenQueryResponse(Message):
628628
# openFlags; double offset; double dataSize; int numThreads; int oprType;
629629
# struct *SpecColl_PI; struct KeyValPair_PI;"
630630

631+
class GenQuery2Request(Message):
632+
_name = 'Genquery2Input_PI'
633+
query_string = StringProperty()
634+
zone = StringProperty()
635+
sql_only = IntegerProperty()
636+
column_mappings = IntegerProperty()
631637

632638
class FileOpenRequest(Message):
633639
_name = 'DataObjInp_PI'

irods/query2.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import json
2+
3+
from irods.api_number import api_number
4+
from irods.exception import OperationNotSupported
5+
from irods.message import GenQuery2Request, STR_PI, iRODSMessage
6+
7+
8+
class Query2(object):
9+
"""This class provides an interface to GenQuery2 API, an experimental
10+
iRODS API for querying iRODS. GenQuery2 is an improved version of the
11+
traditional GenQuery interface.
12+
"""
13+
14+
def __init__(self, session, zone, query, override_not_supported=False):
15+
self.session = session
16+
self.zone = zone
17+
self.query = query
18+
if not (self._is_supported() or override_not_supported):
19+
raise OperationNotSupported(
20+
"GenQuery2 is not supported by default on this iRODS version.")
21+
22+
def execute(self):
23+
"""Execute this GenQuery2 query, and return the results."""
24+
return self._exec_genquery2()
25+
26+
def get_sql(self):
27+
"""Return the SQL query that this GenQuery2 query will be translated to."""
28+
return self._exec_genquery2(sql_flag=True)
29+
30+
def _exec_genquery2(self, sql_flag=False):
31+
msg = GenQuery2Request()
32+
msg.query_string = self.query
33+
msg.zone = self.zone
34+
msg.sql_only = 1 if sql_flag else 0
35+
msg.column_mappings = 0
36+
message = iRODSMessage('RODS_API_REQ',
37+
msg=msg,
38+
int_info=api_number['GENQUERY2_AN'])
39+
with self.session.pool.get_connection() as conn:
40+
conn.send(message)
41+
response = conn.recv()
42+
result = response.get_main_message(STR_PI).myStr
43+
return result if sql_flag else json.loads(result)
44+
45+
def _is_supported(self):
46+
"""Checks whether this iRODS server supports GenQuery2."""
47+
return self.session.server_version >= (4, 3, 2)

irods/session.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import threading
1010
import weakref
1111
from irods.query import Query
12+
from irods.query2 import Query2
1213
from irods.pool import Pool
1314
from irods.account import iRODSAccount
1415
from irods.api_number import api_number
@@ -269,6 +270,9 @@ def configure(self, **kwargs):
269270
def query(self, *args, **kwargs):
270271
return Query(self, *args, **kwargs)
271272

273+
def query2(self, *args, **kwargs):
274+
return Query2(self, *args, **kwargs)
275+
272276
@property
273277
def username(self):
274278
return self.pool.account.client_user

irods/test/query2_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import unittest
2+
3+
import irods.test.helpers as helpers
4+
5+
6+
class TestQuery2(unittest.TestCase):
7+
8+
def setUp(self):
9+
self.sess = helpers.make_session()
10+
11+
if self.sess.server_version < (4, 3, 2):
12+
self.skipTest(
13+
'GenQuery2 is not available by default in iRODS before v4.3.2.')
14+
15+
self.coll_path_a = '/{}/home/{}/test_query2_coll_a'.format(
16+
self.sess.zone, self.sess.username)
17+
self.coll_path_b = '/{}/home/{}/test_query2_coll_b'.format(
18+
self.sess.zone, self.sess.username)
19+
self.sess.collections.create(self.coll_path_a)
20+
self.sess.collections.create(self.coll_path_b)
21+
22+
def test_select(self):
23+
query = "SELECT COLL_NAME WHERE COLL_NAME = '{}'".format(
24+
self.coll_path_a)
25+
q = self.sess.query2(self.sess.zone, query)
26+
query_result = q.execute()
27+
query_sql = q.get_sql()
28+
self.assertIn([self.coll_path_a], query_result)
29+
self.assertEqual(len(query_result), 1)
30+
self.assertEqual(query_sql, "select distinct t0.coll_name from R_COLL_MAIN t0 inner join R_OBJT_ACCESS pcoa on t0.coll_id = pcoa.object_id inner join R_TOKN_MAIN pct on pcoa.access_type_id = pct.token_id inner join R_USER_MAIN pcu on pcoa.user_id = pcu.user_id where t0.coll_name = ? and pcoa.access_type_id >= 1000 fetch first 256 rows only")
31+
32+
def test_select_or(self):
33+
query = "SELECT COLL_NAME WHERE COLL_NAME = '{}' OR COLL_NAME = '{}'".format(
34+
self.coll_path_a, self.coll_path_b)
35+
q = self.sess.query2(self.sess.zone, query)
36+
query_result = q.execute()
37+
query_sql = q.get_sql()
38+
self.assertIn([self.coll_path_a], query_result)
39+
self.assertIn([self.coll_path_b], query_result)
40+
self.assertEqual(len(query_result), 2)
41+
self.assertEqual(query_sql, "select distinct t0.coll_name from R_COLL_MAIN t0 inner join R_OBJT_ACCESS pcoa on t0.coll_id = pcoa.object_id inner join R_TOKN_MAIN pct on pcoa.access_type_id = pct.token_id inner join R_USER_MAIN pcu on pcoa.user_id = pcu.user_id where t0.coll_name = ? or t0.coll_name = ? and pcoa.access_type_id >= 1000 fetch first 256 rows only")
42+
43+
def test_select_and(self):
44+
query = "SELECT COLL_NAME WHERE COLL_NAME LIKE '{}' AND COLL_NAME LIKE '{}'".format(
45+
"%test_query2_coll%", "%query2_coll_a%")
46+
q = self.sess.query2(self.sess.zone, query)
47+
query_result = q.execute()
48+
query_sql = q.get_sql()
49+
self.assertIn([self.coll_path_a], query_result)
50+
self.assertEqual(len(query_result), 1)
51+
self.assertEqual(query_sql, "select distinct t0.coll_name from R_COLL_MAIN t0 inner join R_OBJT_ACCESS pcoa on t0.coll_id = pcoa.object_id inner join R_TOKN_MAIN pct on pcoa.access_type_id = pct.token_id inner join R_USER_MAIN pcu on pcoa.user_id = pcu.user_id where t0.coll_name like ? and t0.coll_name like ? and pcoa.access_type_id >= 1000 fetch first 256 rows only")

0 commit comments

Comments
 (0)