changeset 11:9174aadb93a5 database-connection-manager

Add LTPDA Repository utility functions into utils.repository
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Mon, 05 Dec 2011 16:20:06 +0100
parents 75007001cbfe
children 86aabb42dd84
files m-toolbox/classes/+utils/@repository/getCollectionIDs.m m-toolbox/classes/+utils/@repository/getObjectType.m m-toolbox/classes/+utils/@repository/getUser.m m-toolbox/classes/+utils/@repository/repository.m
diffstat 4 files changed, 77 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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
--- /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
--- /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
--- /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