1
1
/*
2
- * Copyright (c) 2015, 2020 , Oracle and/or its affiliates.
2
+ * Copyright (c) 2015, 2023 , Oracle and/or its affiliates.
3
3
*
4
4
* This program is free software; you can redistribute it and/or modify
5
5
* it under the terms of the GNU General Public License, version 2.0, as
@@ -2209,6 +2209,8 @@ class Op_collection_add
2209
2209
JSON_INSERT(<json>, '$._id', <id>)
2210
2210
2211
2211
where <json> and <id> are given as constructor parameters.
2212
+
2213
+ <id> can either be a string or an expression which generates the <id>
2212
2214
*/
2213
2215
2214
2216
struct Insert_id
@@ -2219,8 +2221,14 @@ struct Insert_id
2219
2221
typedef cdk::string string;
2220
2222
2221
2223
const cdk::Expression &m_doc;
2224
+ const cdk::Expression *m_id_exr = nullptr ;
2222
2225
std::string m_id;
2223
2226
2227
+ Insert_id (const cdk::Expression &doc, const cdk::Expression &id_expr)
2228
+ : m_doc(doc){
2229
+ m_id_exr = &id_expr;
2230
+ }
2231
+
2224
2232
Insert_id (const cdk::Expression &doc, const std::string &id)
2225
2233
: m_doc(doc), m_id(id)
2226
2234
{}
@@ -2248,7 +2256,11 @@ struct Insert_id
2248
2256
sprc->list_begin ();
2249
2257
m_doc.process_if (sprc->list_el ()); // the document to modify
2250
2258
sprc->list_el ()->scalar ()->val ()->str (" $._id" );
2251
- sprc->list_el ()->scalar ()->val ()->str (m_id);
2259
+ if (m_id_exr) {
2260
+ m_id_exr->process_if (sprc->list_el ());
2261
+ } else {
2262
+ sprc->list_el ()->scalar ()->val ()->str (m_id);
2263
+ }
2252
2264
sprc->list_end ();
2253
2265
}
2254
2266
@@ -2407,6 +2419,22 @@ class Op_collection_remove
2407
2419
}
2408
2420
};
2409
2421
2422
+ /*
2423
+ Represents an expression which generates _id.
2424
+ In this case, will use the _id column value.
2425
+ */
2426
+ struct Extract_id : cdk::Expression {
2427
+ struct : cdk::api::Column_ref {
2428
+ const cdk::api::Table_ref *table () const override { return nullptr ; }
2429
+ const cdk::string name () const override { return " _id" ; }
2430
+ } m_id;
2431
+
2432
+ Extract_id () {}
2433
+
2434
+ void process (cdk::Expression::Processor &prc) const override {
2435
+ safe_prc (prc)->scalar ()->ref (m_id, nullptr );
2436
+ }
2437
+ };
2410
2438
2411
2439
/*
2412
2440
Implementation of collection CRUD modify operation (Collection_modify_if
@@ -2467,7 +2495,16 @@ class Op_collection_modify
2467
2495
if (m_expr)
2468
2496
return m_expr->process (prc);
2469
2497
2470
- Value::Access::process (parser::Parser_mode::DOCUMENT, m_val, prc);
2498
+ if (m_field == " $" )
2499
+ {
2500
+ Value_expr doc (m_val, parser::Parser_mode::DOCUMENT);
2501
+
2502
+ Extract_id expr_id;
2503
+ Insert_id id (doc, expr_id);
2504
+ id.process (prc);
2505
+ } else {
2506
+ Value::Access::process (parser::Parser_mode::DOCUMENT, m_val, prc);
2507
+ }
2471
2508
}
2472
2509
};
2473
2510
@@ -2494,8 +2531,7 @@ class Op_collection_modify
2494
2531
return new Op_collection_modify (*this );
2495
2532
}
2496
2533
2497
- cdk::Reply* do_send_command () override
2498
- {
2534
+ cdk::Reply *do_send_command () override {
2499
2535
// Do nothing if no update specifications were added
2500
2536
2501
2537
if (m_update.empty ())
@@ -2629,7 +2665,6 @@ class Op_collection_replace
2629
2665
add_operation (SET, " $" , *this );
2630
2666
add_param (" id" , id);
2631
2667
}
2632
-
2633
2668
};
2634
2669
2635
2670
0 commit comments