-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[535] Implement basic support for GenQuery2
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from collections import OrderedDict | ||
import json | ||
|
||
from irods.api_number import api_number | ||
from irods.exception import OperationNotSupported | ||
from irods.message import GenQuery2Request, iRODSMessage, STR_PI | ||
|
||
"""This class provides an interface to GenQuery2 API, an experimental | ||
iRODS API for querying iRODS. GenQuery2 is an improved version of the | ||
traditional GenQuery interface. | ||
""" | ||
|
||
|
||
class Query2(object): | ||
|
||
def __init__(self, session, zone, query, override_not_supported=False): | ||
self.session = session | ||
self.zone = zone | ||
self.query = query | ||
if not (self._is_supported() or override_not_supported): | ||
raise OperationNotSupported( | ||
"GenQuery2 is not supported by default on this iRODS version.") | ||
|
||
def execute(self): | ||
"""Execute this GenQuery2 query, and return the results.""" | ||
return self._exec_genquery2() | ||
|
||
def get_sql(self): | ||
"""Return the SQL query that this GenQuery2 query will be translated to.""" | ||
return self._exec_genquery2(sql_flag=True) | ||
|
||
def _exec_genquery2(self, sql_flag=False): | ||
msg = GenQuery2Request() | ||
msg.query_string = self.query | ||
msg.zone = self.zone | ||
msg.sql_only = 1 if sql_flag else 0 | ||
msg.column_mappings = 0 | ||
message = iRODSMessage('RODS_API_REQ', | ||
msg=msg, | ||
int_info=api_number['GENQUERY2_AN']) | ||
with self.session.pool.get_connection() as conn: | ||
conn.send(message) | ||
response = conn.recv() | ||
result = response.get_main_message(STR_PI).myStr | ||
return result if sql_flag else json.loads(result) | ||
|
||
def _is_supported(self): | ||
"""Checks whether this iRODS server supports GenQuery2.""" | ||
return self.session.server_version >= (4, 3, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
|
||
import irods.test.helpers as helpers | ||
|
||
|
||
class TestQuery2(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.sess = helpers.make_session() | ||
|
||
if self.sess.server_version < (4, 3, 2): | ||
self.skipTest( | ||
'GenQuery2 is not available by default in iRODS before v4.3.2.') | ||
|
||
self.coll_path_a = '/{}/home/{}/test_query2_coll_a'.format( | ||
self.sess.zone, self.sess.username) | ||
self.coll_path_b = '/{}/home/{}/test_query2_coll_b'.format( | ||
self.sess.zone, self.sess.username) | ||
self.sess.collections.create(self.coll_path_a) | ||
self.sess.collections.create(self.coll_path_b) | ||
|
||
def test_select(self): | ||
query = "SELECT COLL_NAME WHERE COLL_NAME = '{}'".format( | ||
self.coll_path_a) | ||
q = self.sess.query2(self.sess.zone, query) | ||
query_result = q.execute() | ||
query_sql = q.get_sql() | ||
self.assertIn([self.coll_path_a], query_result) | ||
self.assertEqual(len(query_result), 1) | ||
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") | ||
|
||
def test_select_or(self): | ||
query = "SELECT COLL_NAME WHERE COLL_NAME = '{}' OR COLL_NAME = '{}'".format( | ||
self.coll_path_a, self.coll_path_b) | ||
q = self.sess.query2(self.sess.zone, query) | ||
query_result = q.execute() | ||
query_sql = q.get_sql() | ||
self.assertIn([self.coll_path_a], query_result) | ||
self.assertIn([self.coll_path_b], query_result) | ||
self.assertEqual(len(query_result), 2) | ||
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") | ||
|
||
def test_select_and(self): | ||
query = "SELECT COLL_NAME WHERE COLL_NAME LIKE '{}' AND COLL_NAME LIKE '{}'".format( | ||
"%test_query2_coll%", "%query2_coll_a%") | ||
q = self.sess.query2(self.sess.zone, query) | ||
query_result = q.execute() | ||
query_sql = q.get_sql() | ||
self.assertIn([self.coll_path_a], query_result) | ||
self.assertEqual(len(query_result), 1) | ||
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") |