| 1 | package MYDan::Subscribe::Conf; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use YAML::XS; | ||
| 6 | |||
| 7 | use Data::Dumper; | ||
| 8 | use POSIX; | ||
| 9 | |||
| 10 | use MYDan::Util::OptConf; | ||
| 11 | use File::Basename; | ||
| 12 | use MYDan::Subscribe::DBI::DB; | ||
| 13 | use MYDan::Notify; | ||
| 14 | |||
| 15 | sub new | ||
| 16 | { | ||
| 17 | my ( $class, %this ) = @_; | ||
| 18 | |||
| 19 | die "path undef" unless $this{path}; | ||
| 20 | $this{db} = MYDan::Subscribe::DBI::DB->new( | ||
| 21 | $this{path}, | ||
| 22 | $MYDan::Subscribe::DBI::DB::TABLE | ||
| 23 | ); | ||
| 24 | |||
| 25 | bless \%this, ref $class || $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub insert | ||
| 29 | { | ||
| 30 | my ( $this, %param ) = @_; | ||
| 31 | $this->{db}->insert( @param{qw( name attr user level)} ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub select | ||
| 35 | { | ||
| 36 | my ( $this, %param, %query ) = @_; | ||
| 37 | |||
| 38 | map{ $query{$_} = [ 1, $param{$_} ] if $param{$_} }qw( name attr user level); | ||
| 39 | $this->{db}->select( '*', %query ); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub delete | ||
| 43 | { | ||
| 44 | my ( $this, %param, %query ) = @_; | ||
| 45 | |||
| 46 | map{ $query{$_} = [ 1, $param{$_} ] if $param{$_} }qw( name attr user level); | ||
| 47 | $this->{db}->delete( %query ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub get | ||
| 51 | { | ||
| 52 | my ( $this, %param, @r, %r ) = @_; | ||
| 53 | my ( $name, $attr ) = map{ $param{$_} ||= 'null' }qw( name attr ); | ||
| 54 | for my $x ( $this->select() ) | ||
| 55 | { | ||
| 56 | next unless _match( $name, $x->[0] ) && _match( $attr, $x->[1] ); | ||
| 57 | my $len = length "$x->[0]$x->[1]"; | ||
| 58 | $r{$x->[2]} = +{ len => $len, res => $x } | ||
| 59 | if ! $r{$x->[2]}{len} || ( $r{$x->[2]}{len} && $r{$x->[2]}{len} <= $len ); | ||
| 60 | } | ||
| 61 | return map{ $_->{res} } values %r; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _match | ||
| 65 | { | ||
| 66 | my ( $s, $m ) = @_; | ||
| 67 | return ( ( $s eq $m || '*' eq $m ) || ( $m =~ s/\*/\\w\+/g && $s =~ /$m/ ) ) ? 1 : 0; | ||
| 68 | } | ||
| 69 | |||
| 70 | 1; |