@@ -460,16 +460,66 @@ int preprocess(const zend_string* sql, char* sql_out, HashTable* named_params)
460
460
}
461
461
462
462
/* map driver specific error message to PDO error */
463
- void _firebird_error (pdo_dbh_t * dbh , pdo_stmt_t * stmt , char const * file , zend_long line ) /* {{{ */
463
+ void _firebird_error (pdo_dbh_t * dbh , pdo_stmt_t * stmt , const char * state , const char * msg ) /* {{{ */
464
464
{
465
465
pdo_error_type * const error_code = stmt ? & stmt -> error_code : & dbh -> error_code ;
466
+ pdo_firebird_db_handle * H = (pdo_firebird_db_handle * )dbh -> driver_data ;
467
+ pdo_firebird_error_info * einfo = & H -> einfo ;
468
+ int sqlcode = -999 ;
469
+
470
+ if (einfo -> errmsg ) {
471
+ pefree (einfo -> errmsg , dbh -> is_persistent );
472
+ einfo -> errmsg = NULL ;
473
+ einfo -> errmsg_length = 0 ;
474
+ }
475
+
476
+ if (H -> isc_status && (H -> isc_status [0 ] == 1 && H -> isc_status [1 ] > 0 )) {
477
+ char buf [512 ];
478
+ size_t buf_size = sizeof (buf ), read_len = 0 ;
479
+ ssize_t tmp_len ;
480
+ const ISC_STATUS * s = H -> isc_status ;
481
+ sqlcode = isc_sqlcode (s );
482
+
483
+ while ((buf_size > (read_len + 1 )) && (tmp_len = fb_interpret (& buf [read_len ], (buf_size - read_len - 1 ), & s )) && tmp_len > 0 ) {
484
+ read_len += tmp_len ;
485
+ buf [read_len ++ ] = ' ' ;
486
+ }
466
487
467
- strcpy (* error_code , "HY000" );
488
+ /* remove last space */
489
+ if (read_len ) {
490
+ buf [read_len -- ] = '\0' ;
491
+ }
492
+
493
+ einfo -> errmsg_length = read_len ;
494
+ einfo -> errmsg = pestrndup (buf , read_len + 1 , dbh -> is_persistent );
495
+
496
+ #if FB_API_VER >= 25
497
+ char sqlstate [sizeof (pdo_error_type )];
498
+ fb_sqlstate (sqlstate , H -> isc_status );
499
+ if (sqlstate != NULL && strlen (sqlstate ) < sizeof (pdo_error_type )) {
500
+ strcpy (* error_code , sqlstate );
501
+ goto end ;
502
+ }
503
+ #endif
504
+ } else if (msg ) {
505
+ einfo -> errmsg_length = strlen (msg );
506
+ einfo -> errmsg = pestrndup (msg , einfo -> errmsg_length + 1 , dbh -> is_persistent );
507
+ }
508
+
509
+ if (state ) {
510
+ strcpy (* error_code , state );
511
+ } else {
512
+ strcpy (* error_code , "HY000" );
513
+ }
514
+
515
+ end :
516
+ einfo -> sqlcode = sqlcode ;
517
+ if (!dbh -> methods ) {
518
+ pdo_throw_exception (0 , einfo -> errmsg , error_code );
519
+ }
468
520
}
469
521
/* }}} */
470
522
471
- #define RECORD_ERROR (dbh ) _firebird_error(dbh, NULL, __FILE__, __LINE__)
472
-
473
523
/* called by PDO to close a db handle */
474
524
static void firebird_handle_closer (pdo_dbh_t * dbh ) /* {{{ */
475
525
{
@@ -478,17 +528,17 @@ static void firebird_handle_closer(pdo_dbh_t *dbh) /* {{{ */
478
528
if (dbh -> in_txn ) {
479
529
if (dbh -> auto_commit ) {
480
530
if (isc_commit_transaction (H -> isc_status , & H -> tr )) {
481
- RECORD_ERROR (dbh );
531
+ firebird_error (dbh );
482
532
}
483
533
} else {
484
534
if (isc_rollback_transaction (H -> isc_status , & H -> tr )) {
485
- RECORD_ERROR (dbh );
535
+ firebird_error (dbh );
486
536
}
487
537
}
488
538
}
489
539
490
540
if (isc_detach_database (H -> isc_status , & H -> db )) {
491
- RECORD_ERROR (dbh );
541
+ firebird_error (dbh );
492
542
}
493
543
494
544
if (H -> date_format ) {
@@ -501,6 +551,11 @@ static void firebird_handle_closer(pdo_dbh_t *dbh) /* {{{ */
501
551
efree (H -> timestamp_format );
502
552
}
503
553
554
+ if (H -> einfo .errmsg ) {
555
+ pefree (H -> einfo .errmsg , dbh -> is_persistent );
556
+ H -> einfo .errmsg = NULL ;
557
+ }
558
+
504
559
pefree (H , dbh -> is_persistent );
505
560
}
506
561
/* }}} */
@@ -547,7 +602,7 @@ static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
547
602
548
603
/* fill the output sqlda with information about the prepared query */
549
604
if (isc_dsql_describe (H -> isc_status , & s , PDO_FB_SQLDA_VERSION , & S -> out_sqlda )) {
550
- RECORD_ERROR (dbh );
605
+ firebird_error (dbh );
551
606
break ;
552
607
}
553
608
@@ -574,7 +629,7 @@ static bool firebird_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, /* {{{ */
574
629
575
630
} while (0 );
576
631
577
- RECORD_ERROR (dbh );
632
+ firebird_error (dbh );
578
633
579
634
zend_hash_destroy (np );
580
635
FREE_HASHTABLE (np );
@@ -612,15 +667,15 @@ static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const zend_string *sql) /*
612
667
613
668
/* execute the statement */
614
669
if (isc_dsql_execute2 (H -> isc_status , & H -> tr , & stmt , PDO_FB_SQLDA_VERSION , & in_sqlda , & out_sqlda )) {
615
- RECORD_ERROR (dbh );
670
+ firebird_error (dbh );
616
671
ret = -1 ;
617
672
goto free_statement ;
618
673
}
619
674
620
675
/* find out how many rows were affected */
621
676
if (isc_dsql_sql_info (H -> isc_status , & stmt , sizeof (info_count ), const_cast (info_count ),
622
677
sizeof (result ), result )) {
623
- RECORD_ERROR (dbh );
678
+ firebird_error (dbh );
624
679
ret = -1 ;
625
680
goto free_statement ;
626
681
}
@@ -648,13 +703,13 @@ static zend_long firebird_handle_doer(pdo_dbh_t *dbh, const zend_string *sql) /*
648
703
649
704
/* commit if we're in auto_commit mode */
650
705
if (dbh -> auto_commit && isc_commit_retaining (H -> isc_status , & H -> tr )) {
651
- RECORD_ERROR (dbh );
706
+ firebird_error (dbh );
652
707
}
653
708
654
709
free_statement :
655
710
656
711
if (isc_dsql_free_statement (H -> isc_status , & stmt , DSQL_drop )) {
657
- RECORD_ERROR (dbh );
712
+ firebird_error (dbh );
658
713
}
659
714
660
715
return ret ;
@@ -746,7 +801,7 @@ static bool firebird_handle_begin(pdo_dbh_t *dbh) /* {{{ */
746
801
}
747
802
#endif
748
803
if (isc_start_transaction (H -> isc_status , & H -> tr , 1 , & H -> db , (unsigned short )(ptpb - tpb ), tpb )) {
749
- RECORD_ERROR (dbh );
804
+ firebird_error (dbh );
750
805
return false;
751
806
}
752
807
return true;
@@ -759,7 +814,7 @@ static bool firebird_handle_commit(pdo_dbh_t *dbh) /* {{{ */
759
814
pdo_firebird_db_handle * H = (pdo_firebird_db_handle * )dbh -> driver_data ;
760
815
761
816
if (isc_commit_transaction (H -> isc_status , & H -> tr )) {
762
- RECORD_ERROR (dbh );
817
+ firebird_error (dbh );
763
818
return false;
764
819
}
765
820
return true;
@@ -772,7 +827,7 @@ static bool firebird_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
772
827
pdo_firebird_db_handle * H = (pdo_firebird_db_handle * )dbh -> driver_data ;
773
828
774
829
if (isc_rollback_transaction (H -> isc_status , & H -> tr )) {
775
- RECORD_ERROR (dbh );
830
+ firebird_error (dbh );
776
831
return false;
777
832
}
778
833
return true;
@@ -804,7 +859,7 @@ static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const zend_string *sql,
804
859
805
860
/* allocate the statement */
806
861
if (isc_dsql_allocate_statement (H -> isc_status , & H -> db , s )) {
807
- RECORD_ERROR (dbh );
862
+ firebird_error (dbh );
808
863
return 0 ;
809
864
}
810
865
@@ -820,7 +875,7 @@ static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const zend_string *sql,
820
875
821
876
/* prepare the statement */
822
877
if (isc_dsql_prepare (H -> isc_status , & H -> tr , s , 0 , new_sql , H -> sql_dialect , out_sqlda )) {
823
- RECORD_ERROR (dbh );
878
+ firebird_error (dbh );
824
879
efree (new_sql );
825
880
return 0 ;
826
881
}
@@ -848,7 +903,7 @@ static bool firebird_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *
848
903
if (bval ) {
849
904
/* turning on auto_commit with an open transaction is illegal, because
850
905
we won't know what to do with it */
851
- H -> last_app_error = "Cannot enable auto-commit while a transaction is already open" ;
906
+ firebird_error_with_info ( dbh , "HY000" , "Cannot enable auto-commit while a transaction is already open" ) ;
852
907
return false;
853
908
} else {
854
909
/* close the transaction */
@@ -992,21 +1047,11 @@ static int firebird_handle_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *v
992
1047
static void pdo_firebird_fetch_error_func (pdo_dbh_t * dbh , pdo_stmt_t * stmt , zval * info ) /* {{{ */
993
1048
{
994
1049
pdo_firebird_db_handle * H = (pdo_firebird_db_handle * )dbh -> driver_data ;
995
- const ISC_STATUS * s = H -> isc_status ;
996
- char buf [400 ];
997
- zend_long i = 0 , l , sqlcode = isc_sqlcode (s );
998
-
999
- if (sqlcode ) {
1000
- add_next_index_long (info , sqlcode );
1001
-
1002
- while ((sizeof (buf )> (i + 2 ))&& (l = fb_interpret (& buf [i ],(sizeof (buf )- i - 2 ),& s ))) {
1003
- i += l ;
1004
- strcpy (& buf [i ++ ], " " );
1005
- }
1006
- add_next_index_string (info , buf );
1007
- } else if (H -> last_app_error ) {
1008
- add_next_index_long (info , -999 );
1009
- add_next_index_string (info , const_cast (H -> last_app_error ));
1050
+ if (H -> einfo .sqlcode != IS_NULL ) {
1051
+ add_next_index_long (info , H -> einfo .sqlcode );
1052
+ }
1053
+ if (H -> einfo .errmsg && H -> einfo .errmsg_length ) {
1054
+ add_next_index_stringl (info , H -> einfo .errmsg , H -> einfo .errmsg_length );
1010
1055
}
1011
1056
}
1012
1057
/* }}} */
0 commit comments