SQLite3ClientPlugin.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. /// \brief Contains code to call sqlite3_exec over a network that does not support shared file handles.
  12. ///
  13. #ifndef ___SQLITE_3_CLIENT_PLUGIN_H
  14. #define ___SQLITE_3_CLIENT_PLUGIN_H
  15. #include "RakNetTypes.h"
  16. #include "Export.h"
  17. #include "PluginInterface2.h"
  18. #include "PacketPriority.h"
  19. #include "SocketIncludes.h"
  20. #include "DS_Multilist.h"
  21. #include "RakString.h"
  22. #include "SQLite3PluginCommon.h"
  23. class RakPeerInterface;
  24. namespace RakNet
  25. {
  26. /// \brief Handles results of calls to SQLite3Plugin::_sqlite3_exec()
  27. /// Results from calling SQLite3Plugin::_sqlite3_exec() are handled in this callback.
  28. /// You should implement the callback, and let the plugin know about it via SQLite3Plugin::AddResultHandler()
  29. /// Be sure to call SQLite3Plugin::RemoveResultHandler() or SQLite3Plugin::ClearResultHandlers() if you delete the callback
  30. /// \ingroup SQL_LITE_3_PLUGIN
  31. class SQLite3PluginResultInterface
  32. {
  33. public:
  34. /// Query executed, possibly returning data or an error message
  35. ///
  36. /// \param[out] inputStatement Passed to SQLite3Plugin::_sqlite3_exec
  37. /// \param[out] queryId Returned from SQLite3Plugin::_sqlite3_exec
  38. /// \param[out] dbIdentifier Passed to SQLite3Plugin::_sqlite3_exec
  39. /// \param[out] table Result of call to _sqlite3_exec, should that statement return a result
  40. /// \param[out] errorMsg If _sqlite3_exec failed, then the error message is here, and table will be empty
  41. /// \ingroup SQL_LITE_3_PLUGIN
  42. virtual void _sqlite3_exec(
  43. RakNet::RakString inputStatement,
  44. unsigned int queryId,
  45. RakNet::RakString dbIdentifier,
  46. const SQLite3Table &table,
  47. RakNet::RakString errorMsg)=0;
  48. /// dbIdentifier is unknown on the remote system
  49. ///
  50. /// \param[out] inputStatement Passed to SQLite3Plugin::_sqlite3_exec
  51. /// \param[out] queryId Returned from SQLite3Plugin::_sqlite3_exec
  52. /// \param[out] dbIdentifier Passed to SQLite3Plugin::_sqlite3_exec
  53. /// \ingroup SQL_LITE_3_PLUGIN
  54. virtual void OnUnknownDBIdentifier(
  55. RakNet::RakString inputStatement,
  56. unsigned int queryId,
  57. RakNet::RakString dbIdentifier)=0;
  58. };
  59. /// Sample callback implementation that just prints to the screen the results
  60. /// \ingroup SQL_LITE_3_PLUGIN
  61. class SQLite3PluginResultInterface_Printf : public SQLite3PluginResultInterface
  62. {
  63. virtual void _sqlite3_exec(
  64. RakNet::RakString inputStatement,
  65. unsigned int queryId,
  66. RakNet::RakString dbIdentifier,
  67. const SQLite3Table &table,
  68. RakNet::RakString errorMsg);
  69. virtual void OnUnknownDBIdentifier(
  70. RakNet::RakString inputStatement,
  71. unsigned int queryId,
  72. RakNet::RakString dbIdentifier);
  73. };
  74. /// SQLite version 3 supports remote calls via networked file handles, but not over the regular internet
  75. /// This plugin will serialize calls to and results from sqlite3_exec
  76. /// That's all it does - any remote system can execute SQL queries.
  77. /// Intended as a starting platform to derive from for more advanced functionality (security over who can query, etc).
  78. /// Compatible as a plugin with both RakPeerInterface and PacketizedTCP
  79. /// \ingroup SQL_LITE_3_PLUGIN
  80. class RAK_DLL_EXPORT SQLite3ClientPlugin : public PluginInterface2
  81. {
  82. public:
  83. SQLite3ClientPlugin();
  84. virtual ~SQLite3ClientPlugin();
  85. /// Add an interface to get callbacks for results
  86. /// Up to user to make sure the pointer is valid through the lifetime of use
  87. void AddResultHandler(SQLite3PluginResultInterface *res);
  88. void RemoveResultHandler(SQLite3PluginResultInterface *res);
  89. void ClearResultHandlers(void);
  90. /// Execute a statement on the remote system
  91. /// \note Don't forget to escape your input strings. RakString::SQLEscape() is available for this.
  92. /// \param[in] dbIdentifier Which database to use, added with AddDBHandle()
  93. /// \param[in] inputStatement SQL statement to execute
  94. /// \param[in] priority See RakPeerInterface::Send()
  95. /// \param[in] reliability See RakPeerInterface::Send()
  96. /// \param[in] orderingChannel See RakPeerInterface::Send()
  97. /// \param[in] systemAddress See RakPeerInterface::Send()
  98. /// \return Query ID. Will be returned in _sqlite3_exec
  99. unsigned int _sqlite3_exec(RakNet::RakString dbIdentifier, RakNet::RakString inputStatement,
  100. PacketPriority priority, PacketReliability reliability, char orderingChannel, const SystemAddress &systemAddress);
  101. /// \internal For plugin handling
  102. virtual PluginReceiveResult OnReceive(Packet *packet);
  103. // List of result handlers added with AddResultHandler()
  104. DataStructures::List<SQLite3PluginResultInterface *> resultHandlers;
  105. // Each query returns a numeric id if you want it. This tracks what id to assign next. Increments sequentially.
  106. unsigned int nextQueryId;
  107. };
  108. }
  109. #endif
粤ICP备19079148号