Skip to content

Fix for UE4.25.1 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions Source/CISQLite3/Private/CISQLite3.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2016 conflict.industries MIT License (MIT)

#include "CISQLite3.h"
#include "CISQLite3PrivatePCH.h"

DEFINE_LOG_CATEGORY(LogDatabase)
Expand Down
9 changes: 2 additions & 7 deletions Source/CISQLite3/Private/SQLiteBlueprintFunctionLibrary.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#include "SQLiteBlueprintFunctionLibrary.h"
#include "CISQLite3PrivatePCH.h"
#include "Engine.h"
#include "CString.h"

#include "SQLiteBlueprintFunctionLibrary.h"



#include "Misc/CString.h"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the PCH needs the BlueprintFunctionLibrary or why is this? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea, I'm a complete noob in UE4 and C++ 😆
I don't quite remember why, but this fixed some issues I had..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, found it. CString.h was moved to Misc/CString.h , and the order of imports was important, so they moved.


int32 USQLiteBlueprintFunctionLibrary::CastToInt(FString SQLiteResultValue)
{
return FCString::Atoi(*SQLiteResultValue);
}


bool USQLiteBlueprintFunctionLibrary::CastToBoolean(FString SQLiteResultValue)
{
return FCString::Atoi(*SQLiteResultValue) > 0;
Expand Down
47 changes: 24 additions & 23 deletions Source/CISQLite3/Private/SQLiteDatabase.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "CISQLite3PrivatePCH.h"
#include "SQLiteDatabase.h"
#include "CISQLite3PrivatePCH.h"

#define LOGSQLITE(verbosity, text) UE_LOG(LogDatabase, verbosity, TEXT("SQLite: %s"), text)

Expand All @@ -17,7 +17,7 @@ USQLiteDatabase::USQLiteDatabase(const FObjectInitializer& ObjectInitializer)

bool USQLiteDatabase::CreateDatabase(const FString& Filename, bool RelativeToProjectContentDirectory)
{
const FString actualFilename = RelativeToProjectContentDirectory ? FPaths::ProjectContentDir() + Filename : Filename;
const FString actualFilename = RelativeToProjectContentDirectory ? FPaths::ConvertRelativePathToFull(FPaths::ProjectContentDir()) + Filename : Filename;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember fighting path issues on Android, but I don't think this should be a problem.
Anyone test this on Android coincidentally? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't tested on anything else but Windows, sorry. I'm not sure why I did this either, but IIRC a relative path didn't work correctly.

I'm sorry for the incredibly low quality of the MR - I just managed to make it work on my machine and decided to share ;p

Thanks for your time!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, sharing is always valuable, maybe someone using this on Android will pick it up and verify 👍


sqlite3* db;
int res = sqlite3_open(TCHAR_TO_ANSI(*actualFilename), &db);
Expand All @@ -37,7 +37,7 @@ bool USQLiteDatabase::CreateDatabase(const FString& Filename, bool RelativeToPro

bool USQLiteDatabase::RegisterDatabase(const FString& Name, const FString& Filename, bool RelativeToProjectContentDirectory, bool KeepOpen)
{
const FString actualFilename = RelativeToProjectContentDirectory ? FPaths::ProjectContentDir() + Filename : Filename;
const FString actualFilename = RelativeToProjectContentDirectory ? FPaths::ConvertRelativePathToFull(FPaths::ProjectContentDir()) + Filename : Filename;

if (!IsValidDatabase(actualFilename, true))
{
Expand Down Expand Up @@ -273,12 +273,12 @@ bool USQLiteDatabase::GetDataIntoObjectBP(const FSQLiteDatabaseReference& DataSo

//--------------------------------------------------------------------------------------------------------------

TMap<FString, UProperty*> USQLiteDatabase::CollectProperties(UObject* SourceObject)
TMap<FString, FProperty*> USQLiteDatabase::CollectProperties(UObject* SourceObject)
{

UClass* SourceObjectClass = SourceObject->GetClass();
TMap<FString, UProperty*> Props;
for (TFieldIterator<UProperty> PropIt(SourceObjectClass, EFieldIteratorFlags::SuperClassFlags::IncludeSuper);
TMap<FString, FProperty*> Props;
for (TFieldIterator<FProperty> PropIt(SourceObjectClass, EFieldIteratorFlags::SuperClassFlags::IncludeSuper);
PropIt; ++PropIt)
{
Props.Add(*PropIt->GetNameCPP(), *PropIt);
Expand Down Expand Up @@ -782,6 +782,7 @@ TUniquePtr<SQLiteQueryResult> USQLiteDatabase::RunQueryAndGetResults(const FStri
sqlite3_finalize(preparedStatement);
if (!keepOpen) sqlite3_close(db);

result.InsertedId = sqlite3_last_insert_rowid(db);
result.Results = resultRows;
result.Success = true;
return MakeUnique<SQLiteQueryResult>(MoveTemp(result));
Expand All @@ -797,37 +798,37 @@ void USQLiteDatabase::AssignResultsToObjectProperties(const SQLiteResultValue& R
{
if (propertyMap.Contains(field.Name))
{
UProperty* targetProperty = propertyMap[field.Name];
FProperty* targetProperty = propertyMap[field.Name];

if (field.Type == SQLiteResultValueTypes::Integer)
{
UInt64Property* int64prop = NULL;
UIntProperty* int32prop = NULL;
UInt16Property* int16prop = NULL;
UInt8Property* int8prop = NULL;
UBoolProperty* boolProp = NULL;
FInt64Property* int64prop = NULL;
FIntProperty* int32prop = NULL;
FInt16Property* int16prop = NULL;
FInt8Property* int8prop = NULL;
FBoolProperty* boolProp = NULL;

if ((int64prop = Cast<UInt64Property>(targetProperty)) != NULL)
if ((int64prop = CastField<FInt64Property>(targetProperty)) != NULL)
{
int64prop->SetPropertyValue_InContainer(ObjectToPopulate, field.IntValue);
LOGSQLITE(Verbose, *FString::Printf(TEXT("Property '%s' was set to '%d'"), *field.Name, field.IntValue));
}
else if ((int32prop = Cast<UIntProperty>(targetProperty)) != NULL)
else if ((int32prop = CastField<FIntProperty>(targetProperty)) != NULL)
{
int32prop->SetPropertyValue_InContainer(ObjectToPopulate, (int32)field.IntValue);
LOGSQLITE(Verbose, *FString::Printf(TEXT("Property '%s' was set to '%d'"), *field.Name, field.IntValue));
}
else if ((int16prop = Cast<UInt16Property>(targetProperty)) != NULL)
else if ((int16prop = CastField<FInt16Property>(targetProperty)) != NULL)
{
int16prop->SetPropertyValue_InContainer(ObjectToPopulate, (int16)field.IntValue);
LOGSQLITE(Verbose, *FString::Printf(TEXT("Property '%s' was set to '%d'"), *field.Name, field.IntValue));
}
else if ((int8prop = Cast<UInt8Property>(targetProperty)) != NULL)
else if ((int8prop = CastField<FInt8Property>(targetProperty)) != NULL)
{
int8prop->SetPropertyValue_InContainer(ObjectToPopulate, (int8)field.IntValue);
LOGSQLITE(Verbose, *FString::Printf(TEXT("Property '%s' was set to '%d'"), *field.Name, field.IntValue));
}
else if ((boolProp = Cast<UBoolProperty>(targetProperty)) != NULL)
else if ((boolProp = CastField<FBoolProperty>(targetProperty)) != NULL)
{
boolProp->SetPropertyValue_InContainer(ObjectToPopulate, field.IntValue > 0);
LOGSQLITE(Verbose, *FString::Printf(TEXT("Property '%s' was set to '%d'"), *field.Name, field.IntValue));
Expand All @@ -836,14 +837,14 @@ void USQLiteDatabase::AssignResultsToObjectProperties(const SQLiteResultValue& R

else if (field.Type == SQLiteResultValueTypes::Float)
{
UDoubleProperty* doubleProp = NULL;
UFloatProperty* floatProp = NULL;
if ((doubleProp = Cast<UDoubleProperty>(targetProperty)) != NULL)
FDoubleProperty* doubleProp = NULL;
FFloatProperty* floatProp = NULL;
if ((doubleProp = CastField<FDoubleProperty>(targetProperty)) != NULL)
{
doubleProp->SetPropertyValue_InContainer(ObjectToPopulate, field.DoubleValue);
LOGSQLITE(Verbose, *FString::Printf(TEXT("Property '%s' was set to '%f'"), *field.Name, field.DoubleValue));
}
else if ((floatProp = Cast<UFloatProperty>(targetProperty)) != NULL)
else if ((floatProp = CastField<FFloatProperty>(targetProperty)) != NULL)
{
floatProp->SetPropertyValue_InContainer(ObjectToPopulate, (float)field.DoubleValue);
LOGSQLITE(Verbose, *FString::Printf(TEXT("Property '%s' was set to '%f'"), *field.Name, field.DoubleValue));
Expand All @@ -852,8 +853,8 @@ void USQLiteDatabase::AssignResultsToObjectProperties(const SQLiteResultValue& R

else if (field.Type == SQLiteResultValueTypes::Text)
{
UStrProperty* strProp = NULL;
if ((strProp = Cast<UStrProperty>(targetProperty)) != NULL)
FStrProperty* strProp = NULL;
if ((strProp = CastField<FStrProperty>(targetProperty)) != NULL)
{
strProp->SetPropertyValue_InContainer(ObjectToPopulate, field.StringValue);
LOGSQLITE(Verbose, *FString::Printf(TEXT("Property '%s' was set to '%s'"), *field.Name, *field.StringValue.Mid(0, 64)));
Expand Down
2 changes: 1 addition & 1 deletion Source/CISQLite3/Public/CISQLite3.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#pragma once

#include "ModuleManager.h"
#include "Modules/ModuleManager.h"

class FCISQLite3 : public IModuleInterface
{
Expand Down
3 changes: 2 additions & 1 deletion Source/CISQLite3/Public/SQLiteDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct SQLiteQueryResult
bool Success;
FString ErrorMessage;
TArray<SQLiteResultValue> Results;
int InsertedId = 0;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a functional change - not sure if this is the way you'd like it.. But I really needed the "last insert id" for an inserted record from a regular query (I'm only using this as a C++ module, not using the blueprints), so you might want to check that out.

Copy link
Owner

@Squareys Squareys Aug 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a SQLite (litterally, specific to sqlite) query for that SELECT last_insert_rowid(); or something. It returns the same as sqlite3_last_insert_rowid(db). (Might be slower, though).
If you could check out how that functions is implemented... if it's trivial, we can (and probably should) keep this, otherwise I recommend the query.

};


Expand Down Expand Up @@ -214,7 +215,7 @@ class CISQLITE3_API USQLiteDatabase : public UObject
/** Tries to open a database. */
static bool CanOpenDatabase(const FString& DatabaseFilename);
/** Collects all properties from an UObject and maps them by the property name. */
static TMap<FString, UProperty*> CollectProperties(UObject* SourceObject);
static TMap<FString, FProperty*> 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);
/** Assigns a result row's fields' values to an UObject, ie. assigns them to the properties that have the same name. */
Expand Down