2005-10-11 22:44:16 +04:00
|
|
|
# -*- 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
|
2005-10-11 22:44:16 +04:00
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Chris Cooper <ccooper@deadsquid.com>
|
|
|
|
# Zach Lipton <zach@zachlipton.com>
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK *****
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2005-07-30 00:21:01 +04:00
|
|
|
package Litmus::DBI;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Litmus::Config;
|
2005-08-04 04:59:29 +04:00
|
|
|
use Litmus::Error;
|
2005-08-20 07:39:02 +04:00
|
|
|
use Memoize;
|
2005-07-30 00:21:01 +04:00
|
|
|
|
|
|
|
use base 'Class::DBI::mysql';
|
|
|
|
|
|
|
|
my $dsn = "dbi:mysql:$Litmus::Config::db_name:$Litmus::Config::db_host";
|
|
|
|
|
2005-08-20 07:39:02 +04:00
|
|
|
our %column_aliases;
|
|
|
|
|
2005-07-30 00:21:01 +04:00
|
|
|
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);
|
2005-07-30 00:21:01 +04:00
|
|
|
|
2005-08-20 07:39:02 +04:00
|
|
|
# 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;
|
2005-08-20 07:39:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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);
|
|
|
|
}
|
2005-08-20 07:39:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2005-08-20 07:39:02 +04:00
|
|
|
}
|
2005-07-30 00:21:01 +04:00
|
|
|
|
|
|
|
1;
|
|
|
|
|