| 1 | package DBIx::Class::Async::Storage::DBI; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 100 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 100 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 100 | ▶ | use version; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 100 | our $VERSION = qv('v1.2.0'); | |||||||||||||||||||
| 8 | 100 | our $AUTHORITY = 'cpan:MANWAR'; | |||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 100 | ▶ | use base 'DBIx::Class::Async::Storage'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 100 | ▶ | use DBIx::Class::Async::Storage::DBI::Cursor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | DBIx::Class::Async::Storage::DBI - DBI-based async storage backend for DBIx::Class::Async | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 VERSION | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | Version v1.2.0 | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | # Typically obtained via the storage of an async schema | ||||||||||||||||||||
| 24 | my $storage = $schema->storage; | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | # Enable SQL debugging | ||||||||||||||||||||
| 27 | $storage->debug(1); | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | # Or use a custom debug object | ||||||||||||||||||||
| 30 | my $stats = DBIx::Class::Storage::Statistics->new; | ||||||||||||||||||||
| 31 | $storage->debugobj($stats); | ||||||||||||||||||||
| 32 | $storage->debug(1); | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | # Or send debug output to a specific filehandle | ||||||||||||||||||||
| 35 | open my $log_fh, '>>', 'sql.log'; | ||||||||||||||||||||
| 36 | $storage->debugfh($log_fh); | ||||||||||||||||||||
| 37 | $storage->debug(1); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # Async Cursor / Streaming Support | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | my $rs = $schema->resultset('User')->search({ active => 1 }); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # Create an async cursor to iterate over results without blocking the loop | ||||||||||||||||||||
| 44 | my $cursor = $storage->cursor($rs); | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | # Iterate through the results one by one | ||||||||||||||||||||
| 47 | my $next_item; | ||||||||||||||||||||
| 48 | $next_item = sub { | ||||||||||||||||||||
| 49 | $cursor->next->then(sub { | ||||||||||||||||||||
| 50 | my $row_data = shift; | ||||||||||||||||||||
| 51 | return Future->done unless $row_data; # End of stream | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | say "Processing user data: " . $row_data->{name}; | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # Recurse to get the next item | ||||||||||||||||||||
| 56 | return $next_item->(); | ||||||||||||||||||||
| 57 | }); | ||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | $next_item->()->get; | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # Low-Level Execution | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # Execute raw SQL through the worker pool | ||||||||||||||||||||
| 65 | $storage->execute_all("UPDATE users SET last_login = ?", [ time ]) | ||||||||||||||||||||
| 66 | ->then(sub { | ||||||||||||||||||||
| 67 | say "Bulk update complete."; | ||||||||||||||||||||
| 68 | }); | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | # Modern Async/Await iteration (Recommended) | ||||||||||||||||||||
| 71 | use Future::AsyncAwait; | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | my $cursor = $schema->storage->cursor($rs); | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | async sub process_all_users { | ||||||||||||||||||||
| 76 | while (my $row = await $cursor->next) { | ||||||||||||||||||||
| 77 | say "Streaming user: " . $row->name; | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | say "All users processed."; | ||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =head1 DESCRIPTION | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | This class provides a DBI-based storage backend for L<DBIx::Class::Async::Schema>. | ||||||||||||||||||||
| 85 | It extends L<DBIx::Class::Async::Storage> and implements DBI-specific functionality | ||||||||||||||||||||
| 86 | for managing database connections and operations in an asynchronous worker pool | ||||||||||||||||||||
| 87 | environment. | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | Unlike traditional L<DBIx::Class::Storage::DBI>, this storage class does not | ||||||||||||||||||||
| 90 | maintain database handles in the parent process. Instead, database handles are | ||||||||||||||||||||
| 91 | managed by worker processes, allowing for non-blocking asynchronous database | ||||||||||||||||||||
| 92 | operations using L<Future> objects. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | This class is automatically instantiated when you connect to a database using | ||||||||||||||||||||
| 95 | L<DBIx::Class::Async::Schema/connect> and generally does not need to be | ||||||||||||||||||||
| 96 | instantiated directly. | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head1 METHODS | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | =head2 dbh | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | my $dbh = $storage->dbh; | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | Returns C<undef> in async storage mode. | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | Unlike traditional L<DBIx::Class::Storage::DBI>, the async storage layer does | ||||||||||||||||||||
| 107 | not maintain a database handle in the parent process. Instead, database handles | ||||||||||||||||||||
| 108 | are held by worker processes in the background worker pool, which execute | ||||||||||||||||||||
| 109 | queries asynchronously. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | This method exists for API compatibility with standard L<DBIx::Class> storage | ||||||||||||||||||||
| 112 | objects, but always returns C<undef> to indicate that direct database handle | ||||||||||||||||||||
| 113 | access is not available in async mode. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | my $storage = $schema->storage; | ||||||||||||||||||||
| 116 | my $dbh = $storage->dbh; # Always undef in async mode | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | if (!defined $dbh) { | ||||||||||||||||||||
| 119 | say "Running in async mode - no direct DBH access"; | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | If you need to perform database operations, use the L<DBIx::Class::Async::ResultSet> | ||||||||||||||||||||
| 123 | and L<DBIx::Class::Async::Row> methods which handle async execution transparently | ||||||||||||||||||||
| 124 | through the worker pool. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 1 | ▶ | sub dbh { | ||||||||||||||||||
|
Subroutine
dbh: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 129 | 1 | my $self = shift; | |||||||||||||||||||
| 130 | # In Async mode, the parent process doesn't hold a DBH. | ||||||||||||||||||||
| 131 | # The workers hold the DBHs. | ||||||||||||||||||||
| 132 | 1 | return undef; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =head2 cursor | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | my $cursor = $storage->cursor($resultset); | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | Creates and returns a L<DBIx::Class::Async::Storage::DBI::Cursor> object | ||||||||||||||||||||
| 140 | for asynchronously iterating through a ResultSet's rows. | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | The cursor provides a low-level interface for fetching rows one at a time | ||||||||||||||||||||
| 143 | using Futures, which is useful for processing large result sets without | ||||||||||||||||||||
| 144 | loading all rows into memory at once. | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | B<Arguments> | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =over 4 | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =item * C<$resultset> - A L<DBIx::Class::Async::ResultSet> object | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | B<Returns> | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | A L<DBIx::Class::Async::Storage::DBI::Cursor> object configured for the | ||||||||||||||||||||
| 157 | given ResultSet. | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | use Future::AsyncAwait; | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | my $rs = $schema->resultset('User'); | ||||||||||||||||||||
| 162 | my $cursor = $storage->cursor($rs); | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | my $iter = async sub { | ||||||||||||||||||||
| 165 | while (my $row = await $cursor->next) { | ||||||||||||||||||||
| 166 | say $row->name; | ||||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | $iter->get; | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | The cursor respects the ResultSet's C<rows> attribute for batch fetching: | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | my $rs = $schema->resultset('User')->search(undef, { rows => 50 }); | ||||||||||||||||||||
| 175 | my $cursor = $storage->cursor($rs); # Fetches 50 rows at a time | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | See L<DBIx::Class::Async::Storage::DBI::Cursor> for available cursor methods. | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =cut | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 9 | ▶ | sub cursor { | ||||||||||||||||||
|
Subroutine
cursor: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 182 | 9 | my ($self, $rs) = @_; | |||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | # Just like DBIC, we return a DBI-specific cursor | ||||||||||||||||||||
| 185 | 9 | return DBIx::Class::Async::Storage::DBI::Cursor->new( | |||||||||||||||||||
| 186 | storage => $self, | ||||||||||||||||||||
| 187 | rs => $rs, | ||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||
| 189 | } | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =head2 debug | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | # Get current debug level | ||||||||||||||||||||
| 194 | my $level = $storage->debug; | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | # Set debug level | ||||||||||||||||||||
| 197 | $storage->debug(1); | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | Gets or sets the debug level for the storage layer. | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | When called without arguments, returns the current debug level (defaults to 0). | ||||||||||||||||||||
| 202 | When called with an argument, sets the debug level to the specified value and | ||||||||||||||||||||
| 203 | returns it. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | B<Arguments> | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =over 4 | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =item * C<$level> (optional) - Integer debug level (0 = off, higher values = more verbose) | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | =back | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | B<Returns> | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | The current or newly set debug level. | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | # Enable debugging | ||||||||||||||||||||
| 218 | $storage->debug(1); | ||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | # Check if debugging is enabled | ||||||||||||||||||||
| 221 | if ($storage->debug) { | ||||||||||||||||||||
| 222 | say "Debug mode is on at level " . $storage->debug; | ||||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | # Disable debugging | ||||||||||||||||||||
| 226 | $storage->debug(0); | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | When debugging is enabled, SQL statements will be sent to the L</debugobj> | ||||||||||||||||||||
| 229 | for output. By default, this prints to C<STDERR>, but you can customise the | ||||||||||||||||||||
| 230 | output destination using L</debugobj> or L</debugfh>. | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | 0 | ▶ | sub debug { | ||||||||||||||||||
|
Subroutine
debug: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 235 | 0 | my ($self, $level) = @_; | |||||||||||||||||||
| 236 | 0 | ▶ | return $self->{debug} = $level if defined $level; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 237 | 0 | ▶ | return $self->{debug} || 0; | ||||||||||||||||||
|
Condition0%0/2
$self->{'debug'} || 0
MC/DC0%0/1
$self->{'debug'} || 0
$self->{'debug'}
Truth table0%0/2
A
A= $self->{'debug'}
| |||||||||||||||||||||
| 238 | } | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =head2 debugobj | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | # Get current debug object | ||||||||||||||||||||
| 243 | my $obj = $storage->debugobj; | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | # Set a custom debug object | ||||||||||||||||||||
| 246 | $storage->debugobj($custom_debug_object); | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | Gets or sets the debug object used for SQL statement logging. | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | When called without arguments, returns the current debug object. If no debug | ||||||||||||||||||||
| 251 | object has been set, creates and returns a default L<DBIx::Class::Storage::Statistics> | ||||||||||||||||||||
| 252 | object. | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | When called with an argument, sets the debug object to the provided value. | ||||||||||||||||||||
| 255 | The debug object must implement a C<print> method that will be called with | ||||||||||||||||||||
| 256 | SQL statements and bind values when L</debug> is enabled. | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | B<Arguments> | ||||||||||||||||||||
| 259 | |||||||||||||||||||||
| 260 | =over 4 | ||||||||||||||||||||
| 261 | |||||||||||||||||||||
| 262 | =item * C<$debug_object> (optional) - An object implementing a C<print> method | ||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | =back | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | B<Returns> | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | The current or newly set debug object. | ||||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | # Use default debug object (prints to STDERR) | ||||||||||||||||||||
| 271 | my $stats = $storage->debugobj; | ||||||||||||||||||||
| 272 | $storage->debug(1); | ||||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | # Use a custom debug object | ||||||||||||||||||||
| 275 | package MyDebugger; | ||||||||||||||||||||
| 276 | sub new { bless {}, shift } | ||||||||||||||||||||
| 277 | sub print { my ($self, $msg) = @_; warn "SQL: $msg" } | ||||||||||||||||||||
| 278 | |||||||||||||||||||||
| 279 | package main; | ||||||||||||||||||||
| 280 | $storage->debugobj(MyDebugger->new); | ||||||||||||||||||||
| 281 | $storage->debug(1); | ||||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | This method provides API compatibility with L<DBIx::Class::Storage::DBI>. | ||||||||||||||||||||
| 284 | |||||||||||||||||||||
| 285 | =cut | ||||||||||||||||||||
| 286 | |||||||||||||||||||||
| 287 | 0 | ▶ | sub debugobj { | ||||||||||||||||||
|
Subroutine
debugobj: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 288 | 0 | my $self = shift; | |||||||||||||||||||
| 289 | |||||||||||||||||||||
| 290 | 0 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 291 | 0 | $self->{debugobj} = shift; | |||||||||||||||||||
| 292 | 0 | return $self; | |||||||||||||||||||
| 293 | } | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | 0 | ▶ | return $self->{debugobj} ||= do { | ||||||||||||||||||
|
Condition0%0/3
$self->{"debugobj"} ||= do {
require DBIx::Class::Storage::Statistics;
"DBIx::Class::Storage::Statistics"->new
}
MC/DC0%0/2
$self->{"debugobj"} ||= do {
require DBIx::Class::Storage::Statistics;
"DBIx::Class::Storage::Statistics"->new
}
$self->{"debugobj"}
do {
require DBIx::Class::Storage::Statistics;
"DBIx::Class::Storage::Statistics"->new
}
Truth table0%0/3
A ||= B
A= $self->{"debugobj"}B= do {
require DBIx::Class::Storage::Statistics;
"DBIx::Class::Storage::Statistics"->new
}
| |||||||||||||||||||||
| 296 | 0 | require DBIx::Class::Storage::Statistics; | |||||||||||||||||||
| 297 | 0 | DBIx::Class::Storage::Statistics->new; | |||||||||||||||||||
| 298 | }; | ||||||||||||||||||||
| 299 | } | ||||||||||||||||||||
| 300 | |||||||||||||||||||||
| 301 | =head2 debugfh | ||||||||||||||||||||
| 302 | |||||||||||||||||||||
| 303 | # Get current debug filehandle | ||||||||||||||||||||
| 304 | my $fh = $storage->debugfh; | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | # Set debug output to a filehandle | ||||||||||||||||||||
| 307 | $storage->debugfh(\*STDERR); | ||||||||||||||||||||
| 308 | |||||||||||||||||||||
| 309 | # Log to a file | ||||||||||||||||||||
| 310 | open my $log_fh, '>>', 'sql.log'; | ||||||||||||||||||||
| 311 | $storage->debugfh($log_fh); | ||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | Gets or sets the filehandle used for debug output. | ||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | This is a convenience method that creates a L<DBIx::Class::Storage::Statistics> | ||||||||||||||||||||
| 316 | object configured to print to the specified filehandle, and sets it as the | ||||||||||||||||||||
| 317 | L</debugobj>. | ||||||||||||||||||||
| 318 | |||||||||||||||||||||
| 319 | B<Arguments> | ||||||||||||||||||||
| 320 | |||||||||||||||||||||
| 321 | =over 4 | ||||||||||||||||||||
| 322 | |||||||||||||||||||||
| 323 | =item * C<$filehandle> (optional) - A filehandle glob or reference | ||||||||||||||||||||
| 324 | |||||||||||||||||||||
| 325 | =back | ||||||||||||||||||||
| 326 | |||||||||||||||||||||
| 327 | B<Returns> | ||||||||||||||||||||
| 328 | |||||||||||||||||||||
| 329 | When called without arguments, returns the filehandle from the current L</debugobj> | ||||||||||||||||||||
| 330 | if available, or C<undef> if no debug object is set. | ||||||||||||||||||||
| 331 | |||||||||||||||||||||
| 332 | When called with an argument, sets the debug filehandle and returns C<$self> | ||||||||||||||||||||
| 333 | for method chaining. | ||||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | # Send debug output to a log file | ||||||||||||||||||||
| 336 | open my $log, '>>', 'queries.log' or die $!; | ||||||||||||||||||||
| 337 | $storage->debugfh($log); | ||||||||||||||||||||
| 338 | $storage->debug(1); | ||||||||||||||||||||
| 339 | |||||||||||||||||||||
| 340 | # Send to STDOUT instead of STDERR | ||||||||||||||||||||
| 341 | $storage->debugfh(\*STDOUT); | ||||||||||||||||||||
| 342 | |||||||||||||||||||||
| 343 | This method provides API compatibility with L<DBIx::Class::Storage::DBI>. | ||||||||||||||||||||
| 344 | |||||||||||||||||||||
| 345 | =cut | ||||||||||||||||||||
| 346 | |||||||||||||||||||||
| 347 | 0 | ▶ | sub debugfh { | ||||||||||||||||||
|
Subroutine
debugfh: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 348 | 0 | my $self = shift; | |||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | 0 | ▶ | if (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 351 | 0 | my $fh = shift; | |||||||||||||||||||
| 352 | 0 | require DBIx::Class::Storage::Statistics; | |||||||||||||||||||
| 353 | 0 | my $stats = DBIx::Class::Storage::Statistics->new; | |||||||||||||||||||
| 354 | 0 | $stats->debugfh($fh); | |||||||||||||||||||
| 355 | 0 | $self->debugobj($stats); | |||||||||||||||||||
| 356 | 0 | return $self; | |||||||||||||||||||
| 357 | } | ||||||||||||||||||||
| 358 | |||||||||||||||||||||
| 359 | 0 | ▶ | return $self->debugobj->debugfh if $self->{debugobj}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 360 | 0 | return undef; | |||||||||||||||||||
| 361 | } | ||||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | =head1 ARCHITECTURE | ||||||||||||||||||||
| 364 | |||||||||||||||||||||
| 365 | This storage class implements a distributed, non-blocking execution model that | ||||||||||||||||||||
| 366 | differs fundamentally from traditional L<DBIx::Class::Storage::DBI>: | ||||||||||||||||||||
| 367 | |||||||||||||||||||||
| 368 | =over 4 | ||||||||||||||||||||
| 369 | |||||||||||||||||||||
| 370 | =item B<No Parent Process DBH> | ||||||||||||||||||||
| 371 | |||||||||||||||||||||
| 372 | The parent process never instantiates a L<DBI> handle. All database connections | ||||||||||||||||||||
| 373 | are isolated within worker processes. This prevents the B<"forked handle"> | ||||||||||||||||||||
| 374 | corruption common in multiprocess Perl applications and keeps the main event | ||||||||||||||||||||
| 375 | loop lightweight. | ||||||||||||||||||||
| 376 | |||||||||||||||||||||
| 377 | =item B<Async Operations> | ||||||||||||||||||||
| 378 | |||||||||||||||||||||
| 379 | Every database interaction (search, create, update, etc.) returns an | ||||||||||||||||||||
| 380 | L<IO::Async::Future> object. This allows the main application to continue | ||||||||||||||||||||
| 381 | processing web requests, timers, or other I/O while waiting for the worker | ||||||||||||||||||||
| 382 | to return results. | ||||||||||||||||||||
| 383 | |||||||||||||||||||||
| 384 | =item B<Worker Pool via IO::Async::Function> | ||||||||||||||||||||
| 385 | |||||||||||||||||||||
| 386 | Queries are dispatched to a persistent pool of background workers. By using | ||||||||||||||||||||
| 387 | persistent workers with B<state-cached connections>, the bridge eliminates | ||||||||||||||||||||
| 388 | the latency of C<connect/disconnect> cycles for every query. | ||||||||||||||||||||
| 389 | |||||||||||||||||||||
| 390 | =item B<Transparent API> | ||||||||||||||||||||
| 391 | |||||||||||||||||||||
| 392 | The bridge provides a high degree of parity with the standard DBIC API. | ||||||||||||||||||||
| 393 | Advanced features like C<prefetch>, C<collapse>, and complex transactions | ||||||||||||||||||||
| 394 | (C<txn_do>) are supported through a specialised serialisation layer that | ||||||||||||||||||||
| 395 | reconstructs object graphs across process boundaries. | ||||||||||||||||||||
| 396 | |||||||||||||||||||||
| 397 | =back | ||||||||||||||||||||
| 398 | |||||||||||||||||||||
| 399 | =head1 DEBUGGING | ||||||||||||||||||||
| 400 | |||||||||||||||||||||
| 401 | L<DBIx::Class::Async::Storage::DBI> provides debugging capabilities compatible | ||||||||||||||||||||
| 402 | with standard L<DBIx::Class::Storage::DBI>: | ||||||||||||||||||||
| 403 | |||||||||||||||||||||
| 404 | # Enable basic debugging (output to STDERR) | ||||||||||||||||||||
| 405 | $storage->debug(1); | ||||||||||||||||||||
| 406 | |||||||||||||||||||||
| 407 | # Customise debug output | ||||||||||||||||||||
| 408 | open my $log, '>>', 'sql.log'; | ||||||||||||||||||||
| 409 | $storage->debugfh($log); | ||||||||||||||||||||
| 410 | $storage->debug(1); | ||||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | # Use a custom debug object | ||||||||||||||||||||
| 413 | my $custom_debugger = MyDebugger->new; | ||||||||||||||||||||
| 414 | $storage->debugobj($custom_debugger); | ||||||||||||||||||||
| 415 | $storage->debug(1); | ||||||||||||||||||||
| 416 | |||||||||||||||||||||
| 417 | When debugging is enabled, SQL statements generated by the storage layer | ||||||||||||||||||||
| 418 | will be sent to the debug object's C<print> method. This allows you to | ||||||||||||||||||||
| 419 | inspect the actual SQL being executed by your async queries. | ||||||||||||||||||||
| 420 | |||||||||||||||||||||
| 421 | B<Note>: Debug output occurs in the parent process before queries are sent | ||||||||||||||||||||
| 422 | to workers, so you'll see the SQL as it's generated, not necessarily in | ||||||||||||||||||||
| 423 | the exact order it's executed by the worker pool. | ||||||||||||||||||||
| 424 | |||||||||||||||||||||
| 425 | =cut | ||||||||||||||||||||
| 426 | |||||||||||||||||||||
| 427 | =head1 CAVEATS | ||||||||||||||||||||
| 428 | |||||||||||||||||||||
| 429 | =head2 Transaction Management | ||||||||||||||||||||
| 430 | |||||||||||||||||||||
| 431 | Transactions in L<DBIx::Class::Async> differ from standard L<DBIx::Class> | ||||||||||||||||||||
| 432 | because the I/O is delegated to a worker pool. You cannot use the | ||||||||||||||||||||
| 433 | traditional C<< $schema->storage->txn_begin >> pattern in a non-blocking | ||||||||||||||||||||
| 434 | loop, as subsequent calls might be routed to different workers. | ||||||||||||||||||||
| 435 | |||||||||||||||||||||
| 436 | =head3 The Recommended Approach: C<txn_do> | ||||||||||||||||||||
| 437 | |||||||||||||||||||||
| 438 | To ensure atomicity, use the C<txn_do> or C<txn_batch> methods. These | ||||||||||||||||||||
| 439 | methods bundle multiple operations into a single "Instruction Set" that | ||||||||||||||||||||
| 440 | is dispatched to a B<single worker process>. | ||||||||||||||||||||
| 441 | |||||||||||||||||||||
| 442 | The worker executes the entire block within a local database transaction. | ||||||||||||||||||||
| 443 | If any step fails, the worker performs a local C<rollback> and returns | ||||||||||||||||||||
| 444 | the error to the parent process. | ||||||||||||||||||||
| 445 | |||||||||||||||||||||
| 446 | =head3 Placeholder Resolution | ||||||||||||||||||||
| 447 | |||||||||||||||||||||
| 448 | The async C<txn_do> supports internal variable registration. You can | ||||||||||||||||||||
| 449 | create a record in Step A and use its auto-incremented ID in Step B | ||||||||||||||||||||
| 450 | using the C<$name.id> syntax. This allows for complex, multi-step | ||||||||||||||||||||
| 451 | dependent operations to remain fully asynchronous and atomic. | ||||||||||||||||||||
| 452 | |||||||||||||||||||||
| 453 | B<Security note (raw SQL steps):> for C<< { action => 'raw', sql => ..., bind => [...] } >> | ||||||||||||||||||||
| 454 | steps, C<$name.id> substitution is only performed inside the C<bind> | ||||||||||||||||||||
| 455 | arrayref, where it is passed to the database as a genuine parameter. | ||||||||||||||||||||
| 456 | It is deliberately B<not> substituted into the C<sql> string itself, | ||||||||||||||||||||
| 457 | that string is executed as literal SQL, so splicing a value into it | ||||||||||||||||||||
| 458 | would create a SQL injection hole for any non-numeric ID (e.g. a | ||||||||||||||||||||
| 459 | string/UUID primary key). Always write raw steps with a placeholder in | ||||||||||||||||||||
| 460 | the SQL and the referenced value in C<bind>: | ||||||||||||||||||||
| 461 | |||||||||||||||||||||
| 462 | { | ||||||||||||||||||||
| 463 | action => 'raw', | ||||||||||||||||||||
| 464 | sql => 'UPDATE t SET x = ? WHERE owner_id = ?', | ||||||||||||||||||||
| 465 | bind => [ 1, '$owner.id' ] | ||||||||||||||||||||
| 466 | } | ||||||||||||||||||||
| 467 | |||||||||||||||||||||
| 468 | not: | ||||||||||||||||||||
| 469 | |||||||||||||||||||||
| 470 | { | ||||||||||||||||||||
| 471 | action => 'raw', | ||||||||||||||||||||
| 472 | sql => 'UPDATE t SET x = 1 WHERE owner_id = $owner.id' | ||||||||||||||||||||
| 473 | } # NOT substituted | ||||||||||||||||||||
| 474 | |||||||||||||||||||||
| 475 | =head2 ResultSet State | ||||||||||||||||||||
| 476 | |||||||||||||||||||||
| 477 | Because data is deflated for transport between the worker and the parent, | ||||||||||||||||||||
| 478 | the objects returned by C<on_done> are "POPO" (Plain Old Perl Objects/HashRefs) | ||||||||||||||||||||
| 479 | rather than "Live" L<DBIx::Class::Row> objects. Calling methods like | ||||||||||||||||||||
| 480 | C<update> or C<delete> on these returned objects will not affect the | ||||||||||||||||||||
| 481 | database directly. | ||||||||||||||||||||
| 482 | |||||||||||||||||||||
| 483 | =cut | ||||||||||||||||||||
| 484 | |||||||||||||||||||||
| 485 | =head1 SEE ALSO | ||||||||||||||||||||
| 486 | |||||||||||||||||||||
| 487 | =over 4 | ||||||||||||||||||||
| 488 | |||||||||||||||||||||
| 489 | =item * L<DBIx::Class::Async::Storage> - Base async storage class | ||||||||||||||||||||
| 490 | |||||||||||||||||||||
| 491 | =item * L<DBIx::Class::Async::Storage::DBI::Cursor> - Async cursor implementation | ||||||||||||||||||||
| 492 | |||||||||||||||||||||
| 493 | =item * L<DBIx::Class::Async::Schema> - Async schema class | ||||||||||||||||||||
| 494 | |||||||||||||||||||||
| 495 | =item * L<DBIx::Class::Async::ResultSet> - Async ResultSet class | ||||||||||||||||||||
| 496 | |||||||||||||||||||||
| 497 | =item * L<DBIx::Class::Storage::DBI> - Traditional synchronous DBI storage | ||||||||||||||||||||
| 498 | |||||||||||||||||||||
| 499 | =item * L<DBIx::Class::Storage::Statistics> - Debug object for SQL logging | ||||||||||||||||||||
| 500 | |||||||||||||||||||||
| 501 | =item * L<Future> - Asynchronous result objects | ||||||||||||||||||||
| 502 | |||||||||||||||||||||
| 503 | =item * L<IO::Async> - Asynchronous event-driven programming | ||||||||||||||||||||
| 504 | |||||||||||||||||||||
| 505 | =back | ||||||||||||||||||||
| 506 | |||||||||||||||||||||
| 507 | =head1 AUTHOR | ||||||||||||||||||||
| 508 | |||||||||||||||||||||
| 509 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||||||||||||||||||||
| 510 | |||||||||||||||||||||
| 511 | =head1 REPOSITORY | ||||||||||||||||||||
| 512 | |||||||||||||||||||||
| 513 | L<https://github.com/manwar/DBIx-Class-Async> | ||||||||||||||||||||
| 514 | |||||||||||||||||||||
| 515 | =head1 BUGS | ||||||||||||||||||||
| 516 | |||||||||||||||||||||
| 517 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/DBIx-Class-Async/issues>. | ||||||||||||||||||||
| 518 | I will be notified and then you'll automatically be notified of progress on your | ||||||||||||||||||||
| 519 | bug as I make changes. | ||||||||||||||||||||
| 520 | |||||||||||||||||||||
| 521 | =head1 SUPPORT | ||||||||||||||||||||
| 522 | |||||||||||||||||||||
| 523 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||
| 524 | |||||||||||||||||||||
| 525 | perldoc DBIx::Class::Async::Storage::DBI | ||||||||||||||||||||
| 526 | |||||||||||||||||||||
| 527 | You can also look for information at: | ||||||||||||||||||||
| 528 | |||||||||||||||||||||
| 529 | =over 4 | ||||||||||||||||||||
| 530 | |||||||||||||||||||||
| 531 | =item * BUG Report | ||||||||||||||||||||
| 532 | |||||||||||||||||||||
| 533 | L<https://github.com/manwar/DBIx-Class-Async/issues> | ||||||||||||||||||||
| 534 | |||||||||||||||||||||
| 535 | =item * CPAN Ratings | ||||||||||||||||||||
| 536 | |||||||||||||||||||||
| 537 | L<http://cpanratings.perl.org/d/DBIx-Class-Async> | ||||||||||||||||||||
| 538 | |||||||||||||||||||||
| 539 | =item * Search MetaCPAN | ||||||||||||||||||||
| 540 | |||||||||||||||||||||
| 541 | L<https://metacpan.org/dist/DBIx-Class-Async/> | ||||||||||||||||||||
| 542 | |||||||||||||||||||||
| 543 | =back | ||||||||||||||||||||
| 544 | |||||||||||||||||||||
| 545 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||
| 546 | |||||||||||||||||||||
| 547 | Copyright (C) 2026 Mohammad Sajid Anwar. | ||||||||||||||||||||
| 548 | |||||||||||||||||||||
| 549 | This program is free software; you can redistribute it and / or modify it under | ||||||||||||||||||||
| 550 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||||||||||||||||||||
| 551 | license at: | ||||||||||||||||||||
| 552 | |||||||||||||||||||||
| 553 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||
| 554 | |||||||||||||||||||||
| 555 | Any use, modification, and distribution of the Standard or Modified Versions is | ||||||||||||||||||||
| 556 | governed by this Artistic License.By using, modifying or distributing the Package, | ||||||||||||||||||||
| 557 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||||||||||||||||||||
| 558 | not accept this license. | ||||||||||||||||||||
| 559 | |||||||||||||||||||||
| 560 | If your Modified Version has been derived from a Modified Version made by someone | ||||||||||||||||||||
| 561 | other than you,you are nevertheless required to ensure that your Modified Version | ||||||||||||||||||||
| 562 | complies with the requirements of this license. | ||||||||||||||||||||
| 563 | |||||||||||||||||||||
| 564 | This license does not grant you the right to use any trademark, service mark, | ||||||||||||||||||||
| 565 | tradename, or logo of the Copyright Holder. | ||||||||||||||||||||
| 566 | |||||||||||||||||||||
| 567 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||||||||||||||||||||
| 568 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||||||||||||||||||||
| 569 | Package with respect to any patent claims licensable by the Copyright Holder that | ||||||||||||||||||||
| 570 | are necessarily infringed by the Package. If you institute patent litigation | ||||||||||||||||||||
| 571 | (including a cross-claim or counterclaim) against any party alleging that the | ||||||||||||||||||||
| 572 | Package constitutes direct or contributory patent infringement,then this Artistic | ||||||||||||||||||||
| 573 | License to you shall terminate on the date that such litigation is filed. | ||||||||||||||||||||
| 574 | |||||||||||||||||||||
| 575 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||||||||||||||||||||
| 576 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||||||||||||||||||||
| 577 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||||||||||||||||||||
| 578 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||||||||||||||||||||
| 579 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||||||||||||||||||||
| 580 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||||||||||||||||||||
| 581 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||||||||||||
| 582 | |||||||||||||||||||||
| 583 | =cut | ||||||||||||||||||||
| 584 | |||||||||||||||||||||
| 585 | 100 | 1; # End of DBIx::Class::Async::Storage::DBI | |||||||||||||||||||