Skip to content

Commit

Permalink
Add struct2dbtable function and use it in sqlite functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lostbard committed Oct 11, 2022
1 parent 8b86a28 commit 08464f0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions INDEX
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Database Operations
@octave_sqlite/rollback
Support Functions
dbtable
struct2dbtable
2 changes: 1 addition & 1 deletion inst/@octave_sqlite/fetch.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
endfor

data = __sqlite_fetch__(db, query);
data = dbtable(data);
data = struct2dbtable(data);
endfunction

%!test
Expand Down
6 changes: 3 additions & 3 deletions inst/@octave_sqlite/sqlwrite.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ function sqlwrite (db, tablename, data, varargin)
endif

if isa(data, "struct")
data = dbtable(data);
data = struct2dbtable(data);
endif
if !isa(data, "dbtable")
error ("Expected input data as a dbtable or struct");
if !isa(data, "dbtable") && !isa(data, "table")
error ("Expected input data as a table or struct");
endif

# for some reason, the subref using '.' on data isnt working here
Expand Down
51 changes: 51 additions & 0 deletions inst/struct2dbtable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## Copyright (C) 2022 John Donoghue <john.donoghue@ieee.org>
##
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see
## <https://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {} {@var{t} =} struct2dbtable (@var{astruct})
## Create a table from a struct
##
## If a table function exists, use it to create the table, otherwise, use the
## dbtable type to create it.
##
## @subsubheading Inputs:
## @table @asis
## @item @var{astruct}
## A struct with same number of elements in each field
## @endtable
##
## @subsubheading Outputs:
## @table @asis
## @item @var{t}
## a table (if table exists) or dbtable of the astruct data
## @endtable
##
## @end deftypefn

function t = struct2dbtable (astruct)
if ! isstruct(astruct)
error ("Not a struct");
endif

names = fieldnames(astruct);
values = struct2cell(astruct);

if exist("table") == 2
t = table (values{:}, 'VariableNames', names);
else
t = dbtable (values{:}, 'VariableNames', names');
endif
endfunction

0 comments on commit 08464f0

Please sign in to comment.