public class KiWiConnection extends Object implements AutoCloseable
Modifier and Type | Class and Description |
---|---|
protected static interface |
KiWiConnection.RetryCommand<T> |
protected class |
KiWiConnection.RetryExecution<T>
A generic implementation of an SQL command that might fail (e.g.
|
Modifier and Type | Field and Description |
---|---|
protected CacheManager |
cacheManager |
protected Connection |
connection |
protected KiWiDialect |
dialect |
protected KiWiPersistence |
persistence |
protected TripleTable<KiWiTriple> |
tripleBatch |
Constructor and Description |
---|
KiWiConnection(KiWiPersistence persistence,
KiWiDialect dialect,
CacheManager cacheManager) |
Modifier and Type | Method and Description |
---|---|
void |
close()
Releases this
Connection object's database and JDBC resources
immediately instead of waiting for them to be automatically released. |
void |
commit()
Makes all changes made since the previous
commit/rollback permanent and releases any database locks
currently held by this
Connection object. |
protected KiWiNamespace |
constructNamespaceFromDatabase(ResultSet row) |
protected KiWiNode |
constructNodeFromDatabase(ResultSet row)
Construct an appropriate KiWiNode from the result of an SQL query.
|
protected KiWiTriple |
constructTripleFromDatabase(ResultSet row)
Construct a KiWiTriple from the result of an SQL query.
|
protected String |
constructTripleQuery(KiWiResource subject,
KiWiUriResource predicate,
KiWiNode object,
KiWiResource context,
boolean inferred,
boolean wildcardContext)
Construct the SQL query string from the query pattern passed as arguments
|
protected List<KiWiTriple> |
constructTriplesFromDatabase(ResultSet row,
int maxPrefetch)
Construct a batch of KiWiTriples from the result of an SQL query.
|
void |
deleteAll()
Mark all triples contained in the triple store as deleted, setting the "deleted" flag to true and
updating the timestamp value of "deletedAt".
|
void |
deleteContext(KiWiResource ctx)
Mark all triples contained in the context passed as argument as deleted, setting the "deleted" flag to true and
updating the timestamp value of "deletedAt".
|
void |
deleteNamespace(KiWiNamespace namespace)
Delete the namespace passed as argument from the database and from the caches.
|
void |
deleteTriple(KiWiTriple triple)
Mark the triple passed as argument as deleted, setting the "deleted" flag to true and
updating the timestamp value of "deletedAt".
|
void |
flushBatch() |
boolean |
getAutoCommit()
Retrieves the current auto-commit mode for this
Connection
object. |
int |
getBatchSize()
Return the size of a batch for batched commits.
|
CacheManager |
getCacheManager()
Return the cache manager used by this connection
|
KiWiConfiguration |
getConfiguration() |
Set<String> |
getDatabaseTables()
Return a collection of database tables contained in the database.
|
int |
getDatabaseVersion()
Return the KiWi version of the database this connection is operating on.
|
KiWiDialect |
getDialect() |
Connection |
getJDBCConnection()
Get direct access to the JDBC connection used by this KiWiConnection.
|
protected static Locale |
getLocale(String language) |
String |
getMetadata(String key)
Return the metadata value with the given key; can be used by KiWi modules to retrieve module-specific metadata.
|
long |
getNextSequence()
Get next number in a sequence; for databases without sequence support (e.g.
|
PreparedStatement |
getPreparedStatement(String key)
Return the prepared statement with the given identifier; first looks in the statement cache and if it does
not exist there create a new statement.
|
PreparedStatement |
getPreparedStatement(String key,
int numberOfArguments)
Return the prepared statement with the given identifier; first looks in the statement cache and if it does
not exist there create a new statement.
|
long |
getSize()
Count all non-deleted triples in the triple store
|
long |
getSize(KiWiResource context)
Count all non-deleted triples in the triple store
|
long |
getTransactionId()
Return the current transaction ID
|
long |
getTripleId(KiWiResource subject,
KiWiUriResource predicate,
KiWiNode object,
KiWiResource context)
Return the identifier of the triple with the given subject, predicate, object and context, or null if this
triple does not exist.
|
boolean |
isBatchCommit()
Return true if batched commits are enabled.
|
boolean |
isClosed()
Retrieves whether this
Connection object has been
closed. |
info.aduna.iteration.CloseableIteration<KiWiResource,SQLException> |
listContexts()
List all contexts used in this triple store.
|
info.aduna.iteration.CloseableIteration<KiWiNamespace,SQLException> |
listNamespaces() |
info.aduna.iteration.CloseableIteration<KiWiResource,SQLException> |
listResources()
List all contexts used in this triple store.
|
info.aduna.iteration.CloseableIteration<KiWiUriResource,SQLException> |
listResources(String prefix)
List all contexts used in this triple store.
|
org.openrdf.repository.RepositoryResult<org.openrdf.model.Statement> |
listTriples(KiWiResource subject,
KiWiUriResource predicate,
KiWiNode object,
KiWiResource context,
boolean inferred,
boolean wildcardContext)
Return a Sesame RepositoryResult of statements according to the query pattern given in the arguments.
|
KiWiAnonResource |
loadAnonResource(String id)
Load a KiWiAnonResource by anonymous ID.
|
KiWiBooleanLiteral |
loadLiteral(boolean value)
Load a boolean literal with the boolean value given as argument if it exists.
|
KiWiDateLiteral |
loadLiteral(org.joda.time.DateTime date)
Load a literal with the date value given as argument if it exists.
|
KiWiDoubleLiteral |
loadLiteral(double value)
Load a double literal with the double value given as argument if it exists.
|
KiWiIntLiteral |
loadLiteral(long value)
Load a integer literal with the long value given as argument if it exists.
|
KiWiLiteral |
loadLiteral(String value,
String lang,
KiWiUriResource ltype)
Load a literal based on the value, language and type passed as argument.
|
KiWiNamespace |
loadNamespaceByPrefix(String prefix)
Load a KiWiNamespace with the given prefix, or null if the namespace does not exist.
|
KiWiNamespace |
loadNamespaceByUri(String uri)
Load a KiWiNamespace with the given uri, or null if the namespace does not exist.
|
KiWiNode |
loadNodeById(long id)
Load a KiWiNode by database ID.
|
KiWiNode[] |
loadNodesByIds(long... ids)
Batch load the nodes with the given ids.
|
KiWiTriple |
loadTripleById(long id) |
KiWiUriResource |
loadUriResource(String uri)
Load a KiWiUriResource by URI.
|
protected void |
requireJDBCConnection()
This method must be called by all methods as soon as they actually require a JDBC connection.
|
void |
rollback()
Undoes all changes made in the current transaction
and releases any database locks currently held
by this
Connection object. |
void |
setAutoCommit(boolean autoCommit)
Sets this connection's auto-commit mode to the given state.
|
void |
setBatchCommit(boolean batchCommit)
Enabled batched commits.
|
void |
setBatchSize(int batchSize)
Set the size of a batch for batched commits.
|
void |
setMetadata(String key,
String value)
Update the metadata value for the given key; can be used by KiWi modules to set module-specific metadata.
|
void |
storeNamespace(KiWiNamespace namespace)
Store the namespace passed as argument in the database.
|
void |
storeNode(KiWiNode node)
Store a new node in the database.
|
void |
storeTriple(KiWiTriple triple)
Store a triple in the database.
|
void |
undeleteTriple(KiWiTriple triple)
Mark the triple passed as argument as not deleted, setting the "deleted" flag to false and
clearing the timestamp value of "deletedAt".
|
protected KiWiDialect dialect
protected Connection connection
protected KiWiPersistence persistence
protected CacheManager cacheManager
protected TripleTable<KiWiTriple> tripleBatch
public KiWiConnection(KiWiPersistence persistence, KiWiDialect dialect, CacheManager cacheManager) throws SQLException
SQLException
protected void requireJDBCConnection() throws SQLException
SQLException
public Connection getJDBCConnection() throws SQLException
SQLException
public CacheManager getCacheManager()
public KiWiDialect getDialect()
public KiWiConfiguration getConfiguration()
public KiWiNamespace loadNamespaceByPrefix(String prefix) throws SQLException
prefix
- the prefix to look forSQLException
public KiWiNamespace loadNamespaceByUri(String uri) throws SQLException
uri
- the uri to look forSQLException
public void storeNamespace(KiWiNamespace namespace) throws SQLException
namespace
- the namespace to storeSQLException
- the prefix or URI is already used, or a database error occurredpublic void deleteNamespace(KiWiNamespace namespace) throws SQLException
namespace
- the namespace to deleteSQLException
- in case a database error occurredpublic long getSize() throws SQLException
SQLException
public long getSize(KiWiResource context) throws SQLException
SQLException
public KiWiNode loadNodeById(long id) throws SQLException
id
- the database id of the node to loadSQLException
public KiWiNode[] loadNodesByIds(long... ids) throws SQLException
ids
- array of ids to retrieveSQLException
public KiWiTriple loadTripleById(long id) throws SQLException
SQLException
public KiWiUriResource loadUriResource(String uri) throws SQLException
uri
- the URI of the resource to loadSQLException
public KiWiAnonResource loadAnonResource(String id) throws SQLException
id
- the anonymous ID of the resource to loadSQLException
public KiWiLiteral loadLiteral(String value, String lang, KiWiUriResource ltype) throws SQLException
value
- string value of the literal to loadlang
- language of the literal to load (optional, 2-letter language code with optional country)ltype
- the type of the literal to load (optional)SQLException
public KiWiDateLiteral loadLiteral(org.joda.time.DateTime date) throws SQLException
date
- the date of the date literal to loadSQLException
public KiWiIntLiteral loadLiteral(long value) throws SQLException
value
- the value of the integer literal to loadSQLException
public KiWiDoubleLiteral loadLiteral(double value) throws SQLException
value
- the value of the integer literal to loadSQLException
public KiWiBooleanLiteral loadLiteral(boolean value) throws SQLException
value
- the value of the integer literal to loadSQLException
public void storeNode(KiWiNode node) throws SQLException
node
- SQLException
public void storeTriple(KiWiTriple triple) throws SQLException
triple
- the triple to storeSQLException
NullPointerException
- in case the subject, predicate, object or context have not been persistedpublic long getTripleId(KiWiResource subject, KiWiUriResource predicate, KiWiNode object, KiWiResource context) throws SQLException
subject
- predicate
- object
- context
- SQLException
public void deleteTriple(KiWiTriple triple) throws SQLException
triple
- SQLException
public void deleteContext(KiWiResource ctx) throws SQLException
ctx
- resource identifying the context to be deletedSQLException
public void deleteAll() throws SQLException
SQLException
public void undeleteTriple(KiWiTriple triple) throws SQLException
triple
- SQLException
public info.aduna.iteration.CloseableIteration<KiWiResource,SQLException> listContexts() throws SQLException
SQLException
public info.aduna.iteration.CloseableIteration<KiWiResource,SQLException> listResources() throws SQLException
SQLException
public info.aduna.iteration.CloseableIteration<KiWiUriResource,SQLException> listResources(String prefix) throws SQLException
SQLException
public info.aduna.iteration.CloseableIteration<KiWiNamespace,SQLException> listNamespaces() throws SQLException
SQLException
public org.openrdf.repository.RepositoryResult<org.openrdf.model.Statement> listTriples(KiWiResource subject, KiWiUriResource predicate, KiWiNode object, KiWiResource context, boolean inferred, boolean wildcardContext) throws SQLException
subject
- the subject to query for, or null for a wildcard querypredicate
- the predicate to query for, or null for a wildcard queryobject
- the object to query for, or null for a wildcard querycontext
- the context to query for, or null for a wildcard queryinferred
- if true, the result will also contain triples inferred by the reasoner, if false notwildcardContext
- if true, a null context will be interpreted as a wildcard, if false, a null context will be interpreted as "no context"SQLException
protected String constructTripleQuery(KiWiResource subject, KiWiUriResource predicate, KiWiNode object, KiWiResource context, boolean inferred, boolean wildcardContext)
subject
- the subject to query for, or null for a wildcard querypredicate
- the predicate to query for, or null for a wildcard queryobject
- the object to query for, or null for a wildcard querycontext
- the context to query for, or null for a wildcard queryinferred
- if true, the result will also contain triples inferred by the reasoner, if false notprotected KiWiNamespace constructNamespaceFromDatabase(ResultSet row) throws SQLException
SQLException
protected KiWiNode constructNodeFromDatabase(ResultSet row) throws SQLException
row
- SQLException
protected KiWiTriple constructTripleFromDatabase(ResultSet row) throws SQLException
row
- a database result containing the columns described aboveSQLException
protected List<KiWiTriple> constructTriplesFromDatabase(ResultSet row, int maxPrefetch) throws SQLException
row
- a database result containing the columns described aboveSQLException
public PreparedStatement getPreparedStatement(String key) throws SQLException
key
- the id of the statement in statements.propertiesSQLException
public PreparedStatement getPreparedStatement(String key, int numberOfArguments) throws SQLException
key
- the id of the statement in statements.propertiesSQLException
public long getNextSequence() throws SQLException
SQLException
public Set<String> getDatabaseTables() throws SQLException
SQLException
public String getMetadata(String key) throws SQLException
key
- SQLException
public void setMetadata(String key, String value) throws SQLException
key
- SQLException
public int getDatabaseVersion() throws SQLException
SQLException
public void setAutoCommit(boolean autoCommit) throws SQLException
commit
or the method rollback
.
By default, new connections are in auto-commit
mode.
The commit occurs when the statement completes. The time when the statement completes depends on the type of SQL Statement:
CallableStatement
objects or for statements that return
multiple results, the statement is complete
when all of the associated result sets have been closed, and all update
counts and output parameters have been retrieved.
NOTE: If this method is called during a transaction and the
auto-commit mode is changed, the transaction is committed. If
setAutoCommit
is called and the auto-commit mode is
not changed, the call is a no-op.
autoCommit
- true
to enable auto-commit mode;
false
to disable itSQLException
- if a database access error occurs,
setAutoCommit(true) is called while participating in a distributed transaction,
or this method is called on a closed connectiongetAutoCommit()
public boolean getAutoCommit() throws SQLException
Connection
object.Connection
object's
auto-commit modeSQLException
- if a database access error occurs
or this method is called on a closed connectionsetAutoCommit(boolean)
public boolean isBatchCommit()
public void setBatchCommit(boolean batchCommit)
public int getBatchSize()
public void setBatchSize(int batchSize)
batchSize
- public void commit() throws SQLException
Connection
object.
This method should be
used only when auto-commit mode has been disabled.SQLException
- if a database access error occurs,
this method is called while participating in a distributed transaction,
if this method is called on a closed conection or this
Connection
object is in auto-commit modesetAutoCommit(boolean)
public void rollback() throws SQLException
Connection
object. This method should be
used only when auto-commit mode has been disabled.SQLException
- if a database access error occurs,
this method is called while participating in a distributed transaction,
this method is called on a closed connection or this
Connection
object is in auto-commit modesetAutoCommit(boolean)
public boolean isClosed() throws SQLException
Connection
object has been
closed. A connection is closed if the method close
has been called on it or if certain fatal errors have occurred.
This method is guaranteed to return true
only when
it is called after the method Connection.close
has
been called.
This method generally cannot be called to determine whether a connection to a database is valid or invalid. A typical client can determine that a connection is invalid by catching any exceptions that might be thrown when an operation is attempted.
true
if this Connection
object
is closed; false
if it is still openSQLException
- if a database access error occurspublic void close() throws SQLException
Connection
object's database and JDBC resources
immediately instead of waiting for them to be automatically released.
Calling the method close
on a Connection
object that is already closed is a no-op.
It is strongly recommended that an application explicitly
commits or rolls back an active transaction prior to calling the
close
method. If the close
method is called
and there is an active transaction, the results are implementation-defined.
close
in interface AutoCloseable
SQLException
- SQLException if a database access error occurspublic void flushBatch() throws SQLException
SQLException
public long getTransactionId()
Copyright © 2012–2018 The Apache Software Foundation. All rights reserved.