You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

891 lines
39 KiB

  1. #include <cpphibernate/driver/mariadb.h>
  2. #include "test_helper.h"
  3. #include "test_schema.h"
  4. #include "mariadb_mock.h"
  5. using namespace ::testing;
  6. using namespace ::cpphibernate;
  7. TEST(CppHibernateTests, update_test1)
  8. {
  9. StrictMock<mariadb_mock> mock;
  10. expect_query(mock, "START TRANSACTION");
  11. expect_query(mock, "UPDATE "
  12. "`tbl_test1` "
  13. "SET "
  14. "`str_data`='Xstr_data of class `test1` object `t1`X', "
  15. "`str64_data`='Xstr64_data of class `test1` object `t1`X', "
  16. "`u32_nullable`=null, "
  17. "`u32_ptr_u`='X456X', "
  18. "`u32_ptr_s`='X789X' "
  19. "WHERE "
  20. "`tbl_test1_id`=UuidToBin('X3d12697a-abb9-11e8-98d0-529269fb1459X')",
  21. result_affected_rows(1));
  22. expect_query(mock, "COMMIT");
  23. EXPECT_CALL(
  24. mock,
  25. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  26. .Times(AnyNumber())
  27. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  28. EXPECT_CALL(
  29. mock,
  30. mysql_close(
  31. reinterpret_cast<MYSQL*>(0x1111)));
  32. test1 t1;
  33. t1.id = uuid("3d12697a-abb9-11e8-98d0-529269fb1459");
  34. t1.str_data = "str_data of class `test1` object `t1`";
  35. t1.str64_data = "str64_data of class `test1` object `t1`";
  36. t1.u32_ptr_u = std::make_unique<uint32_t>(456);
  37. t1.u32_ptr_s = std::make_shared<uint32_t>(789);
  38. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  39. auto context = make_context<mariadb_driver>(test_schema, connection);
  40. context.update(t1);
  41. }
  42. TEST(CppHibernateTests, update_test2)
  43. {
  44. StrictMock<mariadb_mock> mock;
  45. expect_query(mock, "START TRANSACTION");
  46. expect_query(mock, "UPDATE "
  47. "`tbl_test2` "
  48. "SET "
  49. "`u8_data`='X1X', "
  50. "`i8_data`='X2X', "
  51. "`u16_data`='X3X', "
  52. "`i16_data`='X4X' "
  53. "WHERE "
  54. "`tbl_test2_id`=UuidToBin('X3d1270dc-abb9-11e8-98d0-529269fb1459X')",
  55. result_affected_rows(1));
  56. expect_query(mock, "COMMIT");
  57. EXPECT_CALL(
  58. mock,
  59. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  60. .Times(AnyNumber())
  61. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  62. EXPECT_CALL(
  63. mock,
  64. mysql_close(
  65. reinterpret_cast<MYSQL*>(0x1111)));
  66. test2 t2;
  67. t2.id = uuid("3d1270dc-abb9-11e8-98d0-529269fb1459");
  68. t2.u8_data = 1;
  69. t2.i8_data = 2;
  70. t2.u16_data = 3;
  71. t2.i16_data = 4;
  72. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  73. auto context = make_context<mariadb_driver>(test_schema, connection);
  74. context.update(t2);
  75. }
  76. TEST(CppHibernateTests, update_test3)
  77. {
  78. StrictMock<mariadb_mock> mock;
  79. expect_query(mock, "START TRANSACTION");
  80. expect_query(mock, "UPDATE "
  81. "`tbl_test3` "
  82. "SET "
  83. "`u32_data`='X5X', "
  84. "`i32_data`='X6X', "
  85. "`u64_data`='X7X', "
  86. "`i64_data`='X8X' "
  87. "WHERE "
  88. "`tbl_test3_id`=UuidToBin('X3d12737a-abb9-11e8-98d0-529269fb1459X')",
  89. result_affected_rows(1));
  90. expect_query(mock, "COMMIT");
  91. EXPECT_CALL(
  92. mock,
  93. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  94. .Times(AnyNumber())
  95. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  96. EXPECT_CALL(
  97. mock,
  98. mysql_close(
  99. reinterpret_cast<MYSQL*>(0x1111)));
  100. test3 t3;
  101. t3.id = uuid("3d12737a-abb9-11e8-98d0-529269fb1459");
  102. t3.u32_data = 5;
  103. t3.i32_data = 6;
  104. t3.u64_data = 7;
  105. t3.i64_data = 8;
  106. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  107. auto context = make_context<mariadb_driver>(test_schema, connection);
  108. context.update(t3);
  109. }
  110. TEST(CppHibernateTests, update_derived1)
  111. {
  112. StrictMock<mariadb_mock> mock;
  113. expect_query(mock, "START TRANSACTION");
  114. expect_query(mock, "UPDATE "
  115. "`tbl_base` "
  116. "SET "
  117. "`name`='Xderived1X' "
  118. "WHERE "
  119. "`tbl_base_id`=UuidToBin('X3d12778a-abb9-11e8-98d0-529269fb1459X')",
  120. result_affected_rows(1));
  121. expect_query(mock, "SELECT Uuid()", result_used({
  122. { "e2488a64-b843-11e8-96f8-529269fb1459" }
  123. }));
  124. expect_query(mock, "INSERT INTO "
  125. "`tbl_test1` "
  126. "SET "
  127. "`tbl_test1_id`=UuidToBin('Xe2488a64-b843-11e8-96f8-529269fb1459X'), "
  128. "`str_data`='Xstr_data of class `test1` object `d1.test1_data`X', "
  129. "`str64_data`='Xstr64_data of class `test1` object `d1.test1_data`X', "
  130. "`u32_nullable`='X32X', "
  131. "`u32_ptr_u`=null, "
  132. "`u32_ptr_s`='X789X'",
  133. result_affected_rows(1));
  134. expect_query(mock, "DELETE "
  135. "`tbl_test1` "
  136. "FROM "
  137. "`tbl_test1` "
  138. "WHERE "
  139. "`tbl_test1_id` IN ("
  140. "SELECT "
  141. "`tbl_test1_id_test1_data` "
  142. "FROM "
  143. "`tbl_derived1` "
  144. "WHERE "
  145. "`tbl_derived1_id`=UuidToBin('X3d12758c-abb9-11e8-98d0-529269fb1459X') AND "
  146. "`tbl_test1_id_test1_data`!=UuidToBin('Xe2488a64-b843-11e8-96f8-529269fb1459X')"
  147. ")");
  148. expect_query(mock, "UPDATE "
  149. "`tbl_derived1` "
  150. "SET "
  151. "`tbl_base_id`=UuidToBin('X3d12778a-abb9-11e8-98d0-529269fb1459X'), "
  152. "`tbl_test1_id_test1_data`=UuidToBin('Xe2488a64-b843-11e8-96f8-529269fb1459X'), "
  153. "`enum_data`='Xtest2X' "
  154. "WHERE "
  155. "`tbl_derived1_id`=UuidToBin('X3d12758c-abb9-11e8-98d0-529269fb1459X')",
  156. result_affected_rows(1));
  157. expect_query(mock, "COMMIT");
  158. EXPECT_CALL(
  159. mock,
  160. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  161. .Times(AnyNumber())
  162. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  163. EXPECT_CALL(
  164. mock,
  165. mysql_close(
  166. reinterpret_cast<MYSQL*>(0x1111)));
  167. derived1 d1;
  168. d1.id = uuid("3d12778a-abb9-11e8-98d0-529269fb1459");
  169. d1.name = "derived1";
  170. d1.derived1_id = uuid("3d12758c-abb9-11e8-98d0-529269fb1459");
  171. d1.enum_data = test_enum::test2;
  172. d1.test1_data.str_data = "str_data of class `test1` object `d1.test1_data`";
  173. d1.test1_data.str64_data = "str64_data of class `test1` object `d1.test1_data`";
  174. d1.test1_data.u32_nullable = 32;
  175. d1.test1_data.u32_ptr_s = std::make_shared<uint32_t>(789);
  176. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  177. auto context = make_context<mariadb_driver>(test_schema, connection);
  178. context.update(static_cast<base&>(d1));
  179. }
  180. TEST(CppHibernateTests, update_derived2)
  181. {
  182. StrictMock<mariadb_mock> mock;
  183. expect_query(mock, "START TRANSACTION");
  184. expect_query(mock, "UPDATE "
  185. "`tbl_base` "
  186. "SET "
  187. "`name`='Xderived2X' "
  188. "WHERE "
  189. "`tbl_base_id`=UuidToBin('X3d127db6-abb9-11e8-98d0-529269fb1459X')",
  190. result_affected_rows(1));
  191. expect_query(mock, "UPDATE "
  192. "`tbl_test2` "
  193. "SET "
  194. "`u8_data`='X10X', "
  195. "`i8_data`='X11X', "
  196. "`u16_data`='X12X', "
  197. "`i16_data`='X13X' "
  198. "WHERE "
  199. "`tbl_test2_id`=UuidToBin('X3d1283a6-abb9-11e8-98d0-529269fb1459X')",
  200. result_affected_rows(1));
  201. expect_query(mock, "DELETE "
  202. "`tbl_test2` "
  203. "FROM "
  204. "`tbl_test2` "
  205. "WHERE "
  206. "`tbl_test2_id` IN ("
  207. "SELECT "
  208. "`tbl_test2_id_test2_nullable` "
  209. "FROM "
  210. "`tbl_derived2` "
  211. "WHERE "
  212. "`tbl_derived2_id`=UuidToBin('X3d127bcc-abb9-11e8-98d0-529269fb1459X') AND "
  213. "`tbl_test2_id_test2_nullable`!=UuidToBin('X3d1283a6-abb9-11e8-98d0-529269fb1459X')"
  214. ")");
  215. expect_query(mock, "SELECT Uuid()",
  216. result_used({
  217. { "ec0f0aac-b8b9-11e8-96f8-529269fb1459" }
  218. }));
  219. expect_query(mock, "INSERT INTO "
  220. "`tbl_test2` "
  221. "SET "
  222. "`tbl_test2_id`=UuidToBin('Xec0f0aac-b8b9-11e8-96f8-529269fb1459X'), "
  223. "`u8_data`='X20X', "
  224. "`i8_data`='X21X', "
  225. "`u16_data`='X22X', "
  226. "`i16_data`='X23X'",
  227. result_affected_rows(1));
  228. expect_query(mock, "DELETE "
  229. "`tbl_test2` "
  230. "FROM "
  231. "`tbl_test2` "
  232. "WHERE "
  233. "`tbl_test2_id` IN ("
  234. "SELECT "
  235. "`tbl_test2_id_test2_ptr_u` "
  236. "FROM "
  237. "`tbl_derived2` "
  238. "WHERE "
  239. "`tbl_derived2_id`=UuidToBin('X3d127bcc-abb9-11e8-98d0-529269fb1459X') AND "
  240. "`tbl_test2_id_test2_ptr_u`!=UuidToBin('Xec0f0aac-b8b9-11e8-96f8-529269fb1459X')"
  241. ")");
  242. expect_query(mock, "DELETE "
  243. "`tbl_test2` "
  244. "FROM "
  245. "`tbl_test2` "
  246. "WHERE "
  247. "`tbl_test2_id` IN ("
  248. "SELECT "
  249. "`tbl_test2_id_test2_ptr_s` "
  250. "FROM "
  251. "`tbl_derived2` "
  252. "WHERE "
  253. "`tbl_derived2_id`=UuidToBin('X3d127bcc-abb9-11e8-98d0-529269fb1459X')"
  254. ")");
  255. expect_query(mock, "UPDATE "
  256. "`tbl_derived2` "
  257. "SET "
  258. "`tbl_base_id`=UuidToBin('X3d127db6-abb9-11e8-98d0-529269fb1459X'), "
  259. "`tbl_test2_id_test2_nullable`=UuidToBin('X3d1283a6-abb9-11e8-98d0-529269fb1459X'), "
  260. "`tbl_test2_id_test2_ptr_u`=UuidToBin('Xec0f0aac-b8b9-11e8-96f8-529269fb1459X'), "
  261. "`tbl_test2_id_test2_ptr_s`=UuidToBin(null) "
  262. "WHERE "
  263. "`tbl_derived2_id`=UuidToBin('X3d127bcc-abb9-11e8-98d0-529269fb1459X')",
  264. result_affected_rows(1));
  265. expect_query(mock, "COMMIT");
  266. EXPECT_CALL(
  267. mock,
  268. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  269. .Times(AnyNumber())
  270. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  271. EXPECT_CALL(
  272. mock,
  273. mysql_close(
  274. reinterpret_cast<MYSQL*>(0x1111)));
  275. derived2 d2;
  276. d2.id = uuid("3d127db6-abb9-11e8-98d0-529269fb1459");
  277. d2.name = "derived2";
  278. d2.derived2_id = uuid("3d127bcc-abb9-11e8-98d0-529269fb1459");
  279. d2.test2_nullable = test2 { };
  280. d2.test2_nullable->id = uuid("3d1283a6-abb9-11e8-98d0-529269fb1459");
  281. d2.test2_nullable->u8_data = 10;
  282. d2.test2_nullable->i8_data = 11;
  283. d2.test2_nullable->u16_data = 12;
  284. d2.test2_nullable->i16_data = 13;
  285. d2.test2_ptr_u = std::make_unique<test2>();
  286. d2.test2_ptr_u->u8_data = 20;
  287. d2.test2_ptr_u->i8_data = 21;
  288. d2.test2_ptr_u->u16_data = 22;
  289. d2.test2_ptr_u->i16_data = 23;
  290. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  291. auto context = make_context<mariadb_driver>(test_schema, connection);
  292. context.update(static_cast<base&>(d2));
  293. }
  294. TEST(CppHibernateTests, update_derived3)
  295. {
  296. StrictMock<mariadb_mock> mock;
  297. expect_query(mock, "START TRANSACTION");
  298. expect_query(mock, "UPDATE "
  299. "`tbl_base` "
  300. "SET "
  301. "`name`='Xderived3X' "
  302. "WHERE "
  303. "`tbl_base_id`=UuidToBin('X3d1288ce-abb9-11e8-98d0-529269fb1459X')",
  304. result_affected_rows(1));
  305. expect_query(mock, "DELETE "
  306. "`tbl_test2` "
  307. "FROM "
  308. "`tbl_test2` "
  309. "WHERE "
  310. "`tbl_test2_id` IN ("
  311. "SELECT "
  312. "`tbl_test2_id_test2_nullable` "
  313. "FROM "
  314. "`tbl_derived2` "
  315. "WHERE "
  316. "`tbl_derived2_id`=UuidToBin('X3d1287a2-abb9-11e8-98d0-529269fb1459X')"
  317. ")");
  318. expect_query(mock, "DELETE "
  319. "`tbl_test2` "
  320. "FROM "
  321. "`tbl_test2` "
  322. "WHERE "
  323. "`tbl_test2_id` IN ("
  324. "SELECT "
  325. "`tbl_test2_id_test2_ptr_u` "
  326. "FROM "
  327. "`tbl_derived2` "
  328. "WHERE "
  329. "`tbl_derived2_id`=UuidToBin('X3d1287a2-abb9-11e8-98d0-529269fb1459X')"
  330. ")");
  331. expect_query(mock, "DELETE "
  332. "`tbl_test2` "
  333. "FROM "
  334. "`tbl_test2` "
  335. "WHERE "
  336. "`tbl_test2_id` IN ("
  337. "SELECT "
  338. "`tbl_test2_id_test2_ptr_s` "
  339. "FROM "
  340. "`tbl_derived2` "
  341. "WHERE "
  342. "`tbl_derived2_id`=UuidToBin('X3d1287a2-abb9-11e8-98d0-529269fb1459X')"
  343. ")");
  344. expect_query(mock, "UPDATE "
  345. "`tbl_derived2` "
  346. "SET "
  347. "`tbl_base_id`=UuidToBin('X3d1288ce-abb9-11e8-98d0-529269fb1459X'), "
  348. "`tbl_test2_id_test2_nullable`=UuidToBin(null), "
  349. "`tbl_test2_id_test2_ptr_u`=UuidToBin(null), "
  350. "`tbl_test2_id_test2_ptr_s`=UuidToBin(null) "
  351. "WHERE "
  352. "`tbl_derived2_id`=UuidToBin('X3d1287a2-abb9-11e8-98d0-529269fb1459X')",
  353. result_affected_rows(1));
  354. expect_query(mock, "UPDATE "
  355. "`tbl_derived3` "
  356. "SET "
  357. "`tbl_derived2_id`=UuidToBin('X3d1287a2-abb9-11e8-98d0-529269fb1459X') "
  358. "WHERE "
  359. "`tbl_derived3_id`=UuidToBin('X3d12866c-abb9-11e8-98d0-529269fb1459X')",
  360. result_affected_rows(1));
  361. expect_query(mock, "UPDATE "
  362. "`tbl_test3` "
  363. "SET "
  364. "`tbl_derived3_id_test3_list`=NULL, "
  365. "`tbl_derived3_index_test3_list`=0 "
  366. "WHERE "
  367. "`tbl_derived3_id_test3_list`=UuidToBin('X3d12866c-abb9-11e8-98d0-529269fb1459X')");
  368. expect_query(mock, "UPDATE "
  369. "`tbl_test3` "
  370. "SET "
  371. "`tbl_derived3_id_test3_list`=UuidToBin('X3d12866c-abb9-11e8-98d0-529269fb1459X'), "
  372. "`tbl_derived3_index_test3_list`='X0X', "
  373. "`u32_data`='X100X', "
  374. "`i32_data`='X101X', "
  375. "`u64_data`='X102X', "
  376. "`i64_data`='X103X' "
  377. "WHERE "
  378. "`tbl_test3_id`=UuidToBin('X3d1289f0-abb9-11e8-98d0-529269fb1459X')",
  379. result_affected_rows(1));
  380. expect_query(mock, "SELECT Uuid()",
  381. result_used({
  382. { "435bd976-b8c3-11e8-96f8-529269fb1459" }
  383. }));
  384. expect_query(mock, "INSERT INTO "
  385. "`tbl_test3` "
  386. "SET "
  387. "`tbl_test3_id`=UuidToBin('X435bd976-b8c3-11e8-96f8-529269fb1459X'), "
  388. "`tbl_derived3_id_test3_list`=UuidToBin('X3d12866c-abb9-11e8-98d0-529269fb1459X'), "
  389. "`tbl_derived3_index_test3_list`='X1X', "
  390. "`tbl_derived3_id_test3_vector`=UuidToBin(null), "
  391. "`tbl_derived3_index_test3_vector`='X0X', "
  392. "`u32_data`='X110X', "
  393. "`i32_data`='X111X', "
  394. "`u64_data`='X112X', "
  395. "`i64_data`='X113X'",
  396. result_affected_rows(1));
  397. expect_query(mock, "DELETE "
  398. "`tbl_test3` "
  399. "FROM "
  400. "`tbl_test3` "
  401. "WHERE "
  402. "(`tbl_derived3_id_test3_list` IS NULL) AND "
  403. "(`tbl_derived3_id_test3_vector` IS NULL)");
  404. expect_query(mock, "UPDATE "
  405. "`tbl_test3` "
  406. "SET "
  407. "`tbl_derived3_id_test3_vector`=NULL, "
  408. "`tbl_derived3_index_test3_vector`=0 "
  409. "WHERE "
  410. "`tbl_derived3_id_test3_vector`=UuidToBin('X3d12866c-abb9-11e8-98d0-529269fb1459X')");
  411. expect_query(mock, "UPDATE "
  412. "`tbl_test3` "
  413. "SET "
  414. "`tbl_derived3_id_test3_vector`=UuidToBin('X3d12866c-abb9-11e8-98d0-529269fb1459X'), "
  415. "`tbl_derived3_index_test3_vector`='X0X', "
  416. "`u32_data`='X200X', "
  417. "`i32_data`='X201X', "
  418. "`u64_data`='X202X', "
  419. "`i64_data`='X203X' "
  420. "WHERE "
  421. "`tbl_test3_id`=UuidToBin('X3d128eb4-abb9-11e8-98d0-529269fb1459X')",
  422. result_affected_rows(1));
  423. expect_query(mock, "SELECT Uuid()",
  424. result_used({
  425. { "1c0a3592-b8c4-11e8-96f8-529269fb1459" }
  426. }));
  427. expect_query(mock, "INSERT INTO "
  428. "`tbl_test3` "
  429. "SET "
  430. "`tbl_test3_id`=UuidToBin('X1c0a3592-b8c4-11e8-96f8-529269fb1459X'), "
  431. "`tbl_derived3_id_test3_list`=UuidToBin(null), "
  432. "`tbl_derived3_index_test3_list`='X0X', "
  433. "`tbl_derived3_id_test3_vector`=UuidToBin('X3d12866c-abb9-11e8-98d0-529269fb1459X'), "
  434. "`tbl_derived3_index_test3_vector`='X1X', "
  435. "`u32_data`='X210X', "
  436. "`i32_data`='X211X', "
  437. "`u64_data`='X212X', "
  438. "`i64_data`='X213X'",
  439. result_affected_rows(1));
  440. expect_query(mock, "UPDATE "
  441. "`tbl_test3` "
  442. "SET "
  443. "`tbl_derived3_id_test3_vector`=UuidToBin('X3d12866c-abb9-11e8-98d0-529269fb1459X'), "
  444. "`tbl_derived3_index_test3_vector`='X2X', "
  445. "`u32_data`='X220X', "
  446. "`i32_data`='X221X', "
  447. "`u64_data`='X222X', "
  448. "`i64_data`='X223X' "
  449. "WHERE "
  450. "`tbl_test3_id`=UuidToBin('X3d129134-abb9-11e8-98d0-529269fb1459X')",
  451. result_affected_rows(1));
  452. expect_query(mock, "DELETE "
  453. "`tbl_test3` "
  454. "FROM "
  455. "`tbl_test3` "
  456. "WHERE "
  457. "(`tbl_derived3_id_test3_list` IS NULL) AND "
  458. "(`tbl_derived3_id_test3_vector` IS NULL)");
  459. expect_query(mock, "COMMIT");
  460. EXPECT_CALL(
  461. mock,
  462. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  463. .Times(AnyNumber())
  464. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  465. EXPECT_CALL(
  466. mock,
  467. mysql_close(
  468. reinterpret_cast<MYSQL*>(0x1111)));
  469. derived3 d3;
  470. d3.id = uuid("3d1288ce-abb9-11e8-98d0-529269fb1459");
  471. d3.name = "derived3";
  472. d3.derived2_id = uuid("3d1287a2-abb9-11e8-98d0-529269fb1459");
  473. d3.derived3_id = uuid("3d12866c-abb9-11e8-98d0-529269fb1459");
  474. d3.test3_list.emplace_back();
  475. d3.test3_list.back().id = uuid("3d1289f0-abb9-11e8-98d0-529269fb1459");
  476. d3.test3_list.back().u32_data = 100;
  477. d3.test3_list.back().i32_data = 101;
  478. d3.test3_list.back().u64_data = 102;
  479. d3.test3_list.back().i64_data = 103;
  480. d3.test3_list.emplace_back();
  481. d3.test3_list.back().u32_data = 110;
  482. d3.test3_list.back().i32_data = 111;
  483. d3.test3_list.back().u64_data = 112;
  484. d3.test3_list.back().i64_data = 113;
  485. d3.test3_vector.emplace_back();
  486. d3.test3_vector.back().id = uuid("3d128eb4-abb9-11e8-98d0-529269fb1459");
  487. d3.test3_vector.back().u32_data = 200;
  488. d3.test3_vector.back().i32_data = 201;
  489. d3.test3_vector.back().u64_data = 202;
  490. d3.test3_vector.back().i64_data = 203;
  491. d3.test3_vector.emplace_back();
  492. d3.test3_vector.back().u32_data = 210;
  493. d3.test3_vector.back().i32_data = 211;
  494. d3.test3_vector.back().u64_data = 212;
  495. d3.test3_vector.back().i64_data = 213;
  496. d3.test3_vector.emplace_back();
  497. d3.test3_vector.back().id = uuid("3d129134-abb9-11e8-98d0-529269fb1459");
  498. d3.test3_vector.back().u32_data = 220;
  499. d3.test3_vector.back().i32_data = 221;
  500. d3.test3_vector.back().u64_data = 222;
  501. d3.test3_vector.back().i64_data = 223;
  502. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  503. auto context = make_context<mariadb_driver>(test_schema, connection);
  504. context.update(static_cast<derived2&>(d3));
  505. }
  506. TEST(CppHibernateTests, update_dynamic_base)
  507. {
  508. StrictMock<mariadb_mock> mock;
  509. expect_query(mock, "START TRANSACTION");
  510. expect_query(mock, "SELECT "
  511. "`tbl_derived2_id` "
  512. "FROM "
  513. "`tbl_derived2` "
  514. "WHERE "
  515. "`tbl_base_id`='Xf9f13c08-c6e2-11e8-a8d5-f2801f1b9fd1X'",
  516. result_stored({
  517. { "ae0e7888-c6e6-11e8-a8d5-f2801f1b9fd1" }
  518. }));
  519. expect_query(mock, "UPDATE "
  520. "`tbl_base` "
  521. "SET "
  522. "`name`='Xdynamic derived 1X' "
  523. "WHERE "
  524. "`tbl_base_id`=UuidToBin('Xf9f13c08-c6e2-11e8-a8d5-f2801f1b9fd1X')",
  525. result_affected_rows(1));
  526. expect_query(mock, "DELETE "
  527. "`tbl_test2` "
  528. "FROM "
  529. "`tbl_test2` "
  530. "WHERE "
  531. "`tbl_test2_id` IN ("
  532. "SELECT "
  533. "`tbl_test2_id_test2_nullable` "
  534. "FROM "
  535. "`tbl_derived2` "
  536. "WHERE "
  537. "`tbl_derived2_id`=UuidToBin('Xae0e7888-c6e6-11e8-a8d5-f2801f1b9fd1X')"
  538. ")");
  539. expect_query(mock, "DELETE "
  540. "`tbl_test2` "
  541. "FROM "
  542. "`tbl_test2` "
  543. "WHERE "
  544. "`tbl_test2_id` IN ("
  545. "SELECT "
  546. "`tbl_test2_id_test2_ptr_u` "
  547. "FROM "
  548. "`tbl_derived2` "
  549. "WHERE "
  550. "`tbl_derived2_id`=UuidToBin('Xae0e7888-c6e6-11e8-a8d5-f2801f1b9fd1X')"
  551. ")");
  552. expect_query(mock, "DELETE "
  553. "`tbl_test2` "
  554. "FROM "
  555. "`tbl_test2` "
  556. "WHERE "
  557. "`tbl_test2_id` IN ("
  558. "SELECT "
  559. "`tbl_test2_id_test2_ptr_s` "
  560. "FROM "
  561. "`tbl_derived2` "
  562. "WHERE "
  563. "`tbl_derived2_id`=UuidToBin('Xae0e7888-c6e6-11e8-a8d5-f2801f1b9fd1X')"
  564. ")");
  565. expect_query(mock, "UPDATE "
  566. "`tbl_derived2` "
  567. "SET "
  568. "`tbl_base_id`=UuidToBin('Xf9f13c08-c6e2-11e8-a8d5-f2801f1b9fd1X'), "
  569. "`tbl_test2_id_test2_nullable`=UuidToBin(null), "
  570. "`tbl_test2_id_test2_ptr_u`=UuidToBin(null), "
  571. "`tbl_test2_id_test2_ptr_s`=UuidToBin(null) "
  572. "WHERE "
  573. "`tbl_derived2_id`=UuidToBin('Xae0e7888-c6e6-11e8-a8d5-f2801f1b9fd1X')",
  574. result_affected_rows(1));
  575. expect_query(mock, "COMMIT");
  576. EXPECT_CALL(
  577. mock,
  578. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  579. .Times(AnyNumber())
  580. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  581. EXPECT_CALL(
  582. mock,
  583. mysql_close(
  584. reinterpret_cast<MYSQL*>(0x1111)));
  585. std::unique_ptr<base> b;
  586. b.reset(new derived2());
  587. auto& d2 = *dynamic_cast<derived2*>(b.get());
  588. d2.id = uuid("f9f13c08-c6e2-11e8-a8d5-f2801f1b9fd1");
  589. d2.name = "dynamic derived 1";
  590. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  591. auto context = make_context<mariadb_driver>(test_schema, connection);
  592. context.update(b);
  593. }
  594. TEST(CppHibernateTests, update_dummy_owner)
  595. {
  596. StrictMock<mariadb_mock> mock;
  597. expect_query(mock, "START TRANSACTION");
  598. expect_query(mock, "UPDATE "
  599. "`tbl_dummy_id` "
  600. "SET "
  601. "`tbl_dummy_owner_id_dummies`=NULL, "
  602. "`tbl_dummy_owner_index_dummies`=0 "
  603. "WHERE "
  604. "`tbl_dummy_owner_id_dummies`=UuidToBin('X00000000-0000-0000-0000-000000000001X')");
  605. expect_query(mock, "SELECT Uuid()", result_used({
  606. { "00000000-0000-0000-0001-000000000001" }
  607. }));
  608. expect_query(mock, "INSERT INTO "
  609. "`tbl_dummy_id` "
  610. "SET "
  611. "`tbl_dummy_id_id`=UuidToBin('X00000000-0000-0000-0001-000000000001X'), "
  612. "`tbl_dummy_owner_id_dummies`=UuidToBin('X00000000-0000-0000-0000-000000000001X'), "
  613. "`tbl_dummy_owner_index_dummies`='X0X', "
  614. "`data`='X123X'",
  615. result_affected_rows(1));
  616. expect_query(mock, "SELECT Uuid()", result_used({
  617. { "00000000-0000-0000-0001-000000000002" }
  618. }));
  619. expect_query(mock, "INSERT INTO "
  620. "`tbl_dummy_id` "
  621. "SET "
  622. "`tbl_dummy_id_id`=UuidToBin('X00000000-0000-0000-0001-000000000002X'), "
  623. "`tbl_dummy_owner_id_dummies`=UuidToBin('X00000000-0000-0000-0000-000000000001X'), "
  624. "`tbl_dummy_owner_index_dummies`='X1X', "
  625. "`data`='X456X'",
  626. result_affected_rows(1));
  627. expect_query(mock, "SELECT Uuid()", result_used({
  628. { "00000000-0000-0000-0001-000000000003" }
  629. }));
  630. expect_query(mock, "INSERT INTO "
  631. "`tbl_dummy_id` "
  632. "SET "
  633. "`tbl_dummy_id_id`=UuidToBin('X00000000-0000-0000-0001-000000000003X'), "
  634. "`tbl_dummy_owner_id_dummies`=UuidToBin('X00000000-0000-0000-0000-000000000001X'), "
  635. "`tbl_dummy_owner_index_dummies`='X2X', "
  636. "`data`='X789X'",
  637. result_affected_rows(1));
  638. expect_query(mock, "DELETE "
  639. "`tbl_dummy_id` "
  640. "FROM "
  641. "`tbl_dummy_id` "
  642. "WHERE "
  643. "(`tbl_dummy_owner_id_dummies` IS NULL)");
  644. expect_query(mock, "COMMIT");
  645. EXPECT_CALL(
  646. mock,
  647. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  648. .Times(AnyNumber())
  649. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  650. EXPECT_CALL(
  651. mock,
  652. mysql_close(
  653. reinterpret_cast<MYSQL*>(0x1111)));
  654. dummy_owner d;
  655. d.id = uuid("00000000-0000-0000-0000-000000000001");
  656. d.dummies.emplace_back(123);
  657. d.dummies.emplace_back(456);
  658. d.dummies.emplace_back(789);
  659. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  660. auto context = make_context<mariadb_driver>(test_schema, connection);
  661. context.update(d);
  662. }
  663. TEST(CppHibernateTests, update_double_usage)
  664. {
  665. StrictMock<mariadb_mock> mock;
  666. expect_query(mock, "START TRANSACTION");
  667. expect_query(mock, "UPDATE "
  668. "`tbl_double_usage_item` "
  669. "SET "
  670. "`tbl_double_usage_id_single_item`=NULL "
  671. "WHERE "
  672. "`tbl_double_usage_id_single_item`='X1X'");
  673. expect_query(mock, "UPDATE "
  674. "`tbl_double_usage_item` "
  675. "SET "
  676. "`tbl_double_usage_id_single_item`='X1X', "
  677. "`data`='X123X' "
  678. "WHERE "
  679. "`tbl_double_usage_item_id`='X1001X'",
  680. result_affected_rows(1));
  681. expect_query(mock, "DELETE "
  682. "`tbl_double_usage_item` "
  683. "FROM "
  684. "`tbl_double_usage_item` "
  685. "WHERE "
  686. "(`tbl_double_usage_id_single_item` IS NULL) "
  687. "AND (`tbl_double_usage_id_multiple_items` IS NULL)");
  688. expect_query(mock, "UPDATE "
  689. "`tbl_double_usage_item` "
  690. "SET "
  691. "`tbl_double_usage_id_multiple_items`=NULL, "
  692. "`tbl_double_usage_index_multiple_items`=0 "
  693. "WHERE "
  694. "`tbl_double_usage_id_multiple_items`='X1X'");
  695. expect_query(mock, "UPDATE "
  696. "`tbl_double_usage_item` "
  697. "SET "
  698. "`tbl_double_usage_id_multiple_items`='X1X', "
  699. "`tbl_double_usage_index_multiple_items`='X0X', "
  700. "`data`='X456X' "
  701. "WHERE "
  702. "`tbl_double_usage_item_id`='X1002X'",
  703. result_affected_rows(1));
  704. expect_query(mock, "UPDATE "
  705. "`tbl_double_usage_item` "
  706. "SET "
  707. "`tbl_double_usage_id_multiple_items`='X1X', "
  708. "`tbl_double_usage_index_multiple_items`='X1X', "
  709. "`data`='X789X' "
  710. "WHERE "
  711. "`tbl_double_usage_item_id`='X1003X'",
  712. result_affected_rows(1));
  713. expect_query(mock, "DELETE "
  714. "`tbl_double_usage_item` "
  715. "FROM "
  716. "`tbl_double_usage_item` "
  717. "WHERE "
  718. "(`tbl_double_usage_id_single_item` IS NULL) "
  719. "AND (`tbl_double_usage_id_multiple_items` IS NULL)");
  720. expect_query(mock, "COMMIT");
  721. EXPECT_CALL(
  722. mock,
  723. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  724. .Times(AnyNumber())
  725. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  726. EXPECT_CALL(
  727. mock,
  728. mysql_close(
  729. reinterpret_cast<MYSQL*>(0x1111)));
  730. double_usage d;
  731. d.id = 1;
  732. d.single_item.reset(new double_usage_item());
  733. d.single_item->id = 1001;
  734. d.single_item->data = 123;
  735. d.multiple_items.emplace_back();
  736. d.multiple_items.back().id = 1002;
  737. d.multiple_items.back().data = 456;
  738. d.multiple_items.emplace_back();
  739. d.multiple_items.back().id = 1003;
  740. d.multiple_items.back().data = 789;
  741. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  742. auto context = make_context<mariadb_driver>(test_schema, connection);
  743. context.update(d);
  744. }
  745. TEST(CppHibernateTests, update_double_usage_wrapper)
  746. {
  747. StrictMock<mariadb_mock> mock;
  748. expect_query(mock, "START TRANSACTION");
  749. expect_query(mock, "INSERT INTO `tbl_double_usage`",
  750. result_id(101));
  751. expect_query(mock, "INSERT INTO "
  752. "`tbl_double_usage_item` "
  753. "SET "
  754. "`tbl_double_usage_id_single_item`='X101X', "
  755. "`tbl_double_usage_id_multiple_items`=null, "
  756. "`tbl_double_usage_index_multiple_items`='X0X', "
  757. "`data`='X123X'",
  758. result_id(1001));
  759. expect_query(mock, "INSERT INTO "
  760. "`tbl_double_usage_item` "
  761. "SET "
  762. "`tbl_double_usage_id_single_item`=null, "
  763. "`tbl_double_usage_id_multiple_items`='X101X', "
  764. "`tbl_double_usage_index_multiple_items`='X0X', "
  765. "`data`='X456X'",
  766. result_id(1002));
  767. expect_query(mock, "INSERT INTO "
  768. "`tbl_double_usage_item` "
  769. "SET "
  770. "`tbl_double_usage_id_single_item`=null, "
  771. "`tbl_double_usage_id_multiple_items`='X101X', "
  772. "`tbl_double_usage_index_multiple_items`='X1X', "
  773. "`data`='X789X'",
  774. result_id(1003));
  775. expect_query(mock, "DELETE "
  776. "`tbl_double_usage`, `T0` "
  777. "FROM "
  778. "`tbl_double_usage` "
  779. "LEFT JOIN "
  780. "`tbl_double_usage_item` AS `T0` ON `tbl_double_usage`.`tbl_double_usage_id`=`T0`.`tbl_double_usage_id_single_item` "
  781. "WHERE "
  782. "`tbl_double_usage_id` IN ("
  783. "SELECT "
  784. "`tbl_double_usage_id_double_usage` "
  785. "FROM "
  786. "`tbl_double_usage_wrapper` "
  787. "WHERE "
  788. "`tbl_double_usage_wrapper_id`='X1X' "
  789. "AND `tbl_double_usage_id_double_usage`!='X101X'"
  790. ")");
  791. expect_query(mock, "DELETE "
  792. "`tbl_double_usage_item` "
  793. "FROM "
  794. "`tbl_double_usage_item` "
  795. "WHERE "
  796. "(`tbl_double_usage_id_single_item` IS NULL) "
  797. "AND (`tbl_double_usage_id_multiple_items` IS NULL)");
  798. expect_query(mock, "UPDATE "
  799. "`tbl_double_usage_wrapper` "
  800. "SET "
  801. "`tbl_double_usage_id_double_usage`='X101X' "
  802. "WHERE "
  803. "`tbl_double_usage_wrapper_id`='X1X'",
  804. result_affected_rows(1));
  805. expect_query(mock, "COMMIT");
  806. EXPECT_CALL(
  807. mock,
  808. mysql_real_escape_string(reinterpret_cast<MYSQL*>(0x1111), _, _, _))
  809. .Times(AnyNumber())
  810. .WillRepeatedly(WithArgs<1, 2, 3>(EscapeString()));
  811. EXPECT_CALL(
  812. mock,
  813. mysql_close(
  814. reinterpret_cast<MYSQL*>(0x1111)));
  815. double_usage_wrapper d;
  816. d.id = 1;
  817. d.double_usage.reset(new double_usage());
  818. d.double_usage->single_item.reset(new double_usage_item());
  819. d.double_usage->single_item->data = 123;
  820. d.double_usage->multiple_items.emplace_back();
  821. d.double_usage->multiple_items.back().data = 456;
  822. d.double_usage->multiple_items.emplace_back();
  823. d.double_usage->multiple_items.back().data = 789;
  824. ::cppmariadb::connection connection(reinterpret_cast<MYSQL*>(0x1111));
  825. auto context = make_context<mariadb_driver>(test_schema, connection);
  826. context.update(d);
  827. }