-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_db
executable file
·326 lines (287 loc) · 9.63 KB
/
edit_db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/local/bin/perl
use strict;
use warnings;
use Morsulus::Ordinary::Classic;
use Morsulus::Ordinary::Legacy;
use Getopt::Euclid;
use IO::Prompter [-v];
use File::Temp;
use 5.16.0;
my $ord = Morsulus::Ordinary::Classic->new(dbname => $ARGV{-db});
while ($_ = prompt "Search by...", "-s", -menu => [ qw/ reg_owner_name reg_id blazon description exit / ])
{
if ($_ eq 'reg_owner_name') { search_by_owner_name(); next; }
if ($_ eq 'reg_id') { search_by_reg_id(); next; }
if ($_ eq 'blazon') { search_by_blazon(); next; }
if ($_ eq 'description') { search_by_description(); next; }
last;
}
sub search_by_owner_name
{
my $pat = prompt("Enter name pattern:");
return if $pat eq '';
my @regs = $ord->Registration->search({reg_owner_name => { 'regexp', $pat }});
my %regs = map { $ord->get_registration($_)->canonicalize->to_string => $_ } @regs;
while ((my $reg_txt = prompt -number, "Select an item:", -menu => ["done", keys %regs]) ne "done")
{
edit_entry($regs{$reg_txt});
%regs = map { $ord->get_registration($_)->canonicalize->to_string => $_ } @regs;
}
}
sub search_by_reg_id
{
my $pat = prompt("Enter reg_id(s):");
return if $pat eq '';
my @reg_ids = split(/\D+/, $pat);
my @regs = $ord->Registration->search({reg_id => [ @reg_ids ] });
my %regs = map { $ord->get_registration($_)->canonicalize->to_string => $_ } @regs;
while ((my $reg_txt = prompt -number, "Select an item:", -menu => ["done", keys %regs]) ne "done")
{
edit_entry($regs{$reg_txt});
%regs = map { $ord->get_registration($_)->canonicalize->to_string => $_ } @regs;
}
}
sub search_by_blazon
{
my $pat = prompt "Enter blazon pattern:";
return if $pat eq '';
my @regs = $ord->Registration->search({
'text_blazon.blazon' => { 'regexp', $pat },
},
{ join => 'text_blazon' });
my %regs = map { $ord->get_registration($_)->canonicalize->to_string => $_ } @regs;
while ((my $reg_txt = prompt -number, "Select an item:", -menu => ["done", keys %regs]) ne "done")
{
edit_entry($regs{$reg_txt});
}
}
sub search_by_description
{
say "search by description";
}
sub edit_entry
{
my ($item) = @_;
display_registration($item);
while ((my $field = prompt "edit which field? ", "-s") ne 'x')
{
print "edit field $field\n";
edit_reg_owner_name($item) if $field eq 'a';
edit_dates($item) if $field eq 'b';
edit_action($item) if $field eq 'c';
edit_text($item) if $field eq 'd';
edit_notes($item) if $field eq 'e';
edit_descs($item) if $field eq 'f';
$item->update if $field eq 's';
display_registration($item);
}
}
sub display_registration
{
my ($item) = @_;
my $classic = $ord->get_registration($item)->canonicalize;
print "\na) reg_owner_name: ", $classic->name, "\n",
"b) dates: ", $classic->source, "\n",
"c) action: ", $classic->type, "\n",
"d) text: ", $classic->text, "\n",
"e) notes: ", $classic->notes, "\n",
"f) descs: ", $classic->descs, "\n",
"s) save edits \n",
"x) done", "\n";
}
sub display_dates
{
my ($item) = @_;
print "\na) registration date: ", $item->registration_date->date, "\n",
"b) registration kingdom: ", $item->registration_kingdom->kingdom_id, "\n",
"c) release date: ", $item->release_date->date, "\n",
"d) release kingdom: ", $item->release_kingdom->kingdom_id, "\n",
"x) done\n";
}
sub edit_text
{
my ($item) = @_;
if (defined $item->text_blazon_id)
{
print "editing blazon not implemented\n";
return;
}
my $text_name = $item->text_name->name;
while (1)
{
my $new_text_name = prompt "\ncurrentvalue: '", $text_name || '<none>',
"'\npress ENTER to leave alone\nenter - to blank out\nenter new text_name: ";
return if $new_text_name eq '';
return $item->text_name('') if $new_text_name eq '-';
my $new_name = $ord->add_name($new_text_name);
return $item->text_name($new_name);
}
}
sub edit_reg_owner_name
{
my ($item) = @_;
my $reg_owner_name = $item->reg_owner_name->name;
while (1)
{
my $new_reg_owner_name = prompt "\ncurrentvalue: '", $reg_owner_name || '<none>',
"'\npress ENTER to leave alone\nenter new reg_owner_name: ";
return if $new_reg_owner_name eq '';
my $new_name = $ord->add_name($new_reg_owner_name);
return $item->reg_owner_name($new_name);
}
}
sub edit_action
{
my ($item) = @_;
my $action = $item->action;
my @actions = $ord->Action->search(undef, { order_by => 'action_id' });
my %actions = map { $_->action_id => $_} @actions; # keyed on name
while (1)
{
print join("\n", map { join(': ', $_->action_id || '-', $_->action_description || '<none>') } @actions);
my $newaction = prompt "\ncurrentvalue: '", $action || '<none>',
"'\npress ENTER to leave alone\n\nor select code from list: ";
return if $newaction eq '';
return $item->action($actions{$newaction}) if exists $actions{$newaction};
}
}
sub edit_dates
{
my ($item) = @_;
display_dates($item);
while ((my $field = prompt "edit which part? ", "-s") ne 'x')
{
$item->registration_date(edit_date($item->registration_date)) if $field eq 'a';
$item->release_date(edit_date($item->release_date)) if $field eq 'c';
$item->registration_kingdom(edit_kingdom($item->registration_kingdom)) if $field eq 'b';
$item->release_kingdom(edit_kingdom($item->release_kingdom)) if $field eq 'd';
display_dates($item);
}
}
sub edit_date
{
my ($date) = @_;
while (1)
{
my $newdate = prompt "\ncurrent value: '", $date->date, "'\npress ENTER to leave alone\nenter - to blank out\nenter new date as yyyymm: ";
return $date if $newdate eq '';
$newdate = '' if $newdate eq '-';
my $checked_date = $ord->Date->find({ date => $newdate });
return $checked_date if defined $checked_date;
print "bad input\n";
}
}
sub edit_descs
{
my ($item) = @_;
my $tmp_db = File::Temp->new(SUFFIX => '.db');
print $tmp_db $ord->get_registration($item)->to_string;
system 'index', "$tmp_db";
# read results into new item thingy
seek $tmp_db, 0, 0;
my $newitem = <$tmp_db>;
chomp $newitem;
my $newentry = Morsulus::Ordinary::Legacy->from_string($newitem);
my ($blazon) = $ord->add_blazon($newentry->text);
$ord->drop_descs($blazon);
$ord->add_desc($_, $blazon) for $newentry->split_descs;
}
sub edit_kingdom
{
my ($kingdom) = @_;
my @kingdoms = $ord->Kingdom->search(undef, { order_by => 'kingdom_name_nominative' });
my %kingdoms = map { $_->kingdom_id => $_} @kingdoms; # keyed on name
while (1)
{
print join("\n", map { join(': ', $_->kingdom_id || '-', $_->kingdom_name_nominative || '<none>') } @kingdoms);
my $newkingdom = prompt "\ncurrentvalue: '", $kingdom->kingdom_id || '<none>',
"\npress ENTER to leave alone\nenter - to blank out\nor select code from list: ", "-s";
return $kingdom if $newkingdom eq '';
return $kingdoms{$newkingdom} if exists $kingdoms{$newkingdom};
return $kingdoms{''} if $newkingdom eq '-';
}
}
sub check_date
{
my ($date) = @_;
return 1 if $date eq '';
return unless $date =~ /^([0-9]{4})([0-9]{2})$/;
my ($year, $month) = ($1, $2);
return unless $year > 1965;
return unless $month >= 1 && $month <= 12;
return 1;
}
sub edit_notes
{
my ($item) = @_;
while (1)
{
my @notes = $item->notes;
print "a) add a new note\n",
"x) exit\n",
"or select a note to delete it\n";
print $_+1, ") ", $notes[$_]->note_text, "\n" for 0..$#notes;
my $action = prompt "Enter action: ";
return if $action eq 'x';
add_note($item) if $action eq 'a';
next if $action eq 'a';
next unless $action+0 eq $action;
next unless $action > 0;
next unless $action <= @notes;
$ord->drop_note($item, $notes[$action-1])
}
}
sub add_note
{
my ($item) = @_;
while (1)
{
my $note_text = prompt "Enter new note or note pattern: ";
return if $note_text eq '';
my @notes = $ord->Note->search({note_text => { 'regexp', $note_text }});
if (@notes == 0)
{
my $action = prompt "Note not found in existing notes; add new note?", "-y";
next unless $action eq 'y';
$ord->add_note($item, $note_text);
return;
}
elsif (@notes == 1)
{
my $action = prompt "Note found in existing notes; add new note?", "-y";
next unless $action eq 'y';
$ord->add_note($item, $note_text);
return;
}
$note_text = prompt "Pick an existing note or press ENTER to start over: ",
-menu => [ "done", map { $_->note_text } @notes ];
next if $note_text eq 'done';
$ord->add_note($item, $note_text);
return;
}
}
=pod
while (prompt -num, 'Enter a number') {
say "You entered: $_";
}
my $selection
= prompt 'Choose wisely...', -menu => {
wealth => [ 'moderate', 'vast', 'incalculable' ],
health => [ 'hale', 'hearty', 'rude' ],
wisdom => [ 'cosmic', 'folk' ],
}, '>';
__END__
=head1 NAME
edit_db - edit ordinary database
=head1 SYNOPSIS
edit_db -db mydb.db
=head1 REQUIRED
=over
=item -db <file>
SQLite database file to be created or updated with Ordinary entries.
=for Euclid:
file.type: writable
=back
=head1 OPTIONS
=over
=back