OracleStatement class

Represents a prepared SQL statement for executing queries and DML operations.

This class provides methods for executing SQL statements, fetching results, binding parameters, and performing batch operations. It supports both immediate execution and preparation followed by parameter binding.

Statement Lifecycle:

  1. Create statement with execute or prepare
  2. Bind parameters if needed with bindByPos or bindVar
  3. Execute with executeStatement (if prepared)
  4. Fetch results with rows, fetchOne, or fetchAll
  5. Close statement with close when done

Example - Simple Query:

final stmt = await connection.execute(
  'SELECT id, name FROM employees WHERE dept_id = :1',
  params: [10],
);

try {
  await for (final row in stmt.rows()) {
    print('Employee: ${row['id']} - ${row['name']}');
  }
} finally {
  await stmt.close();
}

Example - Prepared Statement with Multiple Executions:

final stmt = await OracleStatement.prepare(
  dpiOracle: dpiOracle,
  connection: connection,
  context: context,
  sql: 'SELECT * FROM users WHERE id = :1',
);

try {
  for (final userId in [1, 2, 3]) {
    await stmt.bindByPos(1, userId);
    await stmt.executeStatement();

    final user = await stmt.fetchOne();
    print('User: $user');
  }
} finally {
  await stmt.close();
}

Example - Batch Insert:

final stmt = await OracleStatement.prepare(
  dpiOracle: dpiOracle,
  connection: connection,
  context: context,
  sql: 'INSERT INTO orders (id, customer, amount) VALUES (:1, :2, :3)',
);

try {
  final rowsAffected = await stmt.executemany([
    [1, 'Alice', 100.50],
    [2, 'Bob', 250.75],
    [3, 'Charlie', 175.25],
  ]);
  print('Inserted $rowsAffected rows');
} finally {
  await stmt.close();
}

Important:

  • Always call close when done to free resources
  • Use executemany for batch operations (much faster than loops)
  • For large result sets, use rows stream instead of fetchAll

See also:

Properties

columnCount int?
no setter
hashCode int
The hash code for this object.
no setterinherited
isDisposed bool
no setter
isExecuted bool
no setter
rowsAffected int
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sql String
no setter

Methods

bindByPos(int position, dynamic value) Future<void>
Bind a value by position (1-based)
bindVar(int position, OracleVariable variable) Future<void>
Bind a variable (for OUT parameters)
close() Future<void>
Close the statement
executemany(List<List> parameters, {bool batchErrors = false, bool arrayDmlRowCounts = false}) Future
Execute statement multiple times with different bind values
executeStatement() Future<void>
Execute the prepared statement (internal method, also used by callproc)
fetchAll() Future<List<Map<String, dynamic>>>
Fetch all rows
fetchOne() Future<Map<String, dynamic>?>
Fetch a single row
getArrayDmlRowCounts() Future<List<int>>
Get array DML row counts
getBatchErrorCount() Future<int>
Get batch error count (for executemany with batchErrors=true)
getColumnInfo() Future<List<ColumnInfo>>
Get column information
getFetchArraySize() Future<int>
Get fetch array size
getStatementType() Future<int>
Get statement type
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
rows() Stream<Map<String, dynamic>>
Iterate over result rows
scroll(int mode, {int offset = 0}) Future<void>
Scroll to a specific row (for scrollable cursors)
setFetchArraySize(int arraySize) Future<void>
Set array size for fetching
setPrefetchRows(int numRows) Future<void>
Set prefetch rows
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

execute({required DpiOracle dpiOracle, required Pointer<dpiConn> connection, required Pointer<dpiContext> context, required String sql, List? params, Map<String, dynamic>? namedParams}) Future<OracleStatement>
Execute a SQL statement
fromPointer({required DpiOracle dpiOracle, required Pointer<dpiConn> connection, required Pointer<dpiContext> context, required Pointer<Pointer<dpiStmt>> statementPtr, required MemoryManager memoryManager, required String sql, bool executed = true}) OracleStatement
Create an OracleStatement from an existing statement pointer (for REF CURSOR)
prepare({required DpiOracle dpiOracle, required Pointer<dpiConn> connection, required Pointer<dpiContext> context, required String sql, bool scrollable = false}) Future<OracleStatement>
Prepare a SQL statement without executing it