Class FluentJdbc
- java.lang.Object
-
- org.soulwing.jdbc.FluentJdbc
-
- All Implemented Interfaces:
JdbcOperations
public class FluentJdbc extends Object implements JdbcOperations
A thread-safeJdbcOperations
implementation.This class must be constructed with a
DataSource
that will be used as necessary to obtain connections to the database.import javax.sql.DataSource; import org.soulwing.jdbc.FluentJdbc; DataSource dataSource = ... // typically injected or retrieved via JNDI FluentJdbc sqlTemplate = new FluentJdbc(dataSource);
A single instance of this class that is constructed with an appropriateDataSource
can be concurrently shared by an arbitrary number of application components.- Author:
- Carl Harris
-
-
Constructor Summary
Constructors Constructor Description FluentJdbc(Connection connection)
Constructs a new instance for use by a single thread.FluentJdbc(DataSource dataSource)
Constructs a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JdbcCall
call(String sql)
Creates a call operation.JdbcCall
call(SQLSource source)
Creates a call operation.void
execute(String sql)
Executes a single SQL (DDL) statement.void
execute(SQLSource source)
Executes a single SQL (DDL) statement.void
executeScript(SQLSource source)
Executes the sequence of SQL statements produced by the given source.boolean
isAutoCommit()
Gets theautoCommit
flag state.boolean
isIgnoreErrors()
Gets theignoreErrors
flag state.JdbcQuery<Void>
query()
Creates a query operation.<T> JdbcQuery<T>
queryForType(Class<T> type)
Creates a query operation for objects of a given type.void
setAutoCommit(boolean autoCommit)
Sets theautoCommit
flag.void
setIgnoreErrors(boolean ignoreErrors)
Sets theignoreErrors
flag.void
setLogger(PrintStream stream)
Sets a print stream to use for SQL statement logging.void
setLogger(PrintStream stream, boolean traceEnabled)
Sets a print stream to use for SQL statement logging.void
setLogger(PrintWriter writer)
Sets a print writer to use for SQL statement logging.void
setLogger(PrintWriter writer, boolean traceEnabled)
Sets a print writer to use for SQL statement logging.void
setLogger(JdbcLogger logger)
Sets the logger to use for SQL statement logging.JdbcUpdate
update()
Creates an update operation.
-
-
-
Constructor Detail
-
FluentJdbc
public FluentJdbc(DataSource dataSource)
Constructs a new instance.- Parameters:
dataSource
- data source that will be used to obtain connections to the database
-
FluentJdbc
public FluentJdbc(Connection connection)
Constructs a new instance for use by a single thread.Use this constructor to create a facade instance that uses the given connection whenever a connection to the database is needed. This allows the facade to be used in conjunction with a framework such as Flyway that wants to explicit manage transaction state by providing a specific connection to each operation that needs one.
Because an instance created via this constructor uses a single database connection, it should not be used by multiple concurrent threads.
- Parameters:
connection
- connection that will be used for all JDBC operations invoked using this facade instance
-
-
Method Detail
-
execute
public void execute(String sql)
Executes a single SQL (DDL) statement.- Specified by:
execute
in interfaceJdbcOperations
- Parameters:
sql
- the SQL statement to execute
-
execute
public void execute(SQLSource source)
Executes a single SQL (DDL) statement.- Specified by:
execute
in interfaceJdbcOperations
- Parameters:
source
- source for the SQL statement to execute
-
executeScript
public void executeScript(SQLSource source)
Executes the sequence of SQL statements produced by the given source.- Specified by:
executeScript
in interfaceJdbcOperations
- Parameters:
source
- source of the SQL statements to execute
-
query
public JdbcQuery<Void> query()
Creates a query operation.This is a synonym for
JdbcOperations.queryForType(Class)
withVoid
as the specified type. A query obtained via this method is typically configured to use aResultSetHandler
to process returned rows.- Specified by:
query
in interfaceJdbcOperations
- Returns:
- query object that can be configured and executed to retrieve rows from the database
-
queryForType
public <T> JdbcQuery<T> queryForType(Class<T> type)
Creates a query operation for objects of a given type.- Specified by:
queryForType
in interfaceJdbcOperations
- Parameters:
type
- subject type of the query- Returns:
- a query that can be configured and executed to retreive rows
from the database and return objects of type
T
.
-
update
public JdbcUpdate update()
Creates an update operation.- Specified by:
update
in interfaceJdbcOperations
- Returns:
- an updater that can be configured and executed to update rows in the database.
-
call
public JdbcCall call(String sql)
Creates a call operation.- Specified by:
call
in interfaceJdbcOperations
- Parameters:
sql
- SQL call statement- Returns:
- call operation
-
call
public JdbcCall call(SQLSource source)
Creates a call operation.- Specified by:
call
in interfaceJdbcOperations
- Parameters:
source
- source for the SQL call statement- Returns:
- call operation;
-
setLogger
public void setLogger(JdbcLogger logger)
Sets the logger to use for SQL statement logging.- Parameters:
logger
- the logger to set (may benull
to disable logging)
-
setLogger
public void setLogger(PrintWriter writer)
Sets a print writer to use for SQL statement logging.- Parameters:
writer
- the logger to set (may benull
to disable logging)
-
setLogger
public void setLogger(PrintWriter writer, boolean traceEnabled)
Sets a print writer to use for SQL statement logging.- Parameters:
writer
- the logger to set (may benull
to disable logging)traceEnabled
- flag indicating whether trace level logging should be used
-
setLogger
public void setLogger(PrintStream stream)
Sets a print stream to use for SQL statement logging.- Parameters:
stream
- the logger to set (may benull
to disable logging)
-
setLogger
public void setLogger(PrintStream stream, boolean traceEnabled)
Sets a print stream to use for SQL statement logging.- Parameters:
stream
- the logger to set (may benull
to disable logging)traceEnabled
- flag indicating whether trace level logging should be used
-
isAutoCommit
public boolean isAutoCommit()
Gets theautoCommit
flag state.- Returns:
true
ifexecuteScript
will use auto-commit mode
-
setAutoCommit
public void setAutoCommit(boolean autoCommit)
Sets theautoCommit
flag.This state of this flag is checked when
executeScript
is invoked. Iftrue
, all statements executed by the script will be auto-committed.In a managed transaction environment (e.g. in a Java EE application) setting this flag to
true
may cause script execution to fail.- Parameters:
autoCommit
- the auto-commit flag state to set
-
isIgnoreErrors
public boolean isIgnoreErrors()
Gets theignoreErrors
flag state.- Returns:
true
ifexecuteScript
will ignore errors at the statement level
-
setIgnoreErrors
public void setIgnoreErrors(boolean ignoreErrors)
Sets theignoreErrors
flag.This state of this flag is checked when
executeScript
is invoked. Iftrue
, errors thrown by the execution of any given statement will be ignored.In general, ignoring errors is effective only when the auto-commit flag is also set to
true
.- Parameters:
ignoreErrors
- the flag state to set
-
-