From e3fb22aca3122b87432194fb49ddfa8a5c8c95e2 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sat, 20 Jun 2020 17:18:46 +0200 Subject: [PATCH] word: database module to connect to postgres Single configuration of authorisation details. --- Shiar_Sheet/DB.pm | 24 ++++++++++++++++++++++++ tools/mkwordlist | 18 +++++++++++++----- writer.plp | 11 ++--------- 3 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 Shiar_Sheet/DB.pm diff --git a/Shiar_Sheet/DB.pm b/Shiar_Sheet/DB.pm new file mode 100644 index 0000000..e978d80 --- /dev/null +++ b/Shiar_Sheet/DB.pm @@ -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; diff --git a/tools/mkwordlist b/tools/mkwordlist index e1729a3..4f9c703 100755 --- a/tools/mkwordlist +++ b/tools/mkwordlist @@ -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 }; diff --git a/writer.plp b/writer.plp index 7cbe9e6..493f745 100644 --- a/writer.plp +++ b/writer.plp @@ -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}) { -- 2.30.0