2shady4u / godot-sqlite

GDExtension wrapper for SQLite (Godot 4.x+)
MIT License
973 stars 83 forks source link
gamedev godot godot-addon godot-plugin godotengine sqlite

NOTE: This branch is compatible with Godot 4.x. Older versions of Godot (3.x) are supported on the godot-3.x-branch as found here.

Godot SQLite banner

godot-sqlite

This GDNative script aims to serve as a custom wrapper that makes SQLite3 available in Godot 4.0+. Additionally, it does not require any additional compilation or mucking about with build scripts.

Supported operating systems:

How to install?

Re-building Godot from scratch is NOT required, the proper way of installing this plugin is to either install it through the Asset Library or to just manually download the build files yourself.

Godot Asset Library

Godot-SQLite is available through the official Godot Asset Library, and can be installed in the following way:

Manually

It's also possible to manually download the build files found in the releases tab, extract them on your system and run the supplied demo-project. Make sure that Godot is correctly loading the gdsqlite.gdextension-resource and that it is available in the res://-environment.

An example project, named "demo", can also be downloaded from the releases tab.

How to use?

Examples of possible usage can be found in the supplied demo-project as downloadable in the releases tab or the source code can be directly inspected here.

Additionally, a video tutorial by Mitch McCollum (finepointcgi) is available here:

Variables

Methods

Frequently Asked Questions (FAQ)

1. My query fails and returns syntax errors, what should I do?

There are a couple of things you can do before panicking, namely:

After exhausting these options, please open an issue that describes the error in proper detail.

2. When should I create function bindings to augment SQLite's set of native functions?

Preferably never.

Creating function should only be seen as a measure of last resort and only be used when you perfectly know what you are doing. Be sure to first check out the available native list of scalar SQL applications that is already available in SQLite3.

3. My Android/iOS/HTML5 application cannot access the database!

Android does not allow modification of files in the res://-folder, thus blocking the plugin from acquiring a read and write lock on the database-file.

In the case of read-only databases, the database files have to be packaged/exported in the *.pck-file by manually adding the file entry to the include_filter of your export_presets.cfg-file. Additionally the connection has to be opened by explicitly setting the read_only-variable of your connection to True before opening your database.

In the case of a read and write database connection, the most painless solution is to copy the entire database to the user://-folder instead as apps have explicit writing privileges there. If there is a better solution, one that does not involve copying the database to a new location, please do enlighten me.

4. Is this plugin compatible with a Godot Server binary? How to set it up?

Follow the steps described in the Godot documentation as found here.

NOTE: If you are using an older version of Linux on your server machine (with glibc version < 2.31), the plugin crashes due to the compiled version of glibc being too recent. In that case you will have to recompile the Linux plugin binary yourself as Github Actions does not support Ubuntu versions lower than Ubuntu 20.04 LTS.

NOTE: The currently installed version of glibc can be obtained with following command:

ldd --version

5. Does this plugin support the FTS5 Extension?

Yes, the SQLite FTS5 Extension is supported by this plugin, although it requires the user to re-compile the plugin.

To re-compile the plugin with FTS5 enabled, follow the instructions as defined in the 'How to contribute?'-section below.
Depending on your choice, following modifications have to be made:

A. Using your own device

Add the enable_fts5-flag to the compilation command:

scons platform=<platform> target_path=<target_path> target_name=libgdsqlite enable_fts5=yes

B. Using Github Actions

Update the common_flags-field of the .github/workflows/build_var.json-file, as follows:

"common_flags": "enable_fts5=no"
"common_flags": "enable_fts5=yes"

Afterwards, push a new commit (containing this change) to the master-branch of your forked remote. This commit triggers a new workflow that re-compiles the plugin's binaries with FTS5 enabled.

6. Does this plugin support some kind of encryption?

Database encryption of any kind is not supported in this plugin. Nor are there any plans to support this feature in an upcoming or future release.

The addition of database encryption might be up for reconsideration if, and only if, a future release of SQLite introduces native support of this feature without requiring the purchase of a license.

A modification of this plugin, which allows encryption, was done by abcjjy by swapping out SQLite with SQLCipher and can be found here. Direct all issues and feature requests regarding this implementation to its own dedicated repository.

