Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fetchn to rely on fetch logic and tweak tests to cover it #273

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions +dj/+internal/GeneralRelvar.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ function clip(self)
end

function [ret,keys] = fetch(self, varargin)
% FETCHN retrieve data from a relation as a struct array
% FETCH retrieve data from a relation as a struct array
% SYNTAX:
% s = self.fetch % retrieve primary key attributes only
% s = self.fetch('*') % retrieve all attributes
Expand Down Expand Up @@ -269,7 +269,7 @@ function clip(self)
%
% See also FETCH1, FETCH, PROJ

[limit, args] = makeLimitClause(varargin{:});
[~, args] = makeLimitClause(varargin{:});
specs = args(cellfun(@ischar, args)); % attribute specifiers
returnKey = nargout==length(specs)+1;
assert(returnKey || (nargout==length(specs) || (nargout==0 && length( ...
Expand All @@ -279,17 +279,18 @@ function clip(self)
assert(~any(strcmp(specs,'*')), '"*" is not allowed in fetchn()')

% submit query
self = self.proj(args{:}); % this copies the object, so now it's a different self
[hdr, sql_] = self.compile;
ret = self.conn.query(sprintf('SELECT %s FROM %s%s%s',...
hdr.sql, sql_, limit));
s = self.fetch(varargin{:});

% copy into output arguments
varargout = cell(length(specs));
for iArg=1:length(specs)
% if renamed, use the renamed attribute
name = regexp(specs{iArg}, '(\w+)\s*$', 'tokens');
varargout{iArg} = ret.(name{1}{1});
if isnumeric(s(1).(name{1}{1})) && length(s(1).(name{1}{1})) == 1
varargout{iArg} = [s.(name{1}{1})]';
else
varargout{iArg} = {s.(name{1}{1})}';
end
end

if returnKey
Expand Down
8 changes: 4 additions & 4 deletions tests/TestExternalFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ function TestExternalFile_checks(test_instance, store, cache)
schema.external.table('main').spec.type_config.subfolding);
% delete value to rely on cache
schema.external.table('main').spec.remove_object(uuid_path);
res = q.fetch('dimension');
value_check = res(1).dimension;
res = q.fetchn('dimension');
value_check = res{1};
test_instance.verifyEqual(value_check, test_val1);
% populate
populate(External.Image);
q = External.Image & 'dimension_id=4';
res = q.fetch('img');
value_check = res(1).img;
res = q.fetch1('img');
value_check = res;
test_instance.verifyEqual(size(value_check), test_val1);
% check used and unused
test_instance.verifyTrue(schema.external.table('main').used.count==2);
Expand Down
15 changes: 8 additions & 7 deletions tests/TestFetch.m
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,15 @@ function TestFetch_testNullable(testCase)
'blob', [] ...
));

q = University.All & 'id=5';
res = q.fetch('*');
q = University.All & 'id<6';
[id, string_fetch, date_fetch, number, blob] = q.fetchn('id', 'string', ...
'date', 'number', 'blob', 'ORDER BY id DESC');

testCase.verifyEqual(res(1).id, 5);
testCase.verifyEqual(res(1).string, '');
testCase.verifyEqual(res(1).date, '');
testCase.verifyEqual(res(1).number, NaN);
testCase.verifyEqual(res(1).blob, '');
testCase.verifyEqual(id(1), 5);
testCase.verifyEqual(string_fetch{1}, '');
testCase.verifyEqual(date_fetch{1}, '');
testCase.verifyEqual(number(1), NaN);
testCase.verifyEqual(blob{1}, '');
end
function TestFetch_testDescribe(testCase)
st = dbstack;
Expand Down