How to write this sql query with SubSonic3? - subsonic

UPDATE Scenarios.Blocks
SET OrderId = OrderId - 1
So, it's basically updating a column with its current value minus one.

This should help:
db.Update<Blocks>.Set(x => x.OrderId == x.OrderId - 1).Execute();
If you want to have a where clause for your update query: Subsonic 3.0 UPDATE, multiple conditions

Related

Cassandra update query to append data to existing value in a column

Can you please provide the query to append data to an existing value in a column of type text? Something similar to this:
UPDATE cycling.upcoming_calendar SET events = events + ['Tour de France Stage 10'] WHERE year = 2015 AND month = 06;
The above query will update a list. My column datatype is text.
In my case, if the column "events" has a value, "Test" I want to update it to the value, "Test , Test1".
Appending data to a text column is not possible in Cassandra. The only possible options I can think of are
Option 1 : Change the column data type to List
Option 2 : Fetch the data from the column in your application and then append the new value to the existing value, and finally update the DB.

Is there anyway we could limit an update?

Generally, I see we can limit the select by select * from table where predicate = value limit by N Am currently in a situation where I have 200 records falling under a predicate, but I want to update the first 100 like update table set column = 1 where predicate = value limit...? and the second half by update table set column = 2 where predicate = value. I think it could be done by having ranges <=,>= in the predicate section, unfortunately, I have none of them.
Currently, I don't think we have this feature as WHERE clause must identify the row or rows to be updated by primary key as per. However, you could further limit the number of rows to be updated by using IF EXISTS condition. Details can be found here

Cassandra Conditional Update combined with IF EXISTS

I know that it's possible to use conditional updates (lightweight transaction) in cassandra.
update myTable
set
col1 = 'abc',
where id = 1
IF priority < 2
This allows me to update only rows with a higher priority. Unfortunately when the row does not exists the statement returns false. Is it possible to combine "IF NOT EXISTS" and "IF" with an OR operation?
Otherwise i have to execute more queries to the cluster. In my use case this could be a big performance issue.
I figured it out - unfortunately it's not possible in cassandra. We have to separate the insert and update statement.
https://issues.apache.org/jira/browse/CASSANDRA-8335
insert ... if not exists
update ... if priority < 2
You can use the IN operator, assuming that "priority" has a finite (and possibly small) allowed set of values, such as 0 and 1. By adding the NULL value to the set, the query will succeed also if the row does not exist.
You're using a single query, but note that the performance implications are non-negligible, and could be the same or worse than using two.
update myTable
set
col1 = 'abc',
where id = 1
IF priority IN (0, 1, NULL)

Cassandra Optimistic Locking

I have a cassandra table1:
CREATE TABLE Policy.table1 (
name VARCHAR ,
date TIMESTAMP ,
version_num INT,
PRIMARY KEY (
name
)) WITH caching = 'all'
-- and memtable_flush_period_in_ms = 7200 ;
;
I need to implement optimistic locking on tis table. When we read a row from table1 we remember its version_num. And when we want to update this row we compare current version_num value and value that we remembered. Also we need to increment version_num on each update.
Problems:
We cannot put version_num into where clause, this will create an error: Bad Request: Non PRIMARY KEY version_num found in where clause:
update table where name = 'abc' and version = 3
We cannot set version_num as a part of primary key, because we need to update its value
If we index version_num it will not help for update statements, same exception will be thrown
The only way I see is to get current version_num value by Java, and if expected and actual version_num values are the same - than execute update. The problem is that in this case we have not atomic operation of checking version_num value and update the row.
Do you see any solution for this problem?
The solution was found there: Cassandra 2.0, Lightweight transactions http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_ltwt_transaction_c.html
In case I execute query:
update table1 set version_num = 5 where name = 'abc' if version_num = 4;
I will receive a row with [applied] column. This row contains boolean value: true = update was successful, false = in other case.

Doctrine2 subquery

I'm trying to write a subquery in doctrine2, to sort a table ordered by date column of another table.
(let's say I'm querying table A, which has an id column, and B has an a_id and a date, in the subquery b.a_id = a.id)
I'm using query builder, and the addSelect method, but since I can't use LIMIT in my query I get this error:
SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more
than 1 row
This error is true, but how could I limit this query to 1 row, if LIMIT is not allowed in doctrine2 and when I try to do it by querybuilder (I mean the subquery) and I'm using setMaxResults, and then getDQl it is still not working.
->addSelect('(SELECT b.date FROM B b WHERE b.conversation = a.id ORDER BY b.date DESC)')
Is there any solution for my problem?
Thanks
Make the query return exactly one row. Try SELECT MAX(b.date) FROM B b WHERE b.conversation = a.id)

Resources