tuple_notes.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "presentity.h"
  2. #include "pa_mod.h"
  3. #include "tuple_notes.h"
  4. #include <cds/logger.h>
  5. /* DB manipulation */
  6. static int db_add_tuple_note(presentity_t *p, presence_tuple_t *t, presence_note_t *n)
  7. {
  8. db_key_t cols[20];
  9. db_val_t vals[20];
  10. int n_updates = 0;
  11. if (!use_db) return 0;
  12. /* set data */
  13. cols[n_updates] = col_pres_id;
  14. vals[n_updates].type = DB_STR;
  15. vals[n_updates].nul = 0;
  16. vals[n_updates].val.str_val = p->pres_id;
  17. n_updates++;
  18. cols[n_updates] = col_tupleid;
  19. vals[n_updates].type = DB_STR;
  20. vals[n_updates].nul = 0;
  21. vals[n_updates].val.str_val = t->data.id;
  22. n_updates++;
  23. cols[n_updates] = col_note;
  24. vals[n_updates].type = DB_STR;
  25. vals[n_updates].nul = 0;
  26. vals[n_updates].val.str_val = n->value;
  27. n_updates++;
  28. cols[n_updates] = col_lang;
  29. vals[n_updates].type = DB_STR;
  30. vals[n_updates].nul = 0;
  31. vals[n_updates].val.str_val = n->lang;
  32. n_updates++;
  33. /* run update */
  34. if (pa_dbf.use_table(pa_db, tuple_notes_table) < 0) {
  35. LOG(L_ERR, "db_add_pres_note: Error in use_table\n");
  36. return -1;
  37. }
  38. if (pa_dbf.insert(pa_db, cols, vals, n_updates) < 0) {
  39. LOG(L_ERR, "db_add_pres_note: Can't insert record\n");
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. int db_add_tuple_notes(presentity_t *p, presence_tuple_t *t)
  45. {
  46. presence_note_t *n;
  47. n = t->data.first_note;
  48. while (n) {
  49. db_add_tuple_note(p, t, n);
  50. n = n->next;
  51. }
  52. return 0;
  53. }
  54. int db_remove_tuple_notes(presentity_t *p, presence_tuple_t *t)
  55. {
  56. db_key_t keys[] = { col_pres_id, col_tupleid };
  57. db_op_t ops[] = { OP_EQ, OP_EQ };
  58. db_val_t k_vals[] = {
  59. { DB_STR, 0, { .str_val = p->pres_id } },
  60. { DB_STR, 0, { .str_val = t->data.id } }
  61. };
  62. if (!use_db) return 0;
  63. if (pa_dbf.use_table(pa_db, tuple_notes_table) < 0) {
  64. LOG(L_ERR, "db_remove_tuple_notes: Error in use_table\n");
  65. return -1;
  66. }
  67. if (pa_dbf.delete(pa_db, keys, ops, k_vals, 2) < 0) {
  68. LOG(L_ERR, "db_remove_tuple_notes: Can't delete record\n");
  69. return -1;
  70. }
  71. return 0;
  72. }
  73. int db_update_tuple_notes(presentity_t *p, presence_tuple_t *t)
  74. {
  75. db_remove_tuple_notes(p, t);
  76. db_add_tuple_notes(p, t);
  77. return 0;
  78. }
  79. int db_read_tuple_notes(presentity_t *p, presence_tuple_t *t, db_con_t* db)
  80. {
  81. db_key_t keys[] = { col_pres_id, col_tupleid };
  82. db_op_t ops[] = { OP_EQ, OP_EQ };
  83. db_val_t k_vals[] = {
  84. { DB_STR, 0, { .str_val = p->pres_id } },
  85. { DB_STR, 0, { .str_val = t->data.id } }
  86. };
  87. int i;
  88. int r = 0;
  89. db_res_t *res = NULL;
  90. db_key_t result_cols[] = { col_note, col_lang };
  91. if (!use_db) return 0;
  92. if (pa_dbf.use_table(db, tuple_notes_table) < 0) {
  93. LOG(L_ERR, "db_read_tuple_notes: Error in use_table\n");
  94. return -1;
  95. }
  96. if (pa_dbf.query (db, keys, ops, k_vals,
  97. result_cols, 2, sizeof(result_cols) / sizeof(db_key_t),
  98. 0, &res) < 0) {
  99. LOG(L_ERR, "db_read_tuple_notes(): Error while querying notes\n");
  100. return -1;
  101. }
  102. if (!res) return 0; /* ? */
  103. for (i = 0; i < res->n; i++) {
  104. presence_note_t *n = NULL;
  105. db_row_t *row = &res->rows[i];
  106. db_val_t *row_vals = ROW_VALUES(row);
  107. str note = STR_NULL;
  108. str lang = STR_NULL;
  109. #define get_str_val(i,dst) do{if(!row_vals[i].nul){dst.s=(char*)row_vals[i].val.string_val;dst.len=strlen(dst.s);}}while(0)
  110. get_str_val(0, note);
  111. get_str_val(1, lang);
  112. #undef get_str_val
  113. n = create_presence_note(&note, &lang);
  114. if (n) add_tuple_note_no_wb(t, n);
  115. }
  116. pa_dbf.free_result(db, res);
  117. return r;
  118. }
  119. /* in memory operations */
  120. void add_tuple_note_no_wb(presence_tuple_t *t, presence_note_t *n)
  121. {
  122. DOUBLE_LINKED_LIST_ADD(t->data.first_note, t->data.last_note, n);
  123. }
  124. void free_tuple_notes(presence_tuple_t *t)
  125. {
  126. presence_note_t *n, *nn;
  127. /* remove all notes for this tuple */
  128. n = t->data.first_note;
  129. while (n) {
  130. nn = n->next;
  131. free_presence_note(n);
  132. n = nn;
  133. }
  134. t->data.first_note = NULL;
  135. t->data.last_note = NULL;
  136. }