ECDatabaseUtils.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef ECDATABASEUTILS_H
  18. #define ECDATABASEUTILS_H
  19. #include <kopano/zcdefs.h>
  20. #include "ECMAPI.h"
  21. #include "kcore.hpp"
  22. #include <kopano/kcodes.h>
  23. #include "ECDatabase.h"
  24. #include "ECDatabaseFactory.h"
  25. #include <kopano/ECLogger.h>
  26. #include <string>
  27. struct propVal;
  28. struct soap;
  29. namespace KC {
  30. #define MAX_PROP_SIZE 8192
  31. #define MAX_QUERY 4096
  32. #define PROPCOL_ULONG "val_ulong"
  33. #define PROPCOL_STRING "val_string"
  34. #define PROPCOL_BINARY "val_binary"
  35. #define PROPCOL_DOUBLE "val_double"
  36. #define PROPCOL_LONGINT "val_longint"
  37. #define PROPCOL_HI "val_hi"
  38. #define PROPCOL_LO "val_lo"
  39. #define _PROPCOL_ULONG(_tab) #_tab "." PROPCOL_ULONG
  40. #define _PROPCOL_STRING(_tab) #_tab "." PROPCOL_STRING
  41. #define _PROPCOL_BINARY(_tab) #_tab "." PROPCOL_BINARY
  42. #define _PROPCOL_DOUBLE(_tab) #_tab "." PROPCOL_DOUBLE
  43. #define _PROPCOL_LONGINT(_tab) #_tab "." PROPCOL_LONGINT
  44. #define _PROPCOL_HI(_tab) #_tab "." PROPCOL_HI
  45. #define _PROPCOL_LO(_tab) #_tab "." PROPCOL_LO
  46. #define PROPCOL_HILO PROPCOL_HI "," PROPCOL_LO
  47. #define _PROPCOL_HILO(_tab) PROPCOL_HI(_tab) "," PROPCOL_LO(_tab)
  48. /* make string of define value */
  49. #ifndef __STRING
  50. #define __STRING(x) #x
  51. #endif
  52. #define STR(macro) __STRING(macro)
  53. // Warning! Code references the ordering of these values! Do not change unless you know what you're doing!
  54. #define PROPCOLVALUEORDER(_tab) _PROPCOL_ULONG(_tab) "," _PROPCOL_STRING(_tab) "," _PROPCOL_BINARY(_tab) "," _PROPCOL_DOUBLE(_tab) "," _PROPCOL_LONGINT(_tab) "," _PROPCOL_HI(_tab) "," _PROPCOL_LO(_tab)
  55. #define PROPCOLVALUEORDER_TRUNCATED(_tab) _PROPCOL_ULONG(_tab) ", LEFT(" _PROPCOL_STRING(_tab) "," STR(TABLE_CAP_STRING) "),LEFT(" _PROPCOL_BINARY(_tab) "," STR(TABLE_CAP_BINARY) ")," _PROPCOL_DOUBLE(_tab) "," _PROPCOL_LONGINT(_tab) "," _PROPCOL_HI(_tab) "," _PROPCOL_LO(_tab)
  56. enum { VALUE_NR_ULONG=0, VALUE_NR_STRING, VALUE_NR_BINARY, VALUE_NR_DOUBLE, VALUE_NR_LONGINT, VALUE_NR_HILO, VALUE_NR_MAX };
  57. #define PROPCOLORDER "0,properties.tag,properties.type," PROPCOLVALUEORDER(properties)
  58. #define PROPCOLORDER_TRUNCATED "0,properties.tag,properties.type," PROPCOLVALUEORDER_TRUNCATED(properties)
  59. #define MVPROPCOLORDER "count(*),mvproperties.tag,mvproperties.type,group_concat(length(mvproperties.val_ulong),':', mvproperties.val_ulong ORDER BY mvproperties.orderid SEPARATOR ''), group_concat(length(mvproperties.val_string),':', mvproperties.val_string ORDER BY mvproperties.orderid SEPARATOR ''), group_concat(length(mvproperties.val_binary),':', mvproperties.val_binary ORDER BY mvproperties.orderid SEPARATOR ''), group_concat(length(mvproperties.val_double),':', mvproperties.val_double ORDER BY mvproperties.orderid SEPARATOR ''), group_concat(length(mvproperties.val_longint),':', mvproperties.val_longint ORDER BY mvproperties.orderid SEPARATOR ''), group_concat(length(mvproperties.val_hi),':', mvproperties.val_hi ORDER BY mvproperties.orderid SEPARATOR ''), group_concat(length(mvproperties.val_lo),':', mvproperties.val_lo ORDER BY mvproperties.orderid SEPARATOR '')"
  60. #define MVIPROPCOLORDER "0,mvproperties.tag,mvproperties.type | 0x2000," PROPCOLVALUEORDER(mvproperties)
  61. #define MVIPROPCOLORDER_TRUNCATED "0,mvproperties.tag,mvproperties.type | 0x2000," PROPCOLVALUEORDER_TRUNCATED(mvproperties)
  62. enum { FIELD_NR_ID=0, FIELD_NR_TAG, FIELD_NR_TYPE, FIELD_NR_ULONG, FIELD_NR_STRING, FIELD_NR_BINARY, FIELD_NR_DOUBLE, FIELD_NR_LONGINT, FIELD_NR_HI, FIELD_NR_LO, FIELD_NR_MAX };
  63. ULONG GetColOffset(ULONG ulPropTag);
  64. std::string GetPropColOrder(unsigned int ulPropTag, const std::string &strSubQuery);
  65. unsigned int GetColWidth(unsigned int ulPropType);
  66. ECRESULT GetPropSize(DB_ROW lpRow, DB_LENGTHS lpLen, unsigned int *lpulSize);
  67. ECRESULT CopySOAPPropValToDatabasePropVal(struct propVal *lpPropVal, unsigned int *ulColId, std::string &strColData, ECDatabase *lpMySQL, bool bTruncate);
  68. ECRESULT CopyDatabasePropValToSOAPPropVal(struct soap *soap, DB_ROW lpRow, DB_LENGTHS lpLen, propVal *lpPropVal);
  69. gsoap_size_t GetMVItemCount(struct propVal *lpPropVal);
  70. ECRESULT CopySOAPPropValToDatabaseMVPropVal(struct propVal *lpPropVal, int nItem, std::string &strColName, std::string &strColData, ECDatabase *lpDatabase);
  71. ECRESULT ParseMVProp(const char *lpRowData, ULONG ulSize, unsigned int *lpulLastPos, std::string *lpstrData);
  72. unsigned int NormalizeDBPropTag(unsigned int ulPropTag);
  73. bool CompareDBPropTag(unsigned int ulPropTag1, unsigned int ulPropTag2);
  74. ECRESULT GetDatabaseSettingAsInteger(ECDatabase *lpDatabase, const std::string &strSettings, unsigned int *lpulResult);
  75. ECRESULT SetDatabaseSetting(ECDatabase *lpDatabase, const std::string &strSettings, unsigned int ulValue);
  76. /**
  77. * This class is used to suppress the lock-error logging for the database passed to its
  78. * constructor during the lifetime of the instance.
  79. * This means the lock-error logging is restored when the scope in which an instance of
  80. * this class exists is exited.
  81. */
  82. class SuppressLockErrorLogging _kc_final {
  83. public:
  84. SuppressLockErrorLogging(ECDatabase *lpDatabase);
  85. ~SuppressLockErrorLogging();
  86. private:
  87. ECDatabase *m_lpDatabase;
  88. bool m_bResetValue;
  89. SuppressLockErrorLogging(const SuppressLockErrorLogging &) = delete;
  90. SuppressLockErrorLogging &operator=(const SuppressLockErrorLogging &) = delete;
  91. };
  92. /**
  93. * This macro allows anyone to create a temporary scope in which the lock-errors
  94. * for a database connection are suppressed.
  95. *
  96. * Simple usage:
  97. * WITH_SUPPRESSED_LOGGING(lpDatabase)
  98. * do_something_with(lpDatabase);
  99. *
  100. * or when more stuff needs to be done with the suppression enabled:
  101. * WITH_SUPPRESSED_LOGGING(lpDatabase) {
  102. * do_something_with(lpDatabase);
  103. * do_something_else_with(lpDatabase);
  104. * }
  105. */
  106. #define WITH_SUPPRESSED_LOGGING(_db) \
  107. for (SuppressLockErrorLogging suppressor(_db), *ptr = NULL; ptr == NULL; ptr = &suppressor)
  108. } /* namespace */
  109. #endif // ECDATABASEUTILS_H