1
0
Fork 0
mirror of https://github.com/DBD-SQLite/DBD-SQLite synced 2025-06-07 06:08:38 -04:00

Add sqlite_error_offset

This commit is contained in:
Kenichi Ishigaki 2022-02-23 19:43:21 +09:00
parent 6d0744549a
commit d4aea379d4
3 changed files with 33 additions and 0 deletions

View file

@ -391,6 +391,24 @@ txn_state(SV* dbh, SV *schema = &PL_sv_undef)
OUTPUT: OUTPUT:
RETVAL RETVAL
static int
error_offset(dbh)
SV *dbh
ALIAS:
DBD::SQLite::db::sqlite_error_offset = 1
CODE:
{
#if SQLITE_VERSION_NUMBER >= 3038000
D_imp_dbh(dbh);
RETVAL = sqlite3_error_offset(imp_dbh->db);
#else
RETVAL = -1;
#endif
}
OUTPUT:
RETVAL
MODULE = DBD::SQLite PACKAGE = DBD::SQLite::st MODULE = DBD::SQLite PACKAGE = DBD::SQLite::st
PROTOTYPES: DISABLE PROTOTYPES: DISABLE

View file

@ -62,6 +62,7 @@ sub driver {
DBD::SQLite::db->install_method('sqlite_db_config'); DBD::SQLite::db->install_method('sqlite_db_config');
DBD::SQLite::db->install_method('sqlite_get_autocommit'); DBD::SQLite::db->install_method('sqlite_get_autocommit');
DBD::SQLite::db->install_method('sqlite_txn_state'); DBD::SQLite::db->install_method('sqlite_txn_state');
DBD::SQLite::db->install_method('sqlite_error_offset');
$methods_are_installed++; $methods_are_installed++;
} }
@ -2431,6 +2432,12 @@ can be imported from DBD::SQLite::Constants. You may pass an optional
schema name (usually "main"). If SQLite does not support this function, schema name (usually "main"). If SQLite does not support this function,
or if you pass a wrong schema name, -1 is returned. or if you pass a wrong schema name, -1 is returned.
=head2 $dbh->sqlite_error_offset()
Returns the byte offset of the start of a problematic input SQL token
or -1 if the most recent error does not reference a specific token in
the input SQL (or DBD::SQLite is built with an older version of SQLite).
=head1 DRIVER FUNCTIONS =head1 DRIVER FUNCTIONS
=head2 DBD::SQLite::compile_options() =head2 DBD::SQLite::compile_options()

View file

@ -25,4 +25,12 @@ ok($@, 'Statement 2 generated an error');
is( $DBI::err, 19, '$DBI::err ok' ); is( $DBI::err, 19, '$DBI::err ok' );
like( $DBI::errstr, qr/column a is not unique|UNIQUE constraint failed/, '$DBI::errstr ok' ); like( $DBI::errstr, qr/column a is not unique|UNIQUE constraint failed/, '$DBI::errstr ok' );
if ($DBD::SQLite::sqlite_version_number && $DBD::SQLite::sqlite_version_number >= 3038000) {
my $sql = 'insert testerror values (1, 5)';
eval { $dbh->do($sql) };
my $offset = $dbh->sqlite_error_offset;
ok $offset != -1, "error offset: $offset";
note substr($sql, 0, $offset) . '<*error*>' . substr($sql, $offset);
}
done_testing; done_testing;