Class JKDataAccessImpl

java.lang.Object
com.jk.data.dataaccess.core.JKDataAccessImpl
All Implemented Interfaces:
JKDataAccessService
Direct Known Subclasses:
H2DataAccess, JKOracleDataAccess, MysqlDataAccess

public class JKDataAccessImpl extends Object implements JKDataAccessService
The Class JKDataAccessImpl.
  • Field Details

    • acceptedTimeInMillis

      public static final int acceptedTimeInMillis
      The Constant acceptedTimeInMillis.
    • maxAcceptedTimeInMillis

      public static final int maxAcceptedTimeInMillis
      The Constant maxAcceptedTimeInMillis.
    • bufferSize

      public static final int bufferSize
      The Constant bufferSize.
    • logger

      protected JKLogger logger
      The logger.
  • Constructor Details

    • JKDataAccessImpl

      public JKDataAccessImpl()
      Instantiates a new JK data access impl.
    • JKDataAccessImpl

      public JKDataAccessImpl(String dataSourcePrefix)
      Instantiates a new JK data access impl.
      Parameters:
      dataSourcePrefix - the data source prefix
    • JKDataAccessImpl

      public JKDataAccessImpl(JKDataSource dataSource)
      Instantiates a new JK data access impl.
      Parameters:
      dataSource - the data source
  • Method Details

    • close

      protected void close(Connection connection)
      Close.
      Parameters:
      connection - the connection
    • close

      protected void close(PreparedStatement ps)
      Close.
      Parameters:
      ps - the ps
    • close

      protected void close(PreparedStatement ps, Connection c)
      Close.
      Parameters:
      ps - the ps
      c - the c
    • close

      protected void close(ResultSet rs)
      Close.
      Parameters:
      rs - the rs
    • close

      protected void close(ResultSet rs, PreparedStatement ps, Connection c)
      Close.
      Parameters:
      rs - the rs
      ps - the ps
      c - the c
    • close

      protected void close(Statement ps)
      Close.
      Parameters:
      ps - the ps
    • executeQueryAsCachedRowSet

      public CachedRowSet executeQueryAsCachedRowSet(String query, Object... params)
      Execute query as cached row set.
      Specified by:
      executeQueryAsCachedRowSet in interface JKDataAccessService
      Parameters:
      query - the query
      params - the params
      Returns:
      the cached row set
    • executeQueryAsIdValue

      public List<JKDbIdValue> executeQueryAsIdValue(String query, Object... params)
      Execute query as id value.
      Specified by:
      executeQueryAsIdValue in interface JKDataAccessService
      Parameters:
      query - the query
      params - the params
      Returns:
      the list
    • executeQueryAsString

      public String executeQueryAsString(String query, Object... params)
      Execute query as string.
      Specified by:
      executeQueryAsString in interface JKDataAccessService
      Parameters:
      query - the query
      params - the params
      Returns:
      the string
    • executeQueryAsString

      public String executeQueryAsString(String query, String fieldSeparator, String recordsSepartor, Object... params)
      Execute query as string.
      Specified by:
      executeQueryAsString in interface JKDataAccessService
      Parameters:
      query - the query
      fieldSeparator - the field separator
      recordsSepartor - the records separtor
      params - the params
      Returns:
      the string
    • executeUpdate

      public Object executeUpdate(JKUpdater updater) throws JKDataAccessException
      Execute update.
      Specified by:
      executeUpdate in interface JKDataAccessService
      Parameters:
      updater - the updater
      Returns:
      the int
      Throws:
      JKDataAccessException - the JK data access exception
    • prepareStatement

      protected PreparedStatement prepareStatement(Connection connection, boolean returnGeneratedKeys, JKUpdater updater) throws SQLException
      Prepare statement.
      Parameters:
      connection - the connection
      returnGeneratedKeys - the return generated keys
      updater - the updater
      Returns:
      the prepared statement
      Throws:
      SQLException - the SQL exception
    • prepareQueryStatement

      protected PreparedStatement prepareQueryStatement(Connection connection, JKFinder finder) throws SQLException
      Prepare query statement.
      Parameters:
      connection - the connection
      finder - the finder
      Returns:
      the prepared statement
      Throws:
      SQLException - the SQL exception
    • executeUpdate

      public Object executeUpdate(JKUpdater updater, boolean ignoreRecordNotFoundException)
      Execute update.
      Specified by:
      executeUpdate in interface JKDataAccessService
      Parameters:
      updater - the updater
      ignoreRecordNotFoundException - the ignore record not found exception
      Returns:
      the int
    • execute

      public int execute(String statement, Object... params) throws JKDataAccessException
      Execute.
      Specified by:
      execute in interface JKDataAccessService
      Parameters:
      statement - the statement
      params - the params
      Returns:
      the int
      Throws:
      JKDataAccessException - the JK data access exception
    • executeQueryAsArray

      public Object[] executeQueryAsArray(String query, Object... params)
      Execute query as array.
      Specified by:
      executeQueryAsArray in interface JKDataAccessService
      Parameters:
      query - the query
      params - the params
      Returns:
      the object[]
    • executeQueryAsList

      public List<List<Object>> executeQueryAsList(String query, Object... params)
      Execute query as list.
      Specified by:
      executeQueryAsList in interface JKDataAccessService
      Parameters:
      query - the query
      params - the params
      Returns:
      the list
    • executeSingleOutputQuery

      public Object executeSingleOutputQuery(String query, Object... params)
      Exeute single output query.
      Specified by:
      executeSingleOutputQuery in interface JKDataAccessService
      Parameters:
      query - the query
      params - the params
      Returns:
      the object
    • findRecord

      public <T> T findRecord(JKFinder finder)
      Find record.
      Specified by:
      findRecord in interface JKDataAccessService
      Type Parameters:
      T - the generic type
      Parameters:
      finder - the finder
      Returns:
      the t
    • findRecord

      public <T> T findRecord(JKFinder finder, String tableName, Object recordId)
      Find record.
      Specified by:
      findRecord in interface JKDataAccessService
      Type Parameters:
      T - the generic type
      Parameters:
      finder - the finder
      tableName - the table name
      recordId - the record id
      Returns:
      the t
    • getConnection

      protected Connection getConnection()
      Gets the connection.
      Returns:
      the connection
    • getConnection

      protected Connection getConnection(boolean query)
      Gets the connection.
      Parameters:
      query - the query
      Returns:
      the connection
    • getDataSource

      protected JKDataSource getDataSource()
      Gets the data source.
      Returns:
      the data source
    • getGeneratedKey

      protected Object getGeneratedKey(PreparedStatement ps) throws SQLException
      Gets the generated key.
      Parameters:
      ps - the ps
      Returns:
      the generated key
      Throws:
      SQLException - the SQL exception
    • getNextId

      protected Long getNextId(Connection connectoin, String tableName, String fieldName) throws JKDataAccessException
      Gets the next id.
      Parameters:
      connectoin - the connectoin
      tableName - the table name
      fieldName - the field name
      Returns:
      the next id
      Throws:
      JKDataAccessException - the JK data access exception
    • getNextId

      protected Long getNextId(Connection con, String tableName, String fieldName, String condition)
      Gets the next id.
      Parameters:
      con - the con
      tableName - the table name
      fieldName - the field name
      condition - the condition
      Returns:
      the next id
    • getNextId

      public Long getNextId(String tableName, String fieldName)
      Gets the next id.
      Specified by:
      getNextId in interface JKDataAccessService
      Parameters:
      tableName - the table name
      fieldName - the field name
      Returns:
      the next id
    • getNextId

      public Long getNextId(String tableName, String fieldName, String condition)
      Gets the next id.
      Specified by:
      getNextId in interface JKDataAccessService
      Parameters:
      tableName - the table name
      fieldName - the field name
      condition - the condition
      Returns:
      the next id
    • getRowsCount

      public int getRowsCount(String query, Object... params)
      Gets the rows count.
      Specified by:
      getRowsCount in interface JKDataAccessService
      Parameters:
      query - the query
      params - the params
      Returns:
      the rows count
    • getSystemDate

      public Date getSystemDate()
      Gets the system date.
      Specified by:
      getSystemDate in interface JKDataAccessService
      Returns:
      the system date
    • getList

      public <T> List<T> getList(JKFinder finder)
      Gets the list.
      Specified by:
      getList in interface JKDataAccessService
      Type Parameters:
      T - the generic type
      Parameters:
      finder - the finder
      Returns:
      the list
    • getList

      public <T> List<T> getList(JKFinder finder, String key)
      Gets the list.
      Specified by:
      getList in interface JKDataAccessService
      Type Parameters:
      T - the generic type
      Parameters:
      finder - the finder
      key - the key
      Returns:
      the list
    • prepareQueryStatement

      protected PreparedStatement prepareQueryStatement(Connection connection, String sql, Object... params) throws SQLException
      Prepare query statement.
      Parameters:
      connection - the connection
      sql - the sql
      params - the params
      Returns:
      the prepared statement
      Throws:
      SQLException - the SQL exception
    • prepareStatement

      protected PreparedStatement prepareStatement(Connection connection, String sql, Object... params) throws SQLException
      Prepare statement.
      Parameters:
      connection - the connection
      sql - the sql
      params - the params
      Returns:
      the prepared statement
      Throws:
      SQLException - the SQL exception
    • prepareStatement

      protected PreparedStatement prepareStatement(Connection connection, boolean returnGeneratedKeys, String sql, Object... params) throws SQLException
      Prepare statement.
      Parameters:
      connection - the connection
      returnGeneratedKeys - the return generated keys
      sql - the sql
      params - the params
      Returns:
      the prepared statement
      Throws:
      SQLException - the SQL exception
    • setParams

      protected PreparedStatement setParams(PreparedStatement prepareStatement, Object... params) throws SQLException
      Sets the params.
      Parameters:
      prepareStatement - the prepare statement
      params - the params
      Returns:
      the prepared statement
      Throws:
      SQLException - the SQL exception
    • printRecordResultSet

      protected void printRecordResultSet(ResultSet rs)
      Prints the record result set.
      Parameters:
      rs - the rs
    • printRecordResultSet

      protected void printRecordResultSet(ResultSet rs, boolean all, PrintStream out)
      Prints the record result set.
      Parameters:
      rs - the rs
      all - the all
      out - the out
    • resetCache

      public static void resetCache()
      Reset cache.
    • runScript

      public void runScript(String fileName)
      Run script.
      Specified by:
      runScript in interface JKDataAccessService
      Parameters:
      fileName - the file name
    • isTableExists

      public boolean isTableExists(String tableName)
      Checks if is table exists.
      Specified by:
      isTableExists in interface JKDataAccessService
      Parameters:
      tableName - the table name
      Returns:
      true, if is table exists
    • setSession

      public void setSession(JKSession session)
      Sets the session.
      Parameters:
      session - the new session
    • describeTable

      public Map<String,String> describeTable(String name)
      Describe table.
      Specified by:
      describeTable in interface JKDataAccessService
      Parameters:
      name - the name
      Returns:
      the map
    • dropTable

      public void dropTable(String tableName)
      Drop table.
      Specified by:
      dropTable in interface JKDataAccessService
      Parameters:
      tableName - the table name
    • executeUpdate

      public Object executeUpdate(String sql, Object... objects)
      Execute update.
      Specified by:
      executeUpdate in interface JKDataAccessService
      Parameters:
      sql - the sql
      objects - the objects
      Returns:
      the object
    • find

      public <T> T find(String query, JKPopulator<T> populator, Object... params)
      Find.
      Specified by:
      find in interface JKDataAccessService
      Type Parameters:
      T - the generic type
      Parameters:
      query - the query
      populator - the populator
      params - the params
      Returns:
      the t
    • getList

      public <T> List<T> getList(String query, JKPopulator<T> populator, Object... params)
      Gets the list.
      Specified by:
      getList in interface JKDataAccessService
      Type Parameters:
      T - the generic type
      Parameters:
      query - the query
      populator - the populator
      params - the params
      Returns:
      the list
    • findAndCacheRecord

      public <T> T findAndCacheRecord(String key, String query, JKPopulator<T> populator, Object... params)
      Find and cache record.
      Specified by:
      findAndCacheRecord in interface JKDataAccessService
      Type Parameters:
      T - the generic type
      Parameters:
      key - the key
      query - the query
      populator - the populator
      params - the params
      Returns:
      the t
    • getListAndCache

      public <T> List<T> getListAndCache(String key, String query, JKPopulator<T> populator, Object... params)
      Gets the list and cache.
      Specified by:
      getListAndCache in interface JKDataAccessService
      Type Parameters:
      T - the generic type
      Parameters:
      key - the key
      query - the query
      populator - the populator
      params - the params
      Returns:
      the list and cache
    • beginTransaction

      public void beginTransaction()
      Begin transaction.
      Specified by:
      beginTransaction in interface JKDataAccessService
    • isInTransaction

      public boolean isInTransaction()
      Checks if is in transaction.
      Returns:
      true, if is in transaction
    • closeTransaction

      public void closeTransaction(boolean commit)
      Close transaction.
      Specified by:
      closeTransaction in interface JKDataAccessService
      Parameters:
      commit - the commit
    • addQueryLog

      protected void addQueryLog(String query, Instant startTimer, Instant endTimer)
      Adds the query log.
      Parameters:
      query - the query
      startTimer - the start timer
      endTimer - the end timer
    • getCaller

      public static String getCaller()
      Gets the caller.
      Returns:
      the caller
    • writeLog

      protected void writeLog(String query, long duration, String caller)
      Write log.
      Parameters:
      query - the query
      duration - the duration
      caller - the caller