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