Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add a flag to new() to operate in synchronous, single-process mode. S…

…implifies database interaction and reduces multiprocess issues for debugging.
  • Loading branch information...
commit 7af3dbc1f27a908e2207d1e585ae2df2e786099b 1 parent 1b8b805
Rocco Caputo authored February 06, 2011
BIN  POE-Component-SimpleDBI-1.29.tar.gz
Binary file not shown
32  lib/POE/Component/SimpleDBI.pm
@@ -4,7 +4,7 @@ use strict; use warnings;
4 4
 
5 5
 # Initialize our version
6 6
 use vars qw( $VERSION );
7  
-$VERSION = '1.29';
  7
+$VERSION = '1.29_04';
8 8
 
9 9
 # Import what we need from the POE namespace
10 10
 use POE;			# For the constants
@@ -25,7 +25,7 @@ BEGIN {
25 25
 # Set things in motion!
26 26
 sub new {
27 27
 	# Get our arguments
28  
-	my( $type, $ALIAS, $PREPARE_CACHED ) = @_;
  28
+	my( $type, $ALIAS, $PREPARE_CACHED, $SYNCHRO_MODE ) = @_;
29 29
 
30 30
 	# Get the session alias
31 31
 	if ( ! defined $ALIAS ) {
@@ -112,6 +112,9 @@ sub new {
112 112
 
113 113
 			# Cache sql statements?
114 114
 			'PREPARE_CACHED'=>	$PREPARE_CACHED,
  115
+
  116
+			# Synchronous mode?
  117
+			'SYNCHRO' => $SYNCHRO_MODE,
115 118
 		},
116 119
 	) or die 'Unable to create a new session!';
117 120
 
@@ -677,10 +680,17 @@ sub DB_CONNECT {
677 680
 		push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
678 681
 	}
679 682
 
680  
-	# Do we have the wheel running?
681  
-	if ( ! defined $_[HEAP]->{'WHEEL'} ) {
682  
-		# Create the wheel
683  
-		$_[KERNEL]->call( $_[SESSION], 'Setup_Wheel' );
  683
+	# Asynchronous mode.
  684
+	if ( ! defined $_[HEAP]->{SYNCHRO} ) {
  685
+
  686
+		# Do we have the wheel running?
  687
+		if ( ! defined $_[HEAP]->{'WHEEL'} ) {
  688
+			# Create the wheel
  689
+			$_[KERNEL]->call( $_[SESSION], 'Setup_Wheel' );
  690
+		}
  691
+	}
  692
+	else {
  693
+		require POE::Component::SimpleDBI::SubProcess;
684 694
 	}
685 695
 
686 696
 	# Check if the subprocess is not active
@@ -952,7 +962,15 @@ sub Check_Queue {
952 962
 			$_[HEAP]->{'ACTIVE'} = 1;
953 963
 
954 964
 			# Put it in the wheel
955  
-			$_[HEAP]->{'WHEEL'}->put( \%queue );
  965
+			if ( defined $_[HEAP]->{SYNCHRO} ) {
  966
+				my $output = (
  967
+					POE::Component::SimpleDBI::SubProcess::process_request(\%queue)
  968
+				);
  969
+				$_[KERNEL]->call($_[SESSION], 'Got_STDOUT', $output) if $output;
  970
+			}
  971
+			else {
  972
+				$_[HEAP]->{'WHEEL'}->put( \%queue );
  973
+			}
956 974
 		} else {
957 975
 			if ( DEBUG ) {
958 976
 				warn 'Check_Queue was called but nothing in the queue!';
122  lib/POE/Component/SimpleDBI/SubProcess.pm
@@ -58,39 +58,7 @@ sub main {
58 58
 
59 59
 		# Process each data structure
60 60
 		foreach my $input ( @$data ) {
61  
-			# Now, we do the actual work depending on what kind of query it was
62  
-			if ( $input->{'ACTION'} eq 'CONNECT' ) {
63  
-				# Connect!
64  
-				DB_CONNECT( $input );
65  
-			} elsif ( $input->{'ACTION'} eq 'DISCONNECT' ) {
66  
-				# Disconnect!
67  
-				DB_DISCONNECT( $input );
68  
-			} elsif ( $input->{'ACTION'} eq 'DO' ) {
69  
-				# Fire off the SQL and return success/failure + rows affected
70  
-				DB_DO( $input );
71  
-			} elsif ( $input->{'ACTION'} eq 'SINGLE' ) {
72  
-				# Return a single result
73  
-				DB_SINGLE( $input );
74  
-			} elsif ( $input->{'ACTION'} eq 'MULTIPLE' ) {
75  
-				# Get many results, then return them all at the same time
76  
-				DB_MULTIPLE( $input );
77  
-			} elsif ( $input->{'ACTION'} eq 'QUOTE' ) {
78  
-				DB_QUOTE( $input );
79  
-			} elsif ( $input->{'ACTION'} eq 'ATOMIC' ) {
80  
-				DB_ATOMIC( $input );
81  
-			} elsif ( $input->{'ACTION'} eq 'EXIT' ) {
82  
-				# Cleanly disconnect from the DB
83  
-				if ( defined $DB ) {
84  
-					$DB->disconnect();
85  
-					undef $DB;
86  
-				}
87  
-
88  
-				# EXIT!
89  
-				return;
90  
-			} else {
91  
-				# Unrecognized action!
92  
-				output( Make_Error( $input->{'ID'}, 'Unknown action sent from parent' ) );
93  
-			}
  61
+			output(process_request($input));
94 62
 		}
95 63
 	}
96 64
 
@@ -108,6 +76,45 @@ sub main {
108 76
 	return;
109 77
 }
110 78
 
  79
+sub process_request {
  80
+	my $input = shift;
  81
+
  82
+	# Now, we do the actual work depending on what kind of query it was
  83
+	if ( $input->{'ACTION'} eq 'CONNECT' ) {
  84
+		# Connect!
  85
+		my ($success, $output) = DB_CONNECT($input, 0);
  86
+		return $output;
  87
+	} elsif ( $input->{'ACTION'} eq 'DISCONNECT' ) {
  88
+		# Disconnect!
  89
+		return DB_DISCONNECT( $input );
  90
+	} elsif ( $input->{'ACTION'} eq 'DO' ) {
  91
+		# Fire off the SQL and return success/failure + rows affected
  92
+		return DB_DO( $input );
  93
+	} elsif ( $input->{'ACTION'} eq 'SINGLE' ) {
  94
+		# Return a single result
  95
+		return DB_SINGLE( $input );
  96
+	} elsif ( $input->{'ACTION'} eq 'MULTIPLE' ) {
  97
+		# Get many results, then return them all at the same time
  98
+		return DB_MULTIPLE( $input );
  99
+	} elsif ( $input->{'ACTION'} eq 'QUOTE' ) {
  100
+		return DB_QUOTE( $input );
  101
+	} elsif ( $input->{'ACTION'} eq 'ATOMIC' ) {
  102
+		return DB_ATOMIC( $input );
  103
+	} elsif ( $input->{'ACTION'} eq 'EXIT' ) {
  104
+		# Cleanly disconnect from the DB
  105
+		if ( defined $DB ) {
  106
+			$DB->disconnect();
  107
+			undef $DB;
  108
+		}
  109
+
  110
+		# EXIT!
  111
+		return;
  112
+	}
  113
+
  114
+	# Unrecognized action!
  115
+	return( Make_Error( $input->{'ID'}, 'Unknown action sent from parent' ) );
  116
+}
  117
+
111 118
 # Connects to the DB
112 119
 sub DB_CONNECT {
113 120
 	# Get the input structure
@@ -189,15 +196,15 @@ sub DB_CONNECT {
189 196
 
190 197
 	# All done!
191 198
 	if ( ! defined $reconn ) {
192  
-		output( $output );
  199
+		return (1, $output);
193 200
 	} else {
194 201
 		# Reconnect attempt, was it successful?
195 202
 		if ( ! exists $output->{'ERROR'} ) {
196  
-			return 1;
  203
+			return (1, $output);
197 204
 		}
198 205
 	}
199 206
 
200  
-	return;
  207
+	return (0, $output);
201 208
 }
202 209
 
203 210
 # Disconnects from the DB
@@ -234,8 +241,7 @@ sub DB_DISCONNECT {
234 241
 	}
235 242
 
236 243
 	# All done!
237  
-	output( $output );
238  
-	return;
  244
+	return $output;
239 245
 }
240 246
 
241 247
 # This subroutine does a DB QUOTE
@@ -250,9 +256,8 @@ sub DB_QUOTE {
250 256
 	# Check if we are connected
251 257
 	if ( ! defined $DB or ! $DB->ping() ) {
252 258
 		# Automatically try to reconnect
253  
-		if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
254  
-			output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
255  
-			return;
  259
+		if ( ! (DB_CONNECT( $CONN ))[0] ) {
  260
+			return Make_Error( 'GONE', 'Lost connection to the database server.' );
256 261
 		}
257 262
 	}
258 263
 
@@ -275,8 +280,7 @@ sub DB_QUOTE {
275 280
 	}
276 281
 
277 282
 	# All done!
278  
-	output( $output );
279  
-	return;
  283
+	return $output;
280 284
 }
281 285
 
282 286
 # This subroutine runs a 'SELECT' style query on the db
@@ -292,9 +296,8 @@ sub DB_MULTIPLE {
292 296
 	# Check if we are connected
293 297
 	if ( ! defined $DB or ! $DB->ping() ) {
294 298
 		# Automatically try to reconnect
295  
-		if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
296  
-			output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
297  
-			return;
  299
+		if ( ! (DB_CONNECT( $CONN, 'RECONNECT' ))[0] ) {
  300
+			return( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
298 301
 		}
299 302
 	}
300 303
 
@@ -373,8 +376,7 @@ sub DB_MULTIPLE {
373 376
 	}
374 377
 
375 378
 	# Return the data structure
376  
-	output( $output );
377  
-	return;
  379
+	return( $output );
378 380
 }
379 381
 
380 382
 # This subroutine runs a 'SELECT ... LIMIT 1' style query on the db
@@ -390,9 +392,8 @@ sub DB_SINGLE {
390 392
 	# Check if we are connected
391 393
 	if ( ! defined $DB or ! $DB->ping() ) {
392 394
 		# Automatically try to reconnect
393  
-		if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
394  
-			output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
395  
-			return;
  395
+		if ( ! (DB_CONNECT( $CONN, 'RECONNECT' ))[0] ) {
  396
+			return Make_Error( 'GONE', 'Lost connection to the database server.' );
396 397
 		}
397 398
 	}
398 399
 
@@ -452,8 +453,7 @@ sub DB_SINGLE {
452 453
 	}
453 454
 
454 455
 	# Return the data structure
455  
-	output( $output );
456  
-	return;
  456
+	return $output;
457 457
 }
458 458
 
459 459
 # This subroutine runs a 'DO' style query on the db
@@ -470,9 +470,8 @@ sub DB_DO {
470 470
 	# Check if we are connected
471 471
 	if ( ! defined $DB or ! $DB->ping() ) {
472 472
 		# Automatically try to reconnect
473  
-		if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
474  
-			output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
475  
-			return;
  473
+		if ( ! (DB_CONNECT( $CONN, 'RECONNECT' ))[0] ) {
  474
+			return Make_Error( 'GONE', 'Lost connection to the database server.' );
476 475
 		}
477 476
 	}
478 477
 
@@ -539,8 +538,7 @@ sub DB_DO {
539 538
 	}
540 539
 
541 540
 	# Return the data structure
542  
-	output( $output );
543  
-	return;
  541
+	return $output;
544 542
 }
545 543
 
546 544
 # This subroutine runs a 'DO' style query on the db in a transaction
@@ -555,9 +553,8 @@ sub DB_ATOMIC {
555 553
 	# Check if we are connected
556 554
 	if ( ! defined $DB or ! $DB->ping() ) {
557 555
 		# Automatically try to reconnect
558  
-		if ( ! DB_CONNECT( $CONN, 'RECONNECT' ) ) {
559  
-			output( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
560  
-			return;
  556
+		if ( ! (DB_CONNECT( $CONN, 'RECONNECT' ))[0] ) {
  557
+			return Make_Error( 'GONE', 'Lost connection to the database server.' );
561 558
 		}
562 559
 	}
563 560
 
@@ -636,8 +633,7 @@ sub DB_ATOMIC {
636 633
 	}
637 634
 
638 635
 	# Return the data structure
639  
-	output( $output );
640  
-	return;
  636
+	return $output;
641 637
 }
642 638
 
643 639
 # This subroutine makes a generic error structure

0 notes on commit 7af3dbc

Please sign in to comment.
Something went wrong with that request. Please try again.