Changes

Jump to: navigation, search

Using database API

1,788 bytes added, 12 January
m
no edit summary
{{man warn|If you are a looking Looking for documentation on how to use the Gramps system as a user instead of ? (as opposed to a program developer, it )|An index of documentation can be found on the [[Portal:Using_Gramps|Gramps documentation web page]].}}
Explanation of the basics, underlying Gramps database. '''This is not intended to be a reference manual''', but an introductory programmer's guide to using the Gramps database access routines.
Separate [httphttps://www.gramps-project.org/docs/ API Reference Documentation for current version 4.0.x of Gramps] and as well as a simple [[Media:API.svg|UML (svg) diagram for 34.41.x]] and [[Gramps Data Model]] overview are available.
Gramps is written in the [http://www.python.org Python] language. A basic familiarity with Python is required before the database objects can be effectively used. If you are new to Python, you may wish to check out the [httphttps://docs.python.org/tut2/tuttutorial/ Python 2.x tutorial] or [https://docs.python.html org/3/tutorial/ Python 3.x tutorial].
== Database API ==
At the root of any database interface is either DbReadBase and/or DbWriteBase. These define the methods to read and write to a database, respectively.
 
The SQLite database engine became an optional add-on plugin for Gramps 5.0 and then replaced BSDDB as the default database backend in the 5.1 version. The BSDDB engine is expected to be retired from the standard install for 5.2 release of Gramps.
The full database hierarchy is:
* '''DbBsddbDbGeneric''' - general read and write implementation to BSDDB databases ([http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/db/write.py gen/db/write.py])implementations** '''DbWriteBase''' - virtual and implementation-independent methods for reading data ([http://svn.code.sfsourceforge.net/p/gramps/codesource/ci/trunkmaster/tree/gramps/gen/db/base.py gen/db/base.py])** '''DbBsddbRead''' - read-only (accessors, getters) implementation to BSDDB databases ([http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/db/read.py gen/db/read.py])*** '''DbReadBase''' - virtual and implementation-independent methods for reading data ([http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/db/base.py gen/db/base.py])*** '''Callback''' - callback and signal functions ([http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/utils/callback.py gen/utils/callback.py])** '''UpdateCallback''' - callback functionality ([http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/updatecallback.py gen/updatecallback.py])** '''DbTxn''' - class for managing Gramps transactions and the undo database ([http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/db/txn.py gen/db/txn.py])* '''DbDjango''' - read and write implementation to Django-based databases ([http://svn.code.sfsourceforge.net/p/gramps/code/trunk/gramps/webapp/dbdjango.py webapp/dbdjango.py])** '''DbWriteBase''' - virtual and implementation-independent methods for reading data ([http:/source/svn.code.sf.netci/pmaster/gramps/code/trunktree/gramps/gen/db/base.py gen/db/base.py])** '''DbReadBase''' - virtual and implementation-independent methods for reading data ([http://svn.code.sf.net/p/gramps/code/trunk/gramps/gen/db/base.py gen/db/base.py])
* '''DbBsddb''' - read and write implementation to BSDDB databases ([http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/db/write.py gen/db/write.py]) (The SQLite database became optional replaced BSDDB as the default database engine in the 5.1 version** '''DbWriteBase''' - virtual and implementation-independent methods for reading data ([http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/db//base.py gen/db/base.py])** '''DbBsddbRead''' - read-only (accessors, getters) implementation to BSDDB databases ([http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/db/read.py gen/db/read.py])*** '''DbReadBase''' - virtual and implementation-independent methods for reading data ([http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/db/base.py gen/db/base.py])*** '''Callback''' - callback and signal functions ([http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/utils/callback.py gen/utils/callback.py])** '''UpdateCallback''' - callback functionality ([http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/updatecallback.py gen/updatecallback.py])** '''DbTxn''' - class for managing Gramps transactions and the undo database ([http://sourceforge.net/p/gramps/source/ci/master/tree/gramps/gen/db/txn.py gen/db/txn.py]) === DbBsddb DB-API === The DB-API interface uses a generic interface backed with the general Python SQL implementation, called [https://www.python.org/dev/peps/pep-0249/ DB-API]. The Gramps' generic interface (gramps.gen.db.generic.DbGeneric) has all of the logic for interacting with the database, except for the specific DB access code. Gramps' DB-API (gramps.plugins.db.dbapi.dbapi) implements the details so as to talk to conforming SQL databases. Other database engines could also implement the DbGeneric details, such as a nosql option.
The DbBsddb interface defines a hierarchical database (nonBy default, Gramps DB-relational) written in [http://wwwAPI uses sqlite.jcea.es/programacion/pybsddb.htm PyBSDDB]. There is no such thing as a database schemaHowever, you can also configure DB-API to use mysql, postgresql, and the meaning of the data is defined in the Python classes above. The data is stored as pickled tuples and unserialized into the primary data types (below)perhaps others.
=== DbDjango ===To be compatible with BSDDB, DB-API stores Gramps data in an identical manner (pickled tuples). However, to allow for fast access, DB-API also stores "flat" data (such as strings and integers) in secondary SQL fields. These are indexed so that data can be selected without having to traverse, unpickle, initialize objects, and compare properties.
The DbDjango interface defines the Gramps data in terms of ''models'' and ''relations'' from the [http://www.djangoproject.com/ Django project]. The database backend can be any implementation that supports Django, including such popular SQL implementations as sqlite, MySQL, Postgresql, and Oracle. The data is retrieved from the SQL fields, serialized and then unserialized into the primary data types (below).=== DbBsddb ===
For more details on The DbBsddb interface defines a hierarchical database (non-relational) written in [http://www.jcea.es/programacion/pybsddb.htm PyBSDDB]. There is no such thing as a database schema, and the meaning of the data is defined in the Python classes above. The data is stored as pickled tuples and unserialized into the Gramps Django project, see [[GEPS 013: Gramps WebappUsing_database_API#Primary_Objects|primary data types (below)]].
=== Using the Database ===
==Primary Objects==
Primary objects are the fundamental objects in the Gramps database. These objects are:
* [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.person Person] - Contains the information specific to an individual person.* [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.family Family] - Contains the information specific to relationships between people. This typically contains one or two parents and zero or more children.* [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.event Event] - Contains the information related to an event. The event is treated as a primary object in the database, it currently does not appear as a primary object to the end user.* [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.place Place] - Contains the information related to a specific place.* [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.src Sourcerepo Repository] - Contains the information related to a source of informationrepository.* Citation [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.src Source] - Contains the information related to a citation into a sourceof information.* [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.mediaobj Media Objectcitation Citation] - Contains the information related to a media object. This includes images, documents, or any other type of related filescitation into a source.* [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.repo Repositorymediaobj Media Object] - Contains the information related to a repositorymedia object. This includes images, documents, or any other type of related files.* [http://www.gramps-project.org/docs/gen/gen_lib.html#module-gramps.gen.lib.note Note] - Contains the information related to a note.
# gramps_id
# title
# long (longitude)# lat (latitude)# place_ref_listplaceref_list
# name
# alternate_namesalt_names# typeplace_type
# code
# alternate_locations alt_loc (deprecated)
# media_list
# citation_list
# title
# author
# publication_infopubinfo (publication information)
# note_list
# media_list
# abbrev (abbreviation)
# change
# source_attribute_listattribute_list# repo_ref_listreporef_list
# tag_list
# private
# note_list
# media_list
# source_attribute_listattribute_list
# change
# tag_list
# path
# mime
# desc (description)
# checksum
# attribute_list
== Secondary Objects ==
In addition, there are a number of secondary objects. In the DbBsddb implementation, these are stored in the primary objects. Typically, this means that DbBsddb objects are stored in [httphttps://wwwdocs.python.org/doc/current3.4/liblibrary/module-pickle.html pickled] format. In the DbDjango implemetation, the secondary objects are additional tables.
The secondary objects include dates, addresses, and source references among other objects.
For this reason, it is always necessary to have reference to the database that contains the objects with which you are working.
The handle should not be visible to the end user, and should not be included in reports or displayed on screen. Instead, the GRAMPS Gramps ID value should be used for this purpose. The GRAMPS Gramps ID is a user defined value for each object, but is not used internally to the database. This allows the user to change the GRAMPS Gramps ID without affecting the database.
Once created, the handle should never be modified.
==== See also ====
Discourse forum discussions:
* [https://gramps.discourse.group/t/reversing-an-object-handle-to-its-creation-date/2614 Reversing an object handle to its creation date]
* [https://gramps.discourse.group/t/question-about-the-gramps-handle-id/248 Question about the Gramps Handle ID]
* [https://gramps.discourse.group/t/reversing-an-object-handle-to-its-creation-date/2614/5 Creation timestamp from a handle] SuperTool script
[https://github.com/gramps-project/gramps/blob/master/gramps/gen/utils/id.py#L53 Line 53 : Creating a Handle] gramps/gen/utils/id.py source code
===Object lifetime===
Care must be taken when working with primary objects. Each instance that is retrieved from the database is unique, and not connected to other instances. The example below exhibits the issue:
person1.set_nickname('Fred')
</pre>
In this case, even though person1 and person2 represent the same person, but they are distinct objects. Changing the nickname of person1 does not affect person2. The person2 object will retain the original nickname.
Changes to the object do not become permanent until the object has been committed to the database. If multiple instances exist in memory at the same time, care must be taken to make sure that data is not lost.
===Transactions and Commits===
In order to support an UNDO feature, the database has the concept of [httphttps://gramps-project.org/docs/gen/gen_db.html#bsddbmodule-gramps.gen.db.txn Transactions].
Transactions are a group of related database commit operations that need treated as a single unit. If all related commits are not undone as a single unit, the database can be left in a corrupted state. The UNDO operation will undo all the commits in the transaction.
for handle in database.get_person_handles():
person = database.get_person_from_handle(handle)
A more efficient method exists, but is more complicated to use. The database can provide a [httphttps://www.gramps-project.org/devdocdocs/apigen/2gen_db.2/private/GrampsDbhtml#module-gramps._GrampsDbBasegen.GrampsCursor-classdb.html cursor cursor] that will iterate through the database without having to load all handles into memory. The cursor returns a handle, data pair. The data represents the serialized data directly from the database. It is the users responsibility to unserialize the data. An example is below:
cursor = database.get_person_cursor()
pair = cursor.first()
database.connect('person-update',self.update_view)
database.connect('person-rebuild',self.update_view)
A full list of the signals that are emitted from the database can be found at the top of the <tt>[https://gramps-project.org/docs/gen/gen_db.html#module-gramps.gen.db.base GrampsDbBase]</tt> class in the <tt>GrampsDbBase[https://github.com/gramps-project/gramps/blob/master/gramps/gen/db/base.py gramps/gen/db/base.py]</tt> module.
[[Category:Developers/Tutorials]]
4,608
edits

Navigation menu