[Koha-devel] [RFC ver2 1/4] DBIx::Class - new C4::Schema object and program to manage C4::Schema::* objects

Andrew Moore andrew.moore at liblime.com
Fri Oct 31 21:05:24 CET 2008


This patch includes a misc/bin/make_schema.pl program that uses
DBIx::Class::Schema::Loader to read the database and produce some
modules that act as an interface to it. This program is designed to be
run each time we change the schema definition in kohastructure.sql and
updatedatabase.pl. It manages the classes in C4/Schema/Result/.

This patch also includes a C4::Schema object that can use those generated
classes as our interface to the database. This module is different
from my previous example in that it does not load the database schema
upon each request, but relies on the C4::Schema::Result::* objects to
describe the database.  The top portion of this module is automatically
generated by misc/bin/make_schema.pl.
---
 C4/Schema.pm            |   68 +++++++++++++++++++++++++++++++++++++++++++++
 misc/bin/make_schema.pl |   70 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 138 insertions(+), 0 deletions(-)
 create mode 100644 C4/Schema.pm
 create mode 100755 misc/bin/make_schema.pl

diff --git a/C4/Schema.pm b/C4/Schema.pm
new file mode 100644
index 0000000..61841f1
--- /dev/null
+++ b/C4/Schema.pm
@@ -0,0 +1,68 @@
+package C4::Schema;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class::Schema';
+
+__PACKAGE__->load_namespaces;
+
+
+# Created by DBIx::Class::Schema::Loader v0.04005 @ 2008-10-31 14:23:18
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:psTYqCIO60Z8q1xG4a9TaQ
+
+
+
+# Copyright 2008 LibLime, Inc.
+#
+# 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., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
+=head1 NAME
+
+C4::Schema - The DBIx::Class interface to Koha's database schema.
+
+=head1 SYNOPSIS
+
+use C4::Schema;
+our $schema = C4::Schema->connect();
+
+=head1 DESCRIPTION
+
+This module provides an object oriented interface to the Koha
+database. It uses the modles that are automatically generated into the
+C4/Schema directory and allows database interaction without writing
+SQL.
+
+=cut
+
+my $context = C4::Context->new();
+
+my $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');
+
+sub connect {
+    my $self = shift;
+    return $self->SUPER::connect( "DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port",
+                                  $db_user, $db_passwd );
+}
+
+
+1;
+
diff --git a/misc/bin/make_schema.pl b/misc/bin/make_schema.pl
new file mode 100755
index 0000000..8224fe9
--- /dev/null
+++ b/misc/bin/make_schema.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+
+# Copyright 2008 LibLime, Inc.
+#
+# 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., 59 Temple Place,
+# Suite 330, Boston, MA  02111-1307 USA
+
+=head1 NAME
+
+make_schema.pl - create database schema modules to be used by DBIx::Class
+
+=head1 SYNOPSIS
+
+make_schema.pl
+
+=head1 DESCRIPTION
+
+B<make_schema.pl> reads the schema in the current database and creates
+a directory of perl modules used to interact with that database. It's
+to be run after database schema changes are made, typically when
+checking in changes to L<updatedatabase.pl> and
+L<kohastrucuture.sql>. The files output will appear in the C4/Schema
+directory.
+
+=head1 SEE ALSO
+
+The L<C4::Schema> module uses the files created by this program.
+
+<DBIx::Class::Schema::Loader>
+
+=cut
+
+use warnings;
+use strict;
+
+use DBIx::Class::Schema::Loader qw( make_schema_at dump_to_dir:. );
+
+use C4::Context;
+
+my $context = C4::Context->new();
+
+my $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');
+
+make_schema_at(
+    'C4::Schema',
+    {   debug                 => 0,
+        use_namespaces        => 1,
+        components            => ['InflateColumn::DateTime'],
+        # really_erase_my_files => 1,
+    },
+    [ "DBI:$db_driver:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_passwd ]
+);
+
-- 
1.5.6




More information about the Koha-devel mailing list