execParams method

PqResult execParams(
  1. String query,
  2. List<String> params, {
  3. Allocator allocator = ffi.malloc,
})

Excecute a sql query with params Example:

final res = pq.execParams(r'insert into knowledge values($1, $2);', ['1','Isaque']);
print('res ${res.affectedRows}');
res.dispose();

Implementation

PqResult execParams(String query, List<String> params,
    {Allocator allocator = ffi.malloc}) {
  if (params.isEmpty) {
    return exec(query);
  }
  final queryP = query.toNativeUtf8();

  int nParams = params.length;

  int resultFormat = 0;
  // Allocate memory for an array params pointers
  Pointer<Pointer<Char>> paramValues = allocator<Pointer<Char>>(nParams);

  // Allocate memory for each param and store the pointer in the array params pointers
  for (int i = 0; i < nParams; i++) {
    paramValues[i] = params[i].toNativeUtf8().cast();
  }

  Pointer<Int> paramLengths = allocator<Int>(nParams);
  Pointer<Int> paramFormats = allocator<Int>(nParams);
  for (int i = 0; i < nParams; i++) {
    paramLengths[i] = params[i].length;
    paramFormats[i] = 0;
  }

  final resultP = pq.PQexecParams(conn, queryP.cast(), nParams, nullptr,
      paramValues, paramLengths, paramFormats, resultFormat);
  //free
  allocator.free(queryP);
  allocator.free(paramValues);
  allocator.free(paramLengths);
  allocator.free(paramFormats);

  final result = PqResult(this, resultP, query);

  return result;
}