From 717b031390d7cd17286da86eaf2eda41a3b2cd4b Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 19 Mar 2013 07:40:35 -0700 Subject: [PATCH] Bug 9869 - Add object oriented database access system to Koha. This patch adds the framework necessary to enable object oriented database access to Koha via DBIx::SearchBuilder. The advantage to SearchBuilder over DBIx::Class is the fact that SearchBuilder does not require Moose as a prerequisite. This patch includes only one table class, AuthorisedValues as a concept test. Each table can be added as neccessary. Test Plan: 1) Apply patch 2) Run t/db_dependent/SearchBuilder.t --- C4/Installer/PerlDependencies.pm | 13 +++++++ Koha/DataObject/AuthorisedValue.pm | 61 ++++++++++++++++++++++++++++++++ Koha/DataObject/AuthorisedValues.pm | 39 +++++++++++++++++++++ Koha/SearchBuilder.pm | 54 +++++++++++++++++++++++++++++ Koha/SearchBuilder/Handle.pm | 65 +++++++++++++++++++++++++++++++++++ Koha/SearchBuilder/Record.pm | 62 +++++++++++++++++++++++++++++++++ t/db_dependent/SearchBuilder.t | 38 ++++++++++++++++++++ 7 files changed, 332 insertions(+), 0 deletions(-) create mode 100644 Koha/DataObject/AuthorisedValue.pm create mode 100644 Koha/DataObject/AuthorisedValues.pm create mode 100644 Koha/SearchBuilder.pm create mode 100644 Koha/SearchBuilder/Handle.pm create mode 100644 Koha/SearchBuilder/Record.pm create mode 100755 t/db_dependent/SearchBuilder.t diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm index 83ba962..a51e692 100644 --- a/C4/Installer/PerlDependencies.pm +++ b/C4/Installer/PerlDependencies.pm @@ -634,6 +634,19 @@ our $PERL_DEPS = { 'required' => '1', 'min_ver' => '0.22', }, + 'DBIx::SearchBuilder' => { + 'usage' => 'Core', + 'required' => '1', + 'min_ver' => '1.56', + }, + 'DBIx::SearchBuilder::Record' => { + 'usage' => 'Core', + 'required' => '1', + }, + 'DBIx::SearchBuilder::Handle' => { + 'usage' => 'Core', + 'required' => '1', + }, }; 1; diff --git a/Koha/DataObject/AuthorisedValue.pm b/Koha/DataObject/AuthorisedValue.pm new file mode 100644 index 0000000..517d0b3 --- /dev/null +++ b/Koha/DataObject/AuthorisedValue.pm @@ -0,0 +1,61 @@ +package Koha::DataObject::AuthorisedValue; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +=head1 NAME + +Koha::DataObject::AuthorisedValue - Row-level class for the authorised_values table + +=cut + +use base qw/Koha::SearchBuilder::Record/; + +use Modern::Perl; + +sub _Init { + my $self = shift; + + $self->Table("authorised_values"); + + return $self->SUPER::_Init(@_); +} + +# Tell Record what the primary keys are +sub _PrimaryKeys { + return ['id']; +} + +sub _ClassAccessible { + { + id => { 'read' => 1, 'auto' => 1, 'public' => 1, }, + category => { 'read' => 1, 'auto' => 1, 'public' => 1, }, + authorised_value => { 'read' => 1, 'auto' => 1, 'public' => 1, }, + lib => { 'read' => 1, 'auto' => 1, 'public' => 1, }, + lib_opac => { 'read' => 1, 'auto' => 1, 'public' => 1, }, + mageurl => { 'read' => 1, 'auto' => 1, 'public' => 1, }, + }; +} + + +=head1 AUTHOR + +Kyle M Hall, Ekhall@bywatersolutions.comE + +=cut + +1; + +__END__ diff --git a/Koha/DataObject/AuthorisedValues.pm b/Koha/DataObject/AuthorisedValues.pm new file mode 100644 index 0000000..18ca4d7 --- /dev/null +++ b/Koha/DataObject/AuthorisedValues.pm @@ -0,0 +1,39 @@ +package Koha::DataObject::AuthorisedValues; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +=head1 NAME + +Koha::DataObject::AuthorisedValues - Table level class for the authorised_values table + +=cut + +use base qw/Koha::SearchBuilder/; + +sub _Init { + my $self = shift; + + $self->Table("authorised_values"); + + return $self->SUPER::_Init(@_); +} + +sub NewItem { + my $self = shift; + return ( Koha::DataObject::AuthorisedValue->new() ); +} + +1; diff --git a/Koha/SearchBuilder.pm b/Koha/SearchBuilder.pm new file mode 100644 index 0000000..a41be5c --- /dev/null +++ b/Koha/SearchBuilder.pm @@ -0,0 +1,54 @@ +package Koha::SearchBuilder; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +=head1 NAME + +Koha::SearchBuilder - Koha specific class derived from DBIx::SearchBuilder + +Base class for plural Koha::DataObject classes. Subclasses must provide the following methods + +B<_Init> - Initializes object + +B - Returns an instance of the singular version of this class. + +=head1 FUNCTIONS + +=cut + +use base qw/DBIx::SearchBuilder/; + +use Modern::Perl; + +sub _Init { + my $self = shift; + + $self->SUPER::_Init(@_); + + my $handle = Koha::SearchBuilder::Handle->new(); + $handle->Connect(); + $self->_Handle( $handle ); +} + +=head1 AUTHOR + +Kyle M Hall, Ekhall@bywatersolutions.comE + +=cut + +1; + +__END__ diff --git a/Koha/SearchBuilder/Handle.pm b/Koha/SearchBuilder/Handle.pm new file mode 100644 index 0000000..2abd710 --- /dev/null +++ b/Koha/SearchBuilder/Handle.pm @@ -0,0 +1,65 @@ +package Koha::SearchBuilder::Handle; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +=head1 NAME + +Koha::SearchBuilder::Handle - Koha specific class derived from DBIx::SearchBuilder::Handle + +=cut + +use base qw/DBIx::SearchBuilder::Handle/; + +use Modern::Perl; + +use C4::Context; + +sub Connect { + my $self = shift; + my %args = (@_); + + my $driver = C4::Context::db_scheme2dbi( C4::Context->config('db_scheme') ); + + $self->SUPER::Connect( + Driver => $driver, + User => C4::Context->config('user'),, + Password => C4::Context->config('pass'), + Host => C4::Context->config('hostname'), + Database => C4::Context->config('database'), + %args, + ); + + if ( $driver eq 'mysql' ) { + my $version = $self->DatabaseVersion; + ($version) = $version =~ /^(\d+\.\d+)/; + $self->dbh->do("SET NAMES 'utf8'") if $version >= 4.1; + } + elsif ( $driver eq 'Pg' ) { + my $version = $self->DatabaseVersion; + ($version) = $version =~ /^(\d+\.\d+)/; + $self->dbh->do("SET bytea_output = 'escape'") if $version >= 9.0; + } +} + +=head1 AUTHOR + +Kyle M Hall, Ekhall@bywatersolutions.comE + +=cut + +1; + +__END__ diff --git a/Koha/SearchBuilder/Record.pm b/Koha/SearchBuilder/Record.pm new file mode 100644 index 0000000..4653de3 --- /dev/null +++ b/Koha/SearchBuilder/Record.pm @@ -0,0 +1,62 @@ +package Koha::SearchBuilder::Record; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +=head1 NAME + +Koha::SearchBuilder::Record - Koha specific class derived from DBIx::SearchBuilder::Record + +Base class for singular (row-level) Koha::DataObject classes. Subclasses must provide the following methods + +B<_Init> - Initializes object + +B<_PrimaryKeys> - Returns an array of the primary keys for this classes table + +Subclasses may override the following methods: + +B<_ClassAccessible> - Describes the table and allows for accessor functions ( e.g. $record->fieldname() ) + +=head1 FUNCTIONS + +=cut + +use base qw/DBIx::SearchBuilder::Record/; + +use Modern::Perl; + +use Koha::SearchBuilder::Handle; + +sub _Init { + my $self = shift; + + $self->SUPER::_Init(@_); + + my $handle = Koha::SearchBuilder::Handle->new(); + $handle->Connect(); + + $self->_Handle( $handle ); +} + +=head1 AUTHOR + +Kyle M Hall, Ekhall@bywatersolutions.comE + +=cut + +1; + +__END__ + diff --git a/t/db_dependent/SearchBuilder.t b/t/db_dependent/SearchBuilder.t new file mode 100755 index 0000000..a5f98c0 --- /dev/null +++ b/t/db_dependent/SearchBuilder.t @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 8; + +BEGIN { + use_ok('Koha::SearchBuilder'); + use_ok('Koha::SearchBuilder::Handle'); + use_ok('Koha::SearchBuilder::Record'); + use_ok('Koha::DataObject::AuthorisedValue'); + use_ok('Koha::DataObject::AuthorisedValues'); +} + +my $av = Koha::DataObject::AuthorisedValue->new(); + +$av->Create( + 'category' => 'Foo', + 'authorised_value' => 'Bar', + 'lib' => 'libFoo', + 'lib_opac' => 'opacFoo', +); + +my $avs = Koha::DataObject::AuthorisedValues->new(); +$avs->Limit( FIELD => "category", VALUE => "Foo" ); +my $record = $avs->Next(); +ok( $record->authorised_value() eq "Bar", "Create, Limit, Next, and field value getter working correctly"); + +my $av_id = $record->id(); + +$av->LoadById($av_id); +ok( $av->lib_opac() eq 'opacFoo', "LoadById functions correctly" ); + +$record->Delete(); + +$av->LoadById($av_id); +ok( !defined( $av->lib_opac() ), "Delete functions correctly" ); -- 1.7.2.5