lib/Org/ReadTables.pm

filter: Statementstmt 90.9% 131 / 144 Branchbran 75.0% 60 / 80 Conditioncond 51.8% 14 / 27 MC/DCmcdc 23.5% 4 / 17 Subroutinesub 92.3% 12 / 13 Podpod 71.4% 5 / 7 totaltotal 78.4% 226 / 288 CC 56 SCAR 27.5
CC
56
Cov
82%
parse
25.8
walk
13.1
inserted
12.4
CRAP 15.7
« blib/lib/Org/ReadTables.pm ↑ Summary
1 1 package Org::ReadTables v0.0.2 {
2  
3 1 ▶ use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶ use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶ use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶ no warnings qw(experimental::signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ################
10 # Parse tables from an Org-mode file into our database
11 #
12 ################
13  
14 1 ▶ use Org::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶ sub new ($class, %args) {
Subroutine new: called CC=6 SCAR=24.3
Pod documented
17 4 my $self = bless {}, $class;
18 4 foreach my $arg (qw(dbh table tables cb cb_table)) {
19 20 ▶ $self->{$arg} = $args{$arg} if defined $args{$arg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{$arg} 7 13 20
20 }
21 4 ▶ if (defined $self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"dbh"}) 0 4 4
22 0 ▶ if (ref $self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'dbh'}) { } 0 0 0
23 0 ▶ if (!$self->{dbh}->can('insert')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}->can("insert")) 0 0 0
24 0 warn "Database handle should provide an 'insert' method";
25 }
26 } else {
27 0 warn "'dbh' should be a database handle";
28 }
29 }
30 4 return $self;
31 }
32  
33 0 ▶ sub errors ($self) {
Subroutine errors: not called CC=1 SCAR=6.9
Pod documented
34 0 return $self->{errors};
35 }
36  
37 1 ▶ sub inserted ($self, $table = undef) {
Subroutine inserted: called CC=5 SCAR=25.2
Pod documented
38 1 ▶ if (defined $table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table) { } 0 1 1
39 0 ▶ return defined $self->{inserted} && exists $self->{inserted}->{$table} ? $self->{inserted}->{$table} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'inserted'} && exists $self->{'inserted'}{$table} ? : 0 0 0
Condition0%0/3
defined $self->{"inserted"} and exists $self->{"inserted"}{$table}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"inserted"} and exists $self->{"inserted"}{$table}
defined $self->{"inserted"} exists $self->{"inserted"}{$table}
Truth table0%0/3
A and B
A= defined $self->{"inserted"}B= exists $self->{"inserted"}{$table}
ABresult
0X0
100
111
40 } else {
41 1 return $self->{inserted}; # All tables
42 }
43 0 return $self->{inserted};
44 }
45  
46 3 ▶ sub saved ($self, $table = undef) {
Subroutine saved: called CC=5 SCAR=24.4
Pod documented
47 3 ▶ if (defined $table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table) { } 0 3 3
48 0 ▶ return defined $self->{saved} && exists $self->{saved}->{$table} ? $self->{saved}->{$table} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'saved'} && exists $self->{'saved'}{$table} ? : 0 0 0
Condition0%0/3
defined $self->{"saved"} and exists $self->{"saved"}{$table}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"saved"} and exists $self->{"saved"}{$table}
defined $self->{"saved"} exists $self->{"saved"}{$table}
Truth table0%0/3
A and B
A= defined $self->{"saved"}B= exists $self->{"saved"}{$table}
ABresult
0X0
100
111
49 } else {
50 3 return $self->{saved}; # All tables
51 }
52 }
53  
54 4 ▶ sub parse ($self, $text, $default_table = undef) {
Subroutine parse: called CC=25 SCAR=32.5
Pod documented
55  
56 4 state $table; # as a 'state' so the nested 'walk' subroutine can use it
57 4 state $db;
58 4 state $errors = [];
59 4 state $inserted = 0; # 'state' so it can be seen by 'walk'
60 4 state $saved; # ...and by 'flush_row'
61 4 state $self_copy;
62 4 state @fixed_columns;
63 4 state @fixed_values;
64 4 state $data_column; # for pivot tables ~~~~~~~~ TODO
65 4 state $caption;
66  
67 101 ▶ sub flush_row {
Subroutine flush_row: called CC=7 SCAR=20.6
Pod undocumented
68 101 my ($columns, $values) = @_;
69 101 my $save_fields = {};
70  
71 101 ▶ return unless defined $table && length($table);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table and length $table 0 101 101
Condition33%1/3
defined $table and length $table
!ll&&!rl&&r
00101
MC/DC0%0/2
defined $table and length $table
defined $table length $table
Truth table33%1/3
A and B
A= defined $tableB= length $table
ABresult
0X0
100
111
72 101 @{$save_fields}{@{$columns}} = @{$values};
73 101 ▶ if ( defined $self_copy->{cb} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self_copy->{'cb'}) { } 64 37 101
elsif (defined $db ? eval { do { $db->insert($table, $save_fields); 1 } } : do { push @{$$saved{$table};}, $save_fields; 1 }) { } 37 0 37
74 64 ▶ if ( $self_copy->{cb}->($table, $save_fields) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_copy->{"cb"}->($table, $save_fields)) 34 30 64
75 34 do { push @{$saved->{$table}}, $save_fields; 1; };
76 34 $inserted++;
77 }
78 ▶ } elsif ( defined $db ?
Branch: 1 branch
BranchTrueFalseTotal
defined $db ? : 0 37 37
79 0 eval { $db->insert($table, $save_fields); 1; } :
80 37 do { push @{$saved->{$table}}, $save_fields; 1; } ) {
81 37 $inserted++;
82 } else {
83 0 push @{$errors} , [ $save_fields, $@ ];
84 }
85 }
86  
87 767 ▶ sub walk {
Subroutine walk: called CC=13 SCAR=25.7
Pod undocumented
88 # Ignore leading TableHLine
89 # Find first row(s) with TableCell and accumulate column names
90 # Require a TableHLine to proceed
91 # For each TableRow, accumulate column values and save row.
92 #
93 # NOTE: Must call an extra time with a 'fake' TableRow afterwards,
94 # to flush the final row.
95  
96 767 my ($el, $level) = @_;
97  
98 767 state $in_header;
99 767 state $found_header_row;
100 767 state $column;
101 767 state $is_data_row;
102 767 state @columns;
103 767 state @values;
104 # print sprintf("%4d %2d%-30s\n", $_[1], $column, ref $_[0]) ;
105 767 ▶ if ((ref $el) =~ /Table\z/i) {
Branch: 5 branches
BranchTrueFalseTotal
if (ref($el) =~ /Table\z/iu) { } 20 747 767
elsif (ref($el) =~ /TableHLine\z/iu) { } 20 727 747
elsif (ref($el) =~ /TableRow\z/iu) { } 141 586 727
elsif (ref($el) =~ /TableCell\z/iu) { } 293 293 586
elsif (ref($el) =~ /Text\z/iu) { } 293 0 293
106 20 $in_header = 1;
107 20 $found_header_row = 0;
108 20 $inserted = 0;
109 20 @columns = @fixed_columns;
110 20 @values = @fixed_values;
111 } elsif ((ref $el) =~ /TableHLine\z/i) {
112 20 ▶ if ($found_header_row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_header_row) 20 0 20
113 #
114 # Skip this table if the callback returns a false value.
115 #
116 20 ▶ if ($in_header && defined $self_copy->{cb_table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_header and defined $self_copy->{"cb_table"}) 18 2 20
Condition66%2/3
$in_header and defined $self_copy->{"cb_table"}
!ll&&!rl&&r
0218
MC/DC50%1/2
$in_header and defined $self_copy->{"cb_table"}
$in_header defined $self_copy->{"cb_table"}
Truth table66%2/3
A and B
A= $in_headerB= defined $self_copy->{"cb_table"}
ABresult
0X0
100
111
117 undef $table unless $self_copy->{cb_table}->({ name => $table,
118 nameref => \$table, # Modifiable
119 ( scalar @fixed_columns ? (
120 18 ▶ fixed => {map {($fixed_columns[$_], $fixed_values[$_])}
Branch: 2 branches
BranchTrueFalseTotal
unless $self_copy->{"cb_table"}->({"name", $table, "nameref", \$table, scalar @fixed_columns ? ("fixed", {map({$fixed_columns[$_], $fixed_values[$_];} 0 .. scalar @fixed_columns - 1)}) : (), "columns", \@columns, "data_column", $data_column, "caption", $caption}) 0 18 18
scalar @fixed_columns ? : 9 9 18
121 0..(scalar @fixed_columns -1)} ) : () ),
122 columns => \@columns,
123 data_column => $data_column,
124 caption => $caption,
125 });
126 }
127 20 $in_header = 0;
128 }
129 } elsif ((ref $el) =~ /TableRow\z/i) {
130 141 ▶ if ($is_data_row) { # Flush previous row
Branch: 1 branch
BranchTrueFalseTotal
if ($is_data_row) 101 40 141
131 101 flush_row (\@columns, \@values);
132 101 $is_data_row = 0;
133 }
134 141 $found_header_row = 1;
135 141 $column = scalar @fixed_columns - 1;
136 141 @values = @fixed_values;
137 } elsif ((ref $el) =~ /TableCell\z/i) {
138 293 $column++;
139 } elsif ((ref $el) =~ /Text\z/i) {
140 293 ▶ if ($in_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_header) { } 55 238 293
141 55 $columns[$column] = lc($el->as_string);
142 } else {
143 238 $values[$column] = $el->as_string;
144 238 $is_data_row = 1;
145 }
146 }
147 }
148  
149 4 my $orgp = Org::Parser->new;
150 4 my $org_doc = $orgp->parse($text);
151 4 my @tables = $org_doc->find('Table');
152 4 $self_copy = $self;
153 4 $saved = {};
154 4 $errors = [];
155 4 $db = $self->{dbh};
156  
157 4 foreach my $t (@tables) {
158 # Look for NAME property as Settings element in
159 # prev_siblings; also look for PROPERTIES drawer
160 # (containing NAME property) in prev_siblings, ignoring
161 # whitespace-only Text elements
162 32 undef $table;
163 32 my $prev_elem = $t->prev_sibling;
164 32 @fixed_columns = ();
165 32 @fixed_values = ();
166  
167 32 ▶ if (defined $prev_elem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_elem) 32 0 32
168 ELEMENT:
169 32 ▶ while (defined $prev_elem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_elem) 120 4 124
170 # Only regard settings and drawers between headlines or tables.
171 120 ▶ last ELEMENT if ref $prev_elem eq 'Org::Element::Table';
Branch: 1 branch
BranchTrueFalseTotal
if ref $prev_elem eq "Org::Element::Table" 20 100 120
172 # Formerly we looked for a
173 # 'Org::Element::Headline' here, but actually that
174 # would always be the *parent* element. Perhaps
175 # we want to default to
176 # »$t->headline->title->as_string« ?
177  
178 # NOTE: Internal links (»#+NAME: sometable«) are
179 # always followed by all-whitespace Text (see
180 # t/drawer.t in Org::Parser). Ignore them.
181 100 ▶ next ELEMENT if ref $prev_elem eq 'Org::Element::Text' &&
Branch: 1 branch
BranchTrueFalseTotal
if ref $prev_elem eq "Org::Element::Text" and $prev_elem->as_string =~ /^\s*\z/u 40 60 100
Condition100%3/3
ref $prev_elem eq "Org::Element::Text" and $prev_elem->as_string =~ /^\s*\z/u
!ll&&!rl&&r
56440
MC/DC100%2/2
ref $prev_elem eq "Org::Element::Text" and $prev_elem->as_string =~ /^\s*\z/u
ref $prev_elem eq "Org::Element::Text" $prev_elem->as_string =~ /^\s*\z/u
Truth table100%3/3
A and B
A= ref $prev_elem eq "Org::Element::Text"B= $prev_elem->as_string =~ /^\s*\z/u
ABresult
0X0
100
111
182 $prev_elem->as_string =~ /^\s*\z/;
183  
184 60 ▶ if (ref $prev_elem eq 'Org::Element::Setting') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $prev_elem eq 'Org::Element::Setting') { } 32 28 60
elsif (ref $prev_elem eq 'Org::Element::Drawer') { } 12 16 28
185 # e.g., »#+NAME: sometable«
186 32 ▶ if ($prev_elem-> name =~ /^name\z/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($prev_elem->name =~ /^name\z/iu) { } 16 16 32
elsif ($prev_elem->name =~ /^data\z/iu) { } 0 16 16
elsif ($prev_elem->name =~ /^caption\z/iu) { } 8 8 16
elsif ($prev_elem->name =~ /^property\z/iu and scalar @{$prev_elem->args;} > 1) { } 8 0 8
187 16 $table = $prev_elem->args->[0];
188 } elsif ($prev_elem-> name =~ /^data\z/i) {
189 0 $data_column = $prev_elem->args->[0];
190 } elsif ($prev_elem-> name =~ /^caption\z/i) {
191 8 $caption = $prev_elem->args->[0];
192 8 ▶ } elsif ($prev_elem->name =~ /^property\z/i && scalar @{$prev_elem->args} > 1) {
Condition33%1/3
$prev_elem->name =~ /^property\z/iu and scalar @{$prev_elem->args;} > 1
!ll&&!rl&&r
008
MC/DC0%0/2
$prev_elem->name =~ /^property\z/iu and scalar @{$prev_elem->args;} > 1
$prev_elem->name =~ /^property\z/iu scalar @{$prev_elem->args;} > 1
Truth table33%1/3
A and B
A= $prev_elem->name =~ /^property\z/iuB= scalar @{$prev_elem->args;} > 1
ABresult
0X0
100
111
193 8 push @fixed_columns, $prev_elem->args->[0];
194 # Double quotes may be used around a
195 # string with spaces; otherwise this will
196 # add a single space between words.
197 8 push @fixed_values, join(' ',@{$prev_elem->args}[1..scalar @{$prev_elem->args}-1]);
198 }
199 } elsif (ref $prev_elem eq 'Org::Element::Drawer') {
200 # We regard the internal-link-target syntax
201 # «#+NAME: some_table» as meaning, Insert the
202 # following orgmode table into database table
203 # `some_table` (see orgmode manual §4.2
204 # "Internal Links")
205 #
206 # TODO: Verify column names and skip
207 # non-existent ones; otherwise, adding
208 # properties that do not map to sql column
209 # names will result in errors and no rows
210 # inserted!
211 #
212 12 foreach my $k ( keys %{$prev_elem->properties} ) {
213 20 ▶ if ($k =~ /^name\z/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k =~ /^name\z/iu) { } 8 12 20
elsif ($k =~ /^data\z/iu) { } 4 8 12
214 8 $table = $prev_elem->properties->{$k};
215 } elsif ($k =~ /^data\z/i) {
216 4 $data_column = $prev_elem->properties->{$k};
217 } else {
218 8 push @fixed_columns, $k;
219 8 push @fixed_values, $prev_elem->properties->{$k};
220 }
221 }
222 12 ▶ last ELEMENT unless !defined $table;
Branch: 1 branch
BranchTrueFalseTotal
if defined $table 8 4 12
223 # Only process one drawer. If neither NAME nor
224 # default table (from new() or in parse()
225 # call) is given, this attached table will be
226 # skipped (see below).
227 }
228 }
229 continue {
230 92 $prev_elem = $prev_elem->prev_sibling;
231 }
232 }
233 32 ▶ $table //= $default_table // $self->{table};
Condition83%5/6
$table //= $default_table // $self->{"table"}
l!l&&r!l&&!r
2426
$default_table // $self->{"table"}
l!l&&r!l&&!r
026
MC/DC0%0/3
$table //= $default_table // $self->{"table"}
$table $default_table $self->{"table"}
Truth table0%0/4
A //= B // C
A= $tableB= $default_tableC= $self->{"table"}
ABCresult
1XX1
01X1
0011
0000
234  
235 # TODO: When NAME property is encountered, check against our `table` or
236 # `tables` attributes to verify we can load this data
237 32 ▶ next unless defined $table;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 6 26 32
238 26 ▶ if (defined $self->{tables} && ref $self->{tables} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"tables"} and ref $self->{"tables"} eq "ARRAY") 8 18 26
Condition66%2/3
defined $self->{"tables"} and ref $self->{"tables"} eq "ARRAY"
!ll&&!rl&&r
1808
MC/DC50%1/2
defined $self->{"tables"} and ref $self->{"tables"} eq "ARRAY"
defined $self->{"tables"} ref $self->{"tables"} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $self->{"tables"}B= ref $self->{"tables"} eq "ARRAY"
ABresult
0X0
100
111
239 8 ▶ next unless any { $table eq $_ } @{$self->{tables}};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $table eq $_; } , @{$$self{"tables"};}) 6 2 8
240 }
241  
242 20 $t->walk(\&walk); # Process all rows in this table
243 20 walk(Org::Element::TableRow->new, 0); # Flush last row in final table
244 20 $self->{errors} = $errors;
245 20 $self->{inserted}->{$table} += $inserted ;
246 20 $self->{saved} = $saved;
247 }
248 4 return $inserted;
249 }
250  
251 };
252  
253 1 1;
254  
255 =encoding utf8
256  
257 =head1 NAME
258  
259 Org::ReadTables - Import Org Mode tables into arrays, or
260 directly into database tables
261  
262 =head1 SYNOPSIS
263  
264 use Org::ReadTables;
265  
266 my $op = Org::ReadTables->new( dbh => $dbh,
267 table => 'example',
268 tables => ['a_table']
269 );
270 # or:
271 # When called without a 'dbh' argument, saves values
272 # which can be retrieved via the 'saved' method.
273 #
274 my $op = Org::ReadTables->new( cb => \&row_callback,
275 cb_table => \&table_callback,
276 );
277 # then:
278 $op->parse( $mojo_file->slurp );
279  
280 =head1 DESCRIPTION
281  
282 L<Org::ReadTables> loads data from one or more C<Emacs> Org Mode
283 tables in an org file into a L<DBI> style database which supports the
284 C<SQL::Abstract/insert> method. The underlying C<DBD> must also
285 support the C<returning> option for insertion.
286  
287 For example, given the following .org file:
288  
289 #+NAME: LCCN_Serial
290 | LCCN | Publication | City | Start_Date | End_Date |
291 |------------+-----------------------+---------+------------+------------|
292 | sn92024097 | Adahooniłigii | Phoenix | | |
293 | sn87062098 | Arizona Daily Citizen | Tucson | | |
294 | sn84020558 | Arizona Republican | Phoenix | | 1930-11-10 |
295 | sn83045137 | Arizona Republic | Phoenix | 1930-11-11 | |
296  
297 and a database containing a table called lccn_serial, the C<parse>
298 method would insert four rows into it, in the fields whose names are
299 given in the column headings.
300  
301 The C<NAME:> org attribute specifies the table name; a default value
302 may be passed in the C<table> parameter to the C<new> method.
303 Additionally, a C<tables> array may be passed by reference against
304 which the names of such tables will be validated; table names not
305 listed will have their org tables skipped.
306  
307 Table names may also be specified in a C<Name> property (not
308 case-sensitive) in an Orgmode Drawer preceding the table. For
309 example:
310  
311 :PROPERTIES:
312 :Name: Locos
313 :END:
314 | Wheel Arrangement | Locomotive Type |
315 |-------------------+-----------------|
316 | oo-oo> | American |
317 | ooo-oo> | Mogul |
318  
319 Additionally, with the Drawer format, fixed column values may
320 optionally be specified:
321  
322 :PROPERTIES:
323 :Name: Locos
324 :Country: .us
325 :END:
326 | Wheel Arrangement | Locomotive Type |
327 |-------------------+-----------------|
328 | oo-oo> | American |
329 | ooo-oo> | Mogul |
330  
331 which would have the effect of adding a 'country' column to the right
332 of each record, all having the value '.us'.
333  
334 =head2 Pivot Tables
335  
336 NOTE: This is a future feature, not yet fully implemented.
337  
338 When it is desirable to enter data two-dimensionally, a construct like
339 this may be used:
340  
341 :PROPERTIES:
342 :Name: sizes
343 :Data: size_desc
344 | class> | A | B | C |
345 | size_code | | | |
346 |------------+-------+-------+-----|
347 | 1 | 1-2 | 22-26 | |
348 | 2 | 3-4 | 26-30 | XS |
349 | 3 | 5-6 | 30-34 | S |
350  
351 where the `Data` property determines which field (column) is assigned
352 the pivoted value. The above table would generate eight data records
353 for the `sizes` table:
354  
355 size_code='1', class='A', size_desc='1-2'
356 size_code='2', class='A', size_desc='3-4'
357 size_code='3', class='A', size_desc='5-6'
358 size_code='1', class='B', size_desc='22-26'
359 size_code='2', class='B', size_desc='26-30'
360 size_code='3', class='B', size_desc='30-34'
361 size_code='2', class='C', size_desc='XS'
362 size_code='3', class='C', size_desc='S'
363  
364 Note that no record is created for class 'C' with size_code '1' as
365 that entry in the pivot table is blank.
366  
367 =head1 ATTRIBUTES
368  
369 L<Org::ReadTables> implements the following attributes.
370  
371 =head2 inserted
372  
373 Returns a hashref, each element's key being the name of a table into
374 which rows were inserted, and its value being the number of rows
375 inserted into that table.
376  
377 =head2 errors
378  
379 Returns a reference to an array, each entry in which itself be an
380 array whose values are:
381  
382 =over 4
383  
384 =item
385 a hash (the column names and values to be inserted), and
386  
387 =item
388 the resulting error report from that insertion
389  
390 =back
391  
392 =head1 METHODS
393  
394 =head2 new
395  
396 Creates a new Org::ReadTables object. Parameters include:
397  
398 =head3 dbh
399  
400 should be an open database handle from, e.g., L<DBD::SQLite>,
401 L<Mojo::SQLite> or L<Mojo::Pg>. Each row to be saved will invoke the
402 'insert' method of this handle (or, more generally, class instance);
403 no other methods will be called, so any object that has provides
404 'insert' may be used. For each found record, the insert() method of
405 this object will be called. Note that no protection is given here
406 against invalid column names or other database errors.
407  
408 =head3 cb
409  
410 Reference to a callback function to be called for each found record,
411 in tables which are processed. Parameters passed are the name of the
412 table, and a reference to a hash of the record's column-names and
413 values. The function should return the count of records successfully
414 saved (either 0 or 1, usually).
415  
416 =head3 cb_table
417  
418 Reference to a callback function to be called at the start of
419 processing of a new table, as they are found in the orgfile.
420 The callback will be passed one argument, a hash with keys:
421  
422 =over
423  
424 =item *
425  
426 name: A string with the name of the table
427  
428 =item *
429  
430 nameref: A reference to the table-name string. This may be changed by
431 the callback.
432  
433 =item *
434  
435 columns: A reference to the array of the names of the columns in the
436 table. The contents of the referred array may be maniuplated to match
437 the actual database field names, for example.
438  
439 =item *
440  
441 fixed: A reference to a hash of fixed column key/values. This may also
442 be changed by the callback.
443  
444 =item *
445  
446 caption: The caption, if any, attached above the table itself
447  
448 =item *
449  
450 data_column: The name of the data column in a pivot table
451  
452 =back
453  
454 ...and should return a true value if the table is to be processed or
455 saved, or a false or 'undef' value to skip the table (the 'cb'
456 callback will not be called for rows in such tables).
457  
458 =head3 table
459  
460 (optional) the default table name, which will be used for all unnamed
461 tables. Use an Orgmode property C<NAME> before each table to name it,
462 as:
463  
464 #+NAME: PostalAbbrev
465 | Code | State |
466 |------+---------|
467 | AZ | Arizona |
468 | FL | Florida |
469 | KS | Kansas |
470  
471 =head3 tables
472  
473 (optional) a reference to a list of valid table names to process
474 (others will be ignored). If C<tables> is not given, C<table> should
475 be present; otherwise for input not containing an Orgmode C<NAME>
476 property, no processing will occur.
477  
478 =head2 parse
479  
480 $op->parse($text, [$default_table]);
481  
482 Parses the given text which should be in Org Mode format. It is the
483 caller's responsibility to slurp a file or other data source. The
484 optional second parameter will be used as the default name of any
485 table not having an Orgmode C<NAME> property, overriding any C<table>
486 value provided via the C<new> method.
487  
488 =head2 saved
489  
490 $op->saved();
491 $op->saved->($selected_table);
492  
493 Returns a hash of the tables read; each key is a table name, the value
494 being an array of hashes of the rows. If a table name is passed,
495 returns the array of hashes only for that particular table, or undef
496 if no such table existed in any input.
497  
498 =head2 inserted
499  
500 $op->inserted();
501 $op->inserted->($selected_table);
502  
503 With no parameter, returns a hash of the tables processed and a count
504 of rows found (and presumably inserted) in each. With a parameter,
505 returns the count of rows for that table, or undef if no such table
506 was processed.
507  
508 =head1 BUGS
509  
510 Report any issues to the author.
511  
512 =head1 AUTHOR
513  
514 William Lindley, C<wlindley@cpan.org>
515  
516 =head1 COPYRIGHT AND LICENSE
517  
518 Copyright 2025, William Lindley.
519  
520 This library is free software; you may redistribute it and/or modify it under
521 the terms of the Artistic License version 2.0.
522  
523 =head1 SEE ALSO
524  
525 L<Org::Parser>, L<https://orgmode.org/>
526  
527 =cut
« blib/lib/Org/ReadTables.pm ↑ Summary