DatabaseDemo.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Urho3D.h>
  23. #include <Urho3D/Core/CoreEvents.h>
  24. #include <Urho3D/Core/ProcessUtils.h>
  25. #include <Urho3D/Database/Database.h>
  26. #include <Urho3D/Database/DatabaseEvents.h>
  27. #include <Urho3D/Engine/Console.h>
  28. #include <Urho3D/Engine/Engine.h>
  29. #include <Urho3D/Engine/EngineEvents.h>
  30. #include <Urho3D/Input/Input.h>
  31. #include <Urho3D/IO/Log.h>
  32. #include <Urho3D/UI/Button.h>
  33. #include "DatabaseDemo.h"
  34. // Expands to this example's entry-point
  35. DEFINE_APPLICATION_MAIN(DatabaseDemo)
  36. DatabaseDemo::DatabaseDemo(Context* context) :
  37. Sample(context),
  38. connection_(0),
  39. row_(0)
  40. {
  41. }
  42. DatabaseDemo::~DatabaseDemo()
  43. {
  44. // Although the managed database connection will be disconnected by Database subsystem automatically in its destructor,
  45. // it is a good practice for a class to balance the number of connect() and disconnect() calls.
  46. GetSubsystem<Database>()->Disconnect(connection_);
  47. }
  48. void DatabaseDemo::Start()
  49. {
  50. // Execute base class startup
  51. Sample::Start();
  52. // Subscribe to console commands and the frame update
  53. SubscribeToEvent(E_CONSOLECOMMAND, HANDLER(DatabaseDemo, HandleConsoleCommand));
  54. SubscribeToEvent(E_UPDATE, HANDLER(DatabaseDemo, HandleUpdate));
  55. // Subscribe key down event
  56. SubscribeToEvent(E_KEYDOWN, HANDLER(DatabaseDemo, HandleEscKeyDown));
  57. // Hide logo to make room for the console
  58. SetLogoVisible(false);
  59. // Show the console by default, make it large. Console will show the text edit field when there is at least one
  60. // subscriber for the console command event
  61. Console* console = GetSubsystem<Console>();
  62. console->SetNumRows((unsigned)(GetSubsystem<Graphics>()->GetHeight() / 16));
  63. console->SetNumBufferedRows(2 * console->GetNumRows());
  64. console->SetCommandInterpreter(GetTypeName());
  65. console->SetVisible(true);
  66. console->GetCloseButton()->SetVisible(false);
  67. // Show OS mouse cursor
  68. GetSubsystem<Input>()->SetMouseVisible(true);
  69. // Open the operating system console window (for stdin / stdout) if not open yet
  70. OpenConsoleWindow();
  71. // Connect to a temporary in-memory database
  72. Database* database = GetSubsystem<Database>();
  73. database->SetUsePooling(false);
  74. connection_ = database->Connect("file://");
  75. // Subscribe to database cursor event to loop through query resultset
  76. SubscribeToEvent(connection_, E_DBCURSOR, HANDLER(DatabaseDemo, HandleDbCursor));
  77. // Show instruction
  78. Print("This demo connects to temporary in-memory database.\n"
  79. "All the tables and their data will be lost after exiting the demo.\n"
  80. "Enter a valid SQL statement in the console input and press Enter to execute.\n"
  81. "For example:\n ");
  82. HandleInput("create table tbl1(col1 varchar(10), col2 smallint)");
  83. HandleInput("insert into tbl1 values('Hello', 10)");
  84. HandleInput("insert into tbl1 values('World', 20)");
  85. HandleInput("select * from tbl1");
  86. }
  87. void DatabaseDemo::HandleConsoleCommand(StringHash eventType, VariantMap& eventData)
  88. {
  89. using namespace ConsoleCommand;
  90. if (eventData[P_ID].GetString() == GetTypeName())
  91. HandleInput(eventData[P_COMMAND].GetString());
  92. }
  93. void DatabaseDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
  94. {
  95. // Check if there is input from stdin
  96. String input = GetConsoleInput();
  97. if (input.Length())
  98. HandleInput(input);
  99. }
  100. void DatabaseDemo::HandleEscKeyDown(StringHash eventType, VariantMap& eventData)
  101. {
  102. // Unlike the other samples, exiting the engine when ESC is pressed instead of just closing the console
  103. if (eventData[KeyDown::P_KEY].GetInt() == KEY_ESC)
  104. engine_->Exit();
  105. }
  106. void DatabaseDemo::HandleDbCursor(StringHash eventType, VariantMap& eventData)
  107. {
  108. ++row_;
  109. using namespace DbCursor;
  110. // In a real application the P_SQL can be used to do the logic branching in a shared event handler
  111. // However, this is not required in this sample demo
  112. int numCols = eventData[P_NUMCOLS].GetInt();
  113. const char** colValues = static_cast<const char**>(eventData[P_COLVALUES].GetVoidPtr());
  114. const char** colHeaders = static_cast<const char**>(eventData[P_COLHEADERS].GetVoidPtr());
  115. for (int i = 0; i < numCols; ++i)
  116. Print(ToString("Row #%d: %s = %s", row_, colHeaders[i], colValues[i]));
  117. }
  118. void DatabaseDemo::HandleInput(const String& input)
  119. {
  120. // Echo input string to stdout
  121. Print(input);
  122. row_ = 0;
  123. if (input == "quit" || input == "exit")
  124. engine_->Exit();
  125. else
  126. connection_->Execute(input, true);
  127. Print(" ");
  128. }
  129. void DatabaseDemo::Print(const String& output)
  130. {
  131. // Logging appears both in the engine console and stdout
  132. LOGRAW(output + "\n");
  133. }