Bugzilla – Attachment 19141 Details for
Bug 8798
Add the use of DBIx::Class
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 8798 - Moving code to Koha::Database and adding tests
Bug-8798---Moving-code-to-KohaDatabase-and-adding-.patch (text/plain), 10.43 KB, created by
Chris Cormack
on 2013-06-18 22:22:19 UTC
(
hide
)
Description:
Bug 8798 - Moving code to Koha::Database and adding tests
Filename:
MIME Type:
Creator:
Chris Cormack
Created:
2013-06-18 22:22:19 UTC
Size:
10.43 KB
patch
obsolete
>From d14434bc00353ed2af69d99758813edad5ab78e3 Mon Sep 17 00:00:00 2001 >From: Chris Cormack <chrisc@catalyst.net.nz> >Date: Wed, 19 Jun 2013 10:17:21 +1200 >Subject: [PATCH] Bug 8798 - Moving code to Koha::Database and adding tests > >- Fixing a bug .. ping does not exist we need to use connected >--- > C4/Context.pm | 127 ---------------------------- > Koha/Database.pm | 185 +++++++++++++++++++++++++++++++++++++++++ > t/db_dependent/Koha_Database.t | 27 ++++++ > 3 files changed, 212 insertions(+), 127 deletions(-) > create mode 100644 Koha/Database.pm > create mode 100644 t/db_dependent/Koha_Database.t > >diff --git a/C4/Context.pm b/C4/Context.pm >index 581c5a3..4e63a70 100644 >--- a/C4/Context.pm >+++ b/C4/Context.pm >@@ -19,7 +19,6 @@ package C4::Context; > use strict; > use warnings; > use vars qw($VERSION $AUTOLOAD $context @context_stack $servers $memcached $ismemcached); >-$ENV{'DBIC_DONT_VALIDATE_RELS'} = 1; # FIXME once the DBIx schema has its schema adjusted we should remove this > BEGIN { > if ($ENV{'HTTP_USER_AGENT'}) { > require CGI::Carp; >@@ -99,7 +98,6 @@ BEGIN { > } > > use DBI; >-require Koha::Schema; > use ZOOM; > use XML::Simple; > use C4::Boolean; >@@ -988,131 +986,6 @@ sub _new_queryparser { > return; > } > >-# _new_schema >-# Internal helper function (not a method!). This creates a new >-# database connection from the data given in the current context, and >-# returns it. >-sub _new_schema { >- my $db_driver; >- if ($context->config("db_scheme")){ >- $db_driver=db_scheme2dbi($context->config("db_scheme")); >- }else{ >- $db_driver="mysql"; >- } >- >- my $db_name = $context->config("database"); >- my $db_host = $context->config("hostname"); >- my $db_port = $context->config("port") || ''; >- my $db_user = $context->config("user"); >- my $db_passwd = $context->config("pass"); >- my $schema = Koha::Schema->connect( >- "DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port", >- $db_user, $db_passwd ); >- return $schema; >-} >- >-=head2 schema >- >- $schema = C4::Context->schema; >- >-Returns a database handle connected to the Koha database for the >-current context. If no connection has yet been made, this method >-creates one, and connects to the database. >- >-This database handle is cached for future use: if you call >-C<C4::Context-E<gt>schema> twice, you will get the same handle both >-times. If you need a second database handle, use C<&new_schema> and >-possibly C<&set_schema>. >- >-=cut >- >-sub schema { >- my $self = shift; >- my $sth; >- >- if ( defined( $context->{"schema"} ) && $context->{"schema"}->ping() ) { >- return $context->{"schema"}; >- } >- >- # No database handle or it died . Create one. >- $context->{"schema"} = &_new_schema(); >- >- return $context->{"schema"}; >-} >- >-=head2 new_schema >- >- $schema = C4::Context->new_schema; >- >-Creates a new connection to the Koha database for the current context, >-and returns the database handle (a C<DBI::db> object). >- >-The handle is not saved anywhere: this method is strictly a >-convenience function; the point is that it knows which database to >-connect to so that the caller doesn't have to know. >- >-=cut >- >-#' >-sub new_schema { >- my $self = shift; >- >- return &_new_schema(); >-} >- >-=head2 set_schema >- >- $my_schema = C4::Connect->new_schema; >- C4::Connect->set_schema($my_schema); >- ... >- C4::Connect->restore_schema; >- >-C<&set_schema> and C<&restore_schema> work in a manner analogous to >-C<&set_context> and C<&restore_context>. >- >-C<&set_schema> saves the current database handle on a stack, then sets >-the current database handle to C<$my_schema>. >- >-C<$my_schema> is assumed to be a good database handle. >- >-=cut >- >-sub set_schema { >- my $self = shift; >- my $new_schema = shift; >- >- # Save the current database handle on the handle stack. >- # We assume that $new_schema is all good: if the caller wants to >- # screw himself by passing an invalid handle, that's fine by >- # us. >- push @{$context->{"schema_stack"}}, $context->{"schema"}; >- $context->{"schema"} = $new_schema; >-} >- >-=head2 restore_schema >- >- C4::Context->restore_schema; >- >-Restores the database handle saved by an earlier call to >-C<C4::Context-E<gt>set_schema>. >- >-=cut >- >-sub restore_schema { >- my $self = shift; >- >- if ($#{$context->{"schema_stack"}} < 0) { >- # Stack underflow >- die "SCHEMA stack underflow"; >- } >- >- # Pop the old database handle and set it. >- $context->{"schema"} = pop @{$context->{"schema_stack"}}; >- >- # FIXME - If it is determined that restore_context should >- # return something, then this function should, too. >-} >- > =head2 marcfromkohafield > > $dbh = C4::Context->marcfromkohafield; >diff --git a/Koha/Database.pm b/Koha/Database.pm >new file mode 100644 >index 0000000..c10a61a >--- /dev/null >+++ b/Koha/Database.pm >@@ -0,0 +1,185 @@ >+package Koha::Database; >+ >+# Copyright 2013 Catalyst IT >+# chrisc@catalyst.net.nz >+# >+# 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 3 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::Database >+ >+=head1 SYNOPSIS >+ >+ use Koha::Database; >+ my $database = Koha::Database->new(); >+ my $schema = $database->schema(); >+ >+=head1 FUNCTIONS >+ >+=cut >+ >+$ENV{'DBIC_DONT_VALIDATE_RELS'} = 1; # FIXME once the DBIx schema has its schema adjusted we should remove this >+ >+use Modern::Perl; >+use Carp; >+use C4::Context; >+use Koha::Schema; >+use base qw(Class::Accessor); >+ >+__PACKAGE__->mk_accessors(qw( )); >+ >+# _new_schema >+# Internal helper function (not a method!). This creates a new >+# database connection from the data given in the current context, and >+# returns it. >+sub _new_schema { >+ my $db_driver; >+ my $context = C4::Context->new(); >+ if ( $context->config("db_scheme") ) { >+ $db_driver = $context->db_scheme2dbi( $context->config("db_scheme") ); >+ } >+ else { >+ $db_driver = "mysql"; >+ } >+ >+ my $db_name = $context->config("database"); >+ my $db_host = $context->config("hostname"); >+ my $db_port = $context->config("port") || ''; >+ my $db_user = $context->config("user"); >+ my $db_passwd = $context->config("pass"); >+ my $schema = Koha::Schema->connect( >+ "DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port", >+ $db_user, $db_passwd ); >+ return $schema; >+} >+ >+=head2 schema >+ >+ $schema = $database->schema; >+ >+Returns a database handle connected to the Koha database for the >+current context. If no connection has yet been made, this method >+creates one, and connects to the database. >+ >+This database handle is cached for future use: if you call >+C<$database-E<gt>schema> twice, you will get the same handle both >+times. If you need a second database handle, use C<&new_schema> and >+possibly C<&set_schema>. >+ >+=cut >+ >+sub schema { >+ my $self = shift; >+ my $sth; >+ if ( defined( $self->{"schema"} ) && $self->{"schema"}->storage->connected() ) { >+ return $self->{"schema"}; >+ } >+ >+ # No database handle or it died . Create one. >+ $self->{"schema"} = &_new_schema(); >+ >+ return $self->{"schema"}; >+} >+ >+=head2 new_schema >+ >+ $schema = $database->new_schema; >+ >+Creates a new connection to the Koha database for the current context, >+and returns the database handle (a C<DBI::db> object). >+ >+The handle is not saved anywhere: this method is strictly a >+convenience function; the point is that it knows which database to >+connect to so that the caller doesn't have to know. >+ >+=cut >+ >+#' >+sub new_schema { >+ my $self = shift; >+ >+ return &_new_schema(); >+} >+ >+=head2 set_schema >+ >+ $my_schema = $database->new_schema; >+ $database->set_schema($my_schema); >+ ... >+ $database->restore_schema; >+ >+C<&set_schema> and C<&restore_schema> work in a manner analogous to >+C<&set_context> and C<&restore_context>. >+ >+C<&set_schema> saves the current database handle on a stack, then sets >+the current database handle to C<$my_schema>. >+ >+C<$my_schema> is assumed to be a good database handle. >+ >+=cut >+ >+sub set_schema { >+ my $self = shift; >+ my $new_schema = shift; >+ >+ # Save the current database handle on the handle stack. >+ # We assume that $new_schema is all good: if the caller wants to >+ # screw himself by passing an invalid handle, that's fine by >+ # us. >+ push @{ $self->{"schema_stack"} }, $self->{"schema"}; >+ $self->{"schema"} = $new_schema; >+} >+ >+=head2 restore_schema >+ >+ $database->restore_schema; >+ >+Restores the database handle saved by an earlier call to >+C<$database-E<gt>set_schema>. >+ >+=cut >+ >+sub restore_schema { >+ my $self = shift; >+ >+ if ( $#{ $self->{"schema_stack"} } < 0 ) { >+ >+ # Stack underflow >+ die "SCHEMA stack underflow"; >+ } >+ >+ # Pop the old database handle and set it. >+ $self->{"schema"} = pop @{ $self->{"schema_stack"} }; >+ >+ # FIXME - If it is determined that restore_context should >+ # return something, then this function should, too. >+} >+ >+=head2 EXPORT >+ >+None by default. >+ >+ >+=head1 AUTHOR >+ >+Chris Cormack, E<lt>chrisc@catalyst.net.nzE<gt> >+ >+=cut >+ >+1; >+ >+__END__ >diff --git a/t/db_dependent/Koha_Database.t b/t/db_dependent/Koha_Database.t >new file mode 100644 >index 0000000..a030078 >--- /dev/null >+++ b/t/db_dependent/Koha_Database.t >@@ -0,0 +1,27 @@ >+#!/usr/bin/perl >+# >+ >+use strict; >+use warnings; >+ >+use Test::More tests => 9; >+ >+BEGIN { >+ use_ok('Koha::Database'); >+} >+ >+my $database; >+ok( $database = Koha::Database->new(), 'Created Koha::Database Object' ); >+ >+my $schema; >+ok( $schema = $database->schema(), 'Get a schema' ); >+my $dbh; >+ok( $dbh = $schema->storage->dbh(), 'Get an old fashioned DBI dbh handle' ); >+ok( $schema->storage->connected(), 'Check our db connection is active' ); >+ok( $schema = $database->schema(), 'Try and get the same schema' ); >+ >+my $new_schema; >+ok( $new_schema = $database->new_schema(), 'Try to get a new schema' ); >+ok( $database->set_schema($new_schema), 'Switch to new schema' ); >+ok( $database->restore_schema(), 'Switch back' ); >+ >-- >1.8.1.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 8798
:
12403
|
12405
|
12429
|
12439
|
12442
|
12443
|
12453
|
12477
|
12478
|
12480
|
12481
|
12506
|
12520
|
12521
|
12538
|
17581
|
17582
|
17583
|
17584
|
17585
|
17586
|
19134
|
19137
|
19138
|
19139
|
19140
| 19141 |
19248
|
19249
|
19250
|
19251