ua.gradsoft.xwikisql
Class SqlDatabase

java.lang.Object
  extended by ua.gradsoft.xwikisql.SqlDatabase
All Implemented Interfaces:
SqlErrorable

public class SqlDatabase
extends java.lang.Object
implements SqlErrorable

Object, which holds name of connection pool and can evaluate sql statements on it.


Method Summary
 void clearError()
          clear all information about error, so wasError become false.
 SqlResult executeQuery(java.lang.String sql)
          execute sql query sql .
 SqlResult executeQuery(java.lang.String sql, java.util.List<java.lang.Object> params)
          execute sql query sql with parameters params .
 java.lang.Integer executeUpdate(java.lang.String sql)
          execute sql statement sql .
 java.lang.Integer executeUpdate(java.lang.String sql, java.util.List<java.lang.Object> params)
          execute sql statement sql with parameters params .
 java.lang.Exception getLastException()
          get last Exception
 boolean getLogEnabled()
          when this propery is set to true - do logging according with settings in xwiki configurations, otherwise - complete disable one.
 java.sql.Connection getNativeConnection()
          direct access to native Java connection.
 SqlPluginApi getSqlPluginApi()
           
 boolean getThrowExceptions()
          when this property is set to true, than engine throws exception on error, otherwise - method usially return null and exceptions are saved for further retrieving.
 java.lang.Exception handleError(java.lang.String msg, java.lang.String sql, java.lang.Exception ex)
          also set own last exception and wasError to appropriative values.
 void setLastException(java.lang.Exception ex)
          set last Exception
 void setLogEnabled(boolean value)
          set logEnabled property,
 void setThrowExceptions(boolean value)
          set throwExceptions property,
 boolean wasError()
          true if error was handled (and later not cleared) during some previous operations on this object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

executeQuery

public SqlResult executeQuery(java.lang.String sql)
execute sql query sql . Shortcat to executeQuery(sql,Collections.emptyList())

Returns:
result of query wrapped to SqlResult object.
See Also:
executeQuery(java.lang.String,java.util.List)

executeQuery

public SqlResult executeQuery(java.lang.String sql,
                              java.util.List<java.lang.Object> params)
execute sql query sql with parameters params .

Database cursor is closed immediatly after query execution (Data is copied into memory if necossary), so user can not think about connection management.

If query is return more than maxRetrievedRows configuration value, then only maxRetrievedRows rows actually retrieved and call of isNotAllRows() of returned SqlResult value must return true.

If error is occured during execution of query, than behavour is depend from throwExceptions configuration variable: when one is set to true, than SqlRuntimeException is thrown, otherwise null is returned and SqlErrorable wasError and lastException fields are set.

Parameters:
sql - - sql query to execute
params - - sql binden variables for this query.
Returns:
result of query wrapped to SqlResult.
Throws:
SqlRuntimeExceptin
See Also:
SqlResult, SqlPluginConfiguration.getThrowExceptions(), SqlPluginConfiguration.getMaxRowsRetrieved(), Statement.executeQuery(java.lang.String)

executeUpdate

public java.lang.Integer executeUpdate(java.lang.String sql)
execute sql statement sql . Shortcat to executeQuery(sql,Collections.emptyList()) ,

See Also:
executeUpdate(java.lang.String,java.util.List)

executeUpdate

public java.lang.Integer executeUpdate(java.lang.String sql,
                                       java.util.List<java.lang.Object> params)
execute sql statement sql with parameters params . Statement can be INSERT, UPDATE, DELETE or DDL statement.

Database cursor is closed immediatly after query execution.

If error is occured during execution of query, than behavour is depend from throwExceptions configuration variable.

Throws:
SqlRuntimeExceptin
See Also:
SqlPluginConfiguration.getThrowExceptions(), Statement.executeUpdate(java.lang.String)

handleError

public java.lang.Exception handleError(java.lang.String msg,
                                       java.lang.String sql,
                                       java.lang.Exception ex)
Description copied from interface: SqlErrorable
also set own last exception and wasError to appropriative values. If configuartion parameter throwException is set, throw received exception

Specified by:
handleError in interface SqlErrorable
Parameters:
msg - - message to log (or throw). Can be null if nothing specifcs.
sql - - sql which cause this error. Can be null if unavaible.
ex - - previous exception in chain, if exists.
Returns:
exception, which consists from message and cause.

wasError

public boolean wasError()
Description copied from interface: SqlErrorable
true if error was handled (and later not cleared) during some previous operations on this object

Specified by:
wasError in interface SqlErrorable
Returns:
true if one of previous actions cause sql error.

getLastException

public java.lang.Exception getLastException()
Description copied from interface: SqlErrorable
get last Exception

Specified by:
getLastException in interface SqlErrorable
Returns:
last exception if wasError, otherwise null.

setLastException

public void setLastException(java.lang.Exception ex)
Description copied from interface: SqlErrorable
set last Exception

Specified by:
setLastException in interface SqlErrorable

clearError

public void clearError()
Description copied from interface: SqlErrorable
clear all information about error, so wasError become false.

Specified by:
clearError in interface SqlErrorable

getThrowExceptions

public boolean getThrowExceptions()
Description copied from interface: SqlErrorable
when this property is set to true, than engine throws exception on error, otherwise - method usially return null and exceptions are saved for further retrieving.

Specified by:
getThrowExceptions in interface SqlErrorable
See Also:
SqlPluginConfiguration.getThrowExceptions(), SqlErrorable.setThrowExceptions(boolean)

setThrowExceptions

public void setThrowExceptions(boolean value)
Description copied from interface: SqlErrorable
set throwExceptions property,

Specified by:
setThrowExceptions in interface SqlErrorable
Parameters:
value - -- value to set
See Also:
SqlErrorable.getThrowExceptions()

getLogEnabled

public boolean getLogEnabled()
Description copied from interface: SqlErrorable
when this propery is set to true - do logging according with settings in xwiki configurations, otherwise - complete disable one. Initial value is true (by default) and set from value of parent object. (and for top-level objects can be set via configuration.)

Specified by:
getLogEnabled in interface SqlErrorable
See Also:
SqlPluginConfiguration.getLogEnabled()

setLogEnabled

public void setLogEnabled(boolean value)
Description copied from interface: SqlErrorable
set logEnabled property,

Specified by:
setLogEnabled in interface SqlErrorable
Parameters:
value - -- value to set
See Also:
SqlErrorable.getLogEnabled()

getNativeConnection

public java.sql.Connection getNativeConnection()
                                        throws java.sql.SQLException
direct access to native Java connection.

Throws:
java.sql.SQLException

getSqlPluginApi

public SqlPluginApi getSqlPluginApi()
Returns:
api