@@ -2520,6 +2520,54 @@ def test_prepared_statements(self):
2520
2520
schema .drop_collection (collection_name )
2521
2521
session .close ()
2522
2522
2523
+ @unittest .skipIf (tests .MYSQL_VERSION < (8 , 0 , 14 ),
2524
+ "Prepared statements not supported" )
2525
+ def test_prepared_statements_find_by_pk (self ):
2526
+ expected_stmt_attrs = (
2527
+ lambda stmt , changed , prepared , repeated , exec_counter :
2528
+ stmt .changed == changed and stmt .prepared == prepared and
2529
+ stmt .repeated == repeated and stmt .exec_counter == exec_counter
2530
+ )
2531
+
2532
+ session = mysqlx .get_session (self .connect_kwargs )
2533
+ schema = session .get_schema (self .schema_name )
2534
+
2535
+ collection_name = "prepared_collection_test_by_pk"
2536
+ collection = schema .get_collection (collection_name )
2537
+ if collection .exists_in_database ():
2538
+ schema .drop_collection ("mycoll" )
2539
+ collection = schema .create_collection (collection_name )
2540
+
2541
+ add_stmt = collection .add (
2542
+ {"name" : "Fred" , "age" : 21 },
2543
+ {"name" : "Barney" , "age" : 28 },
2544
+ {"name" : "Wilma" , "age" : 42 },
2545
+ ).execute ()
2546
+
2547
+ ids = add_stmt .get_generated_ids ()
2548
+
2549
+ # Find by primary key
2550
+ find_stmt = collection .find ("_id = :id" ).sort ("age" )
2551
+ self .assertTrue (expected_stmt_attrs (find_stmt , True , False , False , 0 ))
2552
+
2553
+ # On the first call should: Crud::Find (without prepared statement)
2554
+ doc = find_stmt .bind ("id" , ids [0 ]).execute ().fetch_one ()
2555
+ self .assertEqual (doc ["name" ], "Fred" )
2556
+ self .assertTrue (expected_stmt_attrs (find_stmt , False , False , False , 1 ))
2557
+
2558
+ # On the second call should: Prepare::Prepare + Prepare::Execute
2559
+ doc = find_stmt .bind ("id" , ids [1 ]).execute ().fetch_one ()
2560
+ self .assertEqual (doc ["name" ], "Barney" )
2561
+ self .assertTrue (expected_stmt_attrs (find_stmt , False , True , True , 2 ))
2562
+
2563
+ # On subsequent calls should: Prepare::Execute
2564
+ doc = find_stmt .bind ("id" , ids [2 ]).execute ().fetch_one ()
2565
+ self .assertEqual (doc ["name" ], "Wilma" )
2566
+ self .assertTrue (expected_stmt_attrs (find_stmt , False , True , True , 3 ))
2567
+
2568
+ schema .drop_collection (collection_name )
2569
+ session .close ()
2570
+
2523
2571
2524
2572
@unittest .skipIf (tests .MYSQL_VERSION < (5 , 7 , 14 ), "XPlugin not compatible" )
2525
2573
class MySQLxTableTests (tests .MySQLxTests ):
0 commit comments