connect static method

Future<OracleConnection> connect({
  1. required DpiOracle dpiOracle,
  2. required Pointer<dpiContext> context,
  3. required OracleConfig config,
})

Create and open a connection to Oracle database

Implementation

static Future<OracleConnection> connect({
  required DpiOracle dpiOracle,
  required Pointer<dpiContext> context,
  required OracleConfig config,
}) async {
  final memoryManager = MemoryManager();

  try {
    // Allocate memory for connection
    final connectionPtr = memoryManager.allocate<Pointer<dpiConn>>(sizeOf<Pointer<dpiConn>>());
    final errorInfo = memoryManager.allocate<dpiErrorInfo>(sizeOf<dpiErrorInfo>());

    // Convert strings to native with byte lengths
    final (usernameNative, usernameLen) = StringUtils.toNativeUtf8WithLength(config.username, memoryManager);
    final (passwordNative, passwordLen) = StringUtils.toNativeUtf8WithLength(config.password, memoryManager);
    final (connStringNative, connStringLen) = StringUtils.toNativeUtf8WithLength(config.connectionString, memoryManager);

    // Create connection
    final result = dpiOracle.dpiConn_create(
      context,
      usernameNative,
      usernameLen,
      passwordNative,
      passwordLen,
      connStringNative,
      connStringLen,
      nullptr,
      nullptr,
      connectionPtr,
    );

    if (result == DPI_FAILURE) {
      dpiOracle.dpiContext_getError(context, errorInfo);
      final errorMsg = StringUtils.fromNativeUtf8(errorInfo.ref.message.cast<Char>());
      final errorCode = errorInfo.ref.code;

      throw OracleConnectionException(
        'Failed to create connection',
        errorCode: errorCode,
        errorMessage: errorMsg,
        connectionString: config.connectionString,
        username: config.username,
      );
    }

    return OracleConnection._(
      dpiOracle,
      context,
      connectionPtr,
      config,
      memoryManager,
    );
  } catch (e) {
    memoryManager.dispose();
    if (e is OracleException) {
      rethrow;
    }
    throw OracleConnectionException('Connection failed: $e');
  }
}