Example #1
0
        /// <summary>
        /// Gets a cql UPDATE statement based on the current state
        /// </summary>
        public string GetUpdate(Expression expression, out object[] values, int?ttl, DateTimeOffset?timestamp,
                                MapperFactory mapperFactory)
        {
            Visit(expression);
            var query      = new StringBuilder(DefaultQueryStringCapacity);
            var parameters = new List <object>();

            query.Append("UPDATE ");
            query.Append(GetEscapedTableName());
            if (ttl != null || timestamp != null)
            {
                query.Append(" USING ");
            }
            if (ttl != null)
            {
                query.Append("TTL ?");
                parameters.Add(ttl.Value);
                if (timestamp != null)
                {
                    query.Append(" AND ");
                }
            }
            if (timestamp != null)
            {
                query.Append("TIMESTAMP ? ");
                parameters.Add((timestamp.Value - CqlQueryTools.UnixStart).Ticks / 10);
            }
            query.Append(" SET ");
            var setStatements = new List <string>();

            foreach (var projection in _projections)
            {
                var    column     = projection.Item1;
                var    columnName = Escape(column.ColumnName);
                var    value      = mapperFactory.AdaptValue(_pocoData, column, projection.Item2);
                string operation;
                switch (projection.Item3)
                {
                case ExpressionType.AddAssign:
                    operation = " = " + columnName + " + ?";
                    parameters.Add(value);
                    break;

                case ExpressionType.PreIncrementAssign:
                    operation = " = ? + " + columnName;
                    parameters.Add(value);
                    break;

                case ExpressionType.SubtractAssign:
                    operation = " = " + columnName + " - ?";
                    parameters.Add(value);
                    break;

                case ExpressionType.Increment:
                    //Counter
                    operation = " = " + columnName + " + ?";
                    parameters.Add(projection.Item2);
                    break;

                case ExpressionType.Decrement:
                    //Counter
                    operation = " = " + columnName + " - ?";
                    parameters.Add(projection.Item2);
                    break;

                default:
                    operation = " = ?";
                    parameters.Add(value);
                    break;
                }
                setStatements.Add(columnName + operation);
            }

            if (setStatements.Count == 0)
            {
                throw new CqlArgumentException("Nothing to update");
            }
            query.Append(string.Join(", ", setStatements));

            GenerateConditions(_where, "WHERE", query, parameters, expression);

            GenerateConditions(_ifClause, "IF", query, parameters, expression);

            values = parameters.ToArray();
            return(query.ToString());
        }