mysqli_stmt.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "mysqli_stmt.h"
  2. #include "mysqli.h"
  3. mysqli_stmt::mysqli_stmt()
  4. {
  5. _stmt = nullptr;
  6. }
  7. mysqli_stmt::~mysqli_stmt()
  8. {
  9. close();
  10. }
  11. void mysqli_stmt::close()
  12. {
  13. if (_stmt != nullptr) {
  14. mysql_stmt_close(_stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-close.html
  15. _stmt = nullptr;
  16. }
  17. return;
  18. }
  19. bool mysqli_stmt::bind_param(MYSQL_BIND *bind)
  20. {
  21. if (_stmt == nullptr) {
  22. mysqli::throw_null_reference_error();
  23. }
  24. return mysql_stmt_bind_param(_stmt,bind); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-bind-param.html
  25. }
  26. bool mysqli_stmt::execute()
  27. {
  28. const char *error;
  29. if (_stmt == nullptr) {
  30. mysqli::throw_null_reference_error();
  31. }
  32. int status = mysql_stmt_execute(_stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-execute.html
  33. if (status == 0) {
  34. return true;
  35. }
  36. error = mysql_stmt_error(_stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-error.html
  37. if (error == nullptr) {
  38. error = "";
  39. }
  40. throw error;
  41. }
  42. void mysqli_stmt::data_seek(my_ulonglong offset)
  43. {
  44. if (_stmt == nullptr) {
  45. mysqli::throw_null_reference_error();
  46. }
  47. mysql_stmt_data_seek(_stmt, offset); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-data-seek.html
  48. return;
  49. }
  50. bool mysqli_stmt::bind_result(MYSQL_BIND *bind)
  51. {
  52. if (_stmt == nullptr) {
  53. mysqli::throw_null_reference_error();
  54. }
  55. return 0 == mysql_stmt_bind_result(_stmt, bind); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-bind-result.html
  56. }
  57. bool mysqli_stmt::fetch()
  58. {
  59. const char *error;
  60. int status;
  61. if (_stmt == nullptr) {
  62. mysqli::throw_null_reference_error();
  63. }
  64. status = mysql_stmt_fetch(_stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-fetch.html
  65. switch (status) {
  66. case 0:
  67. {
  68. return true;
  69. }
  70. case MYSQL_NO_DATA:
  71. {
  72. return false;
  73. }
  74. case MYSQL_DATA_TRUNCATED:
  75. {
  76. throw "Data would be truncated.";
  77. }
  78. }
  79. error = mysql_stmt_error(_stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-error.html
  80. if (error == nullptr) {
  81. error = "";
  82. }
  83. throw error;
  84. }
  85. unsigned long mysqli_stmt::param_count()
  86. {
  87. if (_stmt == nullptr) {
  88. mysqli::throw_null_reference_error();
  89. }
  90. return mysql_stmt_param_count(_stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-param-count.html
  91. }
  92. void mysqli_stmt::result_metadata(mysqli_result &result)
  93. {
  94. if (_stmt == nullptr) {
  95. mysqli::throw_null_reference_error();
  96. }
  97. result.free();
  98. result._result = mysql_stmt_result_metadata(_stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-result-metadata.html
  99. return;
  100. }
  101. bool mysqli_stmt::store_result()
  102. {
  103. if (_stmt == nullptr) {
  104. mysqli::throw_null_reference_error();
  105. }
  106. int status = mysql_stmt_store_result(_stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-store-result.html
  107. return status == 0;
  108. }