| 1 | package Win32::WQL; | ||
| 2 | use strict; | ||
| 3 | use Win32::OLE; # qw(EVENTS); | ||
| 4 | # Events support will follow later | ||
| 5 | use parent 'Class::Accessor'; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Win32::WQL - DBI-like wrapper for the WMI | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use Win32::WQL; | ||
| 14 | my $wmi = Win32::WQL->new( machine => 'remote_computer' ); | ||
| 15 | my $sth = $wmi->prepare(<<'WQL'); | ||
| 16 | |||
| 17 | ASSOCIATORS OF {Win32_Directory.Name='C:\\WINNT'} | ||
| 18 | WHERE ResultClass = CIM_DataFile | ||
| 19 | |||
| 20 | WQL | ||
| 21 | |||
| 22 | my $remote_files = $sth->execute; | ||
| 23 | while (my $file = $remote_files->fetch()) { | ||
| 24 | print $file->{Name},"\n"; | ||
| 25 | }; | ||
| 26 | |||
| 27 | =head1 OVERVIEW | ||
| 28 | |||
| 29 | This module implements a bare bones DBI clone | ||
| 30 | which is similar yet different. You will most likely | ||
| 31 | want to use the real thing, L<DBD::WMI>, which | ||
| 32 | is a compatibility layer over this module. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | our $VERSION = '0.11'; | ||
| 37 | |||
| 38 | Win32::OLE->Option(Warn => 3); | ||
| 39 | |||
| 40 | __PACKAGE__->mk_accessors(qw(statement_class event_iterator_class collection_iterator_class wmi)); | ||
| 41 | |||
| 42 | =head1 METHODS | ||
| 43 | |||
| 44 | =head2 C<< new %ARGS >> | ||
| 45 | |||
| 46 | Initializes the thin wrapper over the L<Win32::OLE> | ||
| 47 | WMI instance. All parameters are optional. | ||
| 48 | |||
| 49 | machine | ||
| 50 | |||
| 51 | The parameter is the machine name to connect to. It defaults | ||
| 52 | to the local machine. | ||
| 53 | |||
| 54 | wmi | ||
| 55 | |||
| 56 | A preinitialized WMI object to use. Defaults to creating | ||
| 57 | a fresh instance. | ||
| 58 | |||
| 59 | statement_class | ||
| 60 | |||
| 61 | The class into which the results of C<prepare> | ||
| 62 | are blessed. Defaults to C<Win32::WQL::Statement>. | ||
| 63 | |||
| 64 | event_iterator_class | ||
| 65 | |||
| 66 | The class into which the results of C<fetchrow> | ||
| 67 | are blessed for event queries. Defaults to | ||
| 68 | C<Win32::WQL::Iterator::Event>. | ||
| 69 | |||
| 70 | collection_iterator_class | ||
| 71 | |||
| 72 | The class into which the results of C<fetchrow> | ||
| 73 | are blessed for static queries. Defaults to | ||
| 74 | C<Win32::WQL::Iterator::Collection>. | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub new { | ||
| 79 | my ($package, %args) = @_; | ||
| 80 | my $machine = delete $args{machine} || '.'; | ||
| 81 | my $self = { | ||
| 82 | wmi => Win32::OLE->GetObject("winmgmts:\\\\$machine\\root\\cimV2"), | ||
| 83 | statement_class => 'Win32::WQL::Statement', | ||
| 84 | event_iterator_class => 'Win32::WQL::Iterator::Event', | ||
| 85 | collection_iterator_class => 'Win32::WQL::Iterator::Collection', | ||
| 86 | %args, | ||
| 87 | }; | ||
| 88 | $package->SUPER::new($self); | ||
| 89 | }; | ||
| 90 | |||
| 91 | =head2 C<< $wmi->prepare QUERY >> | ||
| 92 | |||
| 93 | Returns a prepared query by calling | ||
| 94 | |||
| 95 | return $self->statement_class->new({ | ||
| 96 | query => $query, | ||
| 97 | wmi => $self, | ||
| 98 | iterator_class => $class, | ||
| 99 | wmi_method => $method, | ||
| 100 | }); | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub prepare { | ||
| 105 | my ($self,$query) = @_; | ||
| 106 | |||
| 107 | my ($class,$method,$fetch); | ||
| 108 | if ($self->event_query($query)) { | ||
| 109 | $class = $self->event_iterator_class; | ||
| 110 | $method = 'ExecNotificationQuery'; | ||
| 111 | } else { | ||
| 112 | $class = $self->collection_iterator_class; | ||
| 113 | $method = 'ExecQuery'; | ||
| 114 | }; | ||
| 115 | |||
| 116 | return $self->statement_class->new({ | ||
| 117 | query => $query, | ||
| 118 | wmi => $self, | ||
| 119 | iterator_class => $class, | ||
| 120 | wmi_method => $method, | ||
| 121 | }); | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 C<< $wmi->event_query QUERY >> | ||
| 125 | |||
| 126 | Determines whether a query is an event query | ||
| 127 | or a static query. | ||
| 128 | |||
| 129 | Event queries return a row whenever a new event | ||
| 130 | arrives and block if there is no event available. | ||
| 131 | |||
| 132 | Static queries are static and return all rows in | ||
| 133 | one go. | ||
| 134 | |||
| 135 | A query is considered an event query if it | ||
| 136 | matches | ||
| 137 | |||
| 138 | $query =~ /\b__instance(?:\w+)event\b/i | ||
| 139 | or $query =~ /\wEvent\b/i | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub event_query { | ||
| 144 | my ($package, $query) = @_; | ||
| 145 | return | ||
| 146 | ( $query =~ /\b__instance(?:\w+)event\b/i | ||
| 147 | or $query =~ /\wEvent\b/i | ||
| 148 | ) | ||
| 149 | } | ||
| 150 | |||
| 151 | package Win32::WQL::Statement; | ||
| 152 | use strict; | ||
| 153 | use Win32::OLE; | ||
| 154 | use base 'Class::Accessor'; | ||
| 155 | |||
| 156 | __PACKAGE__->mk_accessors(qw(iterator_class wmi query wmi_method)); | ||
| 157 | |||
| 158 | sub execute { | ||
| 159 | my ($self) = @_; | ||
| 160 | my $m = $self->wmi_method; | ||
| 161 | my $i = $self->wmi->wmi->$m( $self->query ); | ||
| 162 | return $self->iterator_class->new({ | ||
| 163 | wql_iterator => $i | ||
| 164 | }); | ||
| 165 | } | ||
| 166 | |||
| 167 | package Win32::WQL::Iterator::Collection; | ||
| 168 | use strict; | ||
| 169 | use base 'Class::Accessor'; | ||
| 170 | __PACKAGE__->mk_accessors(qw(wql_iterator items)); | ||
| 171 | |||
| 172 | use Win32::OLE qw(in); | ||
| 173 | use Data::Dumper; | ||
| 174 | |||
| 175 | # Finite, prefetched list | ||
| 176 | |||
| 177 | sub new { | ||
| 178 | my ($package,$args) = @_; | ||
| 179 | my @items = in delete $args->{wql_iterator}; | ||
| 180 | $args->{items} = \@items; | ||
| 181 | $package->SUPER::new($args); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub fetchrow { | ||
| 185 | my ($self) = @_; | ||
| 186 | if (@{ $self->items }) { | ||
| 187 | shift @{ $self->items }; | ||
| 188 | } else { | ||
| 189 | return () | ||
| 190 | }; | ||
| 191 | } | ||
| 192 | |||
| 193 | package Win32::WQL::Iterator::Event; | ||
| 194 | use strict; | ||
| 195 | use base 'Class::Accessor'; | ||
| 196 | __PACKAGE__->mk_accessors(qw(wql_iterator)); | ||
| 197 | |||
| 198 | # potentially infinite list | ||
| 199 | |||
| 200 | sub fetchrow { | ||
| 201 | my ($self) = @_; | ||
| 202 | return $self->wql_iterator->NextEvent(); | ||
| 203 | } | ||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | =head1 SEE ALSO | ||
| 208 | |||
| 209 | L<DBD::WMI> for more examples | ||
| 210 | |||
| 211 | =head1 TODO | ||
| 212 | |||
| 213 | =over 4 | ||
| 214 | |||
| 215 | =item * Implement parameters for and credentials | ||
| 216 | |||
| 217 | =item * Implement a multiplexer by using multiple, waiting threads so you can C<SELECT> | ||
| 218 | events from more than one WMI namespace | ||
| 219 | |||
| 220 | =back | ||
| 221 | |||
| 222 | =head1 REPOSITORY | ||
| 223 | |||
| 224 | The public repository of this module is | ||
| 225 | L<https://github.com/Corion/dbd-wmi>. | ||
| 226 | |||
| 227 | =head1 SUPPORT | ||
| 228 | |||
| 229 | The public support forum of this module is | ||
| 230 | L<https://perlmonks.org/>. | ||
| 231 | |||
| 232 | =head1 BUG TRACKER | ||
| 233 | |||
| 234 | Please report bugs in this module via the RT CPAN bug queue at | ||
| 235 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=DBD-WMI> | ||
| 236 | or via mail to L<www-mechanize-phantomjs-Bugs@rt.cpan.org>. | ||
| 237 | |||
| 238 | =head1 AUTHOR | ||
| 239 | |||
| 240 | Max Maischein C<corion@cpan.org> | ||
| 241 | |||
| 242 | =head1 COPYRIGHT (c) | ||
| 243 | |||
| 244 | Copyright 2009-2018 by Max Maischein C<corion@cpan.org>. | ||
| 245 | |||
| 246 | =head1 LICENSE | ||
| 247 | |||
| 248 | This module is released under the same terms as Perl itself. | ||
| 249 | |||
| 250 | =cut |