getCurrentSchema method

Future<String> getCurrentSchema()

Get the current schema name for this connection.

Returns the schema that is currently active. By default, this is the same as the username, but it can be changed with setCurrentSchema.

Returns: The current schema name, or empty string if not set.

Example - Check Current Schema:

final schema = await connection.getCurrentSchema();
print('Current schema: $schema');
// Output: Current schema: MYUSER

Example - Verify Schema Before Operations:

final expectedSchema = 'PRODUCTION';
final currentSchema = await connection.getCurrentSchema();

if (currentSchema != expectedSchema) {
  throw Exception('Wrong schema! Expected $expectedSchema, got $currentSchema');
}

// Safe to proceed with operations
await connection.executeUpdate('INSERT INTO orders ...');

See also:

Implementation

Future<String> getCurrentSchema() async {
  _ensureConnected();

  final schemaPtr = _memoryManager.allocate<Pointer<Char>>(sizeOf<Pointer<Char>>());
  final schemaLength = _memoryManager.allocate<Uint32>(sizeOf<Uint32>());

  final result = _dpiOracle.dpiConn_getCurrentSchema(
    _connectionPtr.value,
    schemaPtr,
    schemaLength,
  );

  if (result == DPI_FAILURE) {
    throw OracleConnectionException('Failed to get current schema');
  }

  if (schemaLength.value == 0) {
    return '';
  }

  return StringUtils.fromNativeUtf8(schemaPtr.value);
}