Class RestSqlAdapter

  • All Implemented Interfaces:
    IDatabaseAdapter, java.io.Serializable, java.lang.AutoCloseable, org.api4.java.common.control.ILoggingCustomizable

    public class RestSqlAdapter
    extends java.lang.Object
    implements IDatabaseAdapter
    This is a simple util class for easy database access and query execution in sql. You need to make sure that the respective JDBC connector is in the class path. By default, the adapter uses the mysql driver, but any jdbc driver can be used.
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void checkConnection()
      Checks whether the connection to the database is still alive and re-establishs the connection if it is not.
      void close()
      Close the connection.
      void createTable​(java.lang.String tablename, java.lang.String nameOfPrimaryField, java.util.Collection<java.lang.String> fieldnames, java.util.Map<java.lang.String,​java.lang.String> types, java.util.Collection<java.lang.String> keys)  
      void executeQueriesAtomically​(java.util.List<java.sql.PreparedStatement> queries)
      Executes the given statements atomically.
      com.fasterxml.jackson.databind.JsonNode executeRESTCall​(java.lang.String URL, java.lang.String query)  
      java.lang.String getLoggerName()  
      java.util.List<org.api4.java.datastructure.kvstore.IKVStore> getResultsOfQuery​(java.lang.String query, java.util.List<java.lang.String> values)
      Retrieves the select result for the given query that can have placeholders.
      java.util.List<org.api4.java.datastructure.kvstore.IKVStore> getRowsOfTable​(java.lang.String table, java.util.Map<java.lang.String,​java.lang.String> conditions)
      Retrieves all rows of a table which satisfy certain conditions (WHERE clause).
      int[] insert​(java.lang.String query)  
      int[] insert​(java.lang.String sql, java.util.List<? extends java.lang.Object> values)
      Executes an insert query and returns the row ids of the created entries.
      int[] insert​(java.lang.String table, java.util.Map<java.lang.String,​? extends java.lang.Object> values)
      Creates and executes an insert query for the given table and the values as specified in the map.
      int[] insertMultiple​(java.lang.String tablename, java.util.List<java.lang.String> keys, java.util.List<java.util.List<?>> values)
      Creates a multi-insert statement and executes it.
      int[] insertMultiple​(java.lang.String table, java.util.List<java.lang.String> keys, java.util.List<java.util.List<? extends java.lang.Object>> datarows, int chunkSize)
      Creates a multi-insert statement and executes it.
      java.util.List<org.api4.java.datastructure.kvstore.IKVStore> query​(java.lang.String query)
      Sends a query to the database server which can be an arbitrary query.
      java.util.List<org.api4.java.datastructure.kvstore.IKVStore> select​(java.lang.String query)  
      void setLoggerName​(java.lang.String name)  
      int update​(java.lang.String query)
      Execute the given sql statement as an update.
      int update​(java.lang.String sql, java.util.List<? extends java.lang.Object> values)
      Execute the given sql statement with placeholders as an update filling the placeholders with the given values beforehand.
      int update​(java.lang.String tablename, java.util.Map<java.lang.String,​? extends java.lang.Object> valuesToWrite, java.util.Map<java.lang.String,​? extends java.lang.Object> where)
      Create and execute an update statement for some table updating the values as described in updateValues and only affect those entries satisfying the conditions.
      static java.lang.String whereClauseElement​(java.lang.String key, java.lang.String value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • select

        public java.util.List<org.api4.java.datastructure.kvstore.IKVStore> select​(java.lang.String query)
                                                                            throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • insert

        public int[] insert​(java.lang.String table,
                            java.util.Map<java.lang.String,​? extends java.lang.Object> values)
                     throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Creates and executes an insert query for the given table and the values as specified in the map.
        Specified by:
        insert in interface IDatabaseAdapter
        Parameters:
        table - The table where to insert the data.
        values - The map of key:value pairs to be inserted into the table.
        Returns:
        An array of the row ids of the inserted entries.
        Throws:
        java.sql.SQLException - Thrown, if there was an issue with the query format or the connection to the database.
      • insertMultiple

        public int[] insertMultiple​(java.lang.String tablename,
                                    java.util.List<java.lang.String> keys,
                                    java.util.List<java.util.List<?>> values)
                             throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Creates a multi-insert statement and executes it. The returned array contains the row id's of the inserted rows. (By default it creates chunks of size 10.000 rows per query to be inserted.)
        Specified by:
        insertMultiple in interface IDatabaseAdapter
        Parameters:
        tablename - The table to which the rows are to be added.
        keys - The list of column keys for which values are set.
        values - The list of value lists to be filled into the table.
        Returns:
        An array of row id's of the inserted rows.
        Throws:
        java.sql.SQLException - Thrown, if the sql statement was malformed, could not be executed, or the connection to the database failed.
      • insert

        public int[] insert​(java.lang.String query)
                     throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • update

        public int update​(java.lang.String query)
                   throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Execute the given sql statement as an update.
        Specified by:
        update in interface IDatabaseAdapter
        Parameters:
        query - The sql statement to be executed.
        Returns:
        The number of rows affected by the update statement.
        Throws:
        java.sql.SQLException - Thrown if the statement is malformed or an issue while executing the sql statement occurs.
      • query

        public java.util.List<org.api4.java.datastructure.kvstore.IKVStore> query​(java.lang.String query)
                                                                           throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Sends a query to the database server which can be an arbitrary query.
        Specified by:
        query in interface IDatabaseAdapter
        Parameters:
        query - The sql statement to be executed.
        Returns:
        If there is a result set returned it will be parsed into a list of IKVStores
        Throws:
        java.sql.SQLException - Thrown, if the the sql statement cannot be executed for whatever reasons.
      • executeRESTCall

        public com.fasterxml.jackson.databind.JsonNode executeRESTCall​(java.lang.String URL,
                                                                       java.lang.String query)
                                                                throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • update

        public int update​(java.lang.String tablename,
                          java.util.Map<java.lang.String,​? extends java.lang.Object> valuesToWrite,
                          java.util.Map<java.lang.String,​? extends java.lang.Object> where)
                   throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Create and execute an update statement for some table updating the values as described in updateValues and only affect those entries satisfying the conditions.
        Specified by:
        update in interface IDatabaseAdapter
        Parameters:
        tablename - The table which is to be updated.
        valuesToWrite - The description how entries are to be updated.
        where - The description of the where-clause, conditioning the entries which are to be updated.
        Returns:
        The number of rows affected by the update statement.
        Throws:
        java.sql.SQLException - Thrown if the statement is malformed or an issue while executing the sql statement occurs.
      • whereClauseElement

        public static java.lang.String whereClauseElement​(java.lang.String key,
                                                          java.lang.String value)
      • getLoggerName

        public java.lang.String getLoggerName()
        Specified by:
        getLoggerName in interface org.api4.java.common.control.ILoggingCustomizable
      • setLoggerName

        public void setLoggerName​(java.lang.String name)
        Specified by:
        setLoggerName in interface org.api4.java.common.control.ILoggingCustomizable
      • checkConnection

        public void checkConnection()
                             throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Checks whether the connection to the database is still alive and re-establishs the connection if it is not.
        Specified by:
        checkConnection in interface IDatabaseAdapter
        Throws:
        java.sql.SQLException - Thrown, if there was an issue with reconnecting to the database server.
      • createTable

        public void createTable​(java.lang.String tablename,
                                java.lang.String nameOfPrimaryField,
                                java.util.Collection<java.lang.String> fieldnames,
                                java.util.Map<java.lang.String,​java.lang.String> types,
                                java.util.Collection<java.lang.String> keys)
                         throws java.sql.SQLException
        Specified by:
        createTable in interface IDatabaseAdapter
        Throws:
        java.sql.SQLException
      • getRowsOfTable

        public java.util.List<org.api4.java.datastructure.kvstore.IKVStore> getRowsOfTable​(java.lang.String table,
                                                                                           java.util.Map<java.lang.String,​java.lang.String> conditions)
                                                                                    throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Retrieves all rows of a table which satisfy certain conditions (WHERE clause).
        Specified by:
        getRowsOfTable in interface IDatabaseAdapter
        Parameters:
        table - The table for which all entries shall be returned.
        conditions - The conditions a result entry must satisfy.
        Returns:
        A list of IKVStores containing the data of the table.
        Throws:
        java.sql.SQLException - Thrown, if there was an issue with the connection to the database.
      • getResultsOfQuery

        public java.util.List<org.api4.java.datastructure.kvstore.IKVStore> getResultsOfQuery​(java.lang.String query,
                                                                                              java.util.List<java.lang.String> values)
                                                                                       throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Retrieves the select result for the given query that can have placeholders.
        Specified by:
        getResultsOfQuery in interface IDatabaseAdapter
        Parameters:
        query - The SQL query which is to be executed (with placeholders).
        values - A list of placeholder values that need to be filled in.
        Returns:
        A list of IKVStores containing the result data of the query.
        Throws:
        java.sql.SQLException - Thrown, if there was an issue with the query format or the connection to the database.
      • insert

        public int[] insert​(java.lang.String sql,
                            java.util.List<? extends java.lang.Object> values)
                     throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Executes an insert query and returns the row ids of the created entries.
        Specified by:
        insert in interface IDatabaseAdapter
        Parameters:
        sql - The insert statement which shall be executed that may have placeholders.
        values - A list of values for the placeholders.
        Returns:
        An array of the row ids of the inserted entries.
        Throws:
        java.sql.SQLException - Thrown, if there was an issue with the query format or the connection to the database.
      • insertMultiple

        public int[] insertMultiple​(java.lang.String table,
                                    java.util.List<java.lang.String> keys,
                                    java.util.List<java.util.List<? extends java.lang.Object>> datarows,
                                    int chunkSize)
                             throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Creates a multi-insert statement and executes it. The returned array contains the row id's of the inserted rows.
        Specified by:
        insertMultiple in interface IDatabaseAdapter
        Parameters:
        table - The table to which the rows are to be added.
        keys - The list of column keys for which values are set.
        datarows - The list of value lists to be filled into the table.
        chunkSize - The number of rows which are added within one single database transaction. (10,000 seems to be a good value for this)
        Returns:
        An array of row id's of the inserted rows.
        Throws:
        java.sql.SQLException - Thrown, if the sql statement was malformed, could not be executed, or the connection to the database failed.
      • update

        public int update​(java.lang.String sql,
                          java.util.List<? extends java.lang.Object> values)
                   throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Execute the given sql statement with placeholders as an update filling the placeholders with the given values beforehand.
        Specified by:
        update in interface IDatabaseAdapter
        Parameters:
        sql - The sql statement with placeholders to be executed.
        values - List of values for the respective placeholders.
        Returns:
        The number of rows affected by the update statement.
        Throws:
        java.sql.SQLException - Thrown if the statement is malformed or an issue while executing the sql statement occurs.
      • executeQueriesAtomically

        public void executeQueriesAtomically​(java.util.List<java.sql.PreparedStatement> queries)
                                      throws java.sql.SQLException
        Description copied from interface: IDatabaseAdapter
        Executes the given statements atomically. Only works if no other statements are sent through this adapter in parallel! Only use for single-threaded applications, otherwise side effects may happen as this changes the auto commit settings of the connection temporarily.
        Specified by:
        executeQueriesAtomically in interface IDatabaseAdapter
        Parameters:
        queries - The queries to execute atomically
        Throws:
        java.sql.SQLException - If the status of the connection cannot be changed. If something goes wrong while executing the given statements, they are rolled back before they are committed.
      • close

        public void close()
        Description copied from interface: IDatabaseAdapter
        Close the connection. No more queries can be sent after having the access object closed
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface IDatabaseAdapter