setCurrentSchema method
Set the current schema for this connection.
Changes the default schema for unqualified object references. This affects table lookups, views, procedures, etc. without schema prefixes.
Parameters:
schema: The schema name to switch to (case-sensitive if quoted)
Example - Switch Schema:
// By default, queries use your user's schema
await connection.setCurrentSchema('REPORTING');
// Now this query looks in REPORTING schema
final data = await connection.query('SELECT * FROM sales_summary');
// Equivalent to: SELECT * FROM REPORTING.sales_summary
Example - Multi-Schema Application:
// Application uses different schemas for different purposes
await connection.setCurrentSchema('APP_DATA');
await connection.executeUpdate('INSERT INTO users ...');
await connection.setCurrentSchema('APP_LOGS');
await connection.executeUpdate('INSERT INTO audit_log ...');
// Switch back to default
final username = config.username;
await connection.setCurrentSchema(username);
Note: The user must have privileges to access objects in the specified schema. This does NOT change the connected user.
See also:
- getCurrentSchema to get the current schema
Implementation
Future<void> setCurrentSchema(String schema) async {
_ensureConnected();
final (schemaNative, schemaLen) = StringUtils.toNativeUtf8WithLength(schema, _memoryManager);
final result = _dpiOracle.dpiConn_setCurrentSchema(
_connectionPtr.value,
schemaNative,
schemaLen,
);
if (result == DPI_FAILURE) {
final errorInfo = _memoryManager.allocate<dpiErrorInfo>(sizeOf<dpiErrorInfo>());
_dpiOracle.dpiContext_getError(_context, errorInfo);
final errorMsg = StringUtils.fromNativeUtf8(errorInfo.ref.message.cast<Char>());
throw OracleConnectionException(
'Failed to set current schema',
errorMessage: errorMsg,
);
}
}