@@ -1367,4 +1367,93 @@ suite<"Typed Query Column Count Coverage"> typed_query_col_count_suite = [] {
13671367 };
13681368};
13691369
1370+ // ===================================================================
1371+ // last_insert_id / execute affected rows
1372+ // ===================================================================
1373+
1374+ struct auto_inc_row {
1375+ COLUMN_FIELD (id, uint32_t , column_attr::auto_increment)
1376+ COLUMN_FIELD (label, varchar_type<64 >)
1377+ };
1378+
1379+ suite<" last_insert_id Integration" > last_insert_id_suite = [] {
1380+ " last_insert_id returns auto-generated id after INSERT" _test = [] {
1381+ auto const config = mysql_config_from_env ();
1382+ expect (fatal (config.has_value ()));
1383+
1384+ auto const db = mysql_connection::connect (*config);
1385+ expect (fatal (db));
1386+ auto _ = scope_guard{[&] {
1387+ (void )(db->execute (drop_table (auto_inc_row{}).if_exists ()));
1388+ }};
1389+
1390+ expect (db->execute (drop_table (auto_inc_row{}).if_exists ()).has_value ());
1391+ expect (db->execute (create_table (auto_inc_row{})).has_value ());
1392+
1393+ auto_inc_row row;
1394+ row.label_ = " first" ;
1395+ expect (db->execute (insert_into (auto_inc_row{}).values (row)).has_value ());
1396+
1397+ auto const first_id = db->last_insert_id ();
1398+ expect (first_id >= 1u ) << " first insert should produce id >= 1, got " + std::to_string (first_id);
1399+
1400+ row.label_ = " second" ;
1401+ expect (db->execute (insert_into (auto_inc_row{}).values (row)).has_value ());
1402+
1403+ auto const second_id = db->last_insert_id ();
1404+ expect (second_id == first_id + 1u ) << " second id should be first + 1, got " + std::to_string (second_id);
1405+ };
1406+
1407+ " last_insert_id is zero after DDL" _test = [] {
1408+ auto const config = mysql_config_from_env ();
1409+ expect (fatal (config.has_value ()));
1410+
1411+ auto const db = mysql_connection::connect (*config);
1412+ expect (fatal (db));
1413+ auto _ = scope_guard{[&] {
1414+ (void )(db->execute (drop_table (auto_inc_row{}).if_exists ()));
1415+ }};
1416+
1417+ expect (db->execute (drop_table (auto_inc_row{}).if_exists ()).has_value ());
1418+ expect (db->execute (create_table (auto_inc_row{})).has_value ());
1419+
1420+ expect (db->last_insert_id () == 0u ) << " last_insert_id should be 0 after DDL" ;
1421+ };
1422+
1423+ " execute returns affected row count" _test = [] {
1424+ auto const config = mysql_config_from_env ();
1425+ expect (fatal (config.has_value ()));
1426+
1427+ auto const db = mysql_connection::connect (*config);
1428+ expect (fatal (db));
1429+ auto _ = scope_guard{[&] {
1430+ (void )(db->execute (drop_table (auto_inc_row{}).if_exists ()));
1431+ }};
1432+
1433+ expect (db->execute (drop_table (auto_inc_row{}).if_exists ()).has_value ());
1434+ expect (db->execute (create_table (auto_inc_row{})).has_value ());
1435+
1436+ auto_inc_row row;
1437+ row.label_ = " one" ;
1438+ auto const insert_result = db->execute (insert_into (auto_inc_row{}).values (row));
1439+ expect (fatal (insert_result.has_value ()));
1440+ expect (*insert_result == 1u ) << " INSERT of 1 row should return 1, got " + std::to_string (*insert_result);
1441+
1442+ auto const update_result = db->execute (
1443+ update (auto_inc_row{}).set <auto_inc_row::label>(" updated" ).where (equal<auto_inc_row::label>(" one" )));
1444+ expect (fatal (update_result.has_value ()));
1445+ expect (*update_result == 1u ) << " UPDATE of 1 row should return 1, got " + std::to_string (*update_result);
1446+
1447+ auto const delete_result =
1448+ db->execute (delete_from (auto_inc_row{}).where (equal<auto_inc_row::label>(" updated" )));
1449+ expect (fatal (delete_result.has_value ()));
1450+ expect (*delete_result == 1u ) << " DELETE of 1 row should return 1, got " + std::to_string (*delete_result);
1451+
1452+ auto const empty_delete =
1453+ db->execute (delete_from (auto_inc_row{}).where (equal<auto_inc_row::label>(" nonexistent" )));
1454+ expect (fatal (empty_delete.has_value ()));
1455+ expect (*empty_delete == 0u ) << " DELETE of 0 rows should return 0, got " + std::to_string (*empty_delete);
1456+ };
1457+ };
1458+
13701459} // namespace ds_mysql
0 commit comments