pjs/webtools/litmus/Litmus/DBI.pm

101 строка
2.8 KiB
Perl
Исходник Обычный вид История

# -*- mode: cperl; c-basic-offset: 8; indent-tabs-mode: nil; -*-
=head1 COPYRIGHT
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License
# at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and
# limitations under the License.
#
# The Original Code is Litmus.
#
# The Initial Developer of the Original Code is
# the Mozilla Corporation.
2006-01-25 20:03:40 +03:00
# Portions created by the Initial Developer are Copyright (C) 2006
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Chris Cooper <ccooper@deadsquid.com>
# Zach Lipton <zach@zachlipton.com>
#
# ***** END LICENSE BLOCK *****
=cut
package Litmus::DBI;
use strict;
use warnings;
use Litmus::Config;
use Litmus::Error;
use Memoize;
use base 'Class::DBI::mysql';
my $dsn = "dbi:mysql:$Litmus::Config::db_name:$Litmus::Config::db_host";
our %column_aliases;
Litmus::DBI->set_db('Main',
$dsn,
$Litmus::Config::db_user,
2006-01-15 08:20:06 +03:00
$Litmus::Config::db_pass,
{AutoCommit=>1}
);
Litmus::DBI->autoupdate(1);
# In some cases, we have column names that make sense from a database perspective
# (i.e. subgroup_id), but that don't make sense from a class/object perspective
# (where subgroup would be more appropriate). To handle this, we allow for
# Litmus::DBI's subclasses to set column aliases with the column_alias() sub.
# Takes the database column name and the alias name.
sub column_alias {
2005-08-24 07:05:02 +04:00
my ($self, $db_name, $alias_name) = @_;
$column_aliases{$alias_name} = $db_name;
}
# here's where the actual work happens. We consult our alias list
# (as created by calls to column_alias()) and substitute the
# database column if we find a match
memoize('find_column');
sub find_column {
2005-08-24 07:05:02 +04:00
my $self = shift;
my $wanted = shift;
if (ref $self) {
$wanted =~ s/^.*::(\w+)$/$1/;
}
if ($column_aliases{$wanted}) {
return $column_aliases{$wanted};
} else {
# not an alias, so we use the normal
# find_column() from Class::DBI
$self->SUPER::find_column($wanted);
}
}
sub AUTOLOAD {
2005-08-24 07:05:02 +04:00
my $self = shift;
my @args = @_;
my $name = our $AUTOLOAD;
my $col = $self->find_column($name);
if (!$col) {
internalEror("tried to call Litmus::DBI method $name which does not exist");
}
return $self->$col(@args);
}
1;