From 0f5ab577bcc106aa487dd5b05ca0b991c9a96a04 Mon Sep 17 00:00:00 2001 From: Prajakta Bhosale Date: Wed, 4 Feb 2015 11:23:41 -0600 Subject: [PATCH] Add support for UPDATE ... IF EXISTS Patch by Prajakta Bhosale; reviewed by Tyler Hobbs for CASSANDRA-8610 --- CHANGES.txt | 1 + pylib/cqlshlib/cql3handling.py | 2 +- pylib/cqlshlib/helptopics.py | 3 ++- src/java/org/apache/cassandra/cql3/Cql.g | 6 ++++-- .../apache/cassandra/cql3/statements/UpdateStatement.java | 8 +++++--- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 375dcfebcf..f4c96dc7eb 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 2.0.13: + * Add support for UPDATE ... IF EXISTS (CASSANDRA-8610) * Fix reversal of list prepends (CASSANDRA-8733) * Prevent non-zero default_time_to_live on tables with counters (CASSANDRA-8678) diff --git a/pylib/cqlshlib/cql3handling.py b/pylib/cqlshlib/cql3handling.py index 0b7863cc65..9e9b6ad4b6 100644 --- a/pylib/cqlshlib/cql3handling.py +++ b/pylib/cqlshlib/cql3handling.py @@ -15,7 +15,6 @@ # limitations under the License. import re -from warnings import warn from .cqlhandling import CqlParsingRuleSet, Hint from cql.cqltypes import (cql_types, lookup_casstype, CompositeType, UTF8Type, ColumnToCollectionType, CounterColumnType, DateType) @@ -715,6 +714,7 @@ syntax_rules += r''' ( "AND" [updateopt]= )* )? "SET" ( "," )* "WHERE" + ( "IF" "EXISTS" )? ; ::= updatecol= ( "=" update_rhs=( | ) diff --git a/pylib/cqlshlib/helptopics.py b/pylib/cqlshlib/helptopics.py index 2af8db2b53..0b08726eea 100644 --- a/pylib/cqlshlib/helptopics.py +++ b/pylib/cqlshlib/helptopics.py @@ -563,7 +563,8 @@ class CQL3HelpTopics(CQLHelpTopics): UPDATE [.] [USING [TIMESTAMP ] [AND TTL ]] - SET name1 = value1, name2 = value2 WHERE = keyval; + SET name1 = value1, name2 = value2 WHERE = keyval + [IF EXISTS]; An UPDATE is used to write one or more columns to a record in a table. No results are returned. The record's primary key must be completely diff --git a/src/java/org/apache/cassandra/cql3/Cql.g b/src/java/org/apache/cassandra/cql3/Cql.g index a80746c689..0595c251eb 100644 --- a/src/java/org/apache/cassandra/cql3/Cql.g +++ b/src/java/org/apache/cassandra/cql3/Cql.g @@ -360,18 +360,20 @@ updateStatement returns [UpdateStatement.ParsedUpdate expr] @init { Attributes.Raw attrs = new Attributes.Raw(); List> operations = new ArrayList>(); + boolean ifExists = false; } : K_UPDATE cf=columnFamilyName ( usingClause[attrs] )? K_SET columnOperation[operations] (',' columnOperation[operations])* K_WHERE wclause=whereClause - ( K_IF conditions=updateConditions )? + ( K_IF ( K_EXISTS { ifExists = true; } | conditions=updateConditions ))? { return new UpdateStatement.ParsedUpdate(cf, attrs, operations, wclause, - conditions == null ? Collections.>emptyList() : conditions); + conditions == null ? Collections.>emptyList() : conditions, + ifExists); } ; diff --git a/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java b/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java index 9d98c840ca..594b5db47e 100644 --- a/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/UpdateStatement.java @@ -207,14 +207,16 @@ public class UpdateStatement extends ModificationStatement * @param attrs additional attributes for statement (timestamp, timeToLive) * @param updates a map of column operations to perform * @param whereClause the where clause - */ + * @param ifExists flag to check if row exists + * */ public ParsedUpdate(CFName name, Attributes.Raw attrs, List> updates, List whereClause, - List> conditions) + List> conditions, + boolean ifExists) { - super(name, attrs, conditions, false, false); + super(name, attrs, conditions, false, ifExists); this.updates = updates; this.whereClause = whereClause; }