HorizonDatabasePlugin  0.2.0
SOCI wrapper for UE4(beta)
HorizonDatabase.h
Go to the documentation of this file.
1 /******************************************************
2 * Boost Software License - Version 1.0 - 2016/10/06
3 *
4 * Copyright (c) 2016 dorgon chang
5 * http://dorgon.horizon-studio.net/
6 *
7 * Permission is hereby granted, free of charge, to any person or organization
8 * obtaining a copy of the software and accompanying documentation covered by
9 * this license (the "Software") to use, reproduce, display, distribute,
10 * execute, and transmit the Software, and to prepare derivative works of the
11 * Software, and to permit third-parties to whom the Software is furnished to
12 * do so, all subject to the following:
13 *
14 * The copyright notices in the Software and this entire statement, including
15 * the above license grant, this restriction and the following disclaimer,
16 * must be included in all copies of the Software, in whole or in part, and
17 * all derivative works of the Software, unless such copies or derivative
18 * works are solely in the form of machine-executable object code generated by
19 * a source language processor.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 **********************************************************/
29 
30 #pragma once
31 
32 #include "Object.h"
33 #include "soci/soci.h"
34 #if (USE_SOCI_SQLITE3 == 1)
35 #include "soci/sqlite3/soci-sqlite3.h"
36 #endif //(USE_SOCI_SQLITE3 == 1)
40 
41 #include "HorizonDatabase.generated.h"
42 
43 
44 static const FString HORIZON_SQL_CREATE_TABLE_IF_NOT_EXISTS = "CREATE TABLE IF NOT EXISTS ";
45 static const FString HORIZON_SQL_DELETE_FROM = "DELETE FROM ";
46 static const FString HORIZON_SQL_TRANCATE_TABLE_IF_EXISTS = "TRUNCATE TABLE IF EXISTS ";
47 static const FString HORIZON_SQL_DROP_TABLE_IF_EXISTS = "DROP TABLE IF EXISTS ";
48 static const FString HORIZON_SQL_INSERT_INTO = "INSERT INTO ";
49 static const FString HORIZON_SQL_INSERT_OR_REPLACE_INTO = "INSERT OR REPLACE INTO ";
50 static const FString HORIZON_SQL_SELECT = "SELECT ";
51 static const FString HORIZON_SQL_FROM = " FROM ";
52 static const FString HORIZON_SQL_SELECT_ALL_FROM = "SELECT * FROM ";
53 static const FString HORIZON_SQL_UPDATE = "UPDATE ";
54 static const FString HORIZON_SQL_SET = " SET ";
55 
56 static const FString HORIZON_SQL_SELECT_COUNT_ALL_FROM = "SELECT COUNT(*) FROM ";
57 
58 
59 
60 // All table struct should inherited from this USTRUCT
61 USTRUCT()
62 struct HORIZONDATABASE_API FHorizonDatabaseTable
63 {
64  GENERATED_BODY()
65 };
66 
67 
68 
69 UCLASS(AutoExpandCategories = "HorizonPlugin|HorizonDatabase")
70 class HORIZONDATABASE_API AHorizonDatabase : public AActor
71 {
72  GENERATED_BODY()
73 public:
75 public:
76  virtual void BeginPlay() override;
77  virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
78 
79 public:
80  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HorizonPlugin|HorizonDatabase")
81  TEnumAsByte<EHorizonDatabaseBackEnd::Type> BackEndType;
82 
83 
84  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HorizonPlugin|HorizonDatabase")
85  FString ConnectString;
86 
87 
88  //Open database when BeginPlay()
89  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HorizonPlugin|HorizonDatabase")
90  bool bAutoOpen = true;
91 
92 public://For blueprint
93  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
94  virtual bool Open();
95  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
96  virtual void Close();
97  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
98  bool IsMemoryDB();
99 
100 
101  //DELETE FROM tableName condition
102  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
103  void DeleteData(const FString& tableName, const FString& condition = "");
104 
105  //TRUNCATE TABLE IF EXISTS tableName
106  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
107  void TruncateTable(const FString& tableName);
108 
109 
110 
111  //DROP TABLE IF EXISTS tableName
112  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
113  void DropTable(const FString& tableName);
114 
115  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
116  bool IsTableExists(const FString& tableName);
117 
118  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
119  void UpdateData(const FString& tableName, const FString& updateParam, const FString& condition = "");
120 
121 
122 
123  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
124  void ExecuteSQL(const FString& sqlStmt);
125 
126  UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|HorizonDatabase")
127  int GetTableRowCount(const FString& tableName, const FString& condition);
128 
129 public: //C++
130  void CreateTable(UStruct* pSchema);
131  static FString GetCreateTableSqlStmt(AHorizonDatabase* pDB, UStruct* pSchema);
132 
138  static FString GetInsertBindingSqlStmt(UStruct* pSchema, bool bReplace = true);
139 
140  static FString GetUpdateBindingSqlStmt(UStruct* pSchema);
141 public: //For C++
142 
143  template <typename S>
144  soci::rowset<S> QueryMultiData(const FString& tableName, const FString& condition)
145  {
146  FString sqlStmt = FString::Printf(TEXT("%s%s %s"), *HORIZON_SQL_SELECT_ALL_FROM, *tableName, *condition);
147  return (*SessionPtr).prepare << TCHAR_TO_UTF8(*sqlStmt);
148  }
149 
150  template <typename S>
151  soci::rowset<S> QueryMultiData(const FString& tableName, const FString& parmName, const FString& condition)
152  {
153  FString sqlStmt = FString::Printf(TEXT("%s%s%s%s %s"),
154  *HORIZON_SQL_SELECT, *parmName,
155  *HORIZON_SQL_FROM, *tableName, *condition);
156 
157  return (*SessionPtr).prepare << TCHAR_TO_UTF8(*sqlStmt);
158  }
159  template <typename S>
160  void QueryData(const FString& tableName, S &outData, const FString& condition)
161  {
162  FString sqlStmt = FString::Printf(TEXT("%s%s %s"), *HORIZON_SQL_SELECT_ALL_FROM, *tableName, *condition);
163 
164  (*SessionPtr) << TCHAR_TO_UTF8(*sqlStmt), soci::into(outData);
165  }
166  template <typename S>
167  void QueryData(const FString& tableName, const FString& parmName, S &outData, const FString& condition)
168  {
169  if (SessionPtr.IsValid())
170  {
171  try {
172  FString sqlStmt = FString::Printf(TEXT("%s%s%s%s %s"), *HORIZON_SQL_SELECT, *parmName, *HORIZON_SQL_FROM, *tableName, *condition);
173 
174  (*SessionPtr) << TCHAR_TO_UTF8(*sqlStmt), soci::into(outData);
175  }
176  catch (const std::exception& e)
177  {
178  ensureMsgf(TEXT("AHorizonDatabase::QueryData exception: %s"), *FString(e.what()));
179  throw e;
180  }
181  }
182  }
183 
184  soci::session& GetSession() {
185  verifyf(SessionPtr.IsValid(), TEXT("SessionPtr is not valid, database session not connected"));
186  return *SessionPtr;
187  }
188 
189 private:
190  static FString ConvertToDBDataType(AHorizonDatabase* pDB, const FString& typeName);
191 private:
192  TSharedPtr<soci::session> SessionPtr;
193 };
soci::session & GetSession()
Definition: HorizonDatabase.h:184
Definition: HorizonDatabase.h:70
Definition: HorizonDatabaseEnum.h:35
void QueryData(const FString &tableName, S &outData, const FString &condition)
Definition: HorizonDatabase.h:160
Definition: HorizonDatabase.h:62
Type
Definition: HorizonDatabaseEnum.h:37
Definition: HorizonTestDBTable1.h:104
soci::rowset< S > QueryMultiData(const FString &tableName, const FString &parmName, const FString &condition)
Definition: HorizonDatabase.h:151
void QueryData(const FString &tableName, const FString &parmName, S &outData, const FString &condition)
Definition: HorizonDatabase.h:167
Definition: HorizonDatabase.Build.cs:34