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
ConstructorsConstructorDescriptionCreates 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 withConnectionclass instead, using try-with-resource blockstry (Connection con = sql2o.open()) { return sql2o.createQuery(query, name, returnGeneratedKeys).executeAndFetch(Pojo.class); }Gets theConnectionSourcethat Sql2o uses internally to acquire database connections.Deprecated.usegetConnectionSource()as more general connection providerGets the default column mappings Map.booleanGets 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 databasevoidrunInTransaction(StatementRunnable runnable) Calls theStatementRunnable.run(Connection, Object)method on theStatementRunnableparameter.voidrunInTransaction(StatementRunnable runnable, Object argument) Calls theStatementRunnable.run(Connection, Object)method on theStatementRunnableparameter.voidrunInTransaction(StatementRunnable runnable, Object argument, int isolationLevel) Calls theStatementRunnable.run(Connection, Object)method on theStatementRunnableparameter.<V> VrunInTransaction(StatementRunnableWithResult<V> runnableWithResult) <V> VrunInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument) <V> VrunInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument, int isolationLevel) voidsetConnectionSource(ConnectionSource connectionSource) Sets theConnectionSourcethat Sql2o uses internally to acquire database connections.voidsetDefaultCaseSensitive(boolean defaultCaseSensitive) Sets a value indicating if this instance of Sql2o is case sensitive when mapping between columns names and property names.voidsetDefaultColumnMappings(Map<String, String> defaultColumnMappings) Sets the default column mappings Map.voidwithConnection(StatementRunnable runnable) Invokes the run method on theStatementRunnableWithResultinstance.voidwithConnection(StatementRunnable runnable, Object argument) Invokes the run method on theStatementRunnableWithResultinstance.<V> VwithConnection(StatementRunnableWithResult<V> runnable) Invokes the run method on theStatementRunnableWithResultinstance.<V> VwithConnection(StatementRunnableWithResult<V> runnable, Object argument) Invokes the run method on theStatementRunnableWithResultinstance.
-
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-Quirksallows 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-Quirksallows 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 theConnectionSourcethat Sql2o uses internally to acquire database connections.- Returns:
- The ConnectionSource instance
-
setConnectionSource
Sets theConnectionSourcethat 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- AMapinstance 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 withConnectionclass 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
Queryinstance
-
createQuery
Deprecated.create queries withConnectionclass 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
Queryinstance
-
open
Opens a connection to the database- Parameters:
connectionSource- theConnectionSourceimplementation substitution, that will be used instead of one fromSql2oinstance.- Returns:
- instance of the
Connectionclass.
-
open
Opens a connection to the database- Returns:
- instance of the
Connectionclass.
-
withConnection
Invokes the run method on theStatementRunnableWithResultinstance. 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 theStatementRunnableWithResultinstance. 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 theStatementRunnableWithResultinstance. 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 theStatementRunnableWithResultinstance. 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 returnConnectioninstance, 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
Connectioninstance to use to run statements in the transaction.
-
beginTransaction
Begins a transaction with the given isolation level. Every statement executed on the returnConnectioninstance, 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- theConnectionSourceimplementation substitution, that will be used instead of one fromSql2oinstance.isolationLevel- the isolation level of the transaction- Returns:
- the
Connectioninstance to use to run statements in the transaction.
-
beginTransaction
Begins a transaction with isolation levelConnection.TRANSACTION_READ_COMMITTED. Every statement executed on the returnConnectioninstance, 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
Connectioninstance to use to run statements in the transaction.
-
beginTransaction
Begins a transaction with isolation levelConnection.TRANSACTION_READ_COMMITTED. Every statement executed on the returnConnectioninstance, 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- theConnectionSourceimplementation substitution, that will be used instead of one fromSql2oinstance.- Returns:
- the
Connectioninstance to use to run statements in the transaction.
-
runInTransaction
Calls theStatementRunnable.run(Connection, Object)method on theStatementRunnableparameter. All statements run on theConnectioninstance in therunmethod will be executed in a transaction. The transaction will automatically be committed if therunmethod finishes without throwing an exception. If an exception is thrown within therunmethod, the transaction will automatically be rolled back. The isolation level of the transaction will be set toConnection.TRANSACTION_READ_COMMITTED- Parameters:
runnable- TheStatementRunnableinstance.
-
runInTransaction
Calls theStatementRunnable.run(Connection, Object)method on theStatementRunnableparameter. All statements run on theConnectioninstance in therunmethod will be executed in a transaction. The transaction will automatically be committed if therunmethod finishes without throwing an exception. If an exception is thrown within therunmethod, the transaction will automatically be rolled back. The isolation level of the transaction will be set toConnection.TRANSACTION_READ_COMMITTED- Parameters:
runnable- TheStatementRunnableinstance.argument- An argument which will be forwarded to therunmethod
-
runInTransaction
Calls theStatementRunnable.run(Connection, Object)method on theStatementRunnableparameter. All statements run on theConnectioninstance in therunmethod will be executed in a transaction. The transaction will automatically be committed if therunmethod finishes without throwing an exception. If an exception is thrown within therunmethod, the transaction will automatically be rolled back.- Parameters:
runnable- TheStatementRunnableinstance.argument- An argument which will be forwarded to therunmethodisolationLevel- The isolation level of the transaction
-
runInTransaction
-
runInTransaction
-
runInTransaction
public <V> V runInTransaction(StatementRunnableWithResult<V> runnableWithResult, Object argument, int isolationLevel)
-
Connectionclass instead, using try-with-resource blockstry (Connection con = sql2o.open()) { return sql2o.createQuery(query, name).executeAndFetch(Pojo.class); }