mirror of
https://github.com/DBD-SQLite/DBD-SQLite
synced 2025-06-09 07:08:07 -04:00
DBD-SQLite: effort to resolve #44884; refactored error handling to follow DBI's best practices
This commit is contained in:
parent
2932e4d8d0
commit
0a29d5e86d
1 changed files with 28 additions and 30 deletions
58
dbdimp.c
58
dbdimp.c
|
@ -36,15 +36,13 @@ _sqlite_error(char *file, int line, SV *h, imp_xxh_t *imp_xxh, int rc, char *wha
|
||||||
{
|
{
|
||||||
dTHR;
|
dTHR;
|
||||||
|
|
||||||
SV *errstr = DBIc_ERRSTR(imp_xxh);
|
DBIh_SET_ERR_CHAR(h, imp_xxh, Nullch, rc, what, Nullch, Nullch);
|
||||||
sv_setiv(DBIc_ERR(imp_xxh), (IV)rc);
|
|
||||||
sv_setpv(errstr, what);
|
|
||||||
|
|
||||||
/* #7753: DBD::SQLite error shouldn't include extraneous info */
|
/* #7753: DBD::SQLite error shouldn't include extraneous info */
|
||||||
/* sv_catpvf(errstr, "(%d) at %s line %d", rc, file, line); */
|
/* sv_catpvf(errstr, "(%d) at %s line %d", rc, file, line); */
|
||||||
|
|
||||||
if ( DBIc_TRACE(imp_xxh, 0, 0, 3) ) {
|
if ( DBIc_TRACE_LEVEL(imp_xxh) >= 3 ) {
|
||||||
PerlIO_printf(DBILOGFP, "sqlite error %d recorded: %s at %s line %d\n",
|
PerlIO_printf(DBIc_LOGPIO(imp_xxh), "sqlite error %d recorded: %s at %s line %d\n",
|
||||||
rc, what, file, line);
|
rc, what, file, line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,11 +53,11 @@ _sqlite_tracef(char *file, int line, SV *h, imp_xxh_t *imp_xxh, int level, const
|
||||||
dTHR;
|
dTHR;
|
||||||
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
if ( DBIc_TRACE(imp_xxh, 0, 0, level) ) {
|
if ( DBIc_TRACE_LEVEL(imp_xxh) >= level ) {
|
||||||
char format[8192];
|
char format[8192];
|
||||||
sqlite3_snprintf(8191, format, "sqlite trace: %s at %s line %d\n", fmt, file, line);
|
sqlite3_snprintf(8191, format, "sqlite trace: %s at %s line %d\n", fmt, file, line);
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
PerlIO_vprintf(DBILOGFP, format, ap);
|
PerlIO_vprintf(DBIc_LOGPIO(imp_xxh), format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,11 +68,11 @@ _sqlite_tracef_noline(SV *h, imp_xxh_t *imp_xxh, int level, const char *fmt, ...
|
||||||
dTHR;
|
dTHR;
|
||||||
|
|
||||||
va_list ap;
|
va_list ap;
|
||||||
if ( DBIc_TRACE(imp_xxh, 0, 0, level) ) {
|
if ( DBIc_TRACE_LEVEL(imp_xxh) >= level ) {
|
||||||
char format[8192];
|
char format[8192];
|
||||||
sqlite3_snprintf(8191, format, "sqlite trace: %s\n", fmt);
|
sqlite3_snprintf(8191, format, "sqlite trace: %s\n", fmt);
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
PerlIO_vprintf(DBILOGFP, format, ap);
|
PerlIO_vprintf(DBIc_LOGPIO(imp_xxh), format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -86,13 +84,13 @@ sqlite_db_login(SV *dbh, imp_dbh_t *imp_dbh, char *dbname, char *user, char *pas
|
||||||
int retval;
|
int retval;
|
||||||
char *errmsg = NULL;
|
char *errmsg = NULL;
|
||||||
|
|
||||||
if ( DBIc_TRACE(imp_dbh, 0, 0, 3) ) {
|
if ( DBIc_TRACE_LEVEL(imp_dbh) >= 3 ) {
|
||||||
PerlIO_printf(DBILOGFP, " login '%s' (version %s)\n",
|
PerlIO_printf(DBILOGFP, " login '%s' (version %s)\n",
|
||||||
dbname, sqlite3_version);
|
dbname, sqlite3_version);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( sqlite3_open(dbname, &(imp_dbh->db)) != SQLITE_OK ) {
|
if ((retval = sqlite3_open(dbname, &(imp_dbh->db))) != SQLITE_OK ) {
|
||||||
sqlite_error(dbh, (imp_xxh_t*)imp_dbh, 1, (char*)sqlite3_errmsg(imp_dbh->db));
|
sqlite_error(dbh, (imp_xxh_t*)imp_dbh, retval, (char*)sqlite3_errmsg(imp_dbh->db));
|
||||||
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
|
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
|
||||||
}
|
}
|
||||||
DBIc_IMPSET_on(imp_dbh);
|
DBIc_IMPSET_on(imp_dbh);
|
||||||
|
@ -270,12 +268,12 @@ sqlite_st_prepare (SV *sth, imp_sth_t *imp_sth,
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
|
|
||||||
if (!DBIc_ACTIVE(imp_dbh)) {
|
if (!DBIc_ACTIVE(imp_dbh)) {
|
||||||
sqlite_error(sth, (imp_xxh_t*)imp_sth, retval, "attempt to prepare on inactive database handle");
|
sqlite_error(sth, (imp_xxh_t*)imp_sth, -2, "attempt to prepare on inactive database handle");
|
||||||
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
|
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(statement) < 1) {
|
if (strlen(statement) < 1) {
|
||||||
sqlite_error(sth, (imp_xxh_t*)imp_sth, retval, "attempt to prepare empty statement");
|
sqlite_error(sth, (imp_xxh_t*)imp_sth, -2, "attempt to prepare empty statement");
|
||||||
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
|
return FALSE; /* -> undef in lib/DBD/SQLite.pm */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -350,7 +348,7 @@ sqlite_st_execute (SV *sth, imp_sth_t *imp_sth)
|
||||||
/* warn("execute\n"); */
|
/* warn("execute\n"); */
|
||||||
|
|
||||||
if (!DBIc_ACTIVE(imp_dbh)) {
|
if (!DBIc_ACTIVE(imp_dbh)) {
|
||||||
sqlite_error(sth, (imp_xxh_t*)imp_sth, retval, "attempt to execute on inactive database handle");
|
sqlite_error(sth, (imp_xxh_t*)imp_sth, -2, "attempt to execute on inactive database handle");
|
||||||
return -2; /* -> undef in SQLite.xsi */
|
return -2; /* -> undef in SQLite.xsi */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -496,19 +494,19 @@ sqlite_bind_ph (SV *sth, imp_sth_t *imp_sth,
|
||||||
if (pos==0) {
|
if (pos==0) {
|
||||||
char errmsg[8192];
|
char errmsg[8192];
|
||||||
sqlite3_snprintf(8191, errmsg, "Unknown named parameter: %s", paramstring);
|
sqlite3_snprintf(8191, errmsg, "Unknown named parameter: %s", paramstring);
|
||||||
sqlite_error(sth, (imp_xxh_t*)imp_sth, 0, errmsg);
|
sqlite_error(sth, (imp_xxh_t*)imp_sth, -2, errmsg);
|
||||||
return FALSE; /* -> &sv_no in SQLite.xsi */
|
return FALSE; /* -> &sv_no in SQLite.xsi */
|
||||||
}
|
}
|
||||||
pos = 2 * (pos - 1);
|
pos = 2 * (pos - 1);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
sqlite_error(sth, (imp_xxh_t*)imp_sth, 0, "<param> could not be coerced to a C string");
|
sqlite_error(sth, (imp_xxh_t*)imp_sth, -2, "<param> could not be coerced to a C string");
|
||||||
return FALSE; /* -> &sv_no in SQLite.xsi */
|
return FALSE; /* -> &sv_no in SQLite.xsi */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (is_inout) {
|
if (is_inout) {
|
||||||
sqlite_error(sth, (imp_xxh_t*)imp_sth, 0, "InOut bind params not implemented");
|
sqlite_error(sth, (imp_xxh_t*)imp_sth, -2, "InOut bind params not implemented");
|
||||||
return FALSE; /* -> &sv_no in SQLite.xsi */
|
return FALSE; /* -> &sv_no in SQLite.xsi */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -951,23 +949,23 @@ void
|
||||||
sqlite3_db_create_function( SV *dbh, const char *name, int argc, SV *func )
|
sqlite3_db_create_function( SV *dbh, const char *name, int argc, SV *func )
|
||||||
{
|
{
|
||||||
D_imp_dbh(dbh);
|
D_imp_dbh(dbh);
|
||||||
int rv;
|
int retval;
|
||||||
|
|
||||||
/* Copy the function reference */
|
/* Copy the function reference */
|
||||||
SV *func_sv = newSVsv(func);
|
SV *func_sv = newSVsv(func);
|
||||||
av_push( imp_dbh->functions, func_sv );
|
av_push( imp_dbh->functions, func_sv );
|
||||||
|
|
||||||
/* warn("create_function %s with %d args\n", name, argc); */
|
/* warn("create_function %s with %d args\n", name, argc); */
|
||||||
rv = sqlite3_create_function( imp_dbh->db, name, argc, SQLITE_UTF8,
|
retval = sqlite3_create_function( imp_dbh->db, name, argc, SQLITE_UTF8,
|
||||||
func_sv,
|
func_sv,
|
||||||
imp_dbh->unicode ? sqlite_db_func_dispatcher_unicode
|
imp_dbh->unicode ? sqlite_db_func_dispatcher_unicode
|
||||||
: sqlite_db_func_dispatcher_no_unicode,
|
: sqlite_db_func_dispatcher_no_unicode,
|
||||||
NULL, NULL );
|
NULL, NULL );
|
||||||
if ( rv != SQLITE_OK )
|
if ( retval != SQLITE_OK )
|
||||||
{
|
{
|
||||||
char errmsg[8192];
|
char errmsg[8192];
|
||||||
sqlite3_snprintf(8191, errmsg, "sqlite_create_function failed with error %s", sqlite3_errmsg(imp_dbh->db));
|
sqlite3_snprintf(8191, errmsg, "sqlite_create_function failed with error %s", sqlite3_errmsg(imp_dbh->db));
|
||||||
sqlite_error(dbh, (imp_xxh_t*)imp_dbh, 0, errmsg);
|
sqlite_error(dbh, (imp_xxh_t*)imp_dbh, retval, errmsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -975,14 +973,14 @@ void
|
||||||
sqlite3_db_enable_load_extension( SV *dbh, int onoff )
|
sqlite3_db_enable_load_extension( SV *dbh, int onoff )
|
||||||
{
|
{
|
||||||
D_imp_dbh(dbh);
|
D_imp_dbh(dbh);
|
||||||
int rv;
|
int retval;
|
||||||
|
|
||||||
rv = sqlite3_enable_load_extension( imp_dbh->db, onoff );
|
retval = sqlite3_enable_load_extension( imp_dbh->db, onoff );
|
||||||
if ( rv != SQLITE_OK )
|
if ( retval != SQLITE_OK )
|
||||||
{
|
{
|
||||||
char errmsg[8192];
|
char errmsg[8192];
|
||||||
sqlite3_snprintf(8191, errmsg, "sqlite_enable_load_extension failed with error %s", sqlite3_errmsg(imp_dbh->db));
|
sqlite3_snprintf(8191, errmsg, "sqlite_enable_load_extension failed with error %s", sqlite3_errmsg(imp_dbh->db));
|
||||||
sqlite_error(dbh, (imp_xxh_t*)imp_dbh, 0, errmsg);
|
sqlite_error(dbh, (imp_xxh_t*)imp_dbh, retval, errmsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1183,24 +1181,24 @@ void
|
||||||
sqlite3_db_create_aggregate( SV *dbh, const char *name, int argc, SV *aggr_pkg )
|
sqlite3_db_create_aggregate( SV *dbh, const char *name, int argc, SV *aggr_pkg )
|
||||||
{
|
{
|
||||||
D_imp_dbh(dbh);
|
D_imp_dbh(dbh);
|
||||||
int rv;
|
int retval;
|
||||||
|
|
||||||
/* Copy the aggregate reference */
|
/* Copy the aggregate reference */
|
||||||
SV *aggr_pkg_copy = newSVsv(aggr_pkg);
|
SV *aggr_pkg_copy = newSVsv(aggr_pkg);
|
||||||
av_push( imp_dbh->aggregates, aggr_pkg_copy );
|
av_push( imp_dbh->aggregates, aggr_pkg_copy );
|
||||||
|
|
||||||
rv = sqlite3_create_function( imp_dbh->db, name, argc, SQLITE_UTF8,
|
retval = sqlite3_create_function( imp_dbh->db, name, argc, SQLITE_UTF8,
|
||||||
aggr_pkg_copy,
|
aggr_pkg_copy,
|
||||||
NULL,
|
NULL,
|
||||||
sqlite_db_aggr_step_dispatcher,
|
sqlite_db_aggr_step_dispatcher,
|
||||||
sqlite_db_aggr_finalize_dispatcher
|
sqlite_db_aggr_finalize_dispatcher
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( rv != SQLITE_OK )
|
if ( retval != SQLITE_OK )
|
||||||
{
|
{
|
||||||
char errmsg[8192];
|
char errmsg[8192];
|
||||||
sqlite3_snprintf(8191, errmsg, "sqlite_create_aggregate failed with error %s", sqlite3_errmsg(imp_dbh->db));
|
sqlite3_snprintf(8191, errmsg, "sqlite_create_aggregate failed with error %s", sqlite3_errmsg(imp_dbh->db));
|
||||||
sqlite_error(dbh, (imp_xxh_t*)imp_dbh, 0, errmsg);
|
sqlite_error(dbh, (imp_xxh_t*)imp_dbh, retval, errmsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue