Skip to content
This repository was archived by the owner on Jun 3, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ Saved/
SQLite.sln
temppi/
Build/
/SQLStaticLib/Release
/SQLStaticLib/SQLStaticLib/Release
/SQLStaticLib/SQLStaticLib/x64
/SQLStaticLib/SQLStaticLib/Debug
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "Plugins/VaRest"]
path = Plugins/VaRest
url = https://github.com/KhArtNJava/VaRest.git
3 changes: 3 additions & 0 deletions Config/DefaultEngine.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ AppliedTargetedHardwareClass=Desktop
DefaultGraphicsPerformance=Maximum
AppliedDefaultGraphicsPerformance=Maximum

[/Script/EngineSettings.GameMapsSettings]
GameDefaultMap=/Game/DatabaseTestLevel
EditorStartupMap=/Game/DatabaseTestLevel

Binary file modified Content/Blueprints/BP_UnrealChair.uasset
Binary file not shown.
Binary file modified Content/DatabaseTestLevel.umap
Binary file not shown.
Binary file added Content/DatabaseTestLevelWidget.uasset
Binary file not shown.
Binary file modified Content/Databases/TestDatabase.db
Binary file not shown.
Binary file added Content/Databases/TestDatabase_NEW.db
Binary file not shown.
Binary file added Content/NewWidgetBlueprint.uasset
Binary file not shown.
Binary file added DerivedDataCache/Boot.ddc
Binary file not shown.
Binary file removed Docs/CreateTableExample.png
Binary file not shown.
Binary file removed Docs/CreateTableExample2.png
Binary file not shown.
Binary file added Docs/CreateTableExample3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,93 +1,74 @@

#pragma once
#include "SQLiteDatabase.h"
#include "SQLiteBlueprintNodes.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "SQLiteBlueprintFunctionLibrary.generated.h"


/**
* Blueprint function library, convenience stuff.
*/
UCLASS()
class USQLiteBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
class SQLITE3UE4PLUGIN_API USQLiteBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_UCLASS_BODY()
GENERATED_BODY()

public:
public:

UFUNCTION(BlueprintPure, meta = (DefaultToSelf = "WorldContextObject", HidePin = "WorldContextObject"), Category = "SQLite")
static UObject* NewObjectFromBlueprint(UObject* WorldContextObject, TSubclassOf<UObject> UC);

/**
* Simple casts for blueprint use.
*/

UFUNCTION(BlueprintCallable, Category = "SQLite|Value Conversion")
UFUNCTION(BlueprintCallable, Category = "SQLite|Value Conversion")
static int32 CastToInt(FString SQLiteResultValue);

//UFUNCTION(BlueprintCallable, Category = "SQLite|Value Conversion")
//static int64 CastToInt64(FString SQLiteResultValue);

UFUNCTION(BlueprintCallable, Category = "SQLite|Value Conversion")
UFUNCTION(BlueprintCallable, Category = "SQLite|Value Conversion")
static bool CastToBoolean(FString SQLiteResultValue);

UFUNCTION(BlueprintCallable, Category = "SQLite|Value Conversion")
UFUNCTION(BlueprintCallable, Category = "SQLite|Value Conversion")
static float CastToFloat(FString SQLiteResultValue);
/**
* Blueprint nodes for building queries.
*/

//UFUNCTION(BlueprintCallable, Category = "SQLite|Value Conversion")
//static double CastToDouble(FString SQLiteResultValue);

/**
* Blueprint nodes for building queries.
*/

