Manual_Database.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Copyright (c) 2008-2020 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. #ifdef URHO3D_DATABASE
  23. #include "../Precompiled.h"
  24. #include "../AngelScript/APITemplates.h"
  25. #include "../Database/Database.h"
  26. #include "../AngelScript/Generated_Templates.h"
  27. namespace Urho3D
  28. {
  29. // This function is called before ASRegisterGenerated()
  30. void ASRegisterManualFirst_Database(asIScriptEngine* engine)
  31. {
  32. engine->RegisterObjectType("DbConnection", 0, asOBJ_REF);
  33. engine->RegisterObjectType("DbResult", sizeof(DbResult), asOBJ_VALUE | asOBJ_APP_CLASS_C);
  34. }
  35. // ========================================================================================
  36. static void ConstructDbResult(DbResult* ptr)
  37. {
  38. new(ptr) DbResult();
  39. }
  40. static void ConstructDbResultCopy(const DbResult& other, DbResult* ptr)
  41. {
  42. new(ptr) DbResult(other);
  43. }
  44. static void DestructDbResult(DbResult* ptr)
  45. {
  46. ptr->~DbResult();
  47. }
  48. static CScriptArray* DbResultGetColumns(DbResult* ptr)
  49. {
  50. return VectorToArray<String>(ptr->GetColumns(), "Array<String>");
  51. }
  52. static CScriptArray* DbResultGetRow(unsigned index, DbResult* ptr)
  53. {
  54. const Vector<VariantVector>& rows = ptr->GetRows();
  55. if (index >= rows.Size())
  56. {
  57. asGetActiveContext()->SetException("Index out of bounds");
  58. return nullptr;
  59. }
  60. else
  61. return VectorToArray<Variant>(rows[index], "Array<Variant>");
  62. }
  63. static void RegisterDbResult(asIScriptEngine* engine)
  64. {
  65. engine->RegisterObjectBehaviour("DbResult", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructDbResult), asCALL_CDECL_OBJLAST);
  66. engine->RegisterObjectBehaviour("DbResult", asBEHAVE_CONSTRUCT, "void f(const DbResult&in)", asFUNCTION(ConstructDbResultCopy), asCALL_CDECL_OBJLAST);
  67. engine->RegisterObjectBehaviour("DbResult", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructDbResult), asCALL_CDECL_OBJLAST);
  68. engine->RegisterObjectMethod("DbResult", "DbResult& opAssign(const DbResult&in)", asMETHODPR(DbResult, operator =, (const DbResult&), DbResult&), asCALL_THISCALL);
  69. engine->RegisterObjectMethod("DbResult", "uint get_numColumns() const", asMETHOD(DbResult, GetNumColumns), asCALL_THISCALL);
  70. engine->RegisterObjectMethod("DbResult", "uint get_numRows() const", asMETHOD(DbResult, GetNumRows), asCALL_THISCALL);
  71. engine->RegisterObjectMethod("DbResult", "int64 get_numAffectedRows() const", asMETHOD(DbResult, GetNumAffectedRows), asCALL_THISCALL);
  72. engine->RegisterObjectMethod("DbResult", "Array<String>@ get_columns() const", asFUNCTION(DbResultGetColumns), asCALL_CDECL_OBJLAST);
  73. engine->RegisterObjectMethod("DbResult", "Array<Variant>@ get_row(uint) const", asFUNCTION(DbResultGetRow), asCALL_CDECL_OBJLAST);
  74. }
  75. // ========================================================================================
  76. static void RegisterDbConnection(asIScriptEngine* engine)
  77. {
  78. RegisterObject<DbConnection>(engine, "DbConnection");
  79. engine->RegisterObjectMethod("DbConnection", "DbResult Execute(const String&in, bool useCursorEvent = false)", asMETHOD(DbConnection, Execute), asCALL_THISCALL);
  80. engine->RegisterObjectMethod("DbConnection", "const String& get_connectionString() const", asMETHOD(DbConnection, GetConnectionString), asCALL_THISCALL);
  81. engine->RegisterObjectMethod("DbConnection", "bool get_connected() const", asMETHOD(DbConnection, IsConnected), asCALL_THISCALL);
  82. }
  83. // ========================================================================================
  84. static Database* GetDatabase()
  85. {
  86. return GetScriptContext()->GetSubsystem<Database>();
  87. }
  88. static DBAPI GetDBAPI()
  89. {
  90. return Database::GetAPI();
  91. }
  92. // This function is called after ASRegisterGenerated()
  93. void ASRegisterManualLast_Database(asIScriptEngine* engine)
  94. {
  95. RegisterDbResult(engine);
  96. RegisterDbConnection(engine);
  97. engine->RegisterGlobalFunction("Database@+ get_database()", asFUNCTION(GetDatabase), asCALL_CDECL);
  98. engine->RegisterGlobalFunction("DBAPI get_DBAPI()", asFUNCTION(GetDBAPI), asCALL_CDECL);
  99. }
  100. }
  101. #endif // def URHO3D_DATABASE