call_log.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "call_log.h"
  2. #include "utils/Log.h"
  3. #include "base/strings.hpp"
  4. static int QueryCountCallback(void* user_data, int columns,
  5. char** column_values, char** column_names) {
  6. if (columns != 1) {
  7. LOGE("columns not match");
  8. return 0;
  9. }
  10. *((int*)user_data) = atoi(column_values[0]);
  11. return 0;
  12. }
  13. int GetCallLogCount() {
  14. SQLite lite;
  15. if (0 != lite.Open(CALL_LOG_DATABASE)) {
  16. return -1;
  17. }
  18. int n = 0;
  19. lite.Exec("SELECT count(*) from log", QueryCountCallback, &n);
  20. lite.Close();
  21. return n;
  22. }
  23. int PutCallLog(const CallLogEntry& log) {
  24. SQLite lite;
  25. if (0 != lite.Open(CALL_LOG_DATABASE)) {
  26. return -1;
  27. }
  28. //创建表,确保表存在
  29. std::string sql =
  30. "CREATE TABLE if not exists log("
  31. "id integer PRIMARY KEY AUTOINCREMENT NOT NULL, "
  32. "uri char(64) NOT NULL, "
  33. "contact char(64) NOT NULL, "
  34. "duration unsigned int NOT NULL, "
  35. "created_at unsigned int NOT NULL);";
  36. if (0 != lite.Exec(sql, NULL, NULL)) {
  37. return -1;
  38. }
  39. if (0 != lite.Exec("BEGIN TRANSACTION;", NULL, NULL)) {
  40. return -1;
  41. }
  42. std::string str = base::format(
  43. "INSERT INTO log ('uri', 'contact', 'duration', 'created_at')"
  44. " values ('%s', '%s', %d, %u);",
  45. log.uri.c_str(), log.contact.c_str(), log.duration, time(NULL));
  46. if (0 != lite.Exec(str, NULL, NULL)) {
  47. lite.Exec("ROLLBACK;", NULL, NULL);
  48. return -1;
  49. }
  50. if (0 != lite.Exec("END TRANSACTION;", NULL, NULL)) {
  51. return -1;
  52. }
  53. lite.Close();
  54. return 0;
  55. }
  56. static int QueryRowCallback(void* user_data, int columns,
  57. char** column_values, char** column_names) {
  58. if (columns != 5) {
  59. LOGE("columns not match");
  60. return 0;
  61. }
  62. CallLogEntries* entries = (CallLogEntries*)user_data;
  63. CallLogEntry entry;
  64. entry.id = atoi(column_values[0]);
  65. entry.uri = column_values[1];
  66. entry.contact = column_values[2];
  67. entry.duration = atoi(column_values[3]);
  68. entry.created_at = atoi(column_values[4]);
  69. entries->push_back(entry);
  70. return 0;
  71. }
  72. int GetCallLog(CallLogEntries* records) {
  73. SQLite lite;
  74. if (0 != lite.Open(CALL_LOG_DATABASE)) {
  75. return -1;
  76. }
  77. if (0 != lite.Exec("SELECT * from log", QueryRowCallback, records)) {
  78. return -1;
  79. }
  80. return lite.Close();
  81. }
  82. int DeleteOldCallLog(int n) {
  83. SQLite lite;
  84. if (0 != lite.Open(CALL_LOG_DATABASE)) {
  85. return -1;
  86. }
  87. int ret = lite.Exec(
  88. base::format(
  89. "delete from log where id in(select id from log order by id limit %d)",
  90. n),
  91. NULL, NULL);
  92. lite.Close();
  93. return ret;
  94. }