Class BaseJdbcGenericDao<T extends net.solarnetwork.dao.Entity<K>,K>

java.lang.Object
org.springframework.dao.support.DaoSupport
org.springframework.jdbc.core.support.JdbcDaoSupport
net.solarnetwork.node.dao.jdbc.AbstractJdbcDao<T>
net.solarnetwork.node.dao.jdbc.BaseJdbcGenericDao<T,K>
All Implemented Interfaces:
net.solarnetwork.dao.GenericDao<T,K>, JdbcDao, org.springframework.beans.factory.InitializingBean
Direct Known Subclasses:
BaseJdbcBatchableDao, JdbcSecurityTokenDao

public abstract class BaseJdbcGenericDao<T extends net.solarnetwork.dao.Entity<K>,K> extends AbstractJdbcDao<T> implements net.solarnetwork.dao.GenericDao<T,K>
Base implementation of GenericDao for SolarNode using JDBC.
Version:
2.1
Author:
matt
  • Field Details

  • Constructor Details

    • BaseJdbcGenericDao

      public BaseJdbcGenericDao(Class<? extends T> objectType, Class<? extends K> keyType, org.springframework.jdbc.core.RowMapper<T> rowMapper)
      Constructor.
      Parameters:
      objectType - the entity type
      keyType - the key type
      rowMapper - a mapper to use when mapping entity query result rows to entity objects
      Throws:
      IllegalArgumentException - if any parameter is null
    • BaseJdbcGenericDao

      public BaseJdbcGenericDao(Class<? extends T> objectType, Class<? extends K> keyType, org.springframework.jdbc.core.RowMapper<T> rowMapper, String tableNameTemplate, String entityName, int version)
      Init with an an entity name and table version, deriving various names based on conventions.
      Parameters:
      objectType - the entity type
      keyType - the key type
      rowMapper - a mapper to use when mapping entity query result rows to entity objects
      tableNameTemplate - a template with a %s parameter for the SQL table name
      entityName - The entity name to use. This name forms the basis of the default SQL resource prefix, table name, tables version query, and SQL init resource.
      version - the tables version, to manage DDL migrations
  • Method Details

    • getObjectType

      public Class<? extends T> getObjectType()
      Specified by:
      getObjectType in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • getKeyType

      public Class<? extends K> getKeyType()
      Get the key type.
      Returns:
      the type, never null
    • getRowMapper

      protected org.springframework.jdbc.core.RowMapper<T> getRowMapper()
      Get the default row mapper.
      Returns:
      The row mapper.
    • save

      public K save(T entity)
      Specified by:
      save in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • postEntityEvent

      protected void postEntityEvent(K id, T entity, net.solarnetwork.dao.GenericDao.EntityEventType eventType)
      Post an entity event.

      The AbstractJdbcDao.getEventAdmin() service must be available for the event to be posted.

      Parameters:
      id - the entity ID
      entity - the optional entity
      eventType - the type of event
    • get

      public T get(K id)
      Specified by:
      get in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • findFirst

      protected T findFirst(String sql, Object... parameters)
      Get the first returned query result.

      The getRowMapper() will be used to map the results.

      Parameters:
      sql - the SQL to execute
      parameters - the optional parameters
      Returns:
      the first result, or null
    • primaryKeyArguments

      protected Object[] primaryKeyArguments(K id)
      Get an argument list for a primary key.

      This method handles UUID values as a pair of Long arguments for the most and least significant bits. All other keys are returned as a single argument list as-is.

      Parameters:
      id - the primary key
      Returns:
      the arguments
    • getAll

      public Collection<T> getAll(List<net.solarnetwork.domain.SortDescriptor> sorts)
      Specified by:
      getAll in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • querySql

      protected String querySql(String classPathResource, List<net.solarnetwork.domain.SortDescriptor> sorts)
      Get the SQL to use for a query with optional sort descriptors applied.

      This method will call sqlOrderClauses(String, List) and if that returns any values, applySqlOrderClauses(String, List).

      Parameters:
      classPathResource - the base SQL resource to load
      sorts - the sorts to apply
      Returns:
      the SQL
    • sqlOrderClauses

      protected List<String> sqlOrderClauses(String classPathResource, List<net.solarnetwork.domain.SortDescriptor> sorts)
      Get a list of SQL ORDER BY clause values to apply for a given query and sort descriptors.

      The returned array should contain just the clause values, like my_id DESC.

      This method handles the sort keys defined in GenericDao.StandardSortKey by generating order clauses that are named after the enumeration values themselves. Extending classes can override this method to support more keys, or override this behaviour.

      Parameters:
      classPathResource - the SQL resource the order clauses is to be applied to
      sorts - the sort descriptors
      Returns:
      the order clauses
    • applySqlOrderClauses

      public static String applySqlOrderClauses(String sql, List<String> orderClauses)
      Apply a list of SQL order clauses to a SQL statement.

      This method looks for the last ORDER BY in sql, and replaces it with a newly generated order clause derived from the orderClauses list. If no ORDER BY is found, it will be appended along with the generated order clause.

      Parameters:
      sql - the SQL
      orderClauses - the order clauses, as returned from sqlOrderClause(String, boolean)
      Returns:
      the SQL
    • sqlOrderClause

      public static String sqlOrderClause(String columnName, boolean descending)
      Get a single SQL order clause for a given column and direction.
      Parameters:
      columnName - the column name
      descending - true for descending order, false for ascending
      Returns:
      the SQL clause
    • delete

      public void delete(T entity)
      Specified by:
      delete in interface net.solarnetwork.dao.GenericDao<T extends net.solarnetwork.dao.Entity<K>,K>
    • setInstantParameter

      public static void setInstantParameter(PreparedStatement stmt, int parameterIndex, Instant time) throws SQLException
      Set an Instant as a timestamp statement parameter.
      Parameters:
      stmt - the statement
      parameterIndex - the statement parameter index to set
      time - the time to set
      Throws:
      SQLException - if any SQL error occurs
    • getInstantColumn

      public static Instant getInstantColumn(ResultSet rs, int columnIndex) throws SQLException
      Get an Instant from a timestamp result set column.
      Parameters:
      rs - the result set
      columnIndex - the column index
      Returns:
      the new instant, or null if the column was null
      Throws:
      SQLException - if any SQL error occurs
    • setUuidParameters

      public static void setUuidParameters(PreparedStatement stmt, int parameterIndex, UUID uuid) throws SQLException
      Set a UUID as a pair of long statement parameters.
      Parameters:
      stmt - the statement
      parameterIndex - the statement parameter index to set the UUID upper bits; the lower bits will be set on parameter parameterIndex + 1
      uuid - the UUID to set
      Throws:
      SQLException - if any SQL error occurs
    • getUuidColumns

      public static UUID getUuidColumns(ResultSet rs, int columnIndex) throws SQLException
      Get a UUID from a pair of long result set columns.
      Parameters:
      rs - the result set
      columnIndex - the column index of the UUID upper bits; the lower bits will be read from column columnIndex + 1
      Returns:
      the new UUID, or null if either column was null
      Throws:
      SQLException - if any SQL error occurs