Mercurial > hg > ltpda
changeset 19:69e3d49b4b0c database-connection-manager
Update ltpda_uo.fromRepository
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Mon, 05 Dec 2011 16:20:06 +0100 |
parents | 947e2ff4b1b9 |
children | d58813ab1b92 |
files | m-toolbox/classes/@ltpda_uo/fromRepository.m |
diffstat | 1 files changed, 39 insertions(+), 83 deletions(-) [+] |
line wrap: on
line diff
--- a/m-toolbox/classes/@ltpda_uo/fromRepository.m Mon Dec 05 16:20:06 2011 +0100 +++ b/m-toolbox/classes/@ltpda_uo/fromRepository.m Mon Dec 05 16:20:06 2011 +0100 @@ -12,7 +12,7 @@ % VERSION: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $ % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -function [objs, plhout, ii] = fromRepository(obj, pli, ii) +function [objs, plhout, ii] = fromRepository(obj, pl, ii) VERSION = 'ltpda_uo: $Id: fromRepository.m,v 1.4 2010/03/16 19:16:20 ingo Exp $'; @@ -20,22 +20,8 @@ % Set the method version string in the minfo object ii.setMversion([VERSION '-->' ii.mversion]); - - % Check if the user supplied a DB connection - conn = find(pli, 'conn'); - - % Add default values if necessary - if isempty(conn) - pl = combine(pli, ii.plists.pset('HOSTNAME', '')); - else - pl = pli; - end - + % Get parameters - hostname = find(pl, 'hostname'); - database = find(pl, 'database'); - user = find(pl, 'username'); - passwd = find(pl, 'password'); ids = find(pl, 'id'); cids = find(pl, 'cid'); bin = find(pl, 'binary'); @@ -57,28 +43,15 @@ %%% check if using binary or not: 'yes'/'no' or true/false or %%% 'true'/'false' bin = utils.prog.yes2true(bin); - - % do we have a connection? - closeConn = 0; - if isempty(conn) - closeConn = 1; - % Connect to repository - conn = utils.jmysql.connect(hostname, database, user, passwd); - end - if ~isa(conn, 'database') && ~isa(conn, 'mpipeline.repository.RepositoryConnection') - error('### connection to the database failed.'); - end + + % database connection + conn = LTPDADatabaseConnectionManager().connect(pl); if ~isempty(cids) for kk=1:numel(cids) cid = cids(kk); - if isa(conn, 'database') - % get the ids from the cid - ids = [ids utils.mysql.getObjIds(conn,cid)]; - else - c_ids = double(mpipeline.repository.MySQLUtils.getObjectIDsFromCollectionID(conn, cid)); - ids = [ids c_ids.']; - end + % get the ids from the cid + ids = [ids utils.repository.getCollectionIDs(conn, cid)]; end end @@ -96,62 +69,45 @@ id = ids(kk); utils.helper.msg(utils.const.msg.OPROC2, 'retrieving ID %d', id); - %---- call database constructor - if bin - try + try + %---- call database constructor + if bin obj = ltpda_uo.retrieve(conn, 'binary', id); - - catch - - if ~conn.isConnected() - error('### There is something wrong with the connection.') - end - - warning('!!! Unable to do binary retrieval for object %d; trying to retrieve XML instead.', id); + else obj = ltpda_uo.retrieve(conn, id); end - else - obj = ltpda_uo.retrieve(conn, id); - end - - if ~strcmp(class(obj), requested_class) - error('### You have used the constructor ''%s'' but the object with id=%d is of class ''%s''', requested_class, id, class(obj)); - end - - % - remove connection object from plist first - if isempty(hostname) || isempty(database) - if isa(conn, 'database') - txt = textscan(conn.URL, '%s', 'Delimiter', '/', 'CollectOutput', 1); - plc = plist('hostname', txt{1}{3}, 'database', txt{1}{4}); - else - plc = plist('hostname', char(conn.getHostname), 'database', char(conn.getDatabase)); + + if ~strcmp(class(obj), requested_class) + error('### You have used the constructor ''%s'' but the object with id=%d is of class ''%s''', requested_class, id, class(obj)); + end + + %---- remove the connection from the history plist + if plh.isparam('conn') + plh.remove('conn'); end - plh = combine(pli, plc); - end - - %---- remove the connection from the history plist - if plh.isparam('conn') - plh.remove('conn'); + + %---- Set only the ID of the current object to the plist + plh.pset('ID', id); + + %---- Add history-plist to output array + plhout = [plhout plh]; + + %---- Add to output array + objs = [objs obj]; + + catch ex + % close connection if we own it + if isempty(find(pl, 'conn')) + conn.close(); + end + throw(ex) end - - %---- Set only the ID of the current object to the plist - plh.pset('ID', id); - - %---- Add history-plist to output array - plhout = [plhout plh]; - - %---- Add to output array - objs = [objs obj]; - + end - % close connection - if closeConn - if isa(conn, 'database') - close(conn); - else - conn.closeConnection; - end + % close connection if we own it + if isempty(find(pl, 'conn')) + conn.close(); end end