chiark / gitweb /
Store Unix timestamps in the database, rather than SQL ones.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 24 Jul 2015 17:33:03 +0000 (18:33 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 24 Jul 2015 18:16:54 +0000 (19:16 +0100)
This will make doing calculations with them easier, and removes some
really annoying database-portability problems.

bin/pastebin.userv
bin/shorturl.userv
lib/Odin.pm
sql/setup-pastebin.sql
sql/setup-shorturl.sql

index 3c87cb56cebff2dbc8d2fe3df26ee90db425ba08..ed7ba7a147ff0ac300a9cf555f620e9c2002ce06 100755 (executable)
@@ -42,11 +42,10 @@ EOF
   @ARGV == 0 or Odin::fail "usage: list";
   my $db = Odin::open_db;
   for my $r (@{$db->selectall_arrayref
-               ("SELECT " . Odin::sql_timestamp($db, "stamp") .
-                     ", tag, lang, title
+               ("SELECT tag, stamp, lang, title
                  FROM odin_pastebin WHERE owner = ?
                  ORDER BY stamp", undef, $Odin::WHO)}) {
-    my ($stamp, $tag, $lang, $title) = @$r;
+    my ($tag, $stamp, $lang, $title) = @$r;
     my $t = strftime "%Y-%m-%d %H:%M:%S %z", localtime $stamp;
     printf "%-25s  %-12s  %-16s  %s\n",
       $t, $tag, $lang, encode locale => $title;
index 489168d06a885a779ae00e63260ebb4aeb0c9b57..edad9f6ccf93021b7a4dd8b26e5ae41302e530a0 100755 (executable)
@@ -23,10 +23,10 @@ EOF
   @ARGV == 0 or Odin::fail "usage: list";
   my $db = Odin::open_db;
   for my $r (@{$db->selectall_arrayref
-      ("SELECT " . Odin::sql_timestamp($db, "stamp") . ", tag, url
+      ("SELECT tag, stamp, url
        FROM odin_shorturl WHERE owner = ?
        ORDER BY stamp", undef, $Odin::WHO)}) {
-    my ($stamp, $tag, $url) = @$r;
+    my ($tag, $stamp, $url) = @$r;
     my $t = strftime "%Y-%m-%d %H:%M:%S %z", localtime $stamp;
     printf "%-25s  %-12s  %s\n", $t, $tag, $url;
   }
index a9bff5ad67bcaf05c1269f39f6f9eaaa837dcbfd..551c51f0e83cc7248baa176513a8df201c1af71b 100644 (file)
@@ -99,12 +99,10 @@ sub open_db (@) {
   my $drv = $db->{Driver}{Name};
   if ($drv eq "Pg") {
     $db->{private_odin_retry_p} = sub { $db->state =~ /^40[0P]01$/ };
-    $db->{private_odin_unixstamp} = sub { "extract(epoch from $_[0])" };
   } elsif ($drv eq "SQLite") {
     $db->{private_odin_retry_p} = sub { $db->err == 5 };
-    $db->{private_odin_unixstamp} = sub { "strftime('%s', $_[0])" };
   } else {
-    fail "unsupported database driver `$drv' (patches welcome)", undef;
+    $db->{private_odin_retry_p} = sub { 0 };
   }
 
   return $db;
@@ -131,10 +129,6 @@ sub xact (&$) {
   die $exc;
 }
 
-sub sql_timestamp ($$) {
-  my ($db, $col) = @_;
-  return $db->{private_odin_unixstamp}->($col);
-}
 
 ###--------------------------------------------------------------------------
 ### Sequence numbers and tagging.
@@ -267,8 +261,9 @@ sub new_shorturl ($) {
        undef, $WHOCMP, $url);
     unless (defined $tag) {
       $tag = encode_tag(next_seq($db, "odin_shorturl_seq"));
-      $db->do("INSERT INTO odin_shorturl (tag, owner, url) VALUES (?, ?, ?)",
-             undef, $tag, $WHO, $url);
+      $db->do("INSERT INTO odin_shorturl (tag, stamp, owner, url)
+              VALUES (?, ?, ?, ?)", undef,
+             $tag, $NOW, $WHO, $url);
     }
   } $db;
   return $tag;
@@ -331,9 +326,9 @@ sub new_pastebin (\%) {
   xact {
     $tag = encode_tag next_seq $db, "odin_pastebin_seq";
     $db->do("INSERT INTO odin_pastebin
-              (tag, edithash, owner, $PASTEBIN_PROPCOLS)
-            VALUES (?, ?, ?, $PASTEBIN_PROPPLACES)", undef,
-           $tag, $hash, $WHO, @{$new}{@PASTEBIN_PROPS});
+              (tag, stamp, edithash, owner, $PASTEBIN_PROPCOLS)
+            VALUES (?, ?, ?, ?, $PASTEBIN_PROPPLACES)", undef,
+           $tag, $NOW, $hash, $WHO, @{$new}{@PASTEBIN_PROPS});
   } $db;
   return $tag, $editkey;
 }
index b4e99a15e42c974bb1d2bcd10ee3182f70487838..391f8be96fa7a3dcd2980dca7ac5a5a1e48e394f 100644 (file)
@@ -23,7 +23,7 @@ insert into odin_pastebin_seq (seq) values (10000);
 
 create table odin_pastebin
        (tag varchar(16) primary key,
-        stamp timestamp not null default current_timestamp,
+        stamp bigint not null,
         edithash varchar(128) not null,
         owner varchar(64) not null,
         title varchar(128) not null,
index eaddf90da13f477313f419401fc0a10144b10879..dee90a37b845aa607d40d28f230cbb1f1d4124fb 100644 (file)
@@ -17,7 +17,7 @@ insert into odin_shorturl_seq (seq) values (10000);
 
 create table odin_shorturl
        (tag varchar(16) primary key,
-        stamp timestamp not null default current_timestamp,
+        stamp bigint not null,
         owner varchar(64) not null,
         url text not null);
 create index odin_shorturl_by_owner on odin_shorturl (owner);