use DBI;
use Digest::SHA qw(sha256_hex);
use MIME::Base64;
+use POSIX;
###--------------------------------------------------------------------------
### Early utilities.
return lc $s;
}
+sub print_columns (@) {
+ my @col = reverse @_;
+ my @fmt = ();
+ my @val = ();
+ while (@col && $col[1] eq "") { splice @col, 0, 2; }
+ my ($wd, $v) = splice @col, 0, 2;
+ push @fmt, "%s"; push @val, $v;
+ while (@col) {
+ my ($wd, $v) = splice @col, 0, 2;
+ push @fmt, "%-${wd}s";
+ push @val, $v;
+ }
+ printf join(" ", reverse @fmt) . "\n", reverse @val;
+}
+
+sub fmt_time ($) {
+ my ($t) = @_;
+ return $t == -1 ? "--" : strftime "%Y-%m-%d %H:%M:%S %z", localtime $t;
+}
+
###--------------------------------------------------------------------------
### Database utilities.
die $exc;
}
+sub insert_record ($$%) {
+ my ($db, $table, %fields) = @_;
+ my @var = ();
+ my @val = ();
+
+ for my $v (keys %fields) {
+ push @var, $v;
+ push @val, $fields{$v};
+ }
+ $db->do("INSERT INTO $table (" . join(", ", @var) . ")
+ VALUES (" . join(", ", map { "?" } @var) . ")", undef, @val);
+}
###--------------------------------------------------------------------------
### Sequence numbers and tagging.
undef, $WHOCMP, $url);
unless (defined $tag) {
$tag = encode_tag(next_seq($db, "odin_shorturl_seq"));
- $db->do("INSERT INTO odin_shorturl (tag, stamp, owner, url)
- VALUES (?, ?, ?, ?)", undef,
- $tag, $NOW, $WHO, $url);
+ insert_record $db, "odin_shorturl",
+ tag => $tag, stamp => $NOW, owner => $WHO, url => $url;
}
} $db;
return $tag;
merge_hash %$new, %PASTEBIN_DEFAULTS;
xact {
$tag = encode_tag next_seq $db, "odin_pastebin_seq";
- $db->do("INSERT INTO odin_pastebin
- (tag, stamp, edithash, owner, $PASTEBIN_PROPCOLS)
- VALUES (?, ?, ?, ?, $PASTEBIN_PROPPLACES)", undef,
- $tag, $NOW, $hash, $WHO, @{$new}{@PASTEBIN_PROPS});
+ insert_record $db, "odin_pastebin",
+ tag => $tag, stamp => $NOW, edithash => $hash, owner => $WHO,
+ %$new;
} $db;
return $tag, $editkey;
}