Package org.sql2o
Class Sql2o
java.lang.Object
org.sql2o.Sql2o
Sql2o is the main class for the sql2o library.
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.
- Author:
- Lars Aaberg
-
Constructor Summary
ConstructorDescriptionCreates a new instance of the Sql2o class.Created a new instance of the Sql2o class.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. -
Method Summary
Modifier and TypeMethodDescriptionBegins a transaction with isolation levelConnection.TRANSACTION_READ_COMMITTED
.beginTransaction
(int isolationLevel) Begins a transaction with the given isolation level.beginTransaction
(ConnectionSource connectionSource) Begins a transaction with isolation levelConnection.TRANSACTION_READ_COMMITTED
.beginTransaction
(ConnectionSource connectionSource, int isolationLevel) Begins a transaction with the given isolation level.createQuery
(String query) Deprecated.createQuery
(String query, boolean returnGeneratedKeys) Deprecated.create queries withConnection
class instead, using try-with-resource blockstry (Connection con = sql2o.open()) { return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class); }
Gets theConnectionSource
that Sql2o uses internally to acquire database connections.Deprecated.usegetConnectionSource()
as more general connection providerGets the default column mappings Map.boolean
Gets value indicating if this instance of Sql2o is case sensitive when mapping between columns names and property names.open()
Opens a connection to the databaseopen
(ConnectionSource connectionSource) Opens a connection to the databasevoid
runInTransaction
(StatementRunnable runnable) Calls theStatementRunnable.run(Connection, Object)
method on theStatementRunnable
parameter.void
runInTransaction
(StatementRunnable runnable, Object argument) Calls theStatementRunnable.run(Connection, Object)
method on theStatementRunnable
parameter.void
runInTransaction
(StatementRunnable runnable, Object argument, int isolationLevel) Calls theStatementRunnable.run(Connection, Object)
method on theStatementRunnable
parameter.<V> V
runInTransaction
(StatementRunnableWithResult<V> runnableWithResult) <V> V
runInTransaction
(StatementRunnableWithResult<V> runnableWithResult, Object argument) <V> V
runInTransaction
(StatementRunnableWithResult<V> runnableWithResult, Object argument, int isolationLevel) void
setConnectionSource
(ConnectionSource connectionSource) Sets theConnectionSource
that Sql2o uses internally to acquire database connections.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 theStatementRunnableWithResult
instance.void
withConnection
(StatementRunnable runnable, Object argument) Invokes the run method on theStatementRunnableWithResult
instance.<V> V
withConnection
(StatementRunnableWithResult<V> runnable) Invokes the run method on theStatementRunnableWithResult
instance.<V> V
withConnection
(StatementRunnableWithResult<V> runnable, Object argument) Invokes the run method on theStatementRunnableWithResult
instance.
-
Constructor Details
-
Sql2o
-
Sql2o
Creates a new instance of the Sql2o class. Internally this constructor will create aGenericDatasource
, and call theSql2o(javax.sql.DataSource)
constructor which takes a DataSource as parameter.- Parameters:
url
- JDBC database urluser
- database usernamepass
- database password
-
Sql2o
Created a new instance of the Sql2o class. Internally this constructor will create aGenericDatasource
, and call theSql2o(javax.sql.DataSource)
constructor which takes a DataSource as parameter.- Parameters:
url
- JDBC database urluser
- database usernamepass
- database passwordquirks
-Quirks
allows sql2o to work around known quirks and issues in different JDBC drivers.
-
Sql2o
Creates a new instance of the Sql2o class, which uses the given DataSource to acquire connections to the database.- Parameters:
dataSource
- The DataSource Sql2o uses to acquire connections to the database.
-
Sql2o
Creates a new instance of the Sql2o class, which uses the given DataSource to acquire connections to the database.- Parameters:
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.
-
-
Method Details
-
getQuirks
-
getDataSource
Deprecated.usegetConnectionSource()
as more general connection providerGets the DataSource that Sql2o uses internally to acquire database connections.- Returns:
- The DataSource instance
-
getConnectionSource
Gets theConnectionSource
that Sql2o uses internally to acquire database connections.- Returns:
- The ConnectionSource instance
-
setConnectionSource
Sets theConnectionSource
that Sql2o uses internally to acquire database connections.- Parameters:
connectionSource
- the ConnectionSource instance to use
-
getDefaultColumnMappings
Gets the default column mappings Map. column mappings added to this Map are always available when Sql2o attempts to map between result sets and object instances. -
setDefaultColumnMappings
Sets the default column mappings Map.- Parameters:
defaultColumnMappings
- AMap
instance Sql2o uses internally to map between column names and property names.
-
isDefaultCaseSensitive
public boolean isDefaultCaseSensitive()Gets value indicating if this instance of Sql2o is case sensitive when mapping between columns names and property names.- Returns:
-
setDefaultCaseSensitive
public void setDefaultCaseSensitive(boolean defaultCaseSensitive) Sets a value indicating if this instance of Sql2o is case sensitive when mapping between columns names and property names. This should almost always be false, because most relational databases are not case sensitive.- Parameters:
defaultCaseSensitive
-
-
createQuery
Deprecated.create queries withConnection
class instead, using try-with-resource blockstry (Connection con = sql2o.open()) { return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class); }
Creates aQuery
- Parameters:
query
- the sql query stringreturnGeneratedKeys
- boolean value indicating if the database should return any generated keys.- Returns:
- the
Query
instance
-
createQuery
Deprecated.create queries withConnection
class instead, using try-with-resource blockstry (Connection con = sql2o.open()) { return sql2o.createQuery(query, name).executeAndFetch(Pojo.class); }
Creates aQuery
- Parameters:
query
- the sql query string- Returns:
- the
Query
instance
-
open
Opens a connection to the database- Parameters:
connectionSource
- theConnectionSource
implementation substitution, that will be used instead of one fromSql2o
instance.- Returns:
- instance of the
Connection
class.
-
open
Opens a connection to the database- Returns:
- instance of the
Connection
class.
-
withConnection
Invokes the run method on theStatementRunnableWithResult
instance. This method guarantees that the connection is closed properly, when either the run method completes or if an exception occurs.- Type Parameters:
V
-- Parameters:
runnable
-argument
-- Returns:
-
withConnection
Invokes the run method on theStatementRunnableWithResult
instance. This method guarantees that the connection is closed properly, when either the run method completes or if an exception occurs.- Type Parameters:
V
-- Parameters:
runnable
-- Returns:
-
withConnection
Invokes the run method on theStatementRunnableWithResult
instance. This method guarantees that the connection is closed properly, when either the run method completes or if an exception occurs.- Parameters:
runnable
-
-
withConnection
Invokes the run method on theStatementRunnableWithResult
instance. This method guarantees that the connection is closed properly, when either the run method completes or if an exception occurs.- Parameters:
runnable
-argument
-
-
beginTransaction
Begins a transaction with the given isolation level. Every statement executed on the returnConnection
instance, will be executed in the transaction. It is very important to always call either theConnection.commit()
method or theConnection.rollback()
method to close the transaction. Use proper try-catch logic.- Parameters:
isolationLevel
- the isolation level of the transaction- Returns:
- the
Connection
instance to use to run statements in the transaction.
-
beginTransaction
Begins a transaction with the given isolation level. Every statement executed on the returnConnection
instance, will be executed in the transaction. It is very important to always call either theConnection.commit()
method or theConnection.rollback()
method to close the transaction. Use proper try-catch logic.- Parameters:
connectionSource
- theConnectionSource
implementation substitution, that will be used instead of one fromSql2o
instance.isolationLevel
- the isolation level of the transaction- Returns:
- the
Connection
instance to use to run statements in the transaction.
-
beginTransaction
Begins a transaction with isolation levelConnection.TRANSACTION_READ_COMMITTED
. Every statement executed on the returnConnection
instance, will be executed in the transaction. It is very important to always call either theConnection.commit()
method or theConnection.rollback()
method to close the transaction. Use proper try-catch logic.- Returns:
- the
Connection
instance to use to run statements in the transaction.
-
beginTransaction
Begins a transaction with isolation levelConnection.TRANSACTION_READ_COMMITTED
. Every statement executed on the returnConnection
instance, will be executed in the transaction. It is very important to always call either theConnection.commit()
method or theConnection.rollback()
method to close the transaction. Use proper try-catch logic.- Parameters:
connectionSource
- theConnectionSource
implementation substitution, that will be used instead of one fromSql2o
instance.- Returns:
- the
Connection
instance to use to run statements in the transaction.
-
runInTransaction
Calls theStatementRunnable.run(Connection, Object)
method on theStatementRunnable
parameter. All statements run on theConnection
instance in therun
method will be executed in a transaction. The transaction will automatically be committed if therun
method finishes without throwing an exception. If an exception is thrown within therun
method, the transaction will automatically be rolled back. The isolation level of the transaction will be set toConnection.TRANSACTION_READ_COMMITTED
- Parameters:
runnable
- TheStatementRunnable
instance.
-
runInTransaction
Calls theStatementRunnable.run(Connection, Object)
method on theStatementRunnable
parameter. All statements run on theConnection
instance in therun
method will be executed in a transaction. The transaction will automatically be committed if therun
method finishes without throwing an exception. If an exception is thrown within therun
method, the transaction will automatically be rolled back. The isolation level of the transaction will be set toConnection.TRANSACTION_READ_COMMITTED
- Parameters:
runnable
- TheStatementRunnable
instance.argument
- An argument which will be forwarded to therun
method
-
runInTransaction
Calls theStatementRunnable.run(Connection, Object)
method on theStatementRunnable
parameter. All statements run on theConnection
instance in therun
method will be executed in a transaction. The transaction will automatically be committed if therun
method finishes without throwing an exception. If an exception is thrown within therun
method, the transaction will automatically be rolled back.- Parameters:
runnable
- TheStatementRunnable
instance.argument
- An argument which will be forwarded to therun
methodisolationLevel
- The isolation level of the transaction
-
runInTransaction
-
runInTransaction
-
runInTransaction
public <V> V runInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument, int isolationLevel)
-
Connection
class instead, using try-with-resource blockstry (Connection con = sql2o.open()) { return sql2o.createQuery(query, name).executeAndFetch(Pojo.class); }