/** Start a new query block. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Query Start", CompactNodeTitle = "("), Category = "SQLite|Query")
/** Start a new query block. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Query Start", CompactNodeTitle = "("), Category = "SQLite|Query")
static FSQLiteQueryTermExpectedNode QueryStart(FSQLiteQueryTermExpectedNode LogicOperationOrNone);

/** End a query block. NOTE: To link this query to a Get Data node's query pin, link a Finalize Query node to the end of the query chain and link that to the pin. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Query End", CompactNodeTitle = ")"), Category = "SQLite|Query")
/** End a query block. NOTE: To link this query to a Get Data node's query pin, link a Finalize Query node to the end of the query chain and link that to the pin. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Query End", CompactNodeTitle = ")"), Category = "SQLite|Query")
static FSQLiteQueryLogicExpectedNode QueryEnd(const FSQLiteQueryLogicExpectedNode& LastQueryTerm);

/** A query term. Basically creating a "Key <operator> Value" comparison. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Query Term"), Category = "SQLite|Query")
/** A query term. Basically creating a "Key <operator> Value" comparison. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Query Term"), Category = "SQLite|Query")
static FSQLiteQueryLogicExpectedNode QueryTerm(const FSQLiteQueryTermExpectedNode& LogicOperation, FString Field, FString Operator, FString Value);

/** AND logic node, combining two terms. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "AND", CompactNodeTitle = "AND"), Category = "SQLite|Query|Logic")
/** AND logic node, combining two terms. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "AND", CompactNodeTitle = "AND"), Category = "SQLite|Query|Logic")
static FSQLiteQueryTermExpectedNode QueryLogicAnd(const FSQLiteQueryLogicExpectedNode& LHS);

/** OR logic node, combining two terms. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "OR", CompactNodeTitle = "OR"), Category = "SQLite|Query|Logic")
/** OR logic node, combining two terms. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "OR", CompactNodeTitle = "OR"), Category = "SQLite|Query|Logic")
static FSQLiteQueryTermExpectedNode QueryLogicOr(const FSQLiteQueryLogicExpectedNode& LHS);

/** Finalizes the query. This node's output can be linked to a Get Data node's query source pin. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Finalize Query"), Category = "SQLite|Query")
/** Finalizes the query. This node's output can be linked to a Get Data node's query source pin. */
UFUNCTION(BlueprintPure, meta = (DisplayName = "Finalize Query"), Category = "SQLite|Query")
static FSQLiteQueryFinalizedQuery QueryFinal(const FSQLiteQueryLogicExpectedNode& QueryEndNode);

UFUNCTION(BlueprintPure, meta = (DisplayName = "INTEGER (SQLite)"), Category = "SQLite|Query|DataTypes")
static FString SQLiteINTEGER( FString FieldName, const bool PK, const bool AI,
FString &ForPrimaryKey, const bool Unique, FString &FieldNameOutput);
UFUNCTION(BlueprintPure, meta = (DisplayName = "INTEGER (SQLite)"), Category = "SQLite|Query|DataTypes")
static FSQLiteTableField SQLiteINTEGER(FString FieldName, const bool PK, const bool AI, const bool Unique);

UFUNCTION(BlueprintPure, meta = (DisplayName = "TEXT (SQLite)"), Category = "SQLite|Query|DataTypes")
static FSQLiteTableField SQLiteTEXT(const FString FieldName, const bool PK, const bool Unique);

UFUNCTION(BlueprintPure, meta = (DisplayName = "TEXT (SQLite)"), Category = "SQLite|Query|DataTypes")
static FString SQLiteTEXT(const FString FieldName, const bool PK,
FString &ForPrimaryKey, const bool Unique, FString &FieldNameOutput);
UFUNCTION(BlueprintPure, meta = (DisplayName = "REAL (SQLite)"), Category = "SQLite|Query|DataTypes")
static FSQLiteTableField SQLiteREAL(const FString FieldName, const bool PK, const bool Unique);

UFUNCTION(BlueprintPure, meta = (DisplayName = "REAL (SQLite)"), Category = "SQLite|Query|DataTypes")
static FString SQLiteREAL(const FString FieldName, const bool PK,
FString &ForPrimaryKey, const bool Uniqu, FString &FieldNameOutpute);
UFUNCTION(BlueprintPure, meta = (DisplayName = "NUMERIC (SQLite)"), Category = "SQLite|Query|DataTypes")
static FSQLiteTableField SQLiteNUMERIC(const FString FieldName, const bool PK, const bool Unique);

