question

Erick Ramirez avatar image
Erick Ramirez asked Erick Ramirez edited

Does the C# driver insert nulls into columns if the columns are not bound in a statement?

A user wanted to know how the C# driver handles columns which are not bound an INSERT statement.

csharp driver
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

joao.reis avatar image
joao.reis answered joao.reis edited

Mapper / Linq2Cql

Inserts

On the insert method you have a boolean parameter called "insertNulls" that controls that behavior:

// Mapper - Mapper class
Task InsertAsync<T>(T poco, string executionProfile, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null) 

// Linq2Cql - Table<TEntity> class 
CqlInsert<TEntity> Insert(TEntity entity, bool insertNulls)

Updates

For updates, the Mapper generates statements with all the columns that are specified in the mapping configuration so it will bind those parameters to null values when the mapped class properties have null values. You can override this by using the method overloads that allow you to pass a CQL string and therefore specify which columns you want to update:

_mapper.Update<Song>(Cql.New(
    "SET Artist = ?, ReleaseDate = ?, Title = ? WHERE Id = ?",
    song.Title, song.Artist, song.ReleaseDate, song.Id));

In Linq2Cql you always have to specify the columns that you want to update so if you don't specify a column, the driver will generate a statement without it, i.e., a null value will not be inserted.

Statements

When using statements directly it is up to the user to bind Unset to that particular parameter if they don't want to insert/update that column or to avoid specifying that column in the CQL string.

// using unset
var preparedStatement = session.Prepare(
    @"INSERT INTO table (id, text_sample, int_sample) VALUES (?, ?, ?)");
session.Execute(preparedStatement.Bind(id, Unset.Value, Unset.Value));

With prepared statements specifically, if you don't provide values for all the parameters then the driver will bind Unset to the remaining parameters. Note that Unset is only supported in Apache Cassandra versions 2.2 or later. In earlier versions, an exception will be thrown if you don't provide values for all the parameters on a prepared statement.

Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.