word: database module to connect to postgres
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 20 Jun 2020 15:18:46 +0000 (17:18 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Tue, 20 Oct 2020 20:49:11 +0000 (22:49 +0200)
Single configuration of authorisation details.

Shiar_Sheet/DB.pm [new file with mode: 0644]
tools/mkwordlist
writer.plp

diff --git a/Shiar_Sheet/DB.pm b/Shiar_Sheet/DB.pm
new file mode 100644 (file)
index 0000000..e978d80
--- /dev/null
@@ -0,0 +1,24 @@
+package Shiar_Sheet::DB;
+
+use 5.014;
+use warnings;
+use DBIx::Simple;
+
+our $VERSION = '1.00';
+
+my @dbinfo = (
+       'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
+) or die "database not configured\n";
+our $db;
+
+sub connect {
+       return $db if $db and $db->dbh->ping;
+       $db = DBIx::Simple->new(@dbinfo[0..2], {
+               RaiseError => 1,
+               pg_enable_utf8 => 1,
+       });
+       $db->abstract->{array_datatypes}++;
+       return $db;
+}
+
+1;
index e1729a380bf99b624bc1369e0d3633a14f9d1d4a..4f9c70386905400025a3e4839e76b90d9d976f6d 100755 (executable)
@@ -1,5 +1,13 @@
-#!/bin/sh
-echo 'use utf8;'
-psql sheet -XAt -c "
-       SELECT json_object_agg(coalesce(id::text, ''), forms) FROM _cat_words
-" | sed 's/ : / => /g'
+#!/usr/bin/env perl
+use 5.014;
+use warnings;
+
+BEGIN { push @INC, '.' }
+use Shiar_Sheet::DB;
+use open ':std' => ':utf8';
+my $db = Shiar_Sheet::DB->connect;
+
+say 'use utf8;';
+
+use Data::Dump 'pp';
+say pp { $db->select(_cat_words => "coalesce(id::text, ''), forms")->map };
index 7cbe9e659b78d3e3b68f95fd8c4d7bcb9aa14941..493f74534b3f5961253b6b4fe224a4e5451363fc 100644 (file)
@@ -13,16 +13,9 @@ EOT
 use List::Util qw( pairs pairkeys );
 
 my $db = eval {
-       my @dbinfo = (
-               'DBI:Pg:dbname=sheet;host=localhost', 'sheetadmin', 'fairuse',
-       ) or die "database not configured\n";
-       require DBIx::Simple;
-       DBIx::Simple->new(@dbinfo[0..2], {
-               RaiseError => 1,
-               pg_enable_utf8 => 1,
-       });
+       require Shiar_Sheet::DB;
+       Shiar_Sheet::DB->connect;
 } or Abort('Database error', 501, $@);
-$db->abstract->{array_datatypes}++;
 
 my $user = eval {
        if (defined $post{username}) {