From 08464f0194c17289dd3913dc966c6d7f3197fd36 Mon Sep 17 00:00:00 2001 From: JohnD Date: Tue, 11 Oct 2022 07:39:51 -0400 Subject: [PATCH] Add struct2dbtable function and use it in sqlite functions --- INDEX | 1 + inst/@octave_sqlite/fetch.m | 2 +- inst/@octave_sqlite/sqlwrite.m | 6 ++-- inst/struct2dbtable.m | 51 ++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 inst/struct2dbtable.m diff --git a/INDEX b/INDEX index e292ec8..498c5c2 100644 --- a/INDEX +++ b/INDEX @@ -14,3 +14,4 @@ Database Operations @octave_sqlite/rollback Support Functions dbtable + struct2dbtable diff --git a/inst/@octave_sqlite/fetch.m b/inst/@octave_sqlite/fetch.m index 40e6138..e88c21f 100644 --- a/inst/@octave_sqlite/fetch.m +++ b/inst/@octave_sqlite/fetch.m @@ -79,7 +79,7 @@ endfor data = __sqlite_fetch__(db, query); - data = dbtable(data); + data = struct2dbtable(data); endfunction %!test diff --git a/inst/@octave_sqlite/sqlwrite.m b/inst/@octave_sqlite/sqlwrite.m index 3a5b975..ebcab95 100644 --- a/inst/@octave_sqlite/sqlwrite.m +++ b/inst/@octave_sqlite/sqlwrite.m @@ -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 diff --git a/inst/struct2dbtable.m b/inst/struct2dbtable.m new file mode 100644 index 0000000..c1ff8a6 --- /dev/null +++ b/inst/struct2dbtable.m @@ -0,0 +1,51 @@ +## Copyright (C) 2022 John Donoghue +## +## 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 +## . + +## -*- 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