Package org.sqlite
Class SQLiteConnection
- java.lang.Object
-
- org.sqlite.SQLiteConnection
-
- All Implemented Interfaces:
java.lang.AutoCloseable
,java.sql.Connection
,java.sql.Wrapper
- Direct Known Subclasses:
JDBC3Connection
,SQLitePooledConnectionHandle
public abstract class SQLiteConnection extends java.lang.Object implements java.sql.Connection
-
-
Field Summary
Fields Modifier and Type Field Description private SQLiteConnectionConfig
connectionConfig
private SQLiteConfig.TransactionMode
currentTransactionMode
private DB
db
private boolean
firstStatementExecuted
private CoreDatabaseMetaData
meta
private static java.lang.String
RESOURCE_NAME_PREFIX
-
Constructor Summary
Constructors Constructor Description SQLiteConnection(java.lang.String url, java.lang.String fileName)
Constructor to create a connection to a database at the given location.SQLiteConnection(java.lang.String url, java.lang.String fileName, java.util.Properties prop)
Constructor to create a pre-configured connection to a database at the given location.SQLiteConnection(DB db)
Connection constructor for reusing an existing DB handle
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abort(java.util.concurrent.Executor executor)
void
addCommitListener(SQLiteCommitListener listener)
Add a listener for DB commit/rollback events, see https://www.sqlite.org/c3ref/commit_hook.htmlvoid
addUpdateListener(SQLiteUpdateListener listener)
Add a listener for DB update events, see https://www.sqlite.org/c3ref/update_hook.htmlprotected void
checkCursor(int rst, int rsc, int rsh)
Checks whether the type, concurrency, and holdability settings for aResultSet
are supported by the SQLite interface.protected void
checkOpen()
Whether an SQLite library interface to the database has been established.void
close()
void
commit()
void
deserialize(java.lang.String schema, byte[] buff)
Deserialize the schema using the given byte array.protected static java.lang.String
extractPragmasFromFilename(java.lang.String url, java.lang.String filename, java.util.Properties prop)
Extracts PRAGMA values from the filename and sets them into the Properties object which will be used to build the SQLConfig.private static java.io.File
extractResource(java.net.URL resourceAddr)
Returns a file name from the given resource address.boolean
getAutoCommit()
int
getBusyTimeout()
SQLiteConnectionConfig
getConnectionConfig()
SQLiteConfig.TransactionMode
getCurrentTransactionMode()
DB
getDatabase()
void
getLimit(SQLiteLimits limit)
java.sql.DatabaseMetaData
getMetaData()
int
getNetworkTimeout()
java.lang.String
getSchema()
CoreDatabaseMetaData
getSQLiteDatabaseMetaData()
int
getTransactionIsolation()
java.lang.String
getUrl()
boolean
isClosed()
boolean
isFirstStatementExecuted()
java.lang.String
libversion()
private static DB
open(java.lang.String url, java.lang.String origFileName, java.util.Properties props)
Opens a connection to the database using an SQLite library.void
removeCommitListener(SQLiteCommitListener listener)
Remove a listener registered for DB commit/rollback events.void
removeUpdateListener(SQLiteUpdateListener listener)
Remove a listener registered for DB update events.void
rollback()
byte[]
serialize(java.lang.String schema)
Returns a byte array representing the schema content.void
setAutoCommit(boolean ac)
void
setBusyTimeout(int timeoutMillis)
Sets the timeout value for the connection.void
setCurrentTransactionMode(SQLiteConfig.TransactionMode currentTransactionMode)
void
setFirstStatementExecuted(boolean firstStatementExecuted)
void
setLimit(SQLiteLimits limit, int value)
void
setNetworkTimeout(java.util.concurrent.Executor executor, int milliseconds)
void
setSchema(java.lang.String schema)
void
setTransactionIsolation(int level)
protected void
setTransactionMode(SQLiteConfig.TransactionMode mode)
Sets the mode that will be used to start transactions on this connection.protected java.lang.String
transactionPrefix()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.sql.Connection
beginRequest, clearWarnings, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, endRequest, getCatalog, getClientInfo, getClientInfo, getHoldability, getTypeMap, getWarnings, isReadOnly, isValid, nativeSQL, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, setCatalog, setClientInfo, setClientInfo, setHoldability, setReadOnly, setSavepoint, setSavepoint, setShardingKey, setShardingKey, setShardingKeyIfValid, setShardingKeyIfValid, setTypeMap
-
-
-
-
Field Detail
-
RESOURCE_NAME_PREFIX
private static final java.lang.String RESOURCE_NAME_PREFIX
- See Also:
- Constant Field Values
-
db
private final DB db
-
meta
private CoreDatabaseMetaData meta
-
connectionConfig
private final SQLiteConnectionConfig connectionConfig
-
currentTransactionMode
private SQLiteConfig.TransactionMode currentTransactionMode
-
firstStatementExecuted
private boolean firstStatementExecuted
-
-
Constructor Detail
-
SQLiteConnection
public SQLiteConnection(DB db)
Connection constructor for reusing an existing DB handle- Parameters:
db
-
-
SQLiteConnection
public SQLiteConnection(java.lang.String url, java.lang.String fileName) throws java.sql.SQLException
Constructor to create a connection to a database at the given location.- Parameters:
url
- The location of the database.fileName
- The database.- Throws:
java.sql.SQLException
-
SQLiteConnection
public SQLiteConnection(java.lang.String url, java.lang.String fileName, java.util.Properties prop) throws java.sql.SQLException
Constructor to create a pre-configured connection to a database at the given location.- Parameters:
url
- The location of the database file.fileName
- The database.prop
- The configurations to apply.- Throws:
java.sql.SQLException
-
-
Method Detail
-
getCurrentTransactionMode
public SQLiteConfig.TransactionMode getCurrentTransactionMode()
-
setCurrentTransactionMode
public void setCurrentTransactionMode(SQLiteConfig.TransactionMode currentTransactionMode)
-
setFirstStatementExecuted
public void setFirstStatementExecuted(boolean firstStatementExecuted)
-
isFirstStatementExecuted
public boolean isFirstStatementExecuted()
-
getConnectionConfig
public SQLiteConnectionConfig getConnectionConfig()
-
getSQLiteDatabaseMetaData
public CoreDatabaseMetaData getSQLiteDatabaseMetaData() throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getMetaData
public java.sql.DatabaseMetaData getMetaData() throws java.sql.SQLException
- Specified by:
getMetaData
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
-
getUrl
public java.lang.String getUrl()
-
setSchema
public void setSchema(java.lang.String schema) throws java.sql.SQLException
- Specified by:
setSchema
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
-
getSchema
public java.lang.String getSchema() throws java.sql.SQLException
- Specified by:
getSchema
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
-
abort
public void abort(java.util.concurrent.Executor executor) throws java.sql.SQLException
- Specified by:
abort
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
-
setNetworkTimeout
public void setNetworkTimeout(java.util.concurrent.Executor executor, int milliseconds) throws java.sql.SQLException
- Specified by:
setNetworkTimeout
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
-
getNetworkTimeout
public int getNetworkTimeout() throws java.sql.SQLException
- Specified by:
getNetworkTimeout
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
-
checkCursor
protected void checkCursor(int rst, int rsc, int rsh) throws java.sql.SQLException
Checks whether the type, concurrency, and holdability settings for aResultSet
are supported by the SQLite interface. Supported settings are:- type:
ResultSet.TYPE_FORWARD_ONLY
- concurrency:
ResultSet.CONCUR_READ_ONLY
) - holdability:
ResultSet.CLOSE_CURSORS_AT_COMMIT
- Parameters:
rst
- the type setting.rsc
- the concurrency setting.rsh
- the holdability setting.- Throws:
java.sql.SQLException
- type:
-
setTransactionMode
protected void setTransactionMode(SQLiteConfig.TransactionMode mode)
Sets the mode that will be used to start transactions on this connection.- Parameters:
mode
- One ofSQLiteConfig.TransactionMode
- See Also:
- https://www.sqlite.org/lang_transaction.html
-
getTransactionIsolation
public int getTransactionIsolation()
- Specified by:
getTransactionIsolation
in interfacejava.sql.Connection
- See Also:
Connection.getTransactionIsolation()
-
setTransactionIsolation
public void setTransactionIsolation(int level) throws java.sql.SQLException
- Specified by:
setTransactionIsolation
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
- See Also:
Connection.setTransactionIsolation(int)
-
open
private static DB open(java.lang.String url, java.lang.String origFileName, java.util.Properties props) throws java.sql.SQLException
Opens a connection to the database using an SQLite library. * @throws SQLException- Throws:
java.sql.SQLException
- See Also:
- https://www.sqlite.org/c3ref/c_open_autoproxy.html
-
extractResource
private static java.io.File extractResource(java.net.URL resourceAddr) throws java.io.IOException
Returns a file name from the given resource address.- Parameters:
resourceAddr
- The resource address.- Returns:
- The extracted file name.
- Throws:
java.io.IOException
-
getDatabase
public DB getDatabase()
-
getAutoCommit
public boolean getAutoCommit() throws java.sql.SQLException
- Specified by:
getAutoCommit
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
- See Also:
Connection.getAutoCommit()
-
setAutoCommit
public void setAutoCommit(boolean ac) throws java.sql.SQLException
- Specified by:
setAutoCommit
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
- See Also:
Connection.setAutoCommit(boolean)
-
getBusyTimeout
public int getBusyTimeout()
- Returns:
- The busy timeout value for the connection.
- See Also:
- https://www.sqlite.org/c3ref/busy_timeout.html
-
setBusyTimeout
public void setBusyTimeout(int timeoutMillis) throws java.sql.SQLException
Sets the timeout value for the connection. A timeout value less than or equal to zero turns off all busy handlers.- Parameters:
timeoutMillis
- The timeout value in milliseconds.- Throws:
java.sql.SQLException
- See Also:
- https://www.sqlite.org/c3ref/busy_timeout.html
-
setLimit
public void setLimit(SQLiteLimits limit, int value) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
getLimit
public void getLimit(SQLiteLimits limit) throws java.sql.SQLException
- Throws:
java.sql.SQLException
-
isClosed
public boolean isClosed() throws java.sql.SQLException
- Specified by:
isClosed
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
-
close
public void close() throws java.sql.SQLException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
- See Also:
Connection.close()
-
checkOpen
protected void checkOpen() throws java.sql.SQLException
Whether an SQLite library interface to the database has been established.- Throws:
java.sql.SQLException
-
libversion
public java.lang.String libversion() throws java.sql.SQLException
- Returns:
- Compile-time library version numbers.
- Throws:
java.sql.SQLException
- See Also:
- https://www.sqlite.org/c3ref/c_source_id.html
-
commit
public void commit() throws java.sql.SQLException
- Specified by:
commit
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
- See Also:
Connection.commit()
-
rollback
public void rollback() throws java.sql.SQLException
- Specified by:
rollback
in interfacejava.sql.Connection
- Throws:
java.sql.SQLException
- See Also:
Connection.rollback()
-
addUpdateListener
public void addUpdateListener(SQLiteUpdateListener listener)
Add a listener for DB update events, see https://www.sqlite.org/c3ref/update_hook.html- Parameters:
listener
- The listener to receive update events
-
removeUpdateListener
public void removeUpdateListener(SQLiteUpdateListener listener)
Remove a listener registered for DB update events.- Parameters:
listener
- The listener to no longer receive update events
-
addCommitListener
public void addCommitListener(SQLiteCommitListener listener)
Add a listener for DB commit/rollback events, see https://www.sqlite.org/c3ref/commit_hook.html- Parameters:
listener
- The listener to receive commit events
-
removeCommitListener
public void removeCommitListener(SQLiteCommitListener listener)
Remove a listener registered for DB commit/rollback events.- Parameters:
listener
- The listener to no longer receive commit/rollback events.
-
extractPragmasFromFilename
protected static java.lang.String extractPragmasFromFilename(java.lang.String url, java.lang.String filename, java.util.Properties prop) throws java.sql.SQLException
Extracts PRAGMA values from the filename and sets them into the Properties object which will be used to build the SQLConfig. The sanitized filename is returned.- Parameters:
filename
-prop
-- Returns:
- a PRAGMA-sanitized filename
- Throws:
java.sql.SQLException
-
transactionPrefix
protected java.lang.String transactionPrefix()
-
serialize
public byte[] serialize(java.lang.String schema) throws java.sql.SQLException
Returns a byte array representing the schema content. This method is intended for in-memory schemas. Serialized databases are limited to 2gb.- Parameters:
schema
- The schema to serialize- Returns:
- A byte[] holding the database content
- Throws:
java.sql.SQLException
-
deserialize
public void deserialize(java.lang.String schema, byte[] buff) throws java.sql.SQLException
Deserialize the schema using the given byte array. This method is intended for in-memory database. The call will replace the content of an existing schema. To make sure there is an existing schema, first execute ATTACH ':memory:' AS schema_name- Parameters:
schema
- The schema to serializebuff
- The buffer to deserialize- Throws:
java.sql.SQLException
-
-