1package Magus::DBI;
2#
3# Copyright (c) 2014 Lucas Holt
4# Copyright (c) 2007,2008 Chris Reinhardt. All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met:
9#
10# 1. Redistributions of source code must retain the above copyright notice
11#    this list of conditions and the following disclaimer.
12#
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28
29#
30# MAINTAINER=   ctriv@MidnightBSD.org
31#
32
33
34use strict;
35use warnings;
36use base 'Class::DBI';
37
38
39__PACKAGE__->connection(
40  "dbi:Pg:dbname=$Magus::Config{DBName};host=$Magus::Config{DBHost};",
41  $Magus::Config{DBUser},
42  $Magus::Config{DBPass},
43  {AutoCommit => 1, RaiseError => 1, PrintError => 0}
44);
45
46
47=head2 Magus::DBI->ping
48
49Return true if we can connect to the database, false otherwise.
50
51=cut
52
53sub ping {
54  my ($class) = @_;
55  my $ret = 0;
56
57  eval {
58    my $dbh = $class->db_Main();
59
60    $ret = $dbh->ping;
61  };
62
63  return $ret;
64}
65
66
67
68=head2 $obj->refresh
69
70Get new values for the columns from the database.
71
72=cut
73
74sub refresh {
75  my ($self) = @_;
76
77  my %pk = map { $_ => 1 } $self->primary_columns;
78
79  $self->_attribute_delete(grep { !$pk{$_} } $self->all_columns);
80
81  return $self;
82}
83
84
851;
86__END__
87