Class NativeSqlContext

java.lang.Object
org.litebridge.orm.nativesql.NativeSqlContext

public final class NativeSqlContext extends Object
Provides a context for executing native SQL queries and updates in a transactional database environment.

This class facilitates the execution of raw SQL operations using the capabilities provided by a TransactionalDatabaseProvider.

It supports parameterised SQL queries and updates with flexible parameter binding and ensures that all database interactions are performed in a transactional context.

  • Constructor Details

    • NativeSqlContext

      public NativeSqlContext(TransactionalDatabaseProvider databaseProvider)
      Constructs a new NativeSqlContext instance.
      Parameters:
      databaseProvider - the database provider to use for executing SQL operations
  • Method Details

    • query

      public List<Row> query(String sql, @Nullable Object... bindParameters)
      Executes a SQL query with the given SQL string and a variable-length list of positional bind parameters.

      This method takes a variable number of parameters and delegates to the overloaded query method that accepts a list of bind parameters.

      Parameters:
      sql - the SQL query string to be executed; must not be null
      bindParameters - the variable-length list of parameters to bind to the query
      Returns:
      a list of Row objects representing the result set of the query
      Throws:
      IllegalStateException - if an error occurs during query execution
      See Also:
    • query

      public List<Row> query(String sql, List<@Nullable Object> bindParameters)
      Executes a SQL query with the given SQL string and a list of positional bind parameters.
      Parameters:
      sql - the SQL query string to be executed; must not be null
      bindParameters - the list of parameters to bind to the query
      Returns:
      a list of Row objects representing the result set of the query
      Throws:
      IllegalStateException - if an error occurs during query execution
      See Also:
    • query

      public List<Row> query(String sql, Map<String,@Nullable Object> bindParameters)
      Executes a SQL query with the given SQL string and a map of named bind parameters.
      Parameters:
      sql - the SQL query string to be executed; must not be null
      bindParameters - the map of named parameters to bind to the query
      Returns:
      a list of Row objects representing the result set of the query
      Throws:
      IllegalStateException - if an error occurs during query execution
      See Also:
    • execute

      public UpdateResult execute(String sql, @Nullable Object... bindParameters)
      Executes a SQL update statement with the given SQL string and a variable-length list of positional bind parameters.

      This method delegates to the overloaded execute method that accepts a list of bind parameters.

      Parameters:
      sql - the SQL update statement to execute; must not be null
      bindParameters - the variable-length list of positional parameters to bind to the statement
      Returns:
      an UpdateResult object that encapsulates the outcome of the update operation
      Throws:
      IllegalStateException - if an error occurs while executing the update statement
      See Also:
    • execute

      public UpdateResult execute(String sql, List<@Nullable Object> bindParameters)
      Executes a SQL update statement with the given SQL string and a list of positional bind parameters.
      Parameters:
      sql - the SQL update statement to execute; must not be null
      bindParameters - the variable-length list of positional parameters to bind to the statement
      Returns:
      an UpdateResult object that encapsulates the outcome of the update operation
      Throws:
      IllegalStateException - if an error occurs while executing the update statement
      See Also:
    • execute

      public UpdateResult execute(String sql, Map<String,@Nullable Object> bindParameters)
      Executes a SQL update statement with the given SQL string and a map of named bind parameters.
      Parameters:
      sql - the SQL update statement to execute; must not be null
      bindParameters - the map of named parameters to bind to the statement
      Returns:
      an UpdateResult object that encapsulates the outcome of the update operation
      Throws:
      IllegalStateException - if an error occurs while executing the update statement
      See Also:
    • executeBatch

      public BatchUpdateResult executeBatch(String sql, List<List<Object>> rowBindParameters)
      Executes a batch SQL update statement with the given SQL string and a per-row list containing per-row positional bind parameters.
      Parameters:
      sql - the SQL update statement to execute; must not be null
      rowBindParameters - the list of positional parameters to bind to the statement, per row
      Returns:
      the outcome of the batch update operation
      Throws:
      IllegalStateException - if an error occurs while executing the update statement
      See Also:
    • executeNamedBatch

      public BatchUpdateResult executeNamedBatch(String sql, List<Map<String,@Nullable Object>> rowBindParameters)
      Executes a batch SQL update statement with the given SQL string and a per-row list containing named bind parameters.
      Parameters:
      sql - the SQL update statement to execute; must not be null
      rowBindParameters - the list of named parameters to bind to the statement, per row
      Returns:
      the outcome of the batch update operation
      Throws:
      IllegalStateException - if an error occurs while executing the update statement
      See Also:
    • parse

      public ParsedSql parse(String sql)
      Parses the specified SQL statement and returns a positional parameter-based version along with bind parameter metadata.

      For named parameter-based queries, this translates the SQL into a positional parameter-based version and provides the positional index of each bind parameter.

      This returns the cached parsed SQL statement if it is found in the cache. Otherwise, it parses the input SQL and caches the parsed SQL statement.

      Parameters:
      sql - SQL statement to parse/cache.
      Returns:
      Parsed (and cached) SQL statement and metadata.