dbhelper.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-03-06 lizhen9880 first version
  9. */
  10. #ifndef __DBHELPER_H__
  11. #define __DBHELPER_H__
  12. #include <sqlite3.h>
  13. #include <rtthread.h>
  14. #define DB_SQL_MAX_LEN PKG_SQLITE_SQL_MAX_LEN
  15. int db_helper_init(void);
  16. int db_create_database(const char *sqlstr);
  17. /**
  18. * This function will be used for the operating that is not SELECT.It support executing multiple
  19. * SQL statements.
  20. *
  21. * @param sqlstr the SQL statements strings.if there are more than one
  22. * statements in the sqlstr to execute,separate them by a semicolon(;).
  23. * @param bind the callback function supported by user.bind data and call the sqlite3_step function.
  24. * @param param the parameter for the callback "bind".
  25. * @return success or fail.
  26. */
  27. int db_nonquery_operator(const char *sqlstr, int (*bind)(sqlite3_stmt *, int index, void *arg), void *param);
  28. /**
  29. * This function will be used for the operating that is not SELECT.The additional
  30. * arguments following format are formatted and inserted in the resulting string
  31. * replacing their respective specifiers.
  32. *
  33. * @param sql the SQL statement.
  34. * @param fmt the args format.such as %s string,%d int.
  35. * @param ... the additional arguments
  36. * @return success or fail.
  37. */
  38. int db_nonquery_by_varpara(const char *sql, const char *fmt, ...);
  39. /**
  40. * This function will be used for the transaction that is not SELECT.
  41. *
  42. * @param exec_sqls the callback function of executing SQL statements.
  43. * @param arg the parameter for the callback "exec_sqls".
  44. * @return success or fail.
  45. */
  46. int db_nonquery_transaction(int (*exec_sqls)(sqlite3 *db, void *arg), void *arg);
  47. /**
  48. * This function will be used for the SELECT operating.The additional arguments
  49. * following format are formatted and inserted in the resulting string replacing
  50. * their respective specifiers.
  51. *
  52. * @param sql the SQL statements.
  53. * @param create the callback function supported by user.
  54. * @param arg the parameter for the callback "create".
  55. * @param fmt the args format.such as %s string,%d int.
  56. * @param ... the additional arguments
  57. * @return success or fail.
  58. */
  59. int db_query_by_varpara(const char *sql, int (*create)(sqlite3_stmt *stmt, void *arg), void *arg, const char *fmt, ...);
  60. /**
  61. * This function will return the number of records returned by a select query.
  62. * This function only gets the 1st row of the 1st column.
  63. *
  64. * @param sql the SQL statement SELECT COUNT() FROM .
  65. * @return the count or fail.
  66. */
  67. int db_query_count_result(const char *sql);
  68. /**
  69. * This function will get the blob from the "index" colum.
  70. *
  71. * @param stmt the SQL statement returned by the function sqlite3_step().
  72. * @param index the colum index.the first colum's index value is 0.
  73. * @param out the output buffer.the result will put in this buffer.
  74. * @return the result length or fail.
  75. */
  76. int db_stmt_get_blob(sqlite3_stmt *stmt, int index, unsigned char *out);
  77. /**
  78. * This function will get the text from the "index" colum.
  79. *
  80. * @param stmt the SQL statement returned by the function sqlite3_step().
  81. * @param index the colum index.the first colum's index value is 0.
  82. * @param out the output buffer.the result will put in this buffer.
  83. * @return the result length or fail.
  84. */
  85. int db_stmt_get_text(sqlite3_stmt *stmt, int index, char *out);
  86. /**
  87. * This function will get a integer from the "index" colum.
  88. *
  89. * @param stmt the SQL statement returned by the function sqlite3_step().
  90. * @param index the colum index.the first colum's index value is 0.
  91. * @return the result.
  92. */
  93. int db_stmt_get_int(sqlite3_stmt *stmt, int index);
  94. /**
  95. * This function will get a double precision value from the "index" colum.
  96. *
  97. * @param stmt the SQL statement returned by the function sqlite3_step().
  98. * @param index the colum index.the first colum's index value is 0.
  99. * @return the result.
  100. */
  101. double db_stmt_get_double(sqlite3_stmt *stmt, int index);
  102. /**
  103. * This function will check a table exist or not by table name.
  104. *
  105. * @param tbl_name the table name.
  106. * @return >0:existed; ==0:not existed; <0:ERROR
  107. */
  108. int db_table_is_exist(const char *tbl_name);
  109. /**
  110. * This function will connect DB
  111. *
  112. * @param name the DB filename.
  113. * @return RT_EOK:success
  114. * -RT_ERROR:the input name is too long
  115. */
  116. int db_connect(char *name);
  117. /**
  118. * This function will disconnect DB
  119. *
  120. * @param name the DB filename.
  121. * @return RT_EOK:success
  122. * -RT_ERROR:the input name is too long
  123. */
  124. int db_disconnect(char *name);
  125. /**
  126. * This function will get the current DB filename
  127. *
  128. * @return the current DB filename
  129. *
  130. */
  131. char *db_get_name(void);
  132. #endif