/
SimpleDBI.pm
2005 lines (1608 loc) · 56.4 KB
/
SimpleDBI.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Declare our package
package POE::Component::SimpleDBI;
use strict; use warnings;
# Initialize our version
use vars qw( $VERSION );
$VERSION = '1.27';
# Import what we need from the POE namespace
use POE; # For the constants
use POE::Session; # To create our own :)
use POE::Filter::Reference; # For communicating with the subprocess
use POE::Filter::Line; # For subprocess STDERR messages
use POE::Wheel::Run; # For the nitty-gritty details of 'fork'
# Set some constants
BEGIN {
# Debug fun!
if ( ! defined &DEBUG ) { *DEBUG = sub () { 0 } }
# Our own definition of the max retries
if ( ! defined &MAX_RETRIES ) { *MAX_RETRIES = sub () { 5 } }
}
# Set things in motion!
sub new {
# Get our arguments
my( $type, $ALIAS, $PREPARE_CACHED ) = @_;
# Get the session alias
if ( ! defined $ALIAS ) {
# Debugging info...
if ( DEBUG ) {
warn 'Using default ALIAS = SimpleDBI';
}
# Set the default
$ALIAS = 'SimpleDBI';
}
# Should we disable prepare_cached?
if ( ! defined $PREPARE_CACHED ) {
# Debugging info...
if ( DEBUG ) {
warn 'Setting PREPARE_CACHED = 1 by default';
}
$PREPARE_CACHED = 1;
} else {
if ( $PREPARE_CACHED ) {
$PREPARE_CACHED = 1;
} else {
$PREPARE_CACHED = 0;
}
}
# Create a new session for ourself
POE::Session->create(
# Our subroutines
'inline_states' => {
# Maintenance events
'_start' => \&Start,
'_stop' => \&Stop,
'Setup_Wheel' => \&Setup_Wheel,
# Shutdown stuff
'shutdown' => \&Shutdown,
# IO events
'ChildError' => \&ChildError,
'ChildClosed' => \&ChildClosed,
'Got_STDOUT' => \&Got_STDOUT,
'Got_STDERR' => \&Got_STDERR,
'Got_CHLD' => \&Got_CHLD,
# DB events
'DO' => \&DB_HANDLE,
'SINGLE' => \&DB_HANDLE,
'MULTIPLE' => \&DB_HANDLE,
'QUOTE' => \&DB_HANDLE,
'CONNECT' => \&DB_CONNECT,
'DISCONNECT' => \&DB_DISCONNECT,
'ATOMIC' => \&DB_ATOMIC,
# Queue stuff
'Check_Queue' => \&Check_Queue,
'Clear_Queue' => \&Clear_Queue,
'Delete_Query' => \&Delete_Query,
},
# Set up the heap for ourself
'heap' => {
# The queue of DBI calls
'QUEUE' => [],
'IDCounter' => 0,
# The Wheel::Run object
'WHEEL' => undef,
# How many times have we re-created the wheel?
'Retries' => 0,
# Are we shutting down?
'SHUTDOWN' => 0,
# The DB Info
'DB_DSN' => undef,
'DB_USERNAME' => undef,
'DB_PASSWORD' => undef,
'DB_EVENT' => undef,
'DB_SESSION' => undef,
# Are we connected?
'CONNECTED' => 0,
'ACTIVE' => 0,
# The alias we will run under
'ALIAS' => $ALIAS,
# Cache sql statements?
'PREPARE_CACHED'=> $PREPARE_CACHED,
},
) or die 'Unable to create a new session!';
# Return success
return 1;
}
# This subroutine handles shutdown signals
sub Shutdown {
# Extensive debugging...
if ( DEBUG ) {
warn 'Initiating shutdown procedure!';
}
# Check for duplicate shutdown signals
if ( $_[HEAP]->{'SHUTDOWN'} ) {
# Okay, let's see what's going on
if ( $_[HEAP]->{'SHUTDOWN'} == 1 && ! defined $_[ARG0] ) {
# Duplicate shutdown events
if ( DEBUG ) {
warn 'Duplicate shutdown event was posted to SimpleDBI!';
}
return;
} elsif ( $_[HEAP]->{'SHUTDOWN'} == 2 ) {
# Tried to shutdown_NOW again...
if ( DEBUG ) {
warn 'Duplicate shutdown_NOW event was posted to SimpleDBI!';
}
return;
}
} else {
# Remove our alias so we can be properly terminated
$_[KERNEL]->alias_remove( $_[HEAP]->{'ALIAS'} );
}
# Check if we got "NOW"
if ( defined $_[ARG0] and $_[ARG0] eq 'NOW' ) {
# Actually shut down!
$_[HEAP]->{'SHUTDOWN'} = 2;
# KILL our subprocess
$_[HEAP]->{'WHEEL'}->kill( -9 );
# Delete the wheel, so we have nothing to keep the GC from destructing us...
delete $_[HEAP]->{'WHEEL'};
# Clean up the queue
$_[KERNEL]->call( $_[SESSION], 'Clear_Queue', 'SimpleDBI is shutting down now' );
# Tell the kernel to kill us!
$_[KERNEL]->signal( $_[SESSION], 'KILL' );
} else {
# Gracefully shut down...
$_[HEAP]->{'SHUTDOWN'} = 1;
# Put into the queue EXIT for the child
push( @{ $_[HEAP]->{'QUEUE'} }, { 'ACTION' => 'EXIT', 'ID' => $_[HEAP]->{'IDCounter'}++ } );
# Check if the subprocess is not active
if ( ! $_[HEAP]->{'ACTIVE'} ) {
# Send the query!
$_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
}
}
return;
}
# This subroutine handles MULTIPLE + SINGLE + DO + QUOTE queries
sub DB_HANDLE {
# Get the arguments
my %args = @_[ARG0 .. $#_ ];
# Check for unknown args
foreach my $key ( keys %args ) {
if ( $key !~ /^(?:SQL|PLACEHOLDERS|BAGGAGE|EVENT|SESSION|PREPARE_CACHED|INSERT_ID)$/ ) {
if ( DEBUG ) {
warn "Unknown argument to $_[STATE] -> $key";
}
delete $args{ $key };
}
}
# Add some stuff to the args
$args{'ACTION'} = $_[STATE];
if ( ! exists $args{'SESSION'} ) {
$args{'SESSION'} = $_[SENDER]->ID();
}
# Check for Session
if ( ! defined $args{'SESSION'} ) {
# Nothing much we can do except drop this quietly...
if ( DEBUG ) {
warn "Did not receive a SESSION argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
} else {
if ( ref $args{'SESSION'} ) {
if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
# Convert it!
$args{'SESSION'} = $args{'SESSION'}->ID();
} else {
if ( DEBUG ) {
warn "Received malformed SESSION argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
}
}
}
# Check for Event
if ( ! exists $args{'EVENT'} ) {
# Nothing much we can do except drop this quietly...
if ( DEBUG ) {
warn "Did not receive an EVENT argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
} else {
if ( ref $args{'EVENT'} ) {
# Same quietness...
if ( DEBUG ) {
warn "Received a malformed EVENT argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
}
}
# Check for SQL
if ( ! exists $args{'SQL'} or ! defined $args{'SQL'} or ref $args{'SQL'} ) {
# Extensive debug
if ( DEBUG ) {
warn 'Did not receive/malformed SQL string!';
}
# Okay, send the error to the Event
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
( exists $args{'SQL'} ? ( 'SQL' => $args{'SQL'} ) : () ),
( exists $args{'PLACEHOLDERS'} ? ( 'PLACEHOLDERS' => $args{'PLACEHOLDERS'} ) : () ),
( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
'ERROR' => 'Received an empty/malformed SQL string!',
'ACTION' => $args{'ACTION'},
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
# Check for placeholders
if ( exists $args{'PLACEHOLDERS'} ) {
if ( ! ref $args{'PLACEHOLDERS'} or ref( $args{'PLACEHOLDERS'} ) ne 'ARRAY' ) {
# Extensive debug
if ( DEBUG ) {
warn 'PLACEHOLDERS was not a ref to an ARRAY!';
}
# Okay, send the error to the Event
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
'SQL' => $args{'SQL'},
'PLACEHOLDERS' => $args{'PLACEHOLDERS'},
( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
'ERROR' => 'PLACEHOLDERS is not an array!',
'ACTION' => $args{'ACTION'},
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
}
# check for INSERT_ID
if ( exists $args{'INSERT_ID'} ) {
if ( $args{'INSERT_ID'} ) {
$args{'INSERT_ID'} = 1;
} else {
$args{'INSERT_ID'} = 0;
}
} else {
# set default
$args{'INSERT_ID'} = 1;
}
# check for PREPARE_CACHED
if ( exists $args{'PREPARE_CACHED'} ) {
if ( $args{'PREPARE_CACHED'} ) {
$args{'PREPARE_CACHED'} = 1;
} else {
$args{'PREPARE_CACHED'} = 0;
}
} else {
# What does our global setting say?
$args{'PREPARE_CACHED'} = $_[HEAP]->{'PREPARE_CACHED'};
}
# Check if we have shutdown or not
if ( $_[HEAP]->{'SHUTDOWN'} ) {
# Extensive debug
if ( DEBUG ) {
warn 'Denied query due to SHUTDOWN';
}
# Do not accept this query
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
'SQL' => $args{'SQL'},
( exists $args{'PLACEHOLDERS'} ? ( 'PLACEHOLDERS' => $args{'PLACEHOLDERS'} ) : () ),
( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
'ERROR' => 'POE::Component::SimpleDBI is shutting down now, requests are not accepted!',
'ACTION' => $args{'ACTION'},
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
# Increment the refcount for the session that is sending us this query
$_[KERNEL]->refcount_increment( $args{'SESSION'}, 'SimpleDBI' );
# Add the ID to the query
$args{'ID'} = $_[HEAP]->{'IDCounter'}++;
# Add this query to the queue
push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
# Check if the subprocess is not active
if ( ! $_[HEAP]->{'ACTIVE'} ) {
# Send the query!
$_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
}
# Return the ID for interested parties :)
return $args{'ID'};
}
# This subroutine handles ATOMIC queries
sub DB_ATOMIC {
# Get the arguments
my %args = @_[ARG0 .. $#_ ];
# Check for unknown args
foreach my $key ( keys %args ) {
if ( $key !~ /^(?:SQL|PLACEHOLDERS|BAGGAGE|EVENT|SESSION|PREPARE_CACHED)$/ ) {
if ( DEBUG ) {
warn "Unknown argument to $_[STATE] -> $key";
}
delete $args{ $key };
}
}
# Add some stuff to the args
$args{'ACTION'} = $_[STATE];
if ( ! exists $args{'SESSION'} ) {
$args{'SESSION'} = $_[SENDER]->ID();
}
# Check for Session
if ( ! defined $args{'SESSION'} ) {
# Nothing much we can do except drop this quietly...
if ( DEBUG ) {
warn "Did not receive a SESSION argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
} else {
if ( ref $args{'SESSION'} ) {
if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
# Convert it!
$args{'SESSION'} = $args{'SESSION'}->ID();
} else {
if ( DEBUG ) {
warn "Received malformed SESSION argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
}
}
}
# Check for Event
if ( ! exists $args{'EVENT'} ) {
# Nothing much we can do except drop this quietly...
if ( DEBUG ) {
warn "Did not receive an EVENT argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
} else {
if ( ref $args{'EVENT'} ) {
# Same quietness...
if ( DEBUG ) {
warn "Received a malformed EVENT argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
}
}
# Check for SQL
if ( ! exists $args{'SQL'} or ! defined $args{'SQL'} or ! ref $args{'SQL'} or ref( $args{'SQL'} ) ne 'ARRAY' or scalar @{ $args{'SQL'} } == 0 ) {
if ( DEBUG ) {
warn 'Did not receive/malformed SQL array!';
}
# Okay, send the error to the Event
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
( exists $args{'SQL'} ? ( 'SQL' => $args{'SQL'} ) : () ),
( exists $args{'PLACEHOLDERS'} ? ( 'PLACEHOLDERS' => $args{'PLACEHOLDERS'} ) : () ),
( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
'ERROR' => 'Received an empty/malformed SQL array!',
'ACTION' => $args{'ACTION'},
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
# Check for placeholders
if ( exists $args{'PLACEHOLDERS'} ) {
if ( ! ref $args{'PLACEHOLDERS'} or ref( $args{'PLACEHOLDERS'} ) ne 'ARRAY' ) {
if ( DEBUG ) {
warn 'PLACEHOLDERS was not a ref to an ARRAY!';
}
# Okay, send the error to the Event
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
'SQL' => $args{'SQL'},
'PLACEHOLDERS' => $args{'PLACEHOLDERS'},
( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
'ERROR' => 'PLACEHOLDERS is not an array!',
'ACTION' => $args{'ACTION'},
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
# again, make sure it's an AoA
foreach my $ph ( @{ $args{'PLACEHOLDERS'} } ) {
if ( defined $ph and ( ! ref $ph or ref( $ph ) ne 'ARRAY' ) ) {
if ( DEBUG ) {
warn 'PLACEHOLDERS was not a proper AoA!';
}
# Okay, send the error to the Event
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
'SQL' => $args{'SQL'},
'PLACEHOLDERS' => $args{'PLACEHOLDERS'},
( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
'ERROR' => 'PLACEHOLDERS is not an AoA!',
'ACTION' => $args{'ACTION'},
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
}
}
# check for PREPARE_CACHED
if ( exists $args{'PREPARE_CACHED'} ) {
if ( $args{'PREPARE_CACHED'} ) {
$args{'PREPARE_CACHED'} = 1;
} else {
$args{'PREPARE_CACHED'} = 0;
}
} else {
# What does our global setting say?
$args{'PREPARE_CACHED'} = $_[HEAP]->{'PREPARE_CACHED'};
}
# Check if we have shutdown or not
if ( $_[HEAP]->{'SHUTDOWN'} ) {
# Extensive debug
if ( DEBUG ) {
warn 'Denied query due to SHUTDOWN';
}
# Do not accept this query
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
'SQL' => $args{'SQL'},
( exists $args{'PLACEHOLDERS'} ? ( 'PLACEHOLDERS' => $args{'PLACEHOLDERS'} ) : () ),
( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
'ERROR' => 'POE::Component::SimpleDBI is shutting down now, requests are not accepted!',
'ACTION' => $args{'ACTION'},
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
# Increment the refcount for the session that is sending us this query
$_[KERNEL]->refcount_increment( $args{'SESSION'}, 'SimpleDBI' );
# Add the ID to the query
$args{'ID'} = $_[HEAP]->{'IDCounter'}++;
# Add this query to the queue
push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
# Check if the subprocess is not active
if ( ! $_[HEAP]->{'ACTIVE'} ) {
# Send the query!
$_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
}
# Return the ID for interested parties :)
return $args{'ID'};
}
# This subroutine connects to the DB
sub DB_CONNECT {
# Get the arguments
my %args = @_[ARG0 .. $#_ ];
# If we got no arguments, assume that we will use the old connection
if ( keys %args == 0 ) {
if ( ! exists $_[HEAP]->{'DB_DSN'} ) {
# How should we connect?
if ( DEBUG ) {
warn 'Got CONNECT event but no arguments/did not have a cached copy of connect args';
}
return;
}
}
# Check for unknown args
foreach my $key ( keys %args ) {
if ( $key !~ /^(?:SESSION|EVENT|DSN|USERNAME|PASSWORD|NOW|CLEAR|AUTO_COMMIT)$/ ) {
if ( DEBUG ) {
warn "Unknown argument to CONNECT -> $key";
}
delete $args{ $key };
}
}
# Add the cached copy if applicable
foreach my $key ( qw( DSN USERNAME PASSWORD SESSION EVENT ) ) {
if ( ! exists $args{ $key } and defined $_[HEAP]->{ 'DB_' . $key } ) {
$args{ $key } = $_[HEAP]->{ 'DB_' . $key };
}
}
# Add some stuff to the args
$args{'ACTION'} = 'CONNECT';
if ( ! exists $args{'SESSION'} ) {
$args{'SESSION'} = $_[SENDER]->ID();
}
# Check for Session
if ( ! defined $args{'SESSION'} ) {
# Nothing much we can do except drop this quietly...
if ( DEBUG ) {
warn "Did not receive a SESSION argument -> State: CONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
} else {
if ( ref $args{'SESSION'} ) {
if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
# Convert it!
$args{'SESSION'} = $args{'SESSION'}->ID();
} else {
if ( DEBUG ) {
warn "Received malformed SESSION argument -> State: CONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
}
}
}
# Check for Event
if ( ! exists $args{'EVENT'} ) {
# Nothing much we can do except drop this quietly...
if ( DEBUG ) {
warn "Did not receive an EVENT argument -> State: CONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
} else {
if ( ref $args{'EVENT'} ) {
# Same quietness...
if ( DEBUG ) {
warn "Received a malformed EVENT argument -> State: CONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
}
}
# Check the 3 things we are interested in
foreach my $key ( qw( DSN USERNAME PASSWORD ) ) {
# Check for it!
if ( ! exists $args{ $key } or ! defined $args{ $key } or ref $args{ $key } ) {
# Extensive debug
if ( DEBUG ) {
warn "Did not receive/malformed $key!";
}
# Okay, send the error to the Event
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
( exists $args{'DSN'} ? ( 'DSN' => $args{'DSN'} ) : () ),
( exists $args{'USERNAME'} ? ( 'USERNAME' => $args{'USERNAME'} ) : () ),
( exists $args{'PASSWORD'} ? ( 'PASSWORD' => $args{'PASSWORD'} ) : () ),
'ERROR' => "Cannot connect without the $key!",
'ACTION' => 'CONNECT',
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
}
# set default AutoCommit = 1
if ( ! exists $args{'AUTO_COMMIT'} ) {
$args{'AUTO_COMMIT'} = 1;
} else {
if ( $args{'AUTO_COMMIT'} ) {
$args{'AUTO_COMMIT'} = 1;
} else {
$args{'AUTO_COMMIT'} = 0;
}
}
# Some sanity
if ( exists $args{'NOW'} and $args{'NOW'} and $_[HEAP]->{'CONNECTED'} ) {
# Okay, send the error to the Event
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
'DSN' => $args{'DSN'},
'USERNAME' => $args{'USERNAME'},
'PASSWORD' => $args{'PASSWORD'},
'ERROR' => "Cannot CONNECT NOW when we are already connected!",
'ACTION' => 'CONNECT',
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
# If we got CLEAR, empty the queue
if ( exists $args{'CLEAR'} and $args{'CLEAR'} ) {
$_[KERNEL]->call( $_[SESSION], 'Clear_Queue', 'The request queue was cleared via CONNECT' );
}
# Increment the refcount for the session that is sending us this query
$_[KERNEL]->refcount_increment( $args{'SESSION'}, 'SimpleDBI' );
# Add the ID to the query
$args{'ID'} = $_[HEAP]->{'IDCounter'}++;
# Are we connecting now?
if ( exists $args{'NOW'} and $args{'NOW'} ) {
# Add this query to the top of the queue
unshift( @{ $_[HEAP]->{'QUEUE'} }, \%args );
} else {
# Add this to the bottom of the queue
push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
}
# Do we have the wheel running?
if ( ! defined $_[HEAP]->{'WHEEL'} ) {
# Create the wheel
$_[KERNEL]->call( $_[SESSION], 'Setup_Wheel' );
}
# Check if the subprocess is not active
if ( ! $_[HEAP]->{'ACTIVE'} ) {
# Send the query!
$_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
}
# Save the connection info
foreach my $key ( qw( DSN USERNAME PASSWORD SESSION EVENT AUTO_COMMIT ) ) {
$_[HEAP]->{ 'DB_' . $key } = $args{ $key };
}
# Return the ID for interested parties :)
return $args{'ID'};
}
# This subroutine disconnects from the DB
sub DB_DISCONNECT {
# Get the arguments
my %args = @_[ARG0 .. $#_ ];
# Check for unknown args
foreach my $key ( keys %args ) {
if ( $key !~ /^(?:SESSION|EVENT|NOW|CLEAR)$/ ) {
if ( DEBUG ) {
warn "Unknown argument to DISCONNECT -> $key";
}
delete $args{ $key };
}
}
# Add some stuff to the args
$args{'ACTION'} = 'DISCONNECT';
if ( ! exists $args{'SESSION'} ) {
$args{'SESSION'} = $_[SENDER]->ID();
}
# Check for Session
if ( ! defined $args{'SESSION'} ) {
# Nothing much we can do except drop this quietly...
if ( DEBUG ) {
warn "Did not receive a SESSION argument -> State: DISCONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
} else {
if ( ref $args{'SESSION'} ) {
if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
# Convert it!
$args{'SESSION'} = $args{'SESSION'}->ID();
} else {
if ( DEBUG ) {
warn "Received malformed SESSION argument -> State: DISCONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
}
}
}
# Check for Event
if ( ! exists $args{'EVENT'} ) {
# Nothing much we can do except drop this quietly...
if ( DEBUG ) {
warn "Did not receive an EVENT argument -> State: DISCONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
} else {
if ( ref $args{'EVENT'} ) {
# Same quietness...
if ( DEBUG ) {
warn "Received a malformed EVENT argument -> State: DISCONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
}
return;
}
}
# Some sanity
if ( exists $args{'NOW'} and $args{'NOW'} and ! $_[HEAP]->{'CONNECTED'} ) {
# Okay, send the error to the Event
$_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
'ERROR' => "Cannot DISCONNECT NOW when we are already disconnected!",
'ACTION' => 'DISCONNECT',
'EVENT' => $args{'EVENT'},
'SESSION' => $args{'SESSION'},
}
);
return;
}
# If we got CLEAR, empty the queue
if ( exists $args{'CLEAR'} and $args{'CLEAR'} ) {
$_[KERNEL]->call( $_[SESSION], 'Clear_Queue', 'The request queue was cleared via DISCONNECT' );
}
# Increment the refcount for the session that is sending us this query
$_[KERNEL]->refcount_increment( $args{'SESSION'}, 'SimpleDBI' );
# Add the ID to the query
$args{'ID'} = $_[HEAP]->{'IDCounter'}++;
# Are we disconnecting now?
if ( exists $args{'NOW'} and $args{'NOW'} ) {
# Add this query to the top of the queue
# Do we need to save the running query?
if ( $_[HEAP]->{'ACTIVE'} ) {
# Save the old top query
my $oldquery = shift( @{ $_[HEAP]->{'QUEUE'} } );
# Add it to the top!
unshift( @{ $_[HEAP]->{'QUEUE'} }, \%args );
# Put the old one back on top if it was there
if ( defined $oldquery ) {
unshift( @{ $_[HEAP]->{'QUEUE'} }, $oldquery );
} else {
die "Internal error - ACTIVE with no query!";
}
} else {
# Just shove it on top
unshift( @{ $_[HEAP]->{'QUEUE'} }, \%args );
}
} else {
# Add this to the bottom of the queue
push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
}
# Check if the subprocess is not active
if ( ! $_[HEAP]->{'ACTIVE'} ) {
# Send the query!
$_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
}
# Return the ID for interested parties :)
return $args{'ID'};
}
# This subroutine clears the queue
sub Clear_Queue {
# Get the error string
my $err = $_[ARG0];
# If it is not defined, make it the default
if ( ! defined $err ) {
$err = 'Cleared the queue';
}
# Is the SubProcess active?
my $activequeue = undef;
if ( $_[HEAP]->{'ACTIVE'} ) {
$activequeue = shift( @{ $_[HEAP]->{'QUEUE'} } );
}
# Go over our queue, and do some stuff
foreach my $queue ( @{ $_[HEAP]->{'QUEUE'} } ) {
# Skip the special EXIT actions we might have put on the queue
if ( $queue->{'ACTION'} eq 'EXIT' ) { next }
# Construct the response
my $ret = {
'ERROR' => $err,
'ACTION' => $queue->{'ACTION'},
'EVENT' => $queue->{'EVENT'},
'SESSION' => $queue->{'SESSION'},
'ID' => $queue->{'ID'},
};
# Add needed fields
if ( $queue->{'ACTION'} eq 'CONNECT' ) {
$ret->{'DSN'} = $queue->{'DSN'};
$ret->{'USERNAME'} = $queue->{'USERNAME'};
$ret->{'PASSWORD'} = $queue->{'PASSWORD'};
$ret->{'AUTO_COMMIT'} = $queue->{'AUTO_COMMIT'};
} elsif ( $queue->{'ACTION'} ne 'DISCONNECT' ) {
$ret->{'SQL'} = $queue->{'SQL'};
if ( exists $queue->{'PLACEHOLDERS'} ) {
$ret->{'PLACEHOLDERS'} = $queue->{'PLACEHOLDERS'};
}
if ( exists $queue->{'BAGGAGE'} ) {
$ret->{'BAGGAGE'} = $queue->{'BAGGAGE'};
}
}
# Post a failure event to all the queries on the Queue, informing them that we have been shutdown...
$_[KERNEL]->post( $queue->{'SESSION'}, $queue->{'EVENT'}, $ret );
# Argh, decrement the refcount
$_[KERNEL]->refcount_decrement( $queue->{'SESSION'}, 'SimpleDBI' );
}
# Clear the queue!
$_[HEAP]->{'QUEUE'} = [];
# Reinstate the active request if it exists
if ( defined $activequeue ) {
push( @{ $_[HEAP]->{'QUEUE'} }, $activequeue );
}
# All done!
return 1;
}
# This subroutine does the meat - sends queries to the subprocess
sub Check_Queue {
# Extensive debug
if ( DEBUG ) {
warn 'Checking the queue for events to process';
}
# Check if the subprocess is currently active
if ( ! $_[HEAP]->{'ACTIVE'} ) {
# Check if we have a query in the queue
if ( scalar( @{ $_[HEAP]->{'QUEUE'} } ) > 0 ) {
# Are we connected?
if ( ! $_[HEAP]->{'CONNECTED'} ) {
# Hmpf, maybe this query is the CONNECT one?
if ( $_[HEAP]->{'QUEUE'}->[0]->{'ACTION'} ne 'CONNECT' ) {
# WE ARE DEADLOCKED
if ( DEBUG ) {
warn "DEADLOCK -> Not connected, but $_[HEAP]->{'QUEUE'}->[0]->{'ACTION'} is on top of queue!";
}
return;
}
}
# Extensive debug
if ( DEBUG ) {
warn "Sending a $_[HEAP]->{'QUEUE'}->[0]->{'ACTION'} query to the SubProcess";
}
# Copy what we need from the top of the queue
my %queue;
$queue{'ID'} = $_[HEAP]->{'QUEUE'}->[0]->{'ID'};
$queue{'ACTION'} = $_[HEAP]->{'QUEUE'}->[0]->{'ACTION'};
# CONNECT event?
if ( $queue{'ACTION'} eq 'CONNECT' ) {
$queue{'DSN'} = $_[HEAP]->{'QUEUE'}->[0]->{'DSN'};
$queue{'USERNAME'} = $_[HEAP]->{'QUEUE'}->[0]->{'USERNAME'};
$queue{'PASSWORD'} = $_[HEAP]->{'QUEUE'}->[0]->{'PASSWORD'};
$queue{'AUTO_COMMIT'} = $_[HEAP]->{'QUEUE'}->[0]->{'AUTO_COMMIT'};
} elsif ( $queue{'ACTION'} ne 'DISCONNECT' ) {
$queue{'SQL'} = $_[HEAP]->{'QUEUE'}->[0]->{'SQL'};
if ( exists $_[HEAP]->{'QUEUE'}->[0]->{'PLACEHOLDERS'} ) {
$queue{'PLACEHOLDERS'} = $_[HEAP]->{'QUEUE'}->[0]->{'PLACEHOLDERS'};
}
$queue{'PREPARE_CACHED'} = $_[HEAP]->{'QUEUE'}->[0]->{'PREPARE_CACHED'};
if ( exists $_[HEAP]->{'QUEUE'}->[0]->{'INSERT_ID'} ) {
$queue{'INSERT_ID'} = $_[HEAP]->{'QUEUE'}->[0]->{'INSERT_ID'};
}
}
# Set the child to 'active'
$_[HEAP]->{'ACTIVE'} = 1;
# Put it in the wheel
$_[HEAP]->{'WHEEL'}->put( \%queue );
} else {
if ( DEBUG ) {
warn 'Check_Queue was called but nothing in the queue!';
}
}
} else {
if ( DEBUG ) {
warn 'Check_Queue was called but the SubProcess is active!';
}
}
return;
}
# This subroutine deletes a query from the queue
sub Delete_Query {
# ARG0 = ID
my $id = $_[ARG0];
# Validation
if ( ! defined $id ) {
# Debugging
if ( DEBUG ) {
warn 'Got a Delete_Query event with no arguments!';
}
return;
}
# Check if the id exists + not at the top of the queue :)
if ( defined @{ $_[HEAP]->{'QUEUE'} }[0] ) {
if ( $_[HEAP]->{'QUEUE'}->[0]->{'ID'} eq $id ) {
# Extensive debug
if ( DEBUG ) {
warn 'Could not delete query as it is being processed by the SubProcess!';
}
# Query is still active, nothing we can do...
return 0;
} else {
# Search through the rest of the queue and see what we get
foreach my $count ( @{ $_[HEAP]->{'QUEUE'} } ) {
if ( $_[HEAP]->{'QUEUE'}->[ $count ]->{'ID'} eq $id ) {
# Found a match, delete it!
splice( @{ $_[HEAP]->{'QUEUE'} }, $count, 1 );
# Return success
return 1;
}
}
}
}
# If we got here, we didn't find anything
return;
}