Skip to content

Commit e330a94

Browse files
committed
Add some examples for trigger, view and index
1 parent 761561a commit e330a94

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

demo/database.gd

+28
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,31 @@ func example_of_basic_database_querying():
144144
db.query("PRAGMA encoding;")
145145
cprint("Current database encoding is: {0}".format([db.query_result[0]["encoding"]]))
146146

147+
# Create a TRIGGER and trigger it!
148+
db.query("CREATE TRIGGER increase_salary_after_employee_termination AFTER DELETE ON " + table_name + " BEGIN UPDATE " + table_name + " SET salary = salary + 100;END;")
149+
150+
db.select_rows(table_name, "", ["name", "salary"])
151+
cprint("employees: {0}".format([str(db.query_result_by_reference)]))
152+
153+
cprint("Firing that slacker Paul!")
154+
db.delete_rows(table_name, "name = 'Paul'")
155+
156+
db.select_rows(table_name, "", ["name", "salary"])
157+
cprint("employees: {0}".format([str(db.query_result_by_reference)]))
158+
159+
# Create a VIEW and use it!
160+
db.query("CREATE VIEW cheapest_employee AS SELECT id, name FROM " + table_name + " WHERE salary = (SELECT MIN(salary) FROM company) LIMIT 1;")
161+
162+
# Fire the cheapest employee!
163+
cprint("Firing the cheapest employee!")
164+
db.delete_rows(table_name, "id = (SELECT id FROM cheapest_employee)")
165+
166+
db.select_rows(table_name, "", ["name", "salary"])
167+
cprint("employees: {0}".format([str(db.query_result_by_reference)]))
168+
169+
# Create an INDEX!
170+
db.query("CREATE INDEX idx_name ON " + table_name + "(name);")
171+
147172
# Export the table to a json-file with a specified name
148173
db.export_to_json(json_name + "_new")
149174

@@ -169,6 +194,9 @@ func example_of_basic_database_querying():
169194
cprint("Overwriting database content again with latest backup...")
170195
db.import_from_json(json_name + "_new")
171196

197+
db.query("SELECT * FROM sqlite_master;")
198+
cprint(str(db.query_result_by_reference))
199+
172200
# Try to delete a non-existant table from the database.
173201
if not db.delete_rows(other_table_name, "*"):
174202
cprint("SQL error: " + db.error_message)

src/gdsqlite.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ bool SQLite::import_from_json(String import_path) {
896896

897897
for (auto object : objects_to_import) {
898898
if (object.type != TABLE) {
899-
/* The object is an index or trigger and doesn't have any rows! */
899+
/* The object is an index, view or trigger and doesn't have any rows! */
900900
continue;
901901
}
902902

0 commit comments

Comments
 (0)