mysqli_bind.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "mysqli_bind.h"
  2. #include <string.h>
  3. // Constructor
  4. mysqli_bind::mysqli_bind(int size)
  5. {
  6. _size = 0;
  7. _bind = nullptr;
  8. _is_on_stack = false;
  9. allocate(size);
  10. }
  11. mysqli_bind::mysqli_bind(MYSQL_BIND &bind,int size)
  12. {
  13. _size = size;
  14. _bind = &bind;
  15. _is_on_stack = true;
  16. allocate(size);
  17. }
  18. void mysqli_bind::allocate(int size)
  19. {
  20. if (size > 0)
  21. {
  22. _bind = new MYSQL_BIND[size];
  23. if (_bind != nullptr)
  24. {
  25. _size = size;
  26. memset(_bind,0,sizeof(MYSQL_BIND)*size);
  27. }
  28. }
  29. return;
  30. }
  31. // Destructor
  32. mysqli_bind::~mysqli_bind()
  33. {
  34. clear();
  35. }
  36. int mysqli_bind::size() const
  37. {
  38. return _size;
  39. }
  40. bool mysqli_bind::is_null(int index) const
  41. {
  42. check_index_range(index);
  43. return _bind[index].is_null_value;
  44. }
  45. bool mysqli_bind::is_error(int index) const
  46. {
  47. check_index_range(index);
  48. return _bind[index].error_value;
  49. }
  50. void mysqli_bind::clear()
  51. {
  52. if (_bind != nullptr)
  53. {
  54. if (!_is_on_stack) {
  55. delete[] _bind;
  56. }
  57. _bind = nullptr;
  58. }
  59. _size = 0;
  60. return;
  61. }
  62. void mysqli_bind::check_index_range(int index) const
  63. {
  64. if ((index < 0) || (index >= _size))
  65. {
  66. throw "Bind index out of range.";
  67. }
  68. return;
  69. }
  70. void mysqli_bind::bind(int index,short &value)
  71. {
  72. check_index_range(index);
  73. _bind[index].buffer_type= MYSQL_TYPE_SHORT;
  74. _bind[index].buffer= (char *)&value;
  75. return;
  76. }
  77. void mysqli_bind::bind(int index,int &value)
  78. {
  79. check_index_range(index);
  80. _bind[index].buffer_type= MYSQL_TYPE_LONG;
  81. _bind[index].buffer= (char *)&value;
  82. return;
  83. }
  84. void mysqli_bind::bind(int index,const char *string,int buffer_size,unsigned long &length)
  85. {
  86. check_index_range(index);
  87. _bind[index].buffer_type= MYSQL_TYPE_STRING;
  88. _bind[index].buffer= (char *)string;
  89. _bind[index].buffer_length= buffer_size;
  90. _bind[index].length= &length;
  91. return;
  92. }