1
package # hide from pause
2
Monitoring::Livestatus::Class::Base::Table;
7
use Monitoring::Livestatus::Class::Abstract::Filter;
8
use Monitoring::Livestatus::Class::Abstract::Stats;
10
use Monitoring::Livestatus::Class;
11
my $TRACE = Monitoring::Livestatus::Class->TRACE() || 0;
15
isa => 'Monitoring::Livestatus::Class',
16
handles => [qw/backend_obj/],
24
isa => 'Monitoring::Livestatus::Class::Abstract::Filter',
25
builder => '_build_filter',
26
handles => { apply_filer => 'apply' },
29
sub _build_filter { return Monitoring::Livestatus::Class::Abstract::Filter->new( ctx => shift ); };
35
my @statments = $self->apply_filer($cond);
36
my @tmp = @{ $self->statments || [] };
37
push @tmp, @statments;
38
$self->_statments(\@tmp);
43
my($self, $options) = @_;
44
$self->{'_options'} = $options;
53
isa => 'Monitoring::Livestatus::Class::Abstract::Stats',
54
builder => '_build_stats',
55
handles => { apply_stats => 'apply' },
58
sub _build_stats { return Monitoring::Livestatus::Class::Abstract::Stats->new( ctx => shift ); };
64
my @statments = $self->apply_stats($cond);
65
my @tmp = @{ $self->statments || [] };
66
push @tmp, @statments;
67
$self->_statments(\@tmp);
74
builder => 'build_table_name',
77
sub build_table_name { die "build_table_name must be implemented in " . ref(shift) };
82
has 'primary_keys' => (
84
isa => 'ArrayRef[Str]',
85
builder => 'build_primary_keys',
88
sub build_primary_keys { die "build_primary_keys must be implemented in " . ref(shift) };
90
sub has_single_primary_key {
92
if ( scalar @{ $self->primary_keys } == 1 ){
98
sub single_primary_key {
100
if ( $self->has_single_primary_key ){
101
return $self->primary_keys->[0];
106
has '_statments' => (
108
reader => 'statments',
110
default => sub { return []; }
116
default => sub { return {}; }
122
default => sub { return []; }
128
$self->_columns( \@columns );
135
my $statment = sprintf("GET %s\nLimit: 1",$self->table_name);
136
my ( $hash_ref ) = @{ $self->backend_obj->selectall_arrayref($statment,{ slice => 1}) };
137
my @cols = keys %$hash_ref;
138
return wantarray ? @cols : \@cols;
145
if ( scalar @{ $self->_columns } > 0 ){
146
push @statments, sprintf('Columns: %s',join(' ',@{ $self->_columns }));
148
push @statments, @{ $self->statments };
150
my @data = $self->_execute( @statments );
151
return wantarray ? @data : \@data;
156
my $key = $self->single_primary_key || shift;
158
croak("There was no single primary key to be found.");
161
my @data = $self->hashref_array();
162
for my $row (@data) {
163
if(!defined $row->{$key}) {
164
my %possible_keys = keys %{$row};
165
croak("key $key not found in result set, possible keys are: ".join(', ', sort keys %possible_keys));
167
$indexed{$row->{$key}} = $row;
170
return wantarray ? %indexed : \%indexed;
178
push @statments, sprintf("GET %s",$self->table_name);
179
push @statments, @data;
181
printf STDERR "EXECUTE: %s\n", join("\nEXECUTE: ",@statments)
184
my $statment = join("\n",@statments);
186
my $options = $self->{'_options'};
187
$options->{'slice'} = {};
189
my $return = $self->backend_obj->selectall_arrayref($statment, $options);
191
return wantarray ? @{ $return } : $return;
198
Monitoring::Livestatus::Class::Base::Table - Base class for all table objects.
202
my $class = Monitoring::Livestatus::Class->new(
204
socket => '10.211.55.140:6557',
207
my $table_obj = $class->table('services');
209
my $data = $table_obj->search( {} )->hashref_array();
215
Reference to context object L<Monitoring::Livestatus::Class>
219
Reference to filter object L<Monitoring::Livestatus::Class>
223
Reference to filter object L<Monitoring::Livestatus::Class>
227
Containts the table name.
231
Containts all the statments.
235
Containts all the options.
241
Arguments: $colA, $colB, ...
249
Returns a array or reference to array, depending on the calling context, of all
256
$table_obj->search( { name => 'localhost' } );
257
$table_obj->search( { name => [ 'localhost', 'gateway' ] } );
258
$table_obj->search( [ { name => 'localhost' }, { name => 'gateway' } ] );
264
Returns a array or reference to array, depending on the calling context.
268
my $hashref_array = $table_obj->search( { } )->hashref_array;
269
print Dumper $hashref_array;
274
Returns a hash of hash references.
278
my $hashref_pk = $table_obj->search( { } )->hashref_pk();
279
print Dumper $hashref_pk;
281
=head2 has_single_primary_key
283
=head2 single_primary_key
285
=head2 build_table_name
287
=head2 build_primary_keys
291
See L<Monitoring::Livestatus::Class/AUTHOR> and L<Monitoring::Livestatus::Class/CONTRIBUTORS>.
293
=head1 COPYRIGHT & LICENSE
295
Copyright 2009 Robert Bohne.
297
This program is free software; you can redistribute it and/or modify it
298
under the terms of either: the GNU General Public License as published
299
by the Free Software Foundation; or the Artistic License.
301
See http://dev.perl.org/licenses/ for more information.