create static method

Future<OracleVariable> create({
  1. required DpiOracle dpiOracle,
  2. required Pointer<dpiConn> connection,
  3. required Pointer<dpiContext> context,
  4. required int oracleType,
  5. required int nativeType,
  6. int size = 0,
  7. int arraySize = 1,
  8. BindDirection direction = BindDirection.input,
})

Create a new bind variable

Parameters:

  • dpiOracle: DPI Oracle instance
  • connection: Database connection
  • oracleType: Oracle data type (DPI_ORACLE_TYPE_*)
  • nativeType: Native data type (DPI_NATIVE_TYPE_*)
  • size: Size for string/bytes types
  • arraySize: Array size for batch operations (default: 1)
  • direction: Bind direction (input, output, inOut)

Implementation

static Future<OracleVariable> create({
  required DpiOracle dpiOracle,
  required Pointer<dpiConn> connection,
  required Pointer<dpiContext> context,
  required int oracleType,
  required int nativeType,
  int size = 0,
  int arraySize = 1,
  BindDirection direction = BindDirection.input,
}) async {
  final memoryManager = MemoryManager();

  try {
    final varPtr = memoryManager.allocate<Pointer<dpiVar>>(sizeOf<Pointer<dpiVar>>());
    final dataPtr = memoryManager.allocate<Pointer<dpiData>>(sizeOf<Pointer<dpiData>>());
    final errorInfo = memoryManager.allocate<dpiErrorInfo>(sizeOf<dpiErrorInfo>());

    // Create the variable
    // Note: dataPtr receives a pointer to the data array allocated by ODPI-C.
    // This pointer is required and cannot be null - ODPI-C uses it to return
    // the internal data buffer that will hold values for this variable.
    //
    // IMPORTANT: For dpiStmt_executeMany(), isArray MUST be 0 (false).
    // The arraySize parameter (maxArraySize) controls how many values can be set,
    // but the variable itself is NOT an "array variable" (which is for PL/SQL
    // collection binding). Setting isArray=1 causes DPI-1064 error.
    final result = dpiOracle.dpiConn_newVar(
      connection,
      oracleType,
      nativeType,
      arraySize,  // maxArraySize - number of values the variable can hold
      size,
      0, // sizeIsBytes = false
      0, // isArray = 0 for executeMany() compatibility
      nullptr, // objectType (only used for object types)
      varPtr,
      dataPtr, // receives pointer to ODPI-C allocated data array
    );

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

      throw OracleException(
        'Failed to create variable',
        errorMessage: errorMsg,
      );
    }

    return OracleVariable._(
      dpiOracle,
      connection,
      context,
      varPtr,
      dataPtr.value,  // Store the data buffer pointer from ODPI-C
      memoryManager,
      oracleType,
      nativeType,
      size,
      arraySize,
      direction,
    );
  } catch (e) {
    memoryManager.dispose();
    if (e is OracleException) {
      rethrow;
    }
    throw OracleException('Variable creation failed: $e');
  }
}