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