33 #include "soci/soci.h" 34 #if (USE_SOCI_SQLITE3 == 1) 35 #include "soci/sqlite3/soci-sqlite3.h" 36 #endif //(USE_SOCI_SQLITE3 == 1) 41 #include "HorizonDatabase.generated.h" 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 ";
56 static const FString HORIZON_SQL_SELECT_COUNT_ALL_FROM =
"SELECT COUNT(*) FROM ";
69 UCLASS(AutoExpandCategories =
"HorizonPlugin|HorizonDatabase")
76 virtual void BeginPlay()
override;
80 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"HorizonPlugin|HorizonDatabase")
84 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HorizonPlugin|
HorizonDatabase")
85 FString ConnectString;
89 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "HorizonPlugin|
HorizonDatabase")
90 bool bAutoOpen = true;
93 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
95 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
97 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
102 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
103 void DeleteData(const FString& tableName, const FString& condition = "");
106 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
107 void TruncateTable(const FString& tableName);
112 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
113 void DropTable(const FString& tableName);
115 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
116 bool IsTableExists(const FString& tableName);
118 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
119 void UpdateData(const FString& tableName, const FString& updateParam, const FString& condition = "");
123 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
124 void ExecuteSQL(const FString& sqlStmt);
126 UFUNCTION(BlueprintCallable, Category = "HorizonPlugin|
HorizonDatabase")
127 int GetTableRowCount(const FString& tableName, const FString& condition);
130 void CreateTable(UStruct* pSchema);
131 static FString GetCreateTableSqlStmt(
AHorizonDatabase* pDB, UStruct* pSchema);
138 static FString GetInsertBindingSqlStmt(UStruct* pSchema,
bool bReplace = true);
140 static FString GetUpdateBindingSqlStmt(UStruct* pSchema);
143 template <typename S>
144 soci::rowset<S> QueryMultiData(const FString& tableName, const FString& condition)
146 FString sqlStmt = FString::Printf(TEXT(
"%s%s %s"), *HORIZON_SQL_SELECT_ALL_FROM, *tableName, *condition);
147 return (*SessionPtr).prepare << TCHAR_TO_UTF8(*sqlStmt);
150 template <
typename S>
151 soci::rowset<S>
QueryMultiData(
const FString& tableName,
const FString& parmName,
const FString& condition)
153 FString sqlStmt = FString::Printf(TEXT(
"%s%s%s%s %s"),
154 *HORIZON_SQL_SELECT, *parmName,
155 *HORIZON_SQL_FROM, *tableName, *condition);
157 return (*SessionPtr).prepare << TCHAR_TO_UTF8(*sqlStmt);
159 template <
typename S>
160 void QueryData(
const FString& tableName, S &outData,
const FString& condition)
162 FString sqlStmt = FString::Printf(TEXT(
"%s%s %s"), *HORIZON_SQL_SELECT_ALL_FROM, *tableName, *condition);
164 (*SessionPtr) << TCHAR_TO_UTF8(*sqlStmt), soci::into(outData);
166 template <
typename S>
167 void QueryData(
const FString& tableName,
const FString& parmName, S &outData,
const FString& condition)
169 if (SessionPtr.IsValid())
172 FString sqlStmt = FString::Printf(TEXT(
"%s%s%s%s %s"), *HORIZON_SQL_SELECT, *parmName, *HORIZON_SQL_FROM, *tableName, *condition);
174 (*SessionPtr) << TCHAR_TO_UTF8(*sqlStmt), soci::into(outData);
176 catch (
const std::exception& e)
178 ensureMsgf(TEXT(
"AHorizonDatabase::QueryData exception: %s"), *FString(e.what()));
185 verifyf(SessionPtr.IsValid(), TEXT(
"SessionPtr is not valid, database session not connected"));
190 static FString ConvertToDBDataType(
AHorizonDatabase* pDB,
const FString& typeName);
192 TSharedPtr<soci::session> SessionPtr;
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