/usr/share/doc/libgom-1.0-dev/examples/gom.py is in libgom-1.0-doc 0.3.3-4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #!/usr/bin/python3
from gi.types import GObjectMeta
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gom
# Need a metaclass until we get something like _gclass_init_
# https://bugzilla.gnome.org/show_bug.cgi?id=701843
class ItemResourceMeta(GObjectMeta):
def __init__(cls, name, bases, dct):
super(ItemResourceMeta, cls).__init__(name, bases, dct)
cls.set_table("items")
cls.set_primary_key("id")
cls.set_notnull("name")
class ItemResource(Gom.Resource, metaclass=ItemResourceMeta):
id = GObject.Property(type=int)
name = GObject.Property(type=str)
if __name__ == '__main__':
# Connect to the database
adapter = Gom.Adapter()
adapter.open_sync(":memory:")
# Create the table
repository = Gom.Repository(adapter=adapter)
repository.automatic_migrate_sync(1, [ItemResource])
# Insert an item
item = ItemResource(repository=repository, name="item1")
item.save_sync()
# Fetch the item back
item = repository.find_one_sync(ItemResource, None)
assert item.id == 1
assert item.name == 'item1'
# Insert a new item
item = ItemResource(repository=repository, name="item2")
item.save_sync()
# Fetch them all with a None filter, ordered by name
names = ['item2', 'item1']
sorting = Gom.Sorting(ItemResource, "name", Gom.SortingMode.DESCENDING)
group = repository.find_sorted_sync(ItemResource, None, sorting)
count = len(group)
assert count == 2
group.fetch_sync(0, count)
for i, item in enumerate(group):
assert item.name == names[i]
# Fetch only one of them with a filter, asynchronously
loop = GLib.MainLoop()
def fetch_cb(group, result, user_data):
group.fetch_finish(result)
item = group[0]
assert item.name == "item2"
# Close the database
adapter.close_sync()
loop.quit()
def find_cb(repository, result, user_data):
group = repository.find_finish(result)
count = len(group)
assert count == 1
group.fetch_async(0, count, fetch_cb, None)
filter = Gom.Filter.new_eq(ItemResource, "name", "item2")
group = repository.find_async(ItemResource, filter, find_cb, None)
loop.run()
|