Home > FMAToolbox > Database > DBMerge.m

DBMerge

PURPOSE ^

DBMerge - Merge databases.

SYNOPSIS ^

function target = DBMerge(source,target,varargin)

DESCRIPTION ^

DBMerge - Merge databases.

  USAGE

    target = DBMerge(source,target,<options>)

    source         database where the data is read
    target         database where the data is added

  NOTE

    This function cannot handle overlapping databases.

  SEE

    See also DBCreate, DBDuplicate.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function target = DBMerge(source,target,varargin)
0002 
0003 %DBMerge - Merge databases.
0004 %
0005 %  USAGE
0006 %
0007 %    target = DBMerge(source,target,<options>)
0008 %
0009 %    source         database where the data is read
0010 %    target         database where the data is added
0011 %
0012 %  NOTE
0013 %
0014 %    This function cannot handle overlapping databases.
0015 %
0016 %  SEE
0017 %
0018 %    See also DBCreate, DBDuplicate.
0019 %
0020 
0021 % Copyright (C) 2007-2016 by Michaƫl Zugaro
0022 %
0023 % This program is free software; you can redistribute it and/or modify
0024 % it under the terms of the GNU General Public License as published by
0025 % the Free Software Foundation; either version 3 of the License, or
0026 % (at your option) any later version.
0027 
0028 % Make sure MyM is installed and functional
0029 CheckMyM;
0030 
0031 % Check parameters
0032 if nargin < 2,
0033   error('Incorrect number of parameters (type ''help <a href="matlab:help DBMerge">DBMerge</a>'' for details).');
0034 end
0035 if ~isastring(source),
0036   error('Incorrect source database name (type ''help <a href="matlab:help DBMerge">DBMerge</a>'' for details).');
0037 end
0038 if ~isastring(target),
0039   error('Incorrect target database name (type ''help <a href="matlab:help DBMerge">DBMerge</a>'' for details).');
0040 end
0041 
0042 % Make sure both databases exist
0043 current = DBUse;
0044 try
0045     DBUse(source);
0046 catch
0047   error(['Source database ''' source ''' not found (type ''help <a href="matlab:help DBDuplicate">DBDuplicate</a>'' for details).']);
0048 end
0049 if ~isempty(current), DBUse(current); end
0050 try
0051     DBUse(target);
0052 catch
0053   error(['Target database ''' target ''' not found (type ''help <a href="matlab:help DBDuplicate">DBDuplicate</a>'' for details).']);
0054 end
0055 if ~isempty(current), DBUse(current); end
0056 
0057 % Confirm
0058 disp(['Please make sure that the databases do not overlap, otherwise ''' target ''' will be damaged.']);
0059 s = lower(input('Type ''merge'' to confirm: ','s'));
0060 if ~strcmp(s,'merge'),
0061     disp('*** Cancelled ***');
0062     return
0063 end
0064 
0065 % Copy database contents
0066 try
0067     h = mym(['insert into ' target '.' 'figures select * from ' source '.figures']);
0068     h = mym(['insert into ' target '.' 'variables select * from ' source '.variables']);
0069 catch
0070    error('FMAToolbox:DBMerge:mergeDB',['Could not merge databases ''' source ''' and ''' target ''' (non-unique eid-name pairs?)']);
0071 end
0072 
0073 % Copy external storage if necessary
0074 storage = DBExternalStoragePath;
0075 sourceDirectory = [storage '/' source];
0076 targetDirectory = [storage '/' target];
0077 s = [sourceDirectory '/variables'];
0078 t = [targetDirectory '/variables'];
0079 if isdir(s),
0080     if ~isdir(t), mkdir(t); end
0081     if ~copyfile([s '/*'],t),
0082         error(['Could not copy external storage for variables.']);
0083     end
0084 end
0085 s = [sourceDirectory '/figures'];
0086 t = [targetDirectory '/figures'];
0087 if isdir(s),
0088     if ~isdir(t), mkdir(t); end
0089     if ~copyfile([s '/*'],t),
0090         error(['Could not copy external storage for figures.']);
0091     end
0092 end

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005