2
0

SQLiteObject.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //
  22. // Additional Copyrights
  23. // Copyright 2004 John Vanderbeck
  24. // Copyright 2016 Chris Calef
  25. //-----------------------------------------------------------------------------
  26. //-----------------------------------------------------------------------------
  27. // This code implements support for SQLite into Torque and TorqueScript
  28. //
  29. // Essentially this creates a scriptable object that interfaces with SQLite.
  30. //
  31. // The supported SQL subset of SQLite can be found here:
  32. // http://www.sqlite.org/lang.html
  33. //-----------------------------------------------------------------------------
  34. #ifndef _SQLITEOBJECT_H_
  35. #define _SQLITEOBJECT_H_
  36. #ifndef _SIMBASE_H_
  37. #include "console/simBase.h"
  38. #endif
  39. #include "sqlite3.h"
  40. #include "core/util/tVector.h"
  41. struct sqlite_resultrow
  42. {
  43. VectorPtr<char*> vColumnNames;
  44. VectorPtr<char*> vColumnValues;
  45. };
  46. struct sqlite_resultset
  47. {
  48. S32 iResultSet;
  49. S32 iCurrentRow;
  50. S32 iCurrentColumn;
  51. S32 iNumRows;
  52. S32 iNumCols;
  53. bool bValid;
  54. VectorPtr<sqlite_resultrow*> vRows;
  55. };
  56. class SQLiteObject : public SimObject
  57. {
  58. // This typedef is required for tie ins with the script language.
  59. //--------------------------------------------------------------------------
  60. protected:
  61. typedef SimObject Parent;
  62. //--------------------------------------------------------------------------
  63. public:
  64. SQLiteObject();
  65. ~SQLiteObject();
  66. // These are overloaded functions from SimObject that we handle for
  67. // tie in to the script language. The .cc file has more in depth
  68. // comments on these.
  69. //-----------------------------------------------------------------------
  70. bool processArguments(S32 argc, const char **argv);
  71. bool onAdd();
  72. void onRemove();
  73. static void initPersistFields();
  74. //-----------------------------------------------------------------------
  75. //-----------------------------------------------------------------------
  76. // Called to open a database using the sqlite_open() function.
  77. // If the open fails, the function returns false, and sets the
  78. // global error string. The script interface will automatically
  79. // call the onOpenFailed() script callback and pass this string
  80. // in if it fails. If it succeeds the script interface will call
  81. // the onOpened() script callback.
  82. bool OpenDatabase(const char* filename);
  83. void CloseDatabase();
  84. S32 loadOrSaveDb(const char *zFilename, bool isSave);//This code courtesy of sqlite.org.
  85. S32 ExecuteSQL(const char* sql);
  86. void NextRow(S32 resultSet);
  87. bool EndOfResult(S32 resultSet);
  88. void escapeSingleQuotes(const char* source, char *dest);
  89. // support functions
  90. void ClearErrorString();
  91. void ClearResultSet(S32 index);
  92. sqlite_resultset* GetResultSet(S32 iResultSet);
  93. bool SaveResultSet(sqlite_resultset* pResultSet);
  94. S32 GetResultSetIndex(S32 iResultSet);
  95. S32 GetColumnIndex(S32 iResult, const char* columnName);
  96. S32 numResultSets();
  97. //Prepared Statements! We need a way to make them and extend them to script.
  98. //void prepareStatement(sqlite3_stmt*,);
  99. //void finalizeStatement();
  100. //void bindInteger();
  101. //...
  102. sqlite3* m_pDatabase;
  103. private:
  104. char* m_szErrorString;
  105. VectorPtr<sqlite_resultset*> m_vResultSets;
  106. S32 m_iLastResultSet;
  107. S32 m_iNextResultSet;
  108. // This macro ties us into the script engine, and MUST MUST MUST be declared
  109. // in a public section of the class definition. If it isn't you WILL get
  110. // errors that will confuse you.
  111. //--------------------------------------------------------------------------
  112. public:
  113. DECLARE_CONOBJECT(SQLiteObject);
  114. S32 getLastRowId() { return sqlite3_last_insert_rowid(m_pDatabase); };
  115. //--------------------------------------------------------------------------
  116. };
  117. #endif // _SQLITEOBJECT_H_