1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-29 08:01:23 +03:00

Flesh out the error state captured by SqliteException.java. Doc additions.

FossilOrigin-Name: 5c5397ff15543f4b3620244d9e57e15708eafcab1d42c9f87b4a60f0c01e8858
This commit is contained in:
stephan
2023-10-09 12:45:28 +00:00
parent 4a11142072
commit 66dacae4c3
4 changed files with 51 additions and 11 deletions

View File

@ -29,9 +29,14 @@ public final class Sqlite implements AutoCloseable {
this.db = db;
}
public static Sqlite open(String filename, int flags, String zVfs){
/**
Returns a newly-opened db connection or throws SqliteException if
opening fails. All arguments are as documented for
sqlite3_open_v2().
*/
public static Sqlite open(String filename, int flags, String vfsName){
final OutputPointer.sqlite3 out = new OutputPointer.sqlite3();
final int rc = sqlite3_open_v2(filename, out, flags, zVfs);
final int rc = sqlite3_open_v2(filename, out, flags, vfsName);
final sqlite3 n = out.take();
if( 0!=rc ){
if( null==n ) throw new SqliteException(rc);
@ -55,6 +60,10 @@ public final class Sqlite implements AutoCloseable {
}
}
/**
Returns this object's underlying native db handle, or null if
this instance has been closed.
*/
sqlite3 dbHandle(){ return this.db; }
}

View File

@ -21,22 +21,53 @@ import static org.sqlite.jni.CApi.*;
and C via JNI.
*/
public final class SqliteException extends java.lang.RuntimeException {
int errCode = SQLITE_ERROR;
int xerrCode = SQLITE_ERROR;
int errOffset = -1;
int sysErrno = 0;
/**
Records the given error string and uses SQLITE_ERROR for both the
error code and extended error code.
*/
public SqliteException(String msg){
super(msg);
}
/**
Uses sqlite3_errstr(sqlite3ResultCode) for the error string and
sets both the error code and extended error code to the given
value.
*/
public SqliteException(int sqlite3ResultCode){
super(sqlite3_errstr(sqlite3ResultCode));
errCode = xerrCode = sqlite3ResultCode;
}
/**
Records the current error state of db (which must not be null and
must refer to an opened db object) then closes it.
*/
public SqliteException(sqlite3 db){
super(sqlite3_errmsg(db));
errCode = sqlite3_errcode(db);
xerrCode = sqlite3_extended_errcode(db);
errOffset = sqlite3_error_offset(db);
sysErrno = sqlite3_system_errno(db);
db.close();
}
/**
Records the current error state of db (which must not be null and must
refer to an open database) then closes it.
*/
public SqliteException(Sqlite db){
this(db.dbHandle());
}
public int errcode(){ return errCode; }
public int extendedErrcode(){ return xerrCode; }
public int errorOffset(){ return errOffset; }
public int systemErrno(){ return sysErrno; }
}