Skip to content

Commit

Permalink
cleaned up database.gd and rebuild binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
2shady4u committed Feb 9, 2020
1 parent 8eec636 commit 7f833a2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 62 deletions.
Binary file modified demo/addons/godot-sqlite/bin/osx/libgdsqlite.dylib
100644 → 100755
Binary file not shown.
Binary file modified demo/addons/godot-sqlite/bin/win64/libgdsqlite.dll
Binary file not shown.
Binary file modified demo/addons/godot-sqlite/bin/x11/libgdsqlite.so
Binary file not shown.
128 changes: 67 additions & 61 deletions demo/database.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,22 @@ var addresses : Array = ["California","Texas","Baltimore","Richmond","Texas","At
var salaries : Array = [20000.00,15000.00,20000.00,65000.00,65000.00,65000.00,65000.00]

func _ready():

example_of_basic_database_querying()
example_of_in_memory_and_foreign_key_support()

# Basic example that goes over all the basic features available in the addon, such
# as creating and dropping tables, inserting and deleting rows and doing more elementary
# PRAGMA queries.
func example_of_basic_database_querying():

# Make a big table containing the variable types.
var table_dict : Dictionary = Dictionary()
table_dict["id"] = {"data_type":"int", "primary_key": true, "not_null": true}
table_dict["name"] = {"data_type":"text", "not_null": true}
table_dict["age"] = {"data_type":"int", "not_null": true}
table_dict["address"] = {"data_type":"char(50)"}
table_dict["salary"] = {"data_type":"real"}
#print(table_dict)


db = SQLite.new()
db.path = db_name
db.verbose_mode = true
Expand All @@ -34,7 +41,7 @@ func _ready():
db.drop_table(table_name)
# Create a table with the structure found in table_dict and add it to the database
db.create_table(table_name, table_dict)

var row_array : Array = []
var row_dict : Dictionary = Dictionary()
for i in range(0,ids.size()):
Expand All @@ -44,7 +51,7 @@ func _ready():
row_dict["address"] = addresses[i]
row_dict["salary"] = salaries[i]
row_array.append(row_dict.duplicate())

# Insert a new row in the table
db.insert_row(table_name, row_dict)
row_dict.clear()
Expand Down Expand Up @@ -88,20 +95,20 @@ func _ready():
# Do a normal query
db.query("SELECT COUNT(*) AS 'number_of_employees' FROM " + table_name + ";")
print("There are ", db.query_result[0]["number_of_employees"], " employees in the company")

db.query("PRAGMA encoding;")
print("Current database encoding is: ", db.query_result[0]["encoding"])

# Export the table to a json-file with a specified name
db.export_to_json(json_name + "_new")

# Close the current database
db.close_db()

# Import (and, consequently, open) a database from an old backup json-file
print("Overwriting database content with old backup...")
db.import_from_json(json_name + "_old")

# Check which employees were present in this old json-file
select_condition = ""
selected_array = db.select_rows(table_name, select_condition, ["*"])
Expand All @@ -112,67 +119,66 @@ func _ready():
print("salary: ", typeof(selected_array[0]["salary"]))
print("age: ", typeof(selected_array[0]["age"]))
print("name: ", typeof(selected_array[0]["name"]))

# Import the data (in a destructive manner) from the new backup json-file
print("Overwriting database content again with latest backup...")
db.import_from_json(json_name + "_new")

# Try to delete a non-existant table from the database.
if not db.delete_rows(other_table_name, "*"):
print("SQL error: " + db.error_message)

# Close the imported database
db.close_db()

# Invoke example demonstrating in-memory and foreign key support.
example_of_in_memory_and_foreign_key_support()


# This example demonstrates the in-memory and foreign key support. It's
# rather contrived, but it gets the point across.
func example_of_in_memory_and_foreign_key_support():

# Create the database as usual, and turn on verbose mode.
db = SQLite.new()
db.verbose_mode = true
# Enable in-memory storage.
db.path = ":memory:"
# Enable foreign keys.
db.foreign_keys = true
# Open the database as usual.
db.open_db()

# Create a table for all your friends.
db.create_table("friends", {
"id": {"data_type": "int", "primary_key": true, "not_null": true},
"name": {"data_type": "text", "not_null": true, "unique": true},
"hobby": {"data_type": "int", "foreign_key": "hobbies.id", "not_null": true}
})

# Create a table for all your friends' hobbies.
db.create_table("hobbies", {
"id": {"data_type": "int", "primary_key": true, "not_null": true},
"description": {"data_type": "text", "not_null": true, "unique": true}
})

# ATTENTION: The important thing to note about the "friends" table is the
# definition of the foreign key "hobbies.id". This tells SQLITE to enforce
# the foreign key constraint, and that the field "friends.hobby" is now
# tied to the field "hobbies.id". Consequently, you are now required to
# specify a valid hobby when adding a friend to the database, which in
# turn means you first need to add some hobbies to the database before
# you can add any of your friends and assign them a hobby.

# This won't work! There is no valid hobby with id 23 yet!
db.insert_rows("friends", [
{"id": 1, "name": "John", "hobby": 23}
])

# This will work! You create the hobby with id 23 first, then you can
# create your friend referencing that hobby.
db.insert_rows("hobbies", [
{"id": 23, "description": "Extreme Relaxing"}
])
db.insert_rows("friends", [
{"id": 1, "name": "John", "hobby": 23}
])

# Create the database as usual.
db = SQLite.new()
# Enable in-memory storage.
db.path = ":memory:"
db.verbose_mode = true
# Enable foreign keys.
db.foreign_keys = true
# Open the database as usual.
db.open_db()

# Create a table for all your friends.
db.create_table("friends", {
"id": {"data_type": "int", "primary_key": true, "not_null": true},
"name": {"data_type": "text", "not_null": true, "unique": true},
"hobby": {"data_type": "int", "foreign_key": "hobbies.id", "not_null": true}
})

# Create a table for all your friends' hobbies.
db.create_table("hobbies", {
"id": {"data_type": "int", "primary_key": true, "not_null": true},
"description": {"data_type": "text", "not_null": true, "unique": true}
})

# ATTENTION: The important thing to note about the "friends" table is the
# definition of the foreign key "hobbies.id". This tells SQLITE to enforce
# the foreign key constraint, and that the field "friends.hobby" is now
# tied to the field "hobbies.id". Consequently, you are now required to
# specify a valid hobby when adding a friend to the database, which in
# turn means you first need to add some hobbies to the database before
# you can add any of your friends and assign them a hobby.

# This won't work! There is no valid hobby with id 23 yet!
db.insert_rows("friends", [
{"id": 1, "name": "John", "hobby": 23}
])

# This will work! You create the hobby with id 23 first, then you can
# create your friend referencing that hobby.
db.insert_rows("hobbies", [
{"id": 23, "description": "Extreme Relaxing"}
])
db.insert_rows("friends", [
{"id": 1, "name": "John", "hobby": 23}
])

# Close the database.
db.close_db()
2 changes: 1 addition & 1 deletion godot-cpp

0 comments on commit 7f833a2

Please sign in to comment.