createVar method

Future<OracleVariable> createVar({
  1. required int oracleType,
  2. required int nativeType,
  3. int size = 0,
  4. int arraySize = 1,
  5. BindDirection direction = BindDirection.output,
})

Create a bind variable for OUT or IN/OUT parameters.

Creates a variable that can receive values from stored procedures or functions. Similar to cursor.var() in python-oracledb.

Parameters:

  • oracleType: Oracle data type (DPI_ORACLE_TYPE_*)
  • nativeType: Native type for conversion (DPI_NATIVE_TYPE_*)
  • size: Maximum size for variable-length types (VARCHAR, BYTES)
  • arraySize: Number of elements for array binding (default: 1)
  • direction: Data flow direction (default: BindDirection.output)

Returns: An OracleVariable that can be bound to a statement parameter.

Example - VARCHAR OUT Parameter:

final nameVar = await connection.createVar(
  oracleType: DPI_ORACLE_TYPE_VARCHAR,
  nativeType: DPI_NATIVE_TYPE_BYTES,
  size: 100,  // Max 100 characters
);

await connection.callproc(
  'get_user_name',
  parameters: [userId],
  keywordParameters: {'name': nameVar},
);

final name = await nameVar.getValue();
print('User name: $name');

Example - NUMBER OUT Parameter:

final salaryVar = await connection.createVar(
  oracleType: DPI_ORACLE_TYPE_NUMBER,
  nativeType: DPI_NATIVE_TYPE_DOUBLE,
);

await connection.callproc(
  'calculate_salary',
  parameters: [employeeId],
  keywordParameters: {'salary': salaryVar},
);

final salary = await salaryVar.getValue();
print('Salary: \$${salary.toStringAsFixed(2)}');

Example - DATE OUT Parameter:

final dateVar = await connection.createVar(
  oracleType: DPI_ORACLE_TYPE_TIMESTAMP,
  nativeType: DPI_NATIVE_TYPE_TIMESTAMP,
);

await connection.callproc(
  'get_hire_date',
  parameters: [employeeId],
  keywordParameters: {'hire_date': dateVar},
);

final hireDate = await dateVar.getValue();
print('Hired on: ${hireDate.toIso8601String()}');

Common Type Combinations:

  • VARCHAR: DPI_ORACLE_TYPE_VARCHAR + DPI_NATIVE_TYPE_BYTES
  • NUMBER (int): DPI_ORACLE_TYPE_NUMBER + DPI_NATIVE_TYPE_INT64
  • NUMBER (float): DPI_ORACLE_TYPE_NUMBER + DPI_NATIVE_TYPE_DOUBLE
  • DATE/TIMESTAMP: DPI_ORACLE_TYPE_TIMESTAMP + DPI_NATIVE_TYPE_TIMESTAMP
  • CLOB: DPI_ORACLE_TYPE_CLOB + DPI_NATIVE_TYPE_LOB

See also:

Implementation

Future<OracleVariable> createVar({
  required int oracleType,
  required int nativeType,
  int size = 0,
  int arraySize = 1,
  BindDirection direction = BindDirection.output,
}) async {
  _ensureConnected();

  return OracleVariable.create(
    dpiOracle: _dpiOracle,
    connection: _connectionPtr.value,
    context: _context,
    oracleType: oracleType,
    nativeType: nativeType,
    size: size,
    arraySize: arraySize,
    direction: direction,
  );
}