happy about SQHelp
long time ago, i wrote a SQL helper to refactor the SQL management into the koha
code. There was a long discution about using an ORM in Koha. I was fan of the
idea but now i explored the different solutions and tried to play with them
and my conclusions are:
1. ORMs relies on schemas that are set for ORM uses. Koha don’t fit it.
2. mounths passed and there is finally no code cleanning about it.
3. Moose can provide use an easy way to abstraction but we need to deal with the DBMS by ourself.
In the meantime, i found the idea of the helper very usefull and i wrote it for my daily usage. I’me very happy about it.
Demo:
use 5.10.0;
use utf8;
use strict;
use warnings;
use open qw( :std :utf8 );
use Carp;
use YAML;
use C4::Context;
use SQHelp ':loop';
my $authorize = SQHelp::insert( C4::Context->dbh
, into => 'authorised_values'
, must => [qw( authorised_value lib )]
, also => { category => 'lecteur' }
);
my %source_for = (
Fonction => 'fonctions.txt'
, Site => 'sites.txt'
);
while ( my ($category,$source) = each %source_for ) {
open my $fh,$source or die "can't open $source: $!";
while ( <$fh> ) {
chomp;
my %entry; @entry{qw( authorised_value lib )}
= split /\|/;
$authorize->(%entry);
}
}
for_array {
say join ' | ', map { $_ || 'NULL' } @$_
} C4::Context->dbh->prepare(
q{ select * from authorised_values }
);i also have for_hash, map_hash, map_array to loop into records. The lib itself is a very short. I need a complete evening to code it but now it save my time daily
Related posts:
pour m’avoir signalé l’erreur dans le source: il manquait le split.
insert renvoie une closure qui check que tous les must soient a 1 et qui insert.