NOTE: The natively supported SQLite Encryption Extension (SEE) is not applicable as it requires the purchase of a license for the one-time fee of 2000$

How to export?

The exporting strategy is dependent on the nature of your database.

Read-only databases

If your database serves as static data storage i.e. there's no persistent dynamic data that needs to be saved, then your database is a read-only database. For example, a database that contains a table of monster descriptions and data (experience gained on kill, health points) probably never changes during normal gameplay.

In this case, the database can be packaged in the *.pck-file together with all other assets without any hassle.
To enable this behaviour following conditions need to be met:

You can also open databases in read-only mode that are not packaged, albeit under some restrictions such as the fact that the database files have to copied manually to user://-folder on mobile platforms (Android & iOS) and for web builds.

NOTE: The contents of your PCK file can be verified by using externally available tools as found here.

Read and write databases

NOTE: On mobile platforms (Android & iOS) and for web builds, the method discussed here is not possible and the contents of the res://data/-folder have to be copied to the user://-folder in its entirety instead (see FAQ above).

If your database serves as dynamic data storage i.e. there's persistent dynamic data that needs to be saved during gameplay, then your database is a read and write database. For example, a database that contains a table of character levels and experience dynamically changes whenever the player levels up and/or gains experience.

In this case, the database cannot be packaged in the *.pck-file as the contents of this file are static and cannot be dynamically modified during normal operation.

All *.db-files (and *.json-files if you choose not to include them in the *.pck) should preferably be part of the same folder. For example, in the case of the demo-project this is the data/-folder. During export this folder has to be copied in its entirety and placed alongside the executable that is created by Godot's export command line utilities.
To simplify and automate this process, a script with the name export_data.gd can be found in the demo-project and allows to automatically copy the data-folder's contents to the export folder.

The commands to succesfully export a working executable for the demo-project are:

mkdir build
godot -s export_data.gd
godot --export-debug "Windows Desktop" "build/SQLite Demo.exe"

("Windows Desktop" should be replaced by whatever the name of your relevant build template is.)

For the "Windows Desktop" export target this results in following folder/file-structure:

data/test_backup_base64_old.json
data/test_backup_old.json
libgdsqlite.windows.template_release.x86_64.dll
'SQLite Demo.console.exe'
'SQLite Demo.exe'
'SQlite Demo.pck'

How to contribute?

Using GitHub Actions CI/CD

Fork the repository to your GitHub own account by following the recommended procedure.

Afterwards you can push your own code to the master-branch of this forked repository and GitHub Actions will automatically build new versions for all the supported platforms. These binaries then have to be downloaded and placed inside of the addons/godot-sqlite/bin/-folder, as found in your Godot project, alongside any existing binaries.

NOTE: Compiled binaries for the latest development version of godot-sqlite are available here.

Using your own device

First clone the project and initialize the godot-cpp-submodule as such:

git clone https://github.com/2shady4u/godot-sqlite.git
git submodule update --init

Secondly, SCons, Python and a viable C++ compiler have to be installed on your device. On Windows, both Visual Studio Community (2017+) and/or MinGW-w64 can be used to compile the plugin from scratch. More detailed instructions for compiling the plugin on Windows, Linux and other supported platforms can be found here in the Official Godot documentation.

Depending on the wanted target platform, some additional dependencies have to be installed:

Afterwards, the SContruct file found in this repository should be sufficient to build this project's C++ source code for Linux, Mac OS X, Windows, iOS and android with the help of following command:

scons platform=<platform> target_path=<target_path> target_name=libgdsqlite

In the case of both Android and iOS, an additional arch-parameter has to be supplied to specify the CPU architecture. In the case of android, valid values for the architecture are 'arm64' (= default), 'arm32', 'x86_64' and/or 'x86_32', and in the case of iOS, the valid values are 'universal' (= default), 'arm64' and/or 'x86_64'.

Additionally, in the case of Android, the ANDROID_NDK_ROOT-parameter has to be set to the location of the Android NDK installed previously.

For uncertainties regarding compilation & building specifics, please do check out the .github\workflows\*.yml-scripts and the SConstruct-file as found in this repository.

Tutorials for making and extending GDExtension scripts are available here in the Official Godot Documentation.