public class Sql2o extends Object
An Sql2o
instance represents a way of connecting to one specific database.
To create a new instance, one need to specify either jdbc-url, username and password for the database or a data source.
Internally the Sql2o instance uses a data source to create jdbc connections to the database. If url, username and password was specified in the constructor, a simple data source is created, which works as a simple wrapper around the jdbc driver.
Some jdbc implementations have quirks, therefore it may be necessary to use a constructor with the quirks parameter. When quirks are specified, Sql2o will use workarounds to avoid these quirks.
Constructor and Description |
---|
Sql2o(DataSource dataSource)
Creates a new instance of the Sql2o class, which uses the given DataSource to acquire connections to the database.
|
Sql2o(DataSource dataSource,
Quirks quirks)
Creates a new instance of the Sql2o class, which uses the given DataSource to acquire connections to the database.
|
Sql2o(DataSource dataSource,
QuirksMode quirksMode)
Deprecated.
|
Sql2o(String jndiLookup) |
Sql2o(String url,
String user,
String pass)
Creates a new instance of the Sql2o class.
|
Sql2o(String url,
String user,
String pass,
Quirks quirks)
Created a new instance of the Sql2o class.
|
Sql2o(String url,
String user,
String pass,
QuirksMode quirksMode)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
Connection |
beginTransaction()
Begins a transaction with isolation level
Connection.TRANSACTION_READ_COMMITTED . |
Connection |
beginTransaction(int isolationLevel)
Begins a transaction with the given isolation level.
|
Query |
createQuery(String query)
Deprecated.
create queries with
Connection class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query).executeAndFetch(Pojo.class);
}
|
Query |
createQuery(String query,
boolean returnGeneratedKeys)
Deprecated.
create queries with
Connection class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, returnGeneratedKeys).executeAndFetch(Pojo.class);
}
|
Query |
createQuery(String query,
String name)
Deprecated.
create queries with
Connection class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, name).executeAndFetch(Pojo.class);
}
|
Query |
createQuery(String query,
String name,
boolean returnGeneratedKeys)
Deprecated.
create queries with
Connection class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class);
}
|
DataSource |
getDataSource()
Gets the DataSource that Sql2o uses internally to acquire database connections.
|
Map<String,String> |
getDefaultColumnMappings()
Gets the default column mappings Map.
|
Quirks |
getQuirks() |
boolean |
isDefaultCaseSensitive()
Gets value indicating if this instance of Sql2o is case sensitive when mapping between columns names and property
names.
|
Connection |
open()
Opens a connection to the database
|
void |
runInTransaction(StatementRunnable runnable)
Calls the
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. |
void |
runInTransaction(StatementRunnable runnable,
Object argument)
Calls the
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. |
void |
runInTransaction(StatementRunnable runnable,
Object argument,
int isolationLevel)
Calls the
StatementRunnable.run(Connection, Object) method on the StatementRunnable parameter. |
<V> V |
runInTransaction(StatementRunnableWithResult runnableWithResult) |
<V> V |
runInTransaction(StatementRunnableWithResult runnableWithResult,
Object argument) |
<V> V |
runInTransaction(StatementRunnableWithResult runnableWithResult,
Object argument,
int isolationLevel) |
void |
setDefaultCaseSensitive(boolean defaultCaseSensitive)
Sets a value indicating if this instance of Sql2o is case sensitive when mapping between columns names and property
names.
|
void |
setDefaultColumnMappings(Map<String,String> defaultColumnMappings)
Sets the default column mappings Map.
|
void |
withConnection(StatementRunnable runnable)
Invokes the run method on the
StatementRunnableWithResult instance. |
void |
withConnection(StatementRunnable runnable,
Object argument)
Invokes the run method on the
StatementRunnableWithResult instance. |
<V> V |
withConnection(StatementRunnableWithResult runnable)
Invokes the run method on the
StatementRunnableWithResult instance. |
<V> V |
withConnection(StatementRunnableWithResult runnable,
Object argument)
Invokes the run method on the
StatementRunnableWithResult instance. |
public Sql2o(String jndiLookup)
public Sql2o(String url, String user, String pass)
GenericDatasource
,
and call the Sql2o(javax.sql.DataSource)
constructor which takes a DataSource as parameter.url
- JDBC database urluser
- database usernamepass
- database password@Deprecated public Sql2o(String url, String user, String pass, QuirksMode quirksMode)
public Sql2o(String url, String user, String pass, Quirks quirks)
GenericDatasource
,
and call the Sql2o(javax.sql.DataSource)
constructor which takes a DataSource as parameter.url
- JDBC database urluser
- database usernamepass
- database passwordquirks
- Quirks
allows sql2o to work around known quirks and issues in different JDBC drivers.public Sql2o(DataSource dataSource)
dataSource
- The DataSource Sql2o uses to acquire connections to the database.@Deprecated public Sql2o(DataSource dataSource, QuirksMode quirksMode)
public Sql2o(DataSource dataSource, Quirks quirks)
dataSource
- The DataSource Sql2o uses to acquire connections to the database.quirks
- Quirks
allows sql2o to work around known quirks and issues in different JDBC drivers.public Quirks getQuirks()
public DataSource getDataSource()
public Map<String,String> getDefaultColumnMappings()
Map
instance, which Sql2o internally uses to map column names with property
names.public void setDefaultColumnMappings(Map<String,String> defaultColumnMappings)
defaultColumnMappings
- A Map
instance Sql2o uses internally to map between column names and
property names.public boolean isDefaultCaseSensitive()
public void setDefaultCaseSensitive(boolean defaultCaseSensitive)
defaultCaseSensitive
- @Deprecated public Query createQuery(String query, String name, boolean returnGeneratedKeys)
Connection
class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class);
}
Query
query
- the sql query stringname
- name of query. Only used for logging purposesreturnGeneratedKeys
- boolean value indicating if the database should return any generated keys.Query
instance@Deprecated public Query createQuery(String query, boolean returnGeneratedKeys)
Connection
class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, returnGeneratedKeys).executeAndFetch(Pojo.class);
}
Query
query
- the sql query stringreturnGeneratedKeys
- boolean value indicating if the database should return any generated keys.Query
instance@Deprecated public Query createQuery(String query, String name)
Connection
class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query, name).executeAndFetch(Pojo.class);
}
Query
query
- the sql query stringname
- name of query. Only used for logging purposesQuery
instance@Deprecated public Query createQuery(String query)
Connection
class instead, using try-with-resource blocks
try (Connection con = sql2o.open()) {
return sql2o.createQuery(query).executeAndFetch(Pojo.class);
}
Query
query
- the sql query stringQuery
instancepublic Connection open()
Connection
class.public <V> V withConnection(StatementRunnableWithResult runnable, Object argument)
StatementRunnableWithResult
instance. This method guarantees that
the connection is closed properly, when either the run method completes or if an exception occurs.V
- runnable
- argument
- public <V> V withConnection(StatementRunnableWithResult runnable)
StatementRunnableWithResult
instance. This method guarantees that
the connection is closed properly, when either the run method completes or if an exception occurs.V
- runnable
- public void withConnection(StatementRunnable runnable)
StatementRunnableWithResult
instance. This method guarantees that
the connection is closed properly, when either the run method completes or if an exception occurs.runnable
- public void withConnection(StatementRunnable runnable, Object argument)
StatementRunnableWithResult
instance. This method guarantees that
the connection is closed properly, when either the run method completes or if an exception occurs.runnable
- argument
- public Connection beginTransaction(int isolationLevel)
Connection
instance, will be executed in the transaction. It is very important to always call either the Connection.commit()
method or the Connection.rollback()
method to close the transaction. Use proper try-catch logic.isolationLevel
- the isolation level of the transactionConnection
instance to use to run statements in the transaction.public Connection beginTransaction()
Connection.TRANSACTION_READ_COMMITTED
. Every statement executed on the return Connection
instance, will be executed in the transaction. It is very important to always call either the Connection.commit()
method or the Connection.rollback()
method to close the transaction. Use proper try-catch logic.Connection
instance to use to run statements in the transaction.public void runInTransaction(StatementRunnable runnable)
StatementRunnable.run(Connection, Object)
method on the StatementRunnable
parameter. All statements
run on the Connection
instance in the run
method will be
executed in a transaction. The transaction will automatically be committed if the run
method finishes without throwing an exception. If an exception is thrown within the run
method,
the transaction will automatically be rolled back.
The isolation level of the transaction will be set to Connection.TRANSACTION_READ_COMMITTED
runnable
- The StatementRunnable
instance.public void runInTransaction(StatementRunnable runnable, Object argument)
StatementRunnable.run(Connection, Object)
method on the StatementRunnable
parameter. All statements
run on the Connection
instance in the run
method will be
executed in a transaction. The transaction will automatically be committed if the run
method finishes without throwing an exception. If an exception is thrown within the run
method,
the transaction will automatically be rolled back.
The isolation level of the transaction will be set to Connection.TRANSACTION_READ_COMMITTED
runnable
- The StatementRunnable
instance.argument
- An argument which will be forwarded to the run
methodpublic void runInTransaction(StatementRunnable runnable, Object argument, int isolationLevel)
StatementRunnable.run(Connection, Object)
method on the StatementRunnable
parameter. All statements
run on the Connection
instance in the run
method will be
executed in a transaction. The transaction will automatically be committed if the run
method finishes without throwing an exception. If an exception is thrown within the run
method,
the transaction will automatically be rolled back.runnable
- The StatementRunnable
instance.argument
- An argument which will be forwarded to the run
methodisolationLevel
- The isolation level of the transactionpublic <V> V runInTransaction(StatementRunnableWithResult runnableWithResult)
public <V> V runInTransaction(StatementRunnableWithResult runnableWithResult, Object argument)
public <V> V runInTransaction(StatementRunnableWithResult runnableWithResult, Object argument, int isolationLevel)
Copyright © 2015. All rights reserved.