UFUNCTION(BlueprintPure, meta = (DisplayName = "NUMERIC (SQLite)"), Category = "SQLite|Query|DataTypes")
static FString SQLiteNUMERIC(const FString FieldName, const bool PK,
FString &ForPrimaryKey, const bool Unique, FString &FieldNameOutput);
UFUNCTION(BlueprintPure, meta = (DisplayName = "Primary Key (SQLite)"), Category = "SQLite|Query|DataTypes")
static FSQLitePrimaryKey SQLitePrimaryKey(const TArray<FSQLiteTableField> Fields);

UFUNCTION(BlueprintPure, meta = (DisplayName = "Primary Key (SQLite)"), Category = "SQLite|Query|DataTypes")
static FString SQLitePrimaryKey(const TArray<FString> Fields);
UFUNCTION(BlueprintPure, meta = (DisplayName = "Index (SQLite)"), Category = "SQLite|Query|DataTypes")
static FSQLiteIndex SQLiteIndexFunction(const TArray<FSQLiteTableField> Fields, FString idxName, bool Unique);

UFUNCTION(BlueprintPure, meta = (DisplayName = "Index (SQLite)"), Category = "SQLite|Query|DataTypes")
static FString SQLiteIndex(const TArray<FString> Fields, FString idxName, bool Unique);



};
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
#pragma once
#include "sqlite3.h"
#include "SQLiteBlueprintNodes.h"
#include "SQLiteDatabaseStructs.h"
#include "SQLiteDatabase.generated.h"

USTRUCT(BlueprintType)
struct SQLITE3UE4PLUGIN_API FSQLiteDatabaseReference
{
GENERATED_USTRUCT_BODY()

/** The database name (not the filename) */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Database Reference")
FString DatabaseName;
/** The database name (not the filename) */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Database Reference")
FString DatabaseName;

/** The database tables we want to get data from */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Database Reference")
TArray<FString> Tables;
TArray<FString> Tables;
};

USTRUCT(BlueprintType)
struct SQLITE3UE4PLUGIN_API FSQLiteKeyValuePair
{
GENERATED_USTRUCT_BODY()
/** The database table field name */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Key Value Pair")
FString Key;

/** The database table field name */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Key Value Pair")
FString Key;

/** The value of the field */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Key Value Pair")
FString Value;
FString Value;
};

USTRUCT(BlueprintType)
struct SQLITE3UE4PLUGIN_API FSQLiteQueryResultRow
{
GENERATED_USTRUCT_BODY()

/** A list of field name, field value pairs */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Query Result")
TArray<FSQLiteKeyValuePair> Fields;
/** A list of field name, field value pairs */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Query Result")
TArray<FSQLiteKeyValuePair> Fields;
};

USTRUCT(BlueprintType)
struct SQLITE3UE4PLUGIN_API FSQLiteQueryResult
{
GENERATED_USTRUCT_BODY()

/** The resulting rows from the query */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Query Result")
TArray<FSQLiteQueryResultRow> ResultRows;
/** The resulting rows from the query */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SQLite Query Result")
TArray<FSQLiteQueryResultRow> ResultRows;

/** Was the query successful or not */
UPROPERTY(BlueprintReadOnly, Category = "SQLite Query Result")
bool Success;
bool Success;

/** If the query was unsuccessful a human readable error message will be populated here */
UPROPERTY(BlueprintReadOnly, Category = "SQLite Query Result")
FString ErrorMessage;
FString ErrorMessage;

};

