SQLiteObject.h
Engine/source/sqlite/SQLiteObject.h
Classes:
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22// 23// Additional Copyrights 24// Copyright 2004 John Vanderbeck 25// Copyright 2016 Chris Calef 26//----------------------------------------------------------------------------- 27 28//----------------------------------------------------------------------------- 29// This code implements support for SQLite into Torque and TorqueScript 30// 31// Essentially this creates a scriptable object that interfaces with SQLite. 32// 33// The supported SQL subset of SQLite can be found here: 34// http://www.sqlite.org/lang.html 35//----------------------------------------------------------------------------- 36 37#ifndef _SQLITEOBJECT_H_ 38#define _SQLITEOBJECT_H_ 39 40#ifndef _SIMBASE_H_ 41#include "console/simBase.h" 42#endif 43 44#include "sqlite3.h" 45#include "core/util/tVector.h" 46 47struct sqlite_resultrow 48{ 49 VectorPtr<char*> vColumnNames; 50 VectorPtr<char*> vColumnValues; 51}; 52 53struct sqlite_resultset 54{ 55 S32 iResultSet; 56 S32 iCurrentRow; 57 S32 iCurrentColumn; 58 S32 iNumRows; 59 S32 iNumCols; 60 bool bValid; 61 VectorPtr<sqlite_resultrow*> vRows; 62}; 63 64 65class SQLiteObject : public SimObject 66{ 67 // This typedef is required for tie ins with the script language. 68 //-------------------------------------------------------------------------- 69 protected: 70 typedef SimObject Parent; 71 //-------------------------------------------------------------------------- 72 73 public: 74 SQLiteObject(); 75 ~SQLiteObject(); 76 77 // These are overloaded functions from SimObject that we handle for 78 // tie in to the script language. The .cc file has more in depth 79 // comments on these. 80 //----------------------------------------------------------------------- 81 bool processArguments(S32 argc, const char **argv); 82 bool onAdd(); 83 void onRemove(); 84 static void initPersistFields(); 85 //----------------------------------------------------------------------- 86 87 //----------------------------------------------------------------------- 88 // Called to open a database using the sqlite_open() function. 89 // If the open fails, the function returns false, and sets the 90 // global error string. The script interface will automatically 91 // call the onOpenFailed() script callback and pass this string 92 // in if it fails. If it succeeds the script interface will call 93 // the onOpened() script callback. 94 bool OpenDatabase(const char* filename); 95 void CloseDatabase(); 96 S32 loadOrSaveDb(const char *zFilename, bool isSave);//This code courtesy of sqlite.org. 97 S32 ExecuteSQL(const char* sql); 98 void NextRow(S32 resultSet); 99 bool EndOfResult(S32 resultSet); 100 void escapeSingleQuotes(const char* source, char *dest); 101 102 // support functions 103 void ClearErrorString(); 104 void ClearResultSet(S32 index); 105 sqlite_resultset* GetResultSet(S32 iResultSet); 106 bool SaveResultSet(sqlite_resultset* pResultSet); 107 S32 GetResultSetIndex(S32 iResultSet); 108 S32 GetColumnIndex(S32 iResult, const char* columnName); 109 S32 numResultSets(); 110 111 //Prepared Statements! We need a way to make them and extend them to script. 112 //void prepareStatement(sqlite3_stmt*,); 113 //void finalizeStatement(); 114 //void bindInteger(); 115 //... 116 117 sqlite3* m_pDatabase; 118 private: 119 char* m_szErrorString; 120 VectorPtr<sqlite_resultset*> m_vResultSets; 121 S32 m_iLastResultSet; 122 S32 m_iNextResultSet; 123 124 125 // This macro ties us into the script engine, and MUST MUST MUST be declared 126 // in a public section of the class definition. If it isn't you WILL get 127 // errors that will confuse you. 128 //-------------------------------------------------------------------------- 129 public: 130 DECLARE_CONOBJECT(SQLiteObject); 131 S32 getLastRowId() { return sqlite3_last_insert_rowid(m_pDatabase); }; 132 //-------------------------------------------------------------------------- 133}; 134 135#endif // _SQLITEOBJECT_H_ 136