# HG changeset patch # User Daniele Nicolodi # Date 1323098406 -3600 # Node ID 9174aadb93a56093645453051589d2fbc3f6d60a # Parent 75007001cbfe2866230416a08f93057ecc765fb4 Add LTPDA Repository utility functions into utils.repository diff -r 75007001cbfe -r 9174aadb93a5 m-toolbox/classes/+utils/@repository/getCollectionIDs.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m-toolbox/classes/+utils/@repository/getCollectionIDs.m Mon Dec 05 16:20:06 2011 +0100 @@ -0,0 +1,26 @@ +function ids = getCollectionIDs(conn, cid) +% GETCOLLECTIONIDS Return the IDs of the object composing a collection. +% +% CALL: +% +% ids = utils.repository.getCollectionIDs(conn, cid) +% +% PARAMETERS: +% +% CONN database connection implementing java.sql.Connection +% CID collection id +% + + rows = utils.mysql.execute(conn, 'SELECT nobjs, obj_ids FROM collections WHERE id = ?', cid); + if isempty(rows) + error('### collection %d not found', cid); + end + nobjs = rows{1}; + ids = strread(rows{2}, '%d', 'delimiter', ','); + if length(ids) ~= nobjs + error('### inconsistent collection description'); + end + % transform column vector in row vector + ids = ids'; + +end diff -r 75007001cbfe -r 9174aadb93a5 m-toolbox/classes/+utils/@repository/getObjectType.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m-toolbox/classes/+utils/@repository/getObjectType.m Mon Dec 05 16:20:06 2011 +0100 @@ -0,0 +1,20 @@ +function type = getObjectType(conn, id) +% GETOBJECTTYPE Return the type of the object. +% +% CALL: +% +% ids = utils.repository.getObjectType(conn, cid) +% +% PARAMETERS: +% +% CONN database connection implementing java.sql.Connection +% ID object id +% + + rows = utils.mysql.execute(conn, 'SELECT obj_type FROM objmeta WHERE obj_id = ?', id); + if isempty(rows) + error('### object %d not found', id); + end + type = rows{1}; + +end diff -r 75007001cbfe -r 9174aadb93a5 m-toolbox/classes/+utils/@repository/getUser.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m-toolbox/classes/+utils/@repository/getUser.m Mon Dec 05 16:20:06 2011 +0100 @@ -0,0 +1,20 @@ +function [username, userid] = getUser(conn) +% GETUSER Return username and userid of the current database user. +% +% CALL: +% +% [username, userid] = utils.repository.getUser(conn) +% + + % current database user + rows = utils.mysql.execute(conn, 'SELECT SUBSTRING_INDEX(USER(),''@'',1)'); + username = rows{1}; + + % userid + rows = utils.mysql.execute(conn, 'SELECT id FROM users WHERE username = ?', username); + if isempty(rows) + error('### could not determine user id'); + end + userid = rows{1}; + +end diff -r 75007001cbfe -r 9174aadb93a5 m-toolbox/classes/+utils/@repository/repository.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m-toolbox/classes/+utils/@repository/repository.m Mon Dec 05 16:20:06 2011 +0100 @@ -0,0 +1,11 @@ +classdef repository + + methods (Static) + + type = getObjectType(conn, id); + ids = getCollectionIDs(conn, cid); + varargout = getUser(conn); + + end % static methods + +end