#NEWLINE# def find_associations( self , subject = None , predicate = None , obj = None , assoc_type = None , id_only = True , anyside = None , query = None , **kwargs ) : #NEWLINE# #INDENT# if ( type ( id_only ) is not bool ) : #NEWLINE# #INDENT# raise BadRequest ( ( 'id_only must be type bool, not %s' % type ( id_only ) ) ) #NEWLINE# #UNINDENT# if ( not ( subject or obj or predicate or anyside or query ) ) : #NEWLINE# #INDENT# raise BadRequest ( 'Illegal parameters: No S/P/O or anyside' ) #NEWLINE# #UNINDENT# if ( anyside and ( subject or obj ) ) : #NEWLINE# #INDENT# raise BadRequest ( 'Illegal parameters: anyside cannot be combined with S/O' ) #NEWLINE# #UNINDENT# if ( anyside and predicate and ( type ( anyside ) in ( list , tuple ) ) ) : #NEWLINE# #INDENT# raise BadRequest ( 'Illegal parameters: anyside list cannot be combined with P' ) #NEWLINE# #UNINDENT# if query : #NEWLINE# #INDENT# query [ 'query_args' ] [ 'id_only' ] = id_only #NEWLINE# query [ 'query_args' ] [ 'ds_sub' ] = 'assoc' #NEWLINE# return self . find_by_query ( query ) #NEWLINE# #UNINDENT# ( subject_id , object_id , anyside_ids ) = ( None , None , None ) #NEWLINE# if subject : #NEWLINE# #INDENT# if ( type ( subject ) is str ) : #NEWLINE# #INDENT# subject_id = subject #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( '_id' not in subject ) : #NEWLINE# #INDENT# raise BadRequest ( 'Object id not available in subject' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# subject_id = subject . _id #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if obj : #NEWLINE# #INDENT# if ( type ( obj ) is str ) : #NEWLINE# #INDENT# object_id = obj #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( '_id' not in obj ) : #NEWLINE# #INDENT# raise BadRequest ( 'Object id not available in object' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# object_id = obj . _id #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if anyside : #NEWLINE# #INDENT# if ( type ( anyside ) is str ) : #NEWLINE# #INDENT# anyside_ids = [ anyside ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type ( anyside ) in ( list , tuple ) ) : #NEWLINE# #INDENT# if ( not all ( [ ( type ( o ) in ( str , list , tuple ) ) for o in anyside ] ) ) : #NEWLINE# #INDENT# raise BadRequest ( 'List of object ids or (object id, predicate) expected' ) #NEWLINE# #UNINDENT# anyside_ids = anyside #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( '_id' not in anyside ) : #NEWLINE# #INDENT# raise BadRequest ( 'Object id not available in anyside' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# anyside_ids = [ anyside . _id ] #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# qual_ds_name = self . _get_datastore_name ( ) #NEWLINE# table = ( qual_ds_name + '_assoc' ) #NEWLINE# view_args = self . _get_view_args ( kwargs ) #NEWLINE# if id_only : #NEWLINE# #INDENT# query = ( 'SELECT id FROM ' + table ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# query = ( 'SELECT id, doc, s, st, p, o, ot FROM ' + table ) #NEWLINE# #UNINDENT# query_clause = ' WHERE retired<>true AND ' #NEWLINE# query_args = dict ( s = subject_id , o = object_id , p = predicate ) #NEWLINE# if ( subject and obj ) : #NEWLINE# #INDENT# query_clause += 's=%(s)s AND o=%(o)s' #NEWLINE# if predicate : #NEWLINE# #INDENT# query_clause += ' AND p=%(p)s' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if subject : #NEWLINE# #INDENT# query_clause += 's=%(s)s' #NEWLINE# if predicate : #NEWLINE# #INDENT# query_clause += ' AND p=%(p)s' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if obj : #NEWLINE# #INDENT# query_clause += 'o=%(o)s' #NEWLINE# if predicate : #NEWLINE# #INDENT# query_clause += ' AND p=%(p)s' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if anyside : #NEWLINE# #INDENT# if predicate : #NEWLINE# #INDENT# query_clause += 'p=%(p)s AND (s=%(any)s OR o=%(any)s)' #NEWLINE# query_args [ 'any' ] = anyside #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type ( anyside_ids [ 0 ] ) is str ) : #NEWLINE# #INDENT# for ( i , key ) in enumerate ( anyside_ids ) : #NEWLINE# #INDENT# if ( i > 0 ) : #NEWLINE# #INDENT# query_clause += ' OR ' #NEWLINE# #UNINDENT# argname = ( 'id%s' % i ) #NEWLINE# query_args [ i ] = key #NEWLINE# query_clause += ( ( ( ( '(s=%(' + argname ) + ')s OR o=%(' ) + argname ) + ')s)' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for ( i , ( key , pred ) ) in enumerate ( anyside_ids ) : #NEWLINE# #INDENT# if ( i > 0 ) : #NEWLINE# #INDENT# query_clause += ' OR ' #NEWLINE# #UNINDENT# argname_id = ( 'id%s' % i ) #NEWLINE# argname_p = ( 'p%s' % i ) #NEWLINE# query_args [ argname_id ] = key #NEWLINE# query_args [ argname_p ] = pred #NEWLINE# query_clause += ( ( ( ( ( ( '(p=%(' + argname_p ) + ')s AND (s=%(' ) + argname_id ) + ')s OR o=%(' ) + argname_id ) + ')s))' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if predicate : #NEWLINE# #INDENT# if ( predicate == '*' ) : #NEWLINE# #INDENT# query_clause += 'p is not null' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# query_clause += 'p=%(p)s' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise BadRequest ( 'Illegal arguments' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# extra_clause = view_args . get ( 'extra_clause' , '' ) #NEWLINE# sql = ( ( query + query_clause ) + extra_clause ) #NEWLINE# with self . pool . cursor ( ** self . cursor_args ) as cur : #NEWLINE# #INDENT# cur . execute ( sql , query_args ) #NEWLINE# rows = cur . fetchall ( ) #NEWLINE# #UNINDENT# if id_only : #NEWLINE# #INDENT# assocs = [ self . _prep_id ( row [ 0 ] ) for row in rows ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assocs = [ self . _persistence_dict_to_ion_object ( row [ 1 ] ) for row in rows ] #NEWLINE# #UNINDENT# return assocs