Skip to content

add overloads with ttl parameter to Insert functions on ICqlWriteClient and… #192

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Cassandra/Mapping/CqlBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ public void Insert<T>(T poco, CqlQueryOptions queryOptions = null)

public void Insert<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null)
{
Insert(false, insertNulls, poco, queryOptions);
Insert(false, insertNulls, poco, queryOptions, null);
}

public void Insert<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null)
{
Insert(false, insertNulls, poco, queryOptions, ttl);
}


public void InsertIfNotExists<T>(T poco, CqlQueryOptions queryOptions = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add an overload for InsertIfNotExists<T>() method.

{
InsertIfNotExists(poco, true, queryOptions);
Expand All @@ -49,7 +55,12 @@ public void InsertIfNotExists<T>(T poco, bool insertNulls, CqlQueryOptions query
Insert(true, insertNulls, poco, queryOptions);
}

private void Insert<T>(bool ifNotExists, bool insertNulls, T poco, CqlQueryOptions queryOptions = null)
public void InsertIfNotExists<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null)
{
Insert(true, insertNulls, poco, queryOptions, ttl);
}

private void Insert<T>(bool ifNotExists, bool insertNulls, T poco, CqlQueryOptions queryOptions = null, int? ttl = null)
{
var pocoData = _mapperFactory.PocoDataFactory.GetPocoData<T>();
var queryIdentifier = string.Format("INSERT ID {0}/{1}", pocoData.KeyspaceName, pocoData.TableName);
Expand All @@ -58,7 +69,7 @@ private void Insert<T>(bool ifNotExists, bool insertNulls, T poco, CqlQueryOptio
var values = getBindValues(poco);
//generate INSERT query based on null values (if insertNulls set)
object[] queryParameters;
var cql = _cqlGenerator.GenerateInsert<T>(insertNulls, values, out queryParameters, ifNotExists);
var cql = _cqlGenerator.GenerateInsert<T>(insertNulls, values, out queryParameters, ifNotExists, ttl);

_statements.Add(Cql.New(cql, queryParameters, queryOptions ?? CqlQueryOptions.None));
}
Expand Down
5 changes: 5 additions & 0 deletions src/Cassandra/Mapping/ICqlBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ public interface ICqlBatch : ICqlWriteClient
/// Inserts the specified POCO in Cassandra if not exists.
/// </summary>
void InsertIfNotExists<T>(T poco, CqlQueryOptions queryOptions = null);

/// <summary>
/// Inserts the specified POCO in Cassandra if not exists.
/// </summary>
void InsertIfNotExists<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null);
}
}
22 changes: 22 additions & 0 deletions src/Cassandra/Mapping/ICqlWriteAsyncClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ public interface ICqlWriteAsyncClient
/// <returns></returns>
Task InsertAsync<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null);

/// <summary>
/// Inserts the specified POCO in Cassandra.
/// </summary>
/// <param name="poco">The POCO instance</param>
/// <param name="insertNulls">
/// Determines if the query must be generated using <c>NULL</c> values for <c>null</c> POCO
/// members.
/// <para>
/// Use <c>false</c> if you don't want to consider <c>null</c> values for the INSERT
/// operation (recommended).
/// </para>
/// <para>
/// Use <c>true</c> if you want to override all the values in the table,
/// generating tombstones for null values.
/// </para>
/// </param>
/// <param name="ttl">Time to live (in seconds) for the inserted values. If set, the inserted values are automatically removed
/// from the database after the specified time.</param>
/// <param name="queryOptions">Optional query options</param>
/// <returns></returns>
Task InsertAsync<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null);

/// <summary>
/// Updates the POCO specified in Cassandra.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions src/Cassandra/Mapping/ICqlWriteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ public interface ICqlWriteClient
/// <returns></returns>
void Insert<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null);


/// <summary>
/// Inserts the specified POCO in Cassandra.
/// </summary>
/// <param name="poco">The POCO instance</param>
/// <param name="insertNulls">
/// Determines if the query must be generated using <c>NULL</c> values for <c>null</c> POCO
/// members.
/// <para>
/// Use <c>false</c> if you don't want to consider <c>null</c> values for the INSERT
/// operation (recommended).
/// </para>
/// <para>
/// Use <c>true</c> if you want to override all the values in the table,
/// generating tombstones for null values.
/// </para>
/// </param>
/// <param name="queryOptions">Optional query options</param>
/// <param name="ttl">Time to live (in seconds) for the inserted values. If set, the inserted values are automatically removed
/// from the database after the specified time.</param>
/// <returns></returns>
void Insert<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null);

/// <summary>
/// Updates the POCO specified in Cassandra.
/// </summary>
Expand Down
44 changes: 44 additions & 0 deletions src/Cassandra/Mapping/IMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ public interface IMapper : ICqlQueryAsyncClient, ICqlWriteAsyncClient, ICqlQuery
/// <returns></returns>
Task<AppliedInfo<T>> InsertIfNotExistsAsync<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null);

/// <summary>
/// Inserts the specified POCO in Cassandra, if not exists.
/// </summary>
/// <param name="poco">The POCO instance</param>
/// <param name="insertNulls">
/// Determines if the query must be generated using <c>NULL</c> values for <c>null</c> POCO
/// members.
/// <para>
/// Use <c>false</c> if you don't want to consider <c>null</c> values for the INSERT
/// operation (recommended).
/// </para>
/// <para>
/// Use <c>true</c> if you want to override all the values in the table,
/// generating tombstones for null values.
/// </para>
/// </param>
/// <param name="ttl">Time to live (in seconds) for the inserted values. If set, the inserted values are automatically removed
/// from the database after the specified time.</param>
/// <param name="queryOptions">Optional query options</param>
/// <returns></returns>
Task<AppliedInfo<T>> InsertIfNotExistsAsync<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null);

/// <summary>
/// Inserts the specified POCO in Cassandra, if not exists.
/// <para>
Expand Down Expand Up @@ -129,6 +151,28 @@ public interface IMapper : ICqlQueryAsyncClient, ICqlWriteAsyncClient, ICqlQuery
/// <returns></returns>
AppliedInfo<T> InsertIfNotExists<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null);

/// <summary>
/// Inserts the specified POCO in Cassandra, if not exists.
/// </summary>
/// <param name="poco">The POCO instance</param>
/// <param name="insertNulls">
/// Determines if the query must be generated using <c>NULL</c> values for <c>null</c> POCO
/// members.
/// <para>
/// Use <c>false</c> if you don't want to consider <c>null</c> values for the INSERT
/// operation (recommended).
/// </para>
/// <para>
/// Use <c>true</c> if you want to override all the values in the table,
/// generating tombstones for null values.
/// </para>
/// </param>
/// <param name="ttl">Time to live (in seconds) for the inserted values. If set, the inserted values are automatically removed
/// from the database after the specified time.</param>
/// <param name="queryOptions">Optional query options</param>
/// <returns></returns>
AppliedInfo<T> InsertIfNotExists<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null);

/// <summary>
/// Updates the table for the poco type specified (T) using the CQL statement specified, using lightweight transactions.
/// Prepends "UPDATE tablename" to the CQL statement you specify, getting the table name appropriately from the POCO Type T.
Expand Down
28 changes: 24 additions & 4 deletions src/Cassandra/Mapping/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ public Task InsertAsync<T>(T poco, CqlQueryOptions queryOptions = null)
}

public Task InsertAsync<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null)
{
return InsertAsync(poco, insertNulls, null, queryOptions);
}

public Task InsertAsync<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null)
{
var pocoData = _mapperFactory.PocoDataFactory.GetPocoData<T>();
var queryIdentifier = string.Format("INSERT ID {0}/{1}", pocoData.KeyspaceName, pocoData.TableName);
Expand All @@ -206,7 +211,7 @@ public Task InsertAsync<T>(T poco, bool insertNulls, CqlQueryOptions queryOption
var values = getBindValues(poco);
object[] queryParameters;
//generate INSERT query based on null values (if insertNulls set)
var cql = _cqlGenerator.GenerateInsert<T>(insertNulls, values, out queryParameters);
var cql = _cqlGenerator.GenerateInsert<T>(insertNulls, values, out queryParameters, ttl: ttl);
return ExecuteAsync(Cql.New(cql, queryParameters, queryOptions ?? CqlQueryOptions.None));
}

Expand All @@ -216,6 +221,11 @@ public Task<AppliedInfo<T>> InsertIfNotExistsAsync<T>(T poco, CqlQueryOptions qu
}

public Task<AppliedInfo<T>> InsertIfNotExistsAsync<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null)
{
return InsertIfNotExistsAsync(poco, insertNulls, null, queryOptions);
}

public Task<AppliedInfo<T>> InsertIfNotExistsAsync<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null)
{
var pocoData = _mapperFactory.PocoDataFactory.GetPocoData<T>();
var queryIdentifier = string.Format("INSERT ID {0}/{1}", pocoData.KeyspaceName, pocoData.TableName);
Expand All @@ -224,7 +234,7 @@ public Task<AppliedInfo<T>> InsertIfNotExistsAsync<T>(T poco, bool insertNulls,
var values = getBindValues(poco);
object[] queryParameters;
//generate INSERT query based on null values (if insertNulls set)
var cql = _cqlGenerator.GenerateInsert<T>(insertNulls, values, out queryParameters, true);
var cql = _cqlGenerator.GenerateInsert<T>(insertNulls, values, out queryParameters, true, ttl);
return ExecuteAsyncAndAdapt(
Cql.New(cql, queryParameters, queryOptions ?? CqlQueryOptions.None),
(stmt, rs) => AppliedInfo<T>.FromRowSet(_mapperFactory, cql, rs));
Expand Down Expand Up @@ -453,9 +463,14 @@ public void Insert<T>(T poco, CqlQueryOptions queryOptions = null)
}

public void Insert<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null)
{
Insert(poco, insertNulls, null, queryOptions);
}

public void Insert<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null)
{
//Wait async method to be completed or throw
TaskHelper.WaitToComplete(InsertAsync(poco, insertNulls, queryOptions), _queryAbortTimeout);
TaskHelper.WaitToComplete(InsertAsync(poco, insertNulls, ttl, queryOptions), _queryAbortTimeout);
}

public AppliedInfo<T> InsertIfNotExists<T>(T poco, CqlQueryOptions queryOptions = null)
Expand All @@ -465,7 +480,12 @@ public AppliedInfo<T> InsertIfNotExists<T>(T poco, CqlQueryOptions queryOptions

public AppliedInfo<T> InsertIfNotExists<T>(T poco, bool insertNulls, CqlQueryOptions queryOptions = null)
{
return TaskHelper.WaitToComplete(InsertIfNotExistsAsync(poco, insertNulls, queryOptions), _queryAbortTimeout);
return InsertIfNotExists(poco, insertNulls, null, queryOptions);
}

public AppliedInfo<T> InsertIfNotExists<T>(T poco, bool insertNulls, int? ttl, CqlQueryOptions queryOptions = null)
{
return TaskHelper.WaitToComplete(InsertIfNotExistsAsync(poco, insertNulls, ttl, queryOptions), _queryAbortTimeout);
}

public void Update<T>(T poco, CqlQueryOptions queryOptions = null)
Expand Down