Expand Down Expand Up @@ -121,73 +123,93 @@ class SQLITE3UE4PLUGIN_API USQLiteDatabase : public UObject
{
GENERATED_UCLASS_BODY()

public:
public:

/** Add a database to the list of databases. It will be checked that it's valid (will try to open it) */
UFUNCTION(BlueprintCallable, Category = "SQLite")
/** Add a database to the list of databases. It will be checked that it's valid (will try to open it) */
UFUNCTION(BlueprintCallable, Category = "SQLite")
static bool RegisterDatabase(FString Name, FString Filename, bool RelativeToGameContentDirectory);

/** Checks if the database is registered, ie. that it can be found in Databases. */
UFUNCTION(BlueprintCallable, Category = "SQLite")
/** Checks if the database is registered, ie. that it can be found in Databases. */
UFUNCTION(BlueprintCallable, Category = "SQLite")
static bool IsDatabaseRegistered(FString DatabaseName);

/** Get data from the database using a select statement straight into an UObject, ie. populates its properties. */
UFUNCTION(BlueprintCallable, Category = "SQLite", meta = (DisplayName = "Get Data Into Object (manual query)"))
/** Get data from the database using a select statement straight into an UObject, ie. populates its properties. */
UFUNCTION(BlueprintCallable, Category = "SQLite", meta = (DisplayName = "Get Data Into Object (manual query)"))
static bool GetDataIntoObject(const FString& DatabaseName, const FString& Query, UObject* ObjectToPopulate);

/** Blueprint: Gets data from the database using a select statement straight into an UObject, ie. populates its properties.
* Note: Does not create a new object. ObjectToPopulate is the reference to the object you want to populate. */
UFUNCTION(BlueprintCallable, Category = "SQLite", meta = (DisplayName = "Get Data Into Object"))
/** Blueprint: Gets data from the database using a select statement straight into an UObject, ie. populates its properties.
* Note: Does not create a new object. ObjectToPopulate is the reference to the object you want to populate. */
UFUNCTION(BlueprintCallable, Category = "SQLite", meta = (DisplayName = "Get Data Into Object"))
static bool GetDataIntoObjectBP(const FSQLiteDatabaseReference& DataSource, TArray<FString> Fields, FSQLiteQueryFinalizedQuery Query, UObject* ObjectToPopulate);

/** Get data from the database using a select statement and return the rows. */
UFUNCTION(BlueprintCallable, Category = "SQLite", meta = (DisplayName = "Get Data From Table(s) (manual query)"))
/** Get data from the database using a select statement and return the rows. */
UFUNCTION(BlueprintCallable, Category = "SQLite", meta = (DisplayName = "Get Data From Table(s) (manual query)"))
static FSQLiteQueryResult GetData(const FString& DatabaseName, const FString& Query);

/** Blueprint: Get data from the database. Returns the resulting rows. */
UFUNCTION(BlueprintCallable, Category = "SQLite", meta = (DisplayName = "Get Data From Table(s)"))
/** Blueprint: Get data from the database. Returns the resulting rows. */
UFUNCTION(BlueprintCallable, Category = "SQLite", meta = (DisplayName = "Get Data From Table(s)"))
static FSQLiteQueryResult GetDataBP(const FSQLiteDatabaseReference& DataSource, TArray<FString> Fields, FSQLiteQueryFinalizedQuery Query, int32 MaxResults = -1, int32 ResultOffset = 0);

/** Create table in the database. */
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Create Table"))
static bool CreateTable(const FString DatabaseName, const FString TableName,
const TArray<FString> Fields, const FString PK, FString &TableNameOutput);
/** Create table in the database. */
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Create Table"))
static FSQLiteTable CreateTable(const FString DatabaseName, const FString TableName,
const TArray<FSQLiteTableField> Fields, const FSQLitePrimaryKey PK);

/** Create indexes for table */
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Create Indexes"))
static bool CreateIndexes(const FString DatabaseName, const FString TableName, const TArray<FString> Indexes);
/** Create indexes for table */
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Create Indexes"))
static bool CreateIndexes(const FString DatabaseName, const FString TableName, const TArray<FSQLiteIndex> Indexes);

/** Create index for table */
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Create Index"))
static bool CreateIndex(const FString DatabaseName, const FString TableName, const FString Index);
/** Create index for table */
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Create Index"))
static bool CreateIndex(const FString DatabaseName, const FString TableName, const FSQLiteIndex Index);

/** Drop index*/
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Drop Index"))
/** Drop index*/
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Drop Index"))
static bool DropIndex(const FString DatabaseName, const FString IndexName);

/** Drop index*/
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Drop Table"))
/** Drop Table*/
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Drop Table"))
static bool DropTable(const FString DatabaseName, const FString TableName);

private:
/** Checks database validity (if the file exists and/or if it can be opened). */
static bool IsValidDatabase(FString DatabaseFilename, bool TestByOpening);
/** Tries to open a database. */
static bool CanOpenDatabase(FString DatabaseFilename);
/** Collects all properties from an UObject and maps them by the property name. */
static TMap<FString, UProperty*> CollectProperties(UObject* SourceObject);
/** Constructs an SQL query from the blueprint fed data. */
static FString ConstructQuery(TArray<FString> Tables, TArray<FString> Fields, FSQLiteQueryFinalizedQuery QueryObject, int32 MaxResults = -1, int32 ResultOffset = 0);
/** Runs a query and returns fetched rows. */
static SQLiteQueryResult RunQueryAndGetResults(FString DatabaseName, FString Query);
/** Assigns a result row's fields' values to an UObject, ie. assigns them to the properties that have the same name. */
static void AssignResultsToObjectProperties(const SQLiteResultValue& ResultValue, UObject* ObjectToPopulate);
static void PrepareStatement(const FString* DatabaseName, const FString* Query, sqlite3** Db, int32** SqlReturnCode,
sqlite3_stmt** PreparedStatement);
/** Truncate Table*/
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Truncate Table"))
static bool TruncateTable(const FString DatabaseName, const FString TableName);

/** Is table exists?*/
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Is table exists?"))
static bool IsTableExists(const FString DatabaseName, const FString TableName);

/** Insert rows into table */
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Insert Rows Into Table"))
static void InsertRowsIntoTable(const FString DatabaseName, const FString TableName, TArray<FSQLiteTableRowSimulator> rowsOfFields);

/** Compact database*/
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Compact database"))
static bool Vacuum(const FString DatabaseName);

/** Execute SQL (can be used for insert statement)*/
UFUNCTION(BlueprintCallable, Category = "SQLite|Query", meta = (DisplayName = "Execute SQL"))
static bool ExecSql(const FString DatabaseName, const FString Query);

private:
/** A list of the databases for convenience, easier to refer to them by name rather than a long filename. */
static TMap<FString, FString> Databases;
private:
/** Checks database validity (if the file exists and/or if it can be opened). */
static bool IsValidDatabase(FString DatabaseFilename, bool TestByOpening);
/** Tries to open a database. */
static bool CanOpenDatabase(FString DatabaseFilename);
/** Collects all properties from an UObject and maps them by the property name. */
static TMap<FString, UProperty*> CollectProperties(UObject* SourceObject);
/** Constructs an SQL query from the blueprint fed data. */
static FString ConstructQuery(TArray<FString> Tables, TArray<FString> Fields, FSQLiteQueryFinalizedQuery QueryObject, int32 MaxResults = -1, int32 ResultOffset = 0);
/** Runs a query and returns fetched rows. */
static SQLiteQueryResult RunQueryAndGetResults(FString DatabaseName, FString Query);
/** Assigns a result row's fields' values to an UObject, ie. assigns them to the properties that have the same name. */
static void AssignResultsToObjectProperties(const SQLiteResultValue& ResultValue, UObject* ObjectToPopulate);
static void PrepareStatement(const FString* DatabaseName, const FString* Query, sqlite3** Db, int32** SqlReturnCode,
sqlite3_stmt** PreparedStatement);


private:
/** A list of the databases for convenience, easier to refer to them by name rather than a long filename. */
static TMap<FString, FString> Databases;

};
Loading