Skip to content

Commit 51cbb4e

Browse files
committed
Update documentation
1 parent 76adb85 commit 51cbb4e

File tree

7 files changed

+173
-90
lines changed

7 files changed

+173
-90
lines changed

README.md

Lines changed: 101 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,71 +47,57 @@ Additionally, a video tutorial by [Mitch McCollum (finepointcgi)](https://github
4747
<a href="https://www.youtube.com/watch?v=j-BRiTrw_F0"><img src="https://img.youtube.com/vi/j-BRiTrw_F0/0.jpg"></a>
4848
</p>
4949

50-
## Variables
50+
## Classes
5151

52-
- **path** (String, default="default")
52+
- [SQLiteConnection](#sqlite-connection)
53+
- [SQLiteConnectionParams](#sqlite-connection-params)
54+
- [SQLiteEnums](#sqlite-enums)
5355

54-
Path to the database, should be set before opening the database with `open_db()`. If no database with this name exists, a new one at the supplied path will be created. Both `res://` and `user://` keywords can be used to define the path.
56+
### 🔗 <a name="sqlite-connection">SQLiteConnection</a>
5557

56-
- **error_message** (String, default="")
58+
Represents a connection to an SQLite database.
5759

58-
Contains the zErrMsg returned by the SQLite query in human-readable form. An empty string corresponds with the case in which the query executed succesfully.
60+
#### ~ Properties ~
5961

60-
- **default_extension** (String, default="db")
62+
- **path** (String, default="database.db")
6163

62-
Default extension that is automatically appended to the `path`-variable whenever **no** extension is detected/given.
64+
Path to the database. This is a read-only property that cannot be modified after opening the connection.
6365

64-
***NOTE**: If database files without extension are desired, this variable has to be set to "" (= an empty string) as to skip this automatic procedure entirely.*
66+
- **error_message** (String, default="")
67+
68+
Contains the zErrMsg returned by the SQLite query in human-readable form. An empty string corresponds with the case in which the query executed succesfully.
6569

6670
- **foreign_keys** (Boolean, default=false)
6771

6872
Enables or disables the availability of [foreign keys](https://www.sqlite.org/foreignkeys.html) in the SQLite database.
6973

7074
- **read_only** (Boolean, default=false)
7175

72-
Enabling this property opens the database in read-only modus & allows databases to be packaged inside of the PCK. To make this possible, a custom [VFS](https://www.sqlite.org/vfs.html) is employed which internally takes care of all the file handling using the Godot API.
73-
74-
- **query_result** (Array, default=[])
75-
76-
Contains the results from the latest query **by value**; meaning that this property is safe to use when looping successive queries as it does not get overwritten by any future queries.
77-
78-
- **query_result_by_reference** (Array, default=[])
79-
80-
Contains the results from the latest query **by reference** and is, as a direct result, cleared and repopulated after every new query.
76+
Was this database connection opened in read-only modus or not?
8177

8278
- **last_insert_rowid** (Integer, default=0)
8379

8480
Exposes the `sqlite3_last_insert_rowid()`-method to Godot as described [here](https://www.sqlite.org/c3ref/last_insert_rowid.html).
8581
Attempting to modify this variable directly is forbidden and throws an error.
8682

87-
- **verbosity_level** (Integer, default=1)
83+
- **verbosity_level** (VerbosityLevel, default=NORMAL)
8884

8985
The verbosity_level determines the amount of logging to the Godot console that is handy for debugging your (possibly faulty) SQLite queries.
9086

91-
| Level | Description |
92-
|----------------- | ------------------------------------------- |
93-
| QUIET (0) | Don't print anything to the console |
94-
| NORMAL (1) | Print essential information to the console |
95-
| VERBOSE (2) | Print additional information to the console |
96-
| VERY_VERBOSE (3) | Same as VERBOSE |
97-
9887
***NOTE**: VERBOSE and higher levels might considerably slow down your queries due to excessive logging.*
9988

100-
## Methods
89+
#### ~ Methods ~
10190

102-
- ResultCode rc = **open_db()**
91+
- SQLiteConnection conn = **open(** SQLiteConnectionParams connection_params **)**
10392

104-
Open a new database connection. Multiple concurrently open connections to the same database are possible.
93+
Creates a new [bold]SQLiteConnection[/bold] object. Multiple concurrently open connections to the same database are possible.
94+
Returns null if opening the connection failed. You can use [method get_open_error] to check the error that occurred.
10595

106-
- ResultCode rc = **close_db()**
107-
108-
Close the current database connection.
109-
110-
- ResultCode rc = **query(** String query_string **)**
96+
- ResultCode rc = **query(** String query_string, Array query_result **)**
11197

11298
Query the database using the raw SQL statement defined in `query_string`.
11399

114-
- ResultCode rc = **query_with_bindings(** String query_string, Array param_bindings **)**
100+
- ResultCode rc = **query_with_bindings(** String query_string, Array param_bindings, Array query_result **)**
115101

116102
Binds the parameters contained in the `param_bindings`-variable to the query. Using this function stops any possible attempts at SQL data injection as the parameters are sanitized. More information regarding parameter bindings can be found [here](https://www.sqlite.org/c3ref/bind_blob.html).
117103

@@ -189,23 +175,23 @@ Additionally, a video tutorial by [Mitch McCollum (finepointcgi)](https://github
189175
db.query("DROP TABLE "+ table_name + ";")
190176
```
191177
192-
- ResultCode rc = **insert_row(** String table_name, Dictionary row_dictionary **)**
178+
- ResultCode rc = **insert_row(** String table_name, Dictionary row_dictionary, Array query_result **)**
193179
194180
Each key/value pair of the `row_dictionary`-variable defines the column values of a single row.
195181
196182
Columns should adhere to the table schema as instantiated using the `table_dictionary`-variable and are required if their corresponding **"not_null"**-column value is set to `True`.
197183
198-
- ResultCode rc = **insert_rows(** String table_name, Array row_array **)**
184+
- ResultCode rc = **insert_rows(** String table_name, Array row_array, Array query_result **)**
199185
200-
- Array selected_rows = **select_rows(** String table_name, String query_conditions, Array selected_columns **)**
186+
- Array selected_rows = **select_rows(** String table_name, String query_conditions, Array selected_columns, Array query_result **)**
201187
202188
Returns the results from the latest query **by value**; meaning that this property does not get overwritten by any successive queries.
203189
204-
- ResultCode rc = **update_rows(** String table_name, String query_conditions, Dictionary updated_row_dictionary **)**
190+
- ResultCode rc = **update_rows(** String table_name, String query_conditions, Dictionary updated_row_dictionary, Array query_result **)**
205191
206192
With the `updated_row_dictionary`-variable adhering to the same table schema & conditions as the `row_dictionary`-variable defined previously.
207193
208-
- ResultCode rc = **delete_rows(** String table_name, String query_conditions **)**
194+
- ResultCode rc = **delete_rows(** String table_name, String query_conditions, Array query_result **)**
209195
210196
- ResultCode rc = **import_from_json(** String import_path **)**
211197
@@ -227,7 +213,7 @@ Additionally, a video tutorial by [Mitch McCollum (finepointcgi)](https://github
227213
228214
Can be used together with `import_from_buffer()` to implement database encryption.
229215
230-
- ResultCode rc = **create_function(** String function_name, FuncRef function_reference, int number_of_arguments **)**
216+
- ResultCode rc = **create_function(** String function_name, Callable callable, int number_of_arguments **)**
231217
232218
Bind a [scalar SQL function](https://www.sqlite.org/appfunc.html) to the database that can then be used in subsequent queries.
233219
@@ -279,6 +265,81 @@ Additionally, a video tutorial by [Mitch McCollum (finepointcgi)](https://github
279265
db.load_extension("res://addons/godot-sqlite/extensions/spellfix.dll", "sqlite3_spellfix_init")
280266
```
281267
268+
### 📝 <a name="sqlite-connection-params">SQLiteConnectionParams</a>
269+
270+
Connection parameters that have to be passed to `SQLiteConnection.open()`.
271+
272+
#### ~ Properties ~
273+
274+
- **path** (String, default="database.db")
275+
276+
Path to the database. A new database is automatically created if there is no existing database at the supplied path. Both `res://` and `user://` keywords can be used to define the path.
277+
278+
- **read_only** (Boolean, default=false)
279+
280+
Enabling this property opens the database in read-only modus & allows databases to be packaged inside of the PCK. To make this possible, a custom [VFS](https://www.sqlite.org/vfs.html) is employed which internally takes care of all the file handling using the Godot API.
281+
282+
- **verbosity_level** (VerbosityLevel, default=NORMAL)
283+
284+
The verbosity_level determines the amount of logging to the Godot console that is handy for debugging your (possibly faulty) SQLite queries.
285+
286+
***NOTE**: VERBOSE and higher levels might considerably slow down your queries due to excessive logging.*
287+
288+
### 🔢 <a name="sqlite-enums">SQLiteEnums</a>
289+
290+
Common enums used by the Godot SQLite plugin.
291+
292+
Result codes starting with "RC_SQLITE_" come directly from SQLite. Detailed descriptions for those result codes are available [here](https://sqlite.org/rescode.html).
293+
294+
#### ~ Enumerations ~
295+
296+
- **ResultCode**
297+
298+
- **RC_SQLITE_OK** = 0
299+
- **RC_SQLITE_ERROR** = 1
300+
- **RC_SQLITE_INTERNAL** = 2
301+
- **RC_SQLITE_PERM** = 3
302+
- **RC_SQLITE_ABORT** = 4
303+
- **RC_SQLITE_BUSY** = 5
304+
- **RC_SQLITE_LOCKED** = 6
305+
- **RC_SQLITE_NOMEM** = 7
306+
- **RC_SQLITE_READONLY** = 8
307+
- **RC_SQLITE_INTERRUPT** = 9
308+
- **RC_SQLITE_IOERR** = 10
309+
- **RC_SQLITE_CORRUPT** = 11
310+
- **RC_SQLITE_NOTFOUND** = 12
311+
- **RC_SQLITE_FULL** = 13
312+
- **RC_SQLITE_CANTOPEN** = 14
313+
- **RC_SQLITE_PROTOCOL** = 15
314+
- **RC_SQLITE_EMPTY** = 16
315+
- **RC_SQLITE_SCHEMA** = 17
316+
- **RC_SQLITE_TOOBIG** = 18
317+
- **RC_SQLITE_CONSTRAINT** = 19
318+
- **RC_SQLITE_MISMATCH** = 20
319+
- **RC_SQLITE_MISUSE** = 21
320+
- **RC_SQLITE_NOLFS** = 22
321+
- **RC_SQLITE_AUTH** = 23
322+
- **RC_SQLITE_FORMAT** = 24
323+
- **RC_SQLITE_RANGE** = 25
324+
- **RC_SQLITE_NOTADB** = 26
325+
- **RC_SQLITE_NOTICE** = 27
326+
- **RC_SQLITE_WARNING** = 28
327+
- **RC_SQLITE_ROW** = 100
328+
- **RC_SQLITE_DONE** = 101
329+
- **RC_GDSQLITE_ERROR** = 10000
330+
Error that does not correspond to any of the result codes defined by SQLite.
331+
332+
- **VerbosityLevel**
333+
334+
- **QUIET** = 0
335+
Don't print anything to the console.
336+
- **NORMAL** = 1
337+
Print essential information to the console.
338+
- **VERBOSE** = 2
339+
Print additional information to the console.
340+
- **VERY_VERBOSE** = 3
341+
Same as **VERBOSE**.
342+
282343
## Frequently Asked Questions (FAQ)
283344
284345
### 1. My query fails and returns syntax errors, what should I do?

doc_classes/SQLiteConnection.xml

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,59 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd">
55
<brief_description>
6-
A SQLite wrapper class implemented in GDExtension.
6+
Represents a connection to an SQLite database.
77
</brief_description>
88
<description>
99
[b]Example usage[/b]:
1010
[codeblock]
1111
extends Node
1212

13-
var db = SQLite.new()
13+
var conn: SQLiteConnection = null
1414

1515
func _ready():
16-
var table_name: String = "players"
17-
var table_dict: Dictionary = {
18-
"id": {"data_type":"int", "primary_key": true, "not_null": true, "auto_increment": true},
19-
"name": {"data_type":"text", "not_null": true},
20-
"portrait": {"data_type":"blob", "not_null": true}
21-
}
16+
var table_name: String = "players"
17+
var table_dict: Dictionary = {
18+
"id": {"data_type":"int", "primary_key": true, "not_null": true, "auto_increment": true},
19+
"name": {"data_type":"text", "not_null": true},
20+
"portrait": {"data_type":"blob", "not_null": true}
21+
}
2222

23-
db.path = "res://my_database"
24-
db.verbosity_level = SQLite.VerbosityLevel.NORMAL
25-
db.open_db()
23+
var params := SQLiteConnectionParams.new()
24+
params.path = "res://my_database"
25+
params.verbosity_level = SQLiteEnums.VerbosityLevel.NORMAL
2626

27-
# Check if the table already exists or not.
28-
db.query_with_bindings("SELECT name FROM sqlite_master WHERE type='table' AND name=?;", [table_name])
29-
if not db.query_result.is_empty():
30-
db.drop_table(table_name)
31-
db.create_table(table_name, table_dict)
27+
conn = SQLiteConnection.open(params)
3228

33-
var texture := preload("res://icon.png")
34-
var tex_data: PackedByteArray = texture.get_image().save_png_to_buffer()
35-
var row_dict: Dictionary = {
36-
"name": "Doomguy",
37-
"portrait": tex_data
38-
}
39-
db.insert_row(table_name, row_dict)
29+
# Check if the table already exists or not.
30+
var query_result := []
31+
var query_string := "SELECT name FROM sqlite_master WHERE type='table' AND name=?;"
32+
conn.query_with_bindings(query_string, [table_name], query_result)
33+
if not query_result.is_empty():
34+
conn.drop_table(table_name)
35+
conn.create_table(table_name, table_dict)
4036

41-
db.select_rows(table_name, "name = 'Doomguy'", ["id", "name"])
42-
print(db.query_result)
37+
var texture := preload("res://icon.png")
38+
var tex_data: PackedByteArray = texture.get_image().save_png_to_buffer()
39+
var row_dict: Dictionary = {
40+
"name": "Doomguy",
41+
"portrait": tex_data
42+
}
43+
conn.insert_row(table_name, row_dict)
44+
45+
conn.select_rows(table_name, "name = 'Doomguy'", ["id", "name"], query_result)
46+
print(query_result)
4347
[/codeblock]
4448
</description>
4549
<tutorials>
4650
<link title="Repository's README.md">https://github.com/2shady4u/godot-sqlite/blob/master/README.md</link>
4751
<link title="Script containing multiple usage examples">https://github.com/2shady4u/godot-sqlite/blob/master/demo/database.gd</link>
4852
</tutorials>
4953
<methods>
50-
<method name="open_db">
51-
<return type="ResultCode" />
52-
<description>
53-
Open a new database connection. Multiple concurrently open connections to the same database are possible.
54-
</description>
55-
</method>
56-
<method name="close_db">
54+
<method name="open">
5755
<return type="ResultCode" />
5856
<description>
59-
Close the current database connection.
57+
Creates a new [bold]SQLiteConnection[/bold] object. Multiple concurrently open connections to the same database are possible.
58+
Returns null if opening the connection failed. You can use [method get_open_error] to check the error that occurred.
6059
</description>
6160
</method>
6261
<method name="query">
@@ -122,7 +121,7 @@
122121
<method name="insert_row">
123122
<return type="ResultCode" />
124123
<description>
125-
Each key/value pair of the [code]row_dictionary[/code]-variable defines the column values of a single row.
124+
Each key/value pair of the [code]row_data[/code]-variable defines the column values of a single row.
126125
Columns should adhere to the table schema as instantiated using the [code]table_dictionary[/code]-variable and are required if their corresponding [b]"not_null"[/b]-column value is set to [code]True[/code].
127126
</description>
128127
</method>
@@ -135,7 +134,7 @@
135134
<method name="select_rows">
136135
<return type="Array" />
137136
<description>
138-
Returns the results from the latest query [b]by value[/b]; meaning that this property does not get overwritten by any successive queries.
137+
Select specific rows from a table given the conditions defined in the conditions argument. The [code]columns[/code] input argument should be an array of strings where each string is a valid column name.
139138
</description>
140139
</method>
141140
<method name="update_rows">
@@ -166,14 +165,14 @@
166165
<return type="ResultCode" />
167166
<description>
168167
Drops all database tables and imports the database structure and content encoded in JSON-formatted input buffer.
169-
Can be used together with [method SQLite.export_to_buffer] to implement database encryption.
168+
Can be used together with [method export_to_buffer] to implement database encryption.
170169
</description>
171170
</method>
172171
<method name="export_to_buffer">
173172
<return type="PackedByteArray" />
174173
<description>
175174
Returns the database structure and content as JSON-formatted buffer.
176-
Can be used together with [method SQLite.import_from_buffer] to implement database encryption.
175+
Can be used together with [method import_from_buffer] to implement database encryption.
177176
</description>
178177
</method>
179178
<method name="create_function">
@@ -188,6 +187,12 @@
188187
Check if the given database connection is or is not in autocommit mode, see [url=https://sqlite.org/c3ref/get_autocommit.html]here[/url].
189188
</description>
190189
</method>
190+
<method name="get_open_error">
191+
<return type="ResultCode" />
192+
<description>
193+
Returns the result of the last [method open] call in the current thread.
194+
</description>
195+
</method>
191196
<method name="backup_to">
192197
<return type="ResultCode" />
193198
<description>
@@ -232,7 +237,7 @@
232237
<method name="load_extension">
233238
<return type="int" />
234239
<description>
235-
Loads the extension in the given path. Does not require [method SQLite.enable_load_extension], as it only enables C-API during the call and disables it right after, utilizing the recommended extension loading method declared by the SQLite documentation ([url=https://www.sqlite.org/c3ref/load_extension.html]see[/url]). Returns the SQLite return code.
240+
Loads the extension in the given path. Does not require [method enable_load_extension], as it only enables C-API during the call and disables it right after, utilizing the recommended extension loading method declared by the SQLite documentation ([url=https://www.sqlite.org/c3ref/load_extension.html]see[/url]). Returns the SQLite return code.
236241
- [b]extension_path:[/b] the path to the compiled binary of the extension
237242
- [b]entrypoint:[/b] the extension's entrypoint method (init function). It is defined in the .c file of the extension.
238243
Example for loading the spellfix module:
@@ -244,14 +249,17 @@
244249
</methods>
245250
<members>
246251
<member name="path" type="String" default="default">
247-
Path to the database, should be set before opening the database with [code]open_db()[/code]. If no database with this name exists, a new one at the supplied path will be created. Both [code]res://[/code] and [code]user://[/code] keywords can be used to define the path.
252+
Path to the database. This is a read-only property that cannot be modified after opening the connection.
248253
</member>
249254
<member name="error_message" type="String" default="&quot;&quot;">
250255
Contains the zErrMsg returned by the SQLite query in human-readable form. An empty string corresponds with the case in which the query executed succesfully.
251256
</member>
252257
<member name="foreign_keys" type="bool" default="false">
253258
Enables or disables the availability of [url=https://www.sqlite.org/foreignkeys.html]foreign keys[/url] in the SQLite database.
254259
</member>
260+
<member name="read_only" type="bool" default="false">
261+
Was this database connection opened in read-only modus or not?
262+
</member>
255263
<member name="last_insert_rowid" type="int" default="0">
256264
Exposes the [code]sqlite3_last_insert_rowid()[/code]-method to Godot as described [url=https://www.sqlite.org/c3ref/last_insert_rowid.html]here[/url].
257265
Attempting to modify this variable directly is forbidden and throws an error.

0 commit comments

Comments
 (0)