All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.reactive.adapter.db.ReactiveSQLConnection Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.reactive.adapter.db;

import com.firefly.db.SQLConnection;
import com.firefly.db.SQLResultSet;
import com.firefly.db.TransactionIsolation;
import com.firefly.utils.function.Func1;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * SQLConnection reactor adapter. It wraps SQLConnection using Spring reactor.
 *
 * @author Pengtao Qiu
 */
public interface ReactiveSQLConnection {

    /**
     * Query single column record by SQL. If the database has not record, it will emit the RecordNotFound exception.
     *
     * @param sql    An SQL that may contain one or more '?' placeholders.
     * @param params SQL parameters.
     * @param     The type of column.
     * @return The result that is wrapped by Mono.
     */
     Mono queryForSingleColumn(String sql, Object... params);

    /**
     * Query single column record by named SQL. If the database has not record, it will emit the RecordNotFound exception.
     *
     * @param sql      A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                 "select * from test where id in (:idList)",
     *                 "select * from test where id = :id",
     *                 "select * from test where id = :{id}",
     *                 "select * from test where id = &id"
     * @param paramMap Named SQL parameters.
     * @param       The type of column.
     * @return The future result.
     */
     Mono namedQueryForSingleColumn(String sql, Map paramMap);

    /**
     * Query single column record by named SQL. If the database has not record, it will emit the RecordNotFound exception.
     *
     * @param sql         A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                    "select * from test where id in (:idList)",
     *                    "select * from test where id = :id",
     *                    "select * from test where id = :{id}",
     *                    "select * from test where id = &id"
     * @param paramObject Named SQL parameter object that uses the property name to match parameter.
     * @param          The type of column.
     * @return The future result.
     */
     Mono namedQueryForSingleColumn(String sql, Object paramObject);

    /**
     * Query record and bind object. If the database has not record, it will emit the RecordNotFound exception.
     *
     * @param sql    A SQL that may contain one or more '?' placeholders.
     * @param clazz  The Class reference of bound object.
     * @param params SQL parameters.
     * @param     The type of bound object.
     * @return The result that binds to Class and it is wrapped by Mono.
     */
     Mono queryForObject(String sql, Class clazz, Object... params);

    /**
     * Query record and bind object. If the database has not record, it will emit the RecordNotFound exception.
     *
     * @param sql      A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                 "select * from test where id in (:idList)",
     *                 "select * from test where id = :id",
     *                 "select * from test where id = :{id}",
     *                 "select * from test where id = &id"
     * @param clazz    The Class reference of bound object.
     * @param paramMap Named SQL parameters.
     * @param       The type of bound object.
     * @return The result that binds to Class and it is wrapped by Mono.
     */
     Mono namedQueryForObject(String sql, Class clazz, Map paramMap);

    /**
     * Query record and bind object. If the database has not record, it will emit the RecordNotFound exception.
     *
     * @param sql         A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                    "select * from test where id in (:idList)",
     *                    "select * from test where id = :id",
     *                    "select * from test where id = :{id}",
     *                    "select * from test where id = &id"
     * @param clazz       The Class reference of bound object.
     * @param paramObject Named SQL parameters.
     * @param          The type of bound object.
     * @return The result that binds to Class and it is wrapped by Mono.
     */
     Mono namedQueryForObject(String sql, Class clazz, Object paramObject);

    /**
     * Query record by id. If the database has not record, it will emit the RecordNotFound exception.
     *
     * @param id    Primary key.
     * @param clazz The Class reference of bound object.
     * @param    The type of bound object.
     * @return The result that binds to Class and it is wrapped by Mono.
     */
     Mono queryById(Object id, Class clazz);

    /**
     * Query records and convert records to a Map.
     *
     * @param sql        A SQL that may contain one or more '?' placeholders.
     * @param valueClass The Class reference of bound object.
     * @param params     SQL parameters.
     * @param         The type of primary key.
     * @param         The type of bound object.
     * @return The result that contains a map, the key is primary key of record, the value is bound object. And it is wrapped by Mono.
     */
     Mono> queryForBeanMap(String sql, Class valueClass, Object... params);

    /**
     * Query records and convert records to a Map.
     *
     * @param sql        A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                   "select * from test where id in (:idList)",
     *                   "select * from test where id = :id",
     *                   "select * from test where id = :{id}",
     *                   "select * from test where id = &id"
     * @param valueClass The Class reference of bound object.
     * @param paramMap   Named SQL parameters.
     * @param         The type of primary key.
     * @param         The type of bound object.
     * @return The result that contains a map, the key is primary key of record, the value is bound object. And it is wrapped by Mono.
     */
     Mono> namedQueryForBeanMap(String sql, Class valueClass, Map paramMap);

    /**
     * Query records and convert records to a Map.
     *
     * @param sql         A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                    "select * from test where id in (:idList)",
     *                    "select * from test where id = :id",
     *                    "select * from test where id = :{id}",
     *                    "select * from test where id = &id"
     * @param valueClass  The Class reference of bound object.
     * @param paramObject Named SQL parameters.
     * @param          The type of primary key.
     * @param          The type of bound object.
     * @return The result that contains a map, the key is primary key of record, the value is bound object. And it is wrapped by Mono.
     */
     Mono> namedQueryForBeanMap(String sql, Class valueClass, Object paramObject);

    /**
     * Query records and bind object.
     *
     * @param sql    A SQL that may contain one or more '?' placeholders.
     * @param clazz  The Class reference of bound object.
     * @param params SQL parameters.
     * @param     The type of bound object.
     * @return The result that contains a list, the list element binds to Class. And it is wrapped by Mono.
     */
     Mono> queryForList(String sql, Class clazz, Object... params);

    /**
     * Query records and bind object.
     *
     * @param sql      A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                 "select * from test where id in (:idList)",
     *                 "select * from test where id = :id",
     *                 "select * from test where id = :{id}",
     *                 "select * from test where id = &id"
     * @param clazz    The Class reference of bound object.
     * @param paramMap Named SQL parameters.
     * @param       The type of bound object.
     * @return The result that contains a list, the list element binds to Class. And it is wrapped by Mono.
     */
     Mono> namedQueryForList(String sql, Class clazz, Map paramMap);

    /**
     * Query records and bind object.
     *
     * @param sql         A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                    "select * from test where id in (:idList)",
     *                    "select * from test where id = :id",
     *                    "select * from test where id = :{id}",
     *                    "select * from test where id = &id"
     * @param clazz       The Class reference of bound object.
     * @param paramObject Named SQL parameters.
     * @param          The type of bound object.
     * @return The result that contains a list, the list element binds to Class. And it is wrapped by Mono.
     */
     Mono> namedQueryForList(String sql, Class clazz, Object paramObject);

    /**
     * Query records and convert result set to javabean using handler.
     *
     * @param sql     A SQL that may contain one or more '?' placeholders.
     * @param handler The function that converts result set to javabean.
     * @param params  SQL parameters.
     * @param      The type of converted object.
     * @return The result that is wrapped by Mono.
     */
     Mono query(String sql, Func1 handler, Object... params);

    /**
     * Query records and convert result set to javabean using handler.
     *
     * @param sql      A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                 "select * from test where id in (:idList)",
     *                 "select * from test where id = :id",
     *                 "select * from test where id = :{id}",
     *                 "select * from test where id = &id"
     * @param handler  The function that converts result set to javabean.
     * @param paramMap Named SQL parameters.
     * @param       The type of converted object.
     * @return The result that is wrapped by Mono.
     */
     Mono namedQuery(String sql, Func1 handler, Map paramMap);

    /**
     * Query records and convert result set to javabean using handler.
     *
     * @param sql         A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&", such as,
     *                    "select * from test where id in (:idList)",
     *                    "select * from test where id = :id",
     *                    "select * from test where id = :{id}",
     *                    "select * from test where id = &id"
     * @param handler     The function that converts result set to javabean.
     * @param paramObject Named SQL parameters.
     * @param          The type of converted object.
     * @return The result that is wrapped by Mono.
     */
     Mono namedQuery(String sql, Func1 handler, Object paramObject);

    /**
     * Update records.
     *
     * @param sql    A SQL that may contain one or more '?' placeholders.
     * @param params SQL parameters.
     * @return The affected row number.
     */
    Mono update(String sql, Object... params);

    /**
     * Update records.
     *
     * @param sql      A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&".
     * @param paramMap Named SQL parameters.
     * @return The affected row number.
     */
    Mono namedUpdate(String sql, Map paramMap);

    /**
     * Update records.
     *
     * @param sql         A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&".
     * @param paramObject Named SQL parameters.
     * @return The affected row number.
     */
    Mono namedUpdate(String sql, Object paramObject);

    /**
     * Update record using the mapped javabean.
     *
     * @param object The mapped javabean.
     * @param     The type of javabean.
     * @return The affected row number.
     */
     Mono updateObject(T object);

    /**
     * Insert records
     *
     * @param sql    A SQL that may contain one or more '?' placeholders.
     * @param params SQL parameters.
     * @param     The type of autoincrement id.
     * @return The autoincrement id that is wrapped by Mono.
     */
     Mono insert(String sql, Object... params);

    /**
     * Insert records
     *
     * @param sql      A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&".
     * @param paramMap Named SQL parameters.
     * @param       The type of autoincrement id.
     * @return The autoincrement id that is wrapped by Mono.
     */
     Mono namedInsert(String sql, Map paramMap);

    /**
     * Insert records
     *
     * @param sql         A SQL that may contain one or more placeholders. The placeholder starts with ":" or "&".
     * @param paramObject Named SQL parameters.
     * @param          The type of autoincrement id.
     * @return The autoincrement id that is wrapped by Mono.
     */
     Mono namedInsert(String sql, Object paramObject);

    /**
     * Insert a javabean to the database.
     *
     * @param object The javabean that is mapped to a database table.
     * @param     The type of javabean.
     * @param     The type of autoincrement id.
     * @return The autoincrement id that is wrapped by Mono.
     */
     Mono insertObject(T object);

    /**
     * Batch to insert javabean.
     *
     * @param list    The javabean list.
     * @param clazz   The javabean Class.
     * @param handler The function that converts result set to java type.
     * @param      The type of javabean.
     * @param      The type of autoincrement id.
     * @return The autoincrement id list that is wrapped by Mono.
     */
     Mono insertObjectBatch(List list, Class clazz, Func1 handler);

    /**
     * Batch to insert javabean.
     *
     * @param list  The javabean list.
     * @param clazz The javabean Class.
     * @param    The type of javabean.
     * @param    The type of autoincrement id.
     * @return The autoincrement id list that is wrapped by Mono.
     */
     Mono> insertObjectBatch(List list, Class clazz);

    /**
     * Execute a sql to batch inserting data.
     *
     * @param sql     A SQL that may contain one or more '?' placeholders.
     * @param params  An array of query replacement parameters.  Each row in
     *                this array is one set of batch replacement values.
     * @param handler The function that converts result set to java type.
     * @param      The type of autoincrement id.
     * @return The autoincrement id list that is wrapped by Mono.
     */
     Mono insertBatch(String sql, Object[][] params, Func1 handler);

    /**
     * Delete data by id and return the affected row number.
     *
     * @param id    The table row id.
     * @param clazz The Mapped javabean Class.
     * @param    The type of javabean.
     * @return Affected row number.
     */
     Mono deleteById(Object id, Class clazz);

    /**
     * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
     *
     * @param sql    A SQL that may contain one or more '?' placeholders.
     * @param params An array of query replacement parameters.  Each row in
     *               this array is one set of batch replacement values.
     * @return The number of rows updated per statement.
     */
    Mono executeBatch(String sql, Object[][] params);

    /**
     * Set the transaction isolation.
     *
     * @param transactionIsolation The transaction isolation.
     * @return If return true, set the transaction isolation success.
     */
    Mono setTransactionIsolation(TransactionIsolation transactionIsolation);

    /**
     * Set transaction committing automatically.
     *
     * @param autoCommit If set the true, the transaction will commit automatically.
     * @return If return true, Set auto committing success.
     */
    Mono setAutoCommit(boolean autoCommit);

    /**
     * Get auto committing.
     *
     * @return If return true, the transaction will commit automatically.
     */
    boolean getAutoCommit();

    /**
     * Rollback the transaction.
     *
     * @return If return true, the transaction rollback success.
     */
    Mono rollback();

    /**
     * Commit a transaction.
     *
     * @return If return true, the transaction committing success.
     */
    Mono commit();

    /**
     * Close the connection.
     *
     * @return If return true, close connection success.
     */
    Mono close();

    /**
     * Commit and then close connection.
     *
     * @return If return true, commit and then close connection success.
     */
    Mono commitAndClose();

    /**
     * Rollback and then close connection.
     *
     * @return If return true, rollback and then close connection success.
     */
    Mono rollbackAndClose();

    /**
     * Execute the statements in transaction.
     *
     * @param func1 The statements will be executed in a transaction.
     * @param    The type of transaction result.
     * @return The transaction result that is wrapped by Mono.
     */
     Mono inTransaction(Func1> func1);

    /**
     * Begin a transaction.
     *
     * @return If return true, begin transaction success.
     */
    Mono beginTransaction();

    /**
     * Rollback and then end the transaction.
     *
     * @return If return true, rollback and then end the transaction success.
     */
    Mono rollbackAndEndTransaction();

    /**
     * Commit and then end the transaction.
     *
     * @return If return true, commit and then end the transaction success.
     */
    Mono commitAndEndTransaction();

    /**
     * Get original SQL connection.
     *
     * @return The original SQL connection.
     */
    SQLConnection getSQLConnection();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy