mysqli.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "mysqli.h"
  2. #include <stdlib.h>
  3. mysqli::mysqli(const char *host,const char *username,const char *password,const char *db,int port)
  4. {
  5. _mysql = nullptr;
  6. real_connect(host,username,password,db,port);
  7. }
  8. mysqli::mysqli()
  9. {
  10. _mysql = nullptr;
  11. }
  12. mysqli::~mysqli()
  13. {
  14. close();
  15. }
  16. void mysqli::real_connect(const char *host,const char *username,const char *password,const char *db,int port)
  17. {
  18. if (_mysql == nullptr) {
  19. _mysql = new MYSQL();
  20. if (_mysql == nullptr) {
  21. mysqli::throw_out_of_memory_error();
  22. }
  23. }
  24. mysql_init(_mysql);
  25. // https://dev.mysql.com/doc/refman/5.7/en/mysql-real-connect.html
  26. if (nullptr == mysql_real_connect(_mysql, host, username, password, db, port, nullptr, CLIENT_COMPRESS | CLIENT_MULTI_RESULTS)) {
  27. throw mysql_error(_mysql);
  28. }
  29. return;
  30. }
  31. void mysqli::close()
  32. {
  33. if (_mysql != nullptr) {
  34. mysql_close(_mysql);
  35. delete _mysql;
  36. _mysql = nullptr;
  37. }
  38. return;
  39. }
  40. const char *mysqli::error()
  41. {
  42. if (_mysql == nullptr) {
  43. return "";
  44. }
  45. return mysql_error(_mysql); // https://dev.mysql.com/doc/refman/5.7/en/mysql-error.html
  46. }
  47. void mysqli::prepare(const char *query,unsigned long length,mysqli_stmt &stmt)
  48. {
  49. stmt.close();
  50. stmt._stmt = mysql_stmt_init(_mysql); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-init.html
  51. if (stmt._stmt == nullptr) {
  52. mysqli::throw_null_reference_error();
  53. }
  54. if (0!= mysql_stmt_prepare(stmt._stmt, query, length)) { // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-prepare.html
  55. throw mysql_stmt_error(stmt._stmt); // https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-error.html
  56. }
  57. return;
  58. }
  59. void mysqli::throw_null_reference_error()
  60. {
  61. // Save on memory by calling from one place.
  62. throw "Null reference error";
  63. }
  64. void mysqli::throw_out_of_memory_error()
  65. {
  66. throw "Out of memory.";
  67. }
  68. unsigned int mysqli::field_count()
  69. {
  70. if (_mysql == nullptr) {
  71. mysqli::throw_null_reference_error();
  72. }
  73. return mysql_field_count(_mysql); // https://dev.mysql.com/doc/refman/5.7/en/mysql-field-count.html
  74. }
  75. unsigned int mysqli::errno()
  76. {
  77. if (_mysql == nullptr) {
  78. mysqli::throw_null_reference_error();
  79. }
  80. return mysql_errno(_mysql);
  81. }
  82. bool mysqli::next_result()
  83. {
  84. // Returns true if there's a result. Throws an error if there's an error.
  85. if (_mysql == nullptr) {
  86. mysqli::throw_null_reference_error();
  87. }
  88. int status = mysql_next_result(_mysql); // https://dev.mysql.com/doc/refman/5.7/en/mysql-next-result.html
  89. if (status == 0) {
  90. return true; // There's another result set.
  91. }
  92. if (status == -1) {
  93. return false; // There are no other result sets.
  94. }
  95. // There was an error.
  96. const char *error = mysql_error(_mysql); // https://dev.mysql.com/doc/refman/5.7/en/mysql-error.html
  97. if (error != nullptr) {
  98. throw error; // Throw the error.
  99. }
  100. return false; // This probably will never happen.
  101. }
  102. bool mysqli::more_results()
  103. {
  104. if (_mysql == nullptr) {
  105. mysqli::throw_null_reference_error();
  106. }
  107. return mysql_more_results(_mysql); // https://dev.mysql.com/doc/refman/5.7/en/mysql-more-results.html
  108. }
  109. const char *mysqli::host_info()
  110. {
  111. if (_mysql == nullptr) {
  112. mysqli::throw_null_reference_error();
  113. }
  114. return mysql_get_host_info(_mysql); // https://dev.mysql.com/doc/refman/5.7/en/mysql-get-host-info.html
  115. }
  116. void mysqli::use_result(mysqli_result &result)
  117. {
  118. result.free();
  119. if (_mysql == nullptr) {
  120. mysqli::throw_null_reference_error();
  121. }
  122. result._result = mysql_use_result(_mysql); // https://dev.mysql.com/doc/refman/5.7/en/mysql-use-result.html
  123. return;
  124. }