Manual_Database.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 CScriptArray* DbResultGetColumns(DbResult* ptr)
  45. {
  46. return VectorToArray<String>(ptr->GetColumns(), "Array<String>");
  47. }
  48. static CScriptArray* DbResultGetRow(unsigned index, DbResult* ptr)
  49. {
  50. const Vector<VariantVector>& rows = ptr->GetRows();
  51. if (index >= rows.Size())
  52. {
  53. asGetActiveContext()->SetException("Index out of bounds");
  54. return nullptr;
  55. }
  56. else
  57. return VectorToArray<Variant>(rows[index], "Array<Variant>");
  58. }
  59. static void RegisterDbResult(asIScriptEngine* engine)
  60. {
  61. engine->RegisterObjectBehaviour("DbResult", asBEHAVE_CONSTRUCT, "void f()", AS_FUNCTION_OBJLAST(ConstructDbResult), AS_CALL_CDECL_OBJLAST);
  62. engine->RegisterObjectBehaviour("DbResult", asBEHAVE_CONSTRUCT, "void f(const DbResult&in)", AS_FUNCTION_OBJLAST(ConstructDbResultCopy), AS_CALL_CDECL_OBJLAST);
  63. engine->RegisterObjectBehaviour("DbResult", asBEHAVE_DESTRUCT, "void f()", AS_DESTRUCTOR(DbResult), AS_CALL_CDECL_OBJLAST);
  64. engine->RegisterObjectMethod("DbResult", "DbResult& opAssign(const DbResult&in)", AS_METHODPR(DbResult, operator =, (const DbResult&), DbResult&), AS_CALL_THISCALL);
  65. engine->RegisterObjectMethod("DbResult", "uint get_numColumns() const", AS_METHOD(DbResult, GetNumColumns), AS_CALL_THISCALL);
  66. engine->RegisterObjectMethod("DbResult", "uint get_numRows() const", AS_METHOD(DbResult, GetNumRows), AS_CALL_THISCALL);
  67. engine->RegisterObjectMethod("DbResult", "int64 get_numAffectedRows() const", AS_METHOD(DbResult, GetNumAffectedRows), AS_CALL_THISCALL);
  68. engine->RegisterObjectMethod("DbResult", "Array<String>@ get_columns() const", AS_FUNCTION_OBJLAST(DbResultGetColumns), AS_CALL_CDECL_OBJLAST);
  69. engine->RegisterObjectMethod("DbResult", "Array<Variant>@ get_row(uint) const", AS_FUNCTION_OBJLAST(DbResultGetRow), AS_CALL_CDECL_OBJLAST);
  70. }
  71. // ========================================================================================
  72. static void RegisterDbConnection(asIScriptEngine* engine)
  73. {
  74. RegisterObject<DbConnection>(engine, "DbConnection");
  75. engine->RegisterObjectMethod("DbConnection", "DbResult Execute(const String&in, bool useCursorEvent = false)", AS_METHOD(DbConnection, Execute), AS_CALL_THISCALL);
  76. engine->RegisterObjectMethod("DbConnection", "const String& get_connectionString() const", AS_METHOD(DbConnection, GetConnectionString), AS_CALL_THISCALL);
  77. engine->RegisterObjectMethod("DbConnection", "bool get_connected() const", AS_METHOD(DbConnection, IsConnected), AS_CALL_THISCALL);
  78. }
  79. // ========================================================================================
  80. static Database* GetDatabase()
  81. {
  82. return GetScriptContext()->GetSubsystem<Database>();
  83. }
  84. static DBAPI GetDBAPI()
  85. {
  86. return Database::GetAPI();
  87. }
  88. // This function is called after ASRegisterGenerated()
  89. void ASRegisterManualLast_Database(asIScriptEngine* engine)
  90. {
  91. RegisterDbResult(engine);
  92. RegisterDbConnection(engine);
  93. engine->RegisterGlobalFunction("Database@+ get_database()", AS_FUNCTION(GetDatabase), AS_CALL_CDECL);
  94. engine->RegisterGlobalFunction("DBAPI get_DBAPI()", AS_FUNCTION(GetDBAPI), AS_CALL_CDECL);
  95. }
  96. }
  97. #endif // def URHO3D_DATABASE