Pye-SQL
view release on metacpan or search on metacpan
lib/Pye/SQL.pm view on Meta::CPAN
$sth->finish;
return @msgs;
}
=head2 list_sessions( [ \%opts ] )
Takes all options defined by L<Pye>. The C<sort> option, however, takes a standard
C<ORDER BY> clause definition, e.g. C<id ASC>. This will default to C<date DESC>.
=cut
sub list_sessions {
my ($self, $opts) = @_;
$opts ||= {};
$opts->{skip} ||= 0;
$opts->{limit} ||= 10;
$opts->{sort} ||= 'date DESC';
my $sth = $self->{dbh}->prepare("SELECT session_id AS id, MIN(date) AS date FROM $self->{table} GROUP BY id ORDER BY $opts->{sort} LIMIT $opts->{limit} OFFSET $opts->{skip}");
$sth->execute;
my @sessions;
while (my $row = $sth->fetchrow_hashref) {
my ($d, $t) = $self->_format_datetime($row->{date});
$row->{date} = $d;
$row->{time} = $t;
push(@sessions, $row);
}
$sth->finish;
return @sessions;
}
sub _format_datetime {
my ($self, $date) = @_;
my ($d, $t) = split(/T|\s/, $date);
$t = substr($t, 0, 12);
return ($d, $t);
}
sub _remove_session_logs {
my ($self, $session_id) = @_;
$self->{dbh}->do("DELETE FROM $self->{table} WHERE session_id = ?", undef, "$session_id");
}
sub _build_dsn {
my $opts = shift;
if ($opts->{db_type} eq 'mysql') {
'DBI:mysql:database='.
($opts->{database} || 'logs').
';host='.($opts->{host} || '127.0.0.1').
';port='.($opts->{port} || 3306).
';mysql_enable_utf8=1';
} elsif ($opts->{db_type} eq 'pgsql') {
'dbi:Pg:dbname='.
($opts->{database} || 'logs').
';host='.($opts->{host} || '127.0.0.1').
';port='.($opts->{port} || 5432);
} else {
# sqlite
'dbi:SQLite:dbname='.($opts->{database} || 'logs.db');
}
}
sub _in {
my $val = shift;
foreach (@_) {
return 1 if $val eq $_;
}
return;
}
=head1 CONFIGURATION AND ENVIRONMENT
C<Pye> requires no configuration files or environment variables.
=head1 DEPENDENCIES
C<Pye> depends on the following CPAN modules:
=over
=item * L<Carp>
=item * L<DBI>
=item * L<JSON::MaybeXS>
=item * L<Role::Tiny>
=back
You will also need the appropriate driver for your database:
=over
=item * L<DBD::mysql> for MySQL
=item * L<DBD::Pg> for PostgreSQL
=item * L<DBD::SQLite> for SQLite
=back
=head1 BUGS AND LIMITATIONS
Please report any bugs or feature requests to
C<bug-Pye-SQL@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Pye-SQL>.
=head1 SUPPORT
( run in 0.959 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )