Class SqlJetDb
Connection to database. This class currently is main entry point in SQLJet API.
It allows to perform next tasks:
- Open existed and create new SQLite database.
- Get and modify database's schema.
- Control transactions.
- Read, search and modify data in database.
- Get and set database's options.
- Author:
- TMate Software Ltd., Sergey Scherbina (sergey.scherbina@gmail.com), Dmitry Stadnik (dtrace@seznam.cz)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final java.io.File
File name for in memory database.Fields inherited from class org.tmatesoft.sqljet.core.table.engine.SqlJetEngine
btree, dbHandle, file, FILE_SYSTEM_MANAGER, fileSystem, open, writable
-
Constructor Summary
ConstructorsConstructorDescriptionSqlJetDb
(java.io.File file, boolean writable) Creates connection to database but not open it.SqlJetDb
(java.io.File file, boolean writable, java.lang.String fsName) SqlJetDb
(java.io.File file, boolean writable, ISqlJetFileSystem fs) -
Method Summary
Modifier and TypeMethodDescriptionalterTable
(java.lang.String sql) Alters table.protected void
createIndex
(java.lang.String sql) Create index from SQL clause.createTable
(java.lang.String sql) Create table from SQL clause.createTrigger
(java.lang.String sql) Creates trigger from SQL clause.createView
(java.lang.String sql) Creates view from SQL clause.createVirtualTable
(java.lang.String sql) Creates virtual table from SQL clause.void
dropIndex
(java.lang.String indexName) Drop index.void
dropTable
(java.lang.String tableName) Drop table.void
dropTrigger
(java.lang.String triggerName) Drop trigger.void
dropView
(java.lang.String viewName) Drop view.Get database schema.getTable
(java.lang.String tableName) Open table.getTemporaryDatabase
(boolean inMemory) Opens temporary on-disk database which life span is less or equal to that of this object.static SqlJetDb
open
(java.io.File file, boolean write) Opens connection to data base.static SqlJetDb
open
(java.io.File file, boolean write, java.lang.String fsName) static SqlJetDb
open
(java.io.File file, boolean write, ISqlJetFileSystem fs) java.lang.Object
pragma
(java.lang.String sql) Executes pragma statement.java.lang.Object
Run read-only transaction.java.lang.Object
Run transaction.java.lang.Object
Do some actions with locking database's internal threads synchronization mutex.java.lang.Object
Run modifications in write transaction.Methods inherited from class org.tmatesoft.sqljet.core.table.engine.SqlJetEngine
beginTransaction, checkOpen, close, commit, finalize, getBusyHandler, getCacheSize, getFile, getFileSystem, getJournalMode, getMutex, getOptions, getSafetyLevel, getSchemaInternal, getTransactionMode, isInTransaction, isOpen, isWritable, open, readSchema, refreshSchema, registerFileSystem, rollback, runEngineTransaction, runSynchronized, setBusyHandler, setCacheSize, setJournalMode, setSafetyLevel, unregisterFileSystem
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
IN_MEMORY
public static final java.io.File IN_MEMORYFile name for in memory database.
-
-
Constructor Details
-
SqlJetDb
public SqlJetDb(java.io.File file, boolean writable) Creates connection to database but not open it. Doesn't open database file until not called method
SqlJetEngine.open()
.File could be null or have special value
IN_MEMORY
. If file is null then will be created temporary file which will be deleted at close. If file isIN_MEMORY
then file doesn't created and instead database will placed in memory. If regular file is specified but doesn't exist then it will be tried to created.- Parameters:
file
- path to data base. Could be null orIN_MEMORY
.writable
- if true then will allow data modification.
-
SqlJetDb
-
SqlJetDb
public SqlJetDb(java.io.File file, boolean writable, java.lang.String fsName) throws SqlJetException - Throws:
SqlJetException
-
-
Method Details
-
open
Opens connection to data base. It does not create any locking on database. First lock will be created when be called any method which requires real access to options or schema.
File could be null or have special value
IN_MEMORY
. If file is null then will be created temporary file which will be deleted at close. If file isIN_MEMORY
then file doesn't created and instead database will placed in memory. If regular file is specified but doesn't exist then it will be tried to created.- Parameters:
file
- path to data base. Could be null orIN_MEMORY
.write
- open for writing if true.- Throws:
SqlJetException
- if any trouble with access to file or database format.
-
open
public static SqlJetDb open(java.io.File file, boolean write, ISqlJetFileSystem fs) throws SqlJetException - Parameters:
file
-write
-fs
-- Returns:
- SqlJetDb object for opened database
- Throws:
SqlJetException
-
open
public static SqlJetDb open(java.io.File file, boolean write, java.lang.String fsName) throws SqlJetException - Parameters:
file
-write
-fsName
-- Returns:
- SqlJetDb object for opened database
- Throws:
SqlJetException
-
runWithLock
Do some actions with locking database's internal threads synchronization mutex. It is related only with synchronization of access to one connection from multiple threads. It is not related with transactions and locks of database file. For concurrent access to database from threads or processes use transactions.- Parameters:
op
- operation to run- Returns:
- result of the
ISqlJetRunnableWithLock.runWithLock(SqlJetDb)
call. - Throws:
SqlJetException
- in case operation fails to run.
-
getSchema
Get database schema.- Returns:
- database schema.
- Throws:
SqlJetException
-
getTable
Open table.- Parameters:
tableName
- name of the table to open.- Returns:
- opened table
- Throws:
SqlJetException
-
runWriteTransaction
Run modifications in write transaction.- Parameters:
op
- transaction to run.- Returns:
- result of the
ISqlJetTransaction.run(SqlJetDb)
call. - Throws:
SqlJetException
-
runReadTransaction
Run read-only transaction.- Parameters:
op
- transaction to run.- Returns:
- result of the
ISqlJetTransaction.run(SqlJetDb)
call. - Throws:
SqlJetException
-
runTransaction
public java.lang.Object runTransaction(ISqlJetTransaction op, SqlJetTransactionMode mode) throws SqlJetException Run transaction.- Parameters:
op
- transaction's body (closure).mode
- transaction's mode.- Returns:
- result of the
ISqlJetTransaction.run(SqlJetDb)
call. - Throws:
SqlJetException
-
pragma
Executes pragma statement. If statement queries pragma value then pragma value will be returned.- Throws:
SqlJetException
-
createTable
Create table from SQL clause.- Parameters:
sql
- CREATE TABLE ... sentence.- Returns:
- definition of create table.
- Throws:
SqlJetException
-
createIndex
Create index from SQL clause.- Parameters:
sql
- CREATE INDEX ... sentence.- Returns:
- definition of created index.
- Throws:
SqlJetException
-
dropTable
Drop table.- Parameters:
tableName
- name of table to drop.- Throws:
SqlJetException
-
dropIndex
Drop index.- Parameters:
indexName
- name of the index to drop.- Throws:
SqlJetException
-
dropView
Drop view.- Parameters:
viewName
- name of the view to drop.- Throws:
SqlJetException
-
dropTrigger
Drop trigger.- Parameters:
triggerName
- name of the trigger to drop.- Throws:
SqlJetException
-
alterTable
Alters table.- Parameters:
sql
- ALTER TABLE ... sentence.- Returns:
- altered table schema definition.
- Throws:
SqlJetException
-
createVirtualTable
Creates virtual table from SQL clause.- Parameters:
sql
- CREATE VIRTUAL TABLE ... sentence.- Returns:
- definition of create virtual table.
- Throws:
SqlJetException
-
createView
Creates view from SQL clause.- Parameters:
sql
- CREATE VIEW X AS SELECT ... sentence.- Returns:
- definition of the view being created.
- Throws:
SqlJetException
-
createTrigger
Creates trigger from SQL clause.- Parameters:
sql
- CREATE TRIGGER ... sentence.- Returns:
- definition of the trigger being created.
- Throws:
SqlJetException
-
getTemporaryDatabase
- Returns:
- SqlJetDb object for temporary database
- Throws:
SqlJetException
- See Also:
-
getTemporaryDatabase
Opens temporary on-disk database which life span is less or equal to that of this object. Temporary database is closed and deleted as soon as this database connection is closed. Temporary file is used to store temporary database. Subsequent calls to this method will return the same temporary database In case previously create temporary database is closed by the user, then another one is created by this method.- Parameters:
inMemory
- when true open temporary database in memory.- Returns:
- temporary database being created or existing temporary database.
- Throws:
SqlJetException
-
closeResources
- Overrides:
closeResources
in classSqlJetEngine
- Throws:
SqlJetException
-