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

com.firefly.db.SQLConnection Maven / Gradle / Ivy

package com.firefly.db;

import com.firefly.utils.function.Func1;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * The asynchronous SQL connection. It can execute SQL and bind result to Java bean.
 *
 * @author Pengtao Qiu
 */
public interface SQLConnection {

    /**
     * Query single column record by SQL. If the database has not record, it will emit the RecordNotFound exception.
     *
     * @param sql    A SQL that may contain one or more '?' placeholders.
     * @param params SQL parameters.
     * @param     The type of column.
     * @return The future result.
     */
     CompletableFuture 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.
     */
     CompletableFuture 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.
     */
     CompletableFuture 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 future result.
     */
     CompletableFuture 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 future result.
     */
     CompletableFuture 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 parameter object that uses the property name to match parameter.
     * @param          The type of bound object.
     * @return The future result.
     */
     CompletableFuture 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 future result.
     */
     CompletableFuture 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 future result that contains a map, the key is primary key of record, the value is bound object.
     */
     CompletableFuture> 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 future result that contains a map, the key is primary key of record, the value is bound object.
     */
     CompletableFuture> 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 that uses the property name to match parameter.
     * @param          The type of primary key.
     * @param          The type of bound object.
     * @return The future result that contains a map, the key is primary key of record, the value is bound object.
     */
     CompletableFuture> 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 future result that contains a list.
     */
     CompletableFuture> 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 future result that contains a list.
     */
     CompletableFuture> 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 future result that contains a list.
     */
     CompletableFuture> 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 CompletableFuture.
     */
     CompletableFuture 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 CompletableFuture.
     */
     CompletableFuture 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 CompletableFuture.
     */
     CompletableFuture 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.
     */
    CompletableFuture 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.
     */
    CompletableFuture 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.
     */
    CompletableFuture 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.
     */
     CompletableFuture 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 CompletableFuture.
     */
     CompletableFuture 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 CompletableFuture.
     */
     CompletableFuture 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 CompletableFuture.
     */
     CompletableFuture 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 CompletableFuture.
     */
     CompletableFuture 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 CompletableFuture.
     */
     CompletableFuture 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 CompletableFuture.
     */
     CompletableFuture> 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 CompletableFuture.
     */
     CompletableFuture 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.
     */
     CompletableFuture 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.
     */
    CompletableFuture executeBatch(String sql, Object[][] params);

    /**
     * Set the transaction isolation.
     *
     * @param transactionIsolation The transaction isolation.
     * @return If return true, set the transaction isolation success.
     */
    CompletableFuture 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.
     */
    CompletableFuture 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.
     */
    CompletableFuture rollback();

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

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

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

    /**
     * Rollback and then close connection.
     *
     * @return If return true, rollback and then close connection success.
     */
    CompletableFuture 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.
     */
     CompletableFuture inTransaction(Func1> func1);

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy