Class SqlJetDb
- java.lang.Object
-
- org.tmatesoft.sqljet.core.table.engine.SqlJetEngine
-
- org.tmatesoft.sqljet.core.table.SqlJetDb
-
public class SqlJetDb extends SqlJetEngine
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
Fields Modifier and Type Field Description static java.io.File
IN_MEMORY
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
Constructors Constructor Description SqlJetDb(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ISqlJetTableDef
alterTable(java.lang.String sql)
Alters table.protected void
closeResources()
ISqlJetIndexDef
createIndex(java.lang.String sql)
Create index from SQL clause.ISqlJetTableDef
createTable(java.lang.String sql)
Create table from SQL clause.ISqlJetTriggerDef
createTrigger(java.lang.String sql)
Creates trigger from SQL clause.ISqlJetViewDef
createView(java.lang.String sql)
Creates view from SQL clause.ISqlJetVirtualTableDef
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.ISqlJetSchema
getSchema()
Get database schema.ISqlJetTable
getTable(java.lang.String tableName)
Open table.SqlJetDb
getTemporaryDatabase()
SqlJetDb
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
runReadTransaction(ISqlJetTransaction op)
Run read-only transaction.java.lang.Object
runTransaction(ISqlJetTransaction op, SqlJetTransactionMode mode)
Run transaction.java.lang.Object
runWithLock(ISqlJetRunnableWithLock op)
Do some actions with locking database's internal threads synchronization mutex.java.lang.Object
runWriteTransaction(ISqlJetTransaction op)
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
-
-
-
-
Constructor Detail
-
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
public SqlJetDb(java.io.File file, boolean writable, ISqlJetFileSystem fs)
-
SqlJetDb
public SqlJetDb(java.io.File file, boolean writable, java.lang.String fsName) throws SqlJetException
- Throws:
SqlJetException
-
-
Method Detail
-
open
public static SqlJetDb open(java.io.File file, boolean write) throws SqlJetException
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
public java.lang.Object runWithLock(ISqlJetRunnableWithLock op) throws SqlJetException
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
public ISqlJetSchema getSchema() throws SqlJetException
Get database schema.- Returns:
- database schema.
- Throws:
SqlJetException
-
getTable
public ISqlJetTable getTable(java.lang.String tableName) throws SqlJetException
Open table.- Parameters:
tableName
- name of the table to open.- Returns:
- opened table
- Throws:
SqlJetException
-
runWriteTransaction
public java.lang.Object runWriteTransaction(ISqlJetTransaction op) throws SqlJetException
Run modifications in write transaction.- Parameters:
op
- transaction to run.- Returns:
- result of the
ISqlJetTransaction.run(SqlJetDb)
call. - Throws:
SqlJetException
-
runReadTransaction
public java.lang.Object runReadTransaction(ISqlJetTransaction op) throws SqlJetException
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
public java.lang.Object pragma(java.lang.String sql) throws SqlJetException
Executes pragma statement. If statement queries pragma value then pragma value will be returned.- Throws:
SqlJetException
-
createTable
public ISqlJetTableDef createTable(java.lang.String sql) throws SqlJetException
Create table from SQL clause.- Parameters:
sql
- CREATE TABLE ... sentence.- Returns:
- definition of create table.
- Throws:
SqlJetException
-
createIndex
public ISqlJetIndexDef createIndex(java.lang.String sql) throws SqlJetException
Create index from SQL clause.- Parameters:
sql
- CREATE INDEX ... sentence.- Returns:
- definition of created index.
- Throws:
SqlJetException
-
dropTable
public void dropTable(java.lang.String tableName) throws SqlJetException
Drop table.- Parameters:
tableName
- name of table to drop.- Throws:
SqlJetException
-
dropIndex
public void dropIndex(java.lang.String indexName) throws SqlJetException
Drop index.- Parameters:
indexName
- name of the index to drop.- Throws:
SqlJetException
-
dropView
public void dropView(java.lang.String viewName) throws SqlJetException
Drop view.- Parameters:
viewName
- name of the view to drop.- Throws:
SqlJetException
-
dropTrigger
public void dropTrigger(java.lang.String triggerName) throws SqlJetException
Drop trigger.- Parameters:
triggerName
- name of the trigger to drop.- Throws:
SqlJetException
-
alterTable
public ISqlJetTableDef alterTable(java.lang.String sql) throws SqlJetException
Alters table.- Parameters:
sql
- ALTER TABLE ... sentence.- Returns:
- altered table schema definition.
- Throws:
SqlJetException
-
createVirtualTable
public ISqlJetVirtualTableDef createVirtualTable(java.lang.String sql) throws SqlJetException
Creates virtual table from SQL clause.- Parameters:
sql
- CREATE VIRTUAL TABLE ... sentence.- Returns:
- definition of create virtual table.
- Throws:
SqlJetException
-
createView
public ISqlJetViewDef createView(java.lang.String sql) throws SqlJetException
Creates view from SQL clause.- Parameters:
sql
- CREATE VIEW X AS SELECT ... sentence.- Returns:
- definition of the view being created.
- Throws:
SqlJetException
-
createTrigger
public ISqlJetTriggerDef createTrigger(java.lang.String sql) throws SqlJetException
Creates trigger from SQL clause.- Parameters:
sql
- CREATE TRIGGER ... sentence.- Returns:
- definition of the trigger being created.
- Throws:
SqlJetException
-
getTemporaryDatabase
public SqlJetDb getTemporaryDatabase() throws SqlJetException
- Returns:
- SqlJetDb object for temporary database
- Throws:
SqlJetException
- See Also:
getTemporaryDatabase(boolean)
-
getTemporaryDatabase
public SqlJetDb getTemporaryDatabase(boolean inMemory) throws SqlJetException
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
protected void closeResources() throws SqlJetException
- Overrides:
closeResources
in classSqlJetEngine
- Throws:
SqlJetException
-
-