Yii2 Update many records with difference conditions - frameworks

I want update many records with yii2 for insert use batchinsert() method and for update I don't know!!!
update my_tbl set fld1 = 321321 where res = 46513,
fld1 = 89876 where res = 54646,
fld1 = 64564 where res = 54654;

There is no bacthupdate() function in Yii2
Only batchinsert() is available
If you want you can iterate and update command or perform a sequence of single update like separate command.
Not also structured solution are actually available in Yii2

Related

java Mongo DB update field on condition

I am using spring-boot-starter-data-mongodb and I am performing an update Many to update several records in one operation
UpdateResult result = collection.updateMany(query, update);
The update field is defined like this
Bson update = Updates.combine(
Updates.set(field1, 1),
Updates.set(field2, 2);
But now I want to define a conditional update for field3. if field3 == null then field3=3
How can I achieve this? Thanks for your help

How to update multiple rows with different values in a single execution in mongodb?

We know that in mysql instead of executing the below queries 3 times we can do the same in a single exection given in 1> and 2>
UPDATE feedbacks SET _id = '5c6a8bcfce1454086fefb879' WHERE user_rol = '26-02-2018';
UPDATE feedbacks SET _id = '5c6a89d3ce1454086fefb877' WHERE user_rol = '26-02-2017';
UPDATE feedbacks SET _id = '5c6a896ece1454086fefb876' WHERE user_rol = '26-02-2016';
1>
INSERT INTO feedbacks (_id, added_on)
VALUES
('5c6a8bcfce1454086fefb879', '26-02-2018'),
('5c6a89d3ce1454086fefb877', '26-02-2017'),
('5c6a896ece1454086fefb876', '26-02-2016')
ON DUPLICATE KEY UPDATE added_on = VALUES(added_on)
2>
UPDATE feedbacks
SET added_on = CASE
WHEN _id = '5c6a8bcfce1454086fefb879' THEN '26-02-2018'
WHEN _id = '5c6a89d3ce1454086fefb877' THEN '26-02-2017'
WHEN _id = '5c6a896ece1454086fefb876' THEN '26-02-2016'
END
WHERE _id IN ('5c6a8bcfce1454086fefb879', '5c6a89d3ce1454086fefb877', '5c6a896ece1454086fefb876')
Now my question is can we have any way to do the same (updating multiple rows with different values in a single execution) in mongodb?
In mongo with same condition or query, you can update multiple records by setting the 'multi' flag as true. Please refer link.
https://docs.mongodb.com/manual/reference/method/db.collection.update/
But, conditions are different this will not work.
For Bulk Update, You can refer:
https://docs.mongodb.com/manual/reference/method/Bulk.find.update/#Bulk.find.update

How write RQLQuery?

I am new to ATG, and I have this question. How can I write my RQLQuery that provide me data, such as this SQL query?
select avg(rating) from rating WHERE album_id = ?;
I'm trying this way:
RqlStatement statement;
Object rqlparam[] = new Object[1];
rqlparam[0] = album_Id;
statement= RqlStatement.parseRqlStatement("album_id= ? 0");
MutableRepository repository = (MutableRepository) getrMember();
RepositoryView albumView = repository.getView(ALBUM);
This query returns me an item for a specific album_id, how can I improve my RQL query so that it returns to me the average field value, as SQL query above.
There is no RQL syntax that will allow for the calculation of an average value for items in the query. As such you have two options. You can either execute your current statement:
album_id= ? 0
And then loop through the resulting RepositoryItem[] and calculate the average yourself (this could be time consuming on large datasets and means you'll have to load all the results into memory, so perhaps not the best solution) or you can implement a SqlPassthroughQuery that you execute.
Object params[] = new Object[1];
params[0] = albumId;
Builder builder = (Builder)view.getQueryBuilder();
String str = "select avg(rating) from rating WHERE album_id = 1 group by album_id";
RepositoryItem[] items =
view.executeQuery (builder.createSqlPassthroughQuery(str, params));
This will execute the average calculation on the database (something it is quite good at doing) and save you CPU cycles and memory in the application.
That said, don't make a habit of using SqlPassthroughQuery as means you don't get to use the repository cache as much, which could be detrimental to your application.

AREL: writing complex update statements with from clause

I tried looking for an example of using Arel::UpdateManager to form an update statement with a from clause (as in UPDATE t SET t.itty = "b" FROM .... WHERE ...), couldn.t find any. The way I've seen it, Arel::UpdateManager sets the main engine on initialization and allows to set the various fields and values to update. Is there actually a way to do this?
Another aside would be to find out how to express Postgres posix regex matching into ARel, but this might be impossible by now.
As far as I see the current version of arel gem is not support FROM keyword for the sql query. You can generate a query using the SET, and WHERE keywords only, like:
UPDATE t SET t.itty = "b" WHERE ...
and the code, which copies a value from field2 to field1 for the units table, will be like:
relation = Unit.all
um = Arel::UpdateManager.new(relation.engine)
um.table(relation.table)
um.ast.wheres = relation.wheres.to_a
um.set(Arel::Nodes::SqlLiteral.new('field1 = "field2"'))
ActiveRecord::Base.connection.execute(um.to_sql)
Exactly you can use the additional method to update a relation. So we create the Arel's UpdateManager, assigning to it the table, where clause, and values to set. Values shell be passed to the method as an argument. Then we need to add FROM keyword to the generated SQL request, we add it only if we have access to external table of the specified one by the UPDATE clause itself. And at the last we executes the query. So we get:
def update_relation!(relation, values)
um = Arel::UpdateManager.new(relation.engine)
um.table(relation.table)
um.ast.wheres = relation.wheres.to_a
um.set(values)
sql = um.to_sql
# appends FROM field to the query if needed
m = sql.match(/WHERE/)
tables = relation.arel.source.to_a.select {|v| v.class == Arel::Table }.map(&:name).uniq
tables.shift
sql.insert(m.begin(0), "FROM #{tables.join(",")} ") if m && !tables.empty?
# executes the query
ActiveRecord::Base.connection.execute(sql)
end
The you can issue the the relation update as:
values = Arel::Nodes::SqlLiteral.new('field1 = "field2", field2 = NULL')
relation = Unit.not_rejected.where(Unit.arel_table[:field2].not_eq(nil))
update_relation!(relation, values)

Update columns based on subquery containing "select unnest" clause

I am attempting to update a boolean column in one table based upon the values in a second.
UPDATE channels
SET contains_photos = TRUE
WHERE id IN (SELECT unnest(ancestors)
FROM channel_tree WHERE id = 11329);
The channel_tree.ancestors column contains an array of channel IDs. The above is failing with the following error:
ERROR: cannot TRUNCATE "channel_tree" because it is being used by active queries in this session
The overriding goal is to set the contains_photos column to true for all ancestors of a given channel. Any one know how best to alleviate this error, or even an alternative solution?
No idea why your error says TRUNCATE. It sounds like you have a trigger or rule that is doing a truncate that we can't see.
Here's some alternative ways of doing that same query:
UPDATE channels
SET contains_photos = TRUE
WHERE id = ANY (SELECT ancestors
FROM channel_tree WHERE id = 11329);
Or with a join:
UPDATE channels
SET contains_photos = TRUE
FROM channel_tree
WHERE channels.id = ANY (channel_tree.ancestors)
AND channel_tree.id = 11329;