From 00f7cad354c50b8ba0136a5c3f5ef24504f6bd6b Mon Sep 17 00:00:00 2001 From: guzman-raphael Date: Wed, 12 Aug 2020 12:19:51 -0500 Subject: [PATCH] Update fetchn to rely on fetch logic and tweak tests to cover it. --- +dj/+internal/GeneralRelvar.m | 15 ++++++++------- tests/TestExternalFile.m | 8 ++++---- tests/TestFetch.m | 15 ++++++++------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/+dj/+internal/GeneralRelvar.m b/+dj/+internal/GeneralRelvar.m index 8d4aa193..ef400225 100644 --- a/+dj/+internal/GeneralRelvar.m +++ b/+dj/+internal/GeneralRelvar.m @@ -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 @@ -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( ... @@ -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 diff --git a/tests/TestExternalFile.m b/tests/TestExternalFile.m index 8a0051ab..ce82453d 100644 --- a/tests/TestExternalFile.m +++ b/tests/TestExternalFile.m @@ -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); diff --git a/tests/TestFetch.m b/tests/TestFetch.m index 0eadb3c8..b796e233 100644 --- a/tests/TestFetch.m +++ b/tests/TestFetch.m @@ -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;