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.

192 lines
4.3 KiB

  1. From be9de8b2e0864b46498496af9ffdc31c0e7b764a Mon Sep 17 00:00:00 2001
  2. From: Daniel Kamil Kozar <dkk089@gmail.com>
  3. Date: Sun, 16 Dec 2018 14:21:58 +0100
  4. Subject: [PATCH] Reduce duplication in sql.c
  5. ---
  6. sql.c | 166 ++++++++++++++++++++--------------------------------------
  7. 1 file changed, 58 insertions(+), 108 deletions(-)
  8. --- a/sql.c
  9. +++ b/sql.c
  10. @@ -65,122 +65,72 @@ sql_get_table(sqlite3 *db, const char *s
  11. return ret;
  12. }
  13. +#define sql_get_field(type, column_get_fn) \
  14. + va_list ap;\
  15. + int counter, result;\
  16. + char *sql;\
  17. + type ret;\
  18. + sqlite3_stmt *stmt;\
  19. + \
  20. + va_start(ap, fmt);\
  21. + sql = sqlite3_vmprintf(fmt, ap);\
  22. + va_end(ap);\
  23. +\
  24. + /*DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql); */ \
  25. +\
  26. + switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))\
  27. + {\
  28. + case SQLITE_OK:\
  29. + break;\
  30. + default:\
  31. + DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);\
  32. + sqlite3_free(sql);\
  33. + return -1;\
  34. + }\
  35. +\
  36. + for (counter = 0;\
  37. + ((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2;\
  38. + counter++) {\
  39. + /* While SQLITE_BUSY has a built in timeout,\
  40. + * SQLITE_LOCKED does not, so sleep */\
  41. + if (result == SQLITE_LOCKED)\
  42. + sleep(1);\
  43. + }\
  44. +\
  45. + switch (result)\
  46. + {\
  47. + case SQLITE_DONE:\
  48. + /* no rows returned */\
  49. + ret = 0;\
  50. + break;\
  51. + case SQLITE_ROW:\
  52. + if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)\
  53. + {\
  54. + ret = 0;\
  55. + break;\
  56. + }\
  57. + ret = column_get_fn(stmt, 0);\
  58. + break;\
  59. + default:\
  60. + DPRINTF(E_WARN, L_DB_SQL, "%s: step failed: %s\n%s\n", __func__, sqlite3_errmsg(db), sql);\
  61. + ret = -1;\
  62. + break;\
  63. + }\
  64. + sqlite3_free(sql);\
  65. + sqlite3_finalize(stmt);\
  66. +\
  67. + return ret;
  68. +
  69. int
  70. sql_get_int_field(sqlite3 *db, const char *fmt, ...)
  71. {
  72. - va_list ap;
  73. - int counter, result;
  74. - char *sql;
  75. - int ret;
  76. - sqlite3_stmt *stmt;
  77. -
  78. - va_start(ap, fmt);
  79. - sql = sqlite3_vmprintf(fmt, ap);
  80. - va_end(ap);
  81. -
  82. - //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
  83. -
  84. - switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
  85. - {
  86. - case SQLITE_OK:
  87. - break;
  88. - default:
  89. - DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);
  90. - sqlite3_free(sql);
  91. - return -1;
  92. - }
  93. -
  94. - for (counter = 0;
  95. - ((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2;
  96. - counter++) {
  97. - /* While SQLITE_BUSY has a built in timeout,
  98. - * SQLITE_LOCKED does not, so sleep */
  99. - if (result == SQLITE_LOCKED)
  100. - sleep(1);
  101. - }
  102. -
  103. - switch (result)
  104. - {
  105. - case SQLITE_DONE:
  106. - /* no rows returned */
  107. - ret = 0;
  108. - break;
  109. - case SQLITE_ROW:
  110. - if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
  111. - {
  112. - ret = 0;
  113. - break;
  114. - }
  115. - ret = sqlite3_column_int(stmt, 0);
  116. - break;
  117. - default:
  118. - DPRINTF(E_WARN, L_DB_SQL, "%s: step failed: %s\n%s\n", __func__, sqlite3_errmsg(db), sql);
  119. - ret = -1;
  120. - break;
  121. - }
  122. - sqlite3_free(sql);
  123. - sqlite3_finalize(stmt);
  124. -
  125. - return ret;
  126. + sql_get_field(int, sqlite3_column_int)
  127. }
  128. int64_t
  129. sql_get_int64_field(sqlite3 *db, const char *fmt, ...)
  130. {
  131. - va_list ap;
  132. - int counter, result;
  133. - char *sql;
  134. - int64_t ret;
  135. - sqlite3_stmt *stmt;
  136. -
  137. - va_start(ap, fmt);
  138. - sql = sqlite3_vmprintf(fmt, ap);
  139. - va_end(ap);
  140. -
  141. - //DPRINTF(E_DEBUG, L_DB_SQL, "sql: %s\n", sql);
  142. -
  143. - switch (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL))
  144. - {
  145. - case SQLITE_OK:
  146. - break;
  147. - default:
  148. - DPRINTF(E_ERROR, L_DB_SQL, "prepare failed: %s\n%s\n", sqlite3_errmsg(db), sql);
  149. - sqlite3_free(sql);
  150. - return -1;
  151. - }
  152. -
  153. - for (counter = 0;
  154. - ((result = sqlite3_step(stmt)) == SQLITE_BUSY || result == SQLITE_LOCKED) && counter < 2;
  155. - counter++) {
  156. - /* While SQLITE_BUSY has a built in timeout,
  157. - * SQLITE_LOCKED does not, so sleep */
  158. - if (result == SQLITE_LOCKED)
  159. - sleep(1);
  160. - }
  161. -
  162. - switch (result)
  163. - {
  164. - case SQLITE_DONE:
  165. - /* no rows returned */
  166. - ret = 0;
  167. - break;
  168. - case SQLITE_ROW:
  169. - if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
  170. - {
  171. - ret = 0;
  172. - break;
  173. - }
  174. - ret = sqlite3_column_int64(stmt, 0);
  175. - break;
  176. - default:
  177. - DPRINTF(E_WARN, L_DB_SQL, "%s: step failed: %s\n%s\n", __func__, sqlite3_errmsg(db), sql);
  178. - ret = -1;
  179. - break;
  180. - }
  181. - sqlite3_free(sql);
  182. - sqlite3_finalize(stmt);
  183. -
  184. - return ret;
  185. + sql_get_field(int64_t, sqlite3_column_int64)
  186. }
  187. char *