GenroDb
The most important class, defined in the module gnrsql is GenroDb. An instance of GenroDb represents the database itself.
A GenroDb instance is composed by the following parts:
- Connection parameters (host, port, user, password)
- A tree that represents the description of database's structure. (GnrDataStructure?)
- A tree composed by objects that represents database's element such as SqlPackage?, SqlTable?, SqlColumn?. (SqlStruct?)
GenroDb provides several methods for walking among tables' relations, for inspecting data and executing SQL statements.
How to create a Database with Genro
Preparing description
First of all a database description must be prepared using the class GnrDataStructure?. In our example we want to create a simple database containing movies, directors and dvd. In order to do this we have to write a mixin class and define one single method called configure. In the configure method we describe our database.
In this first part we define a package, a logical structure that represents a set of table. The instruction package adds a package element to the recipe. In our model must be defined at least one package as root element.
class DvdCatalog(object):
def configure(self):
pkg_ctlg = self.structdata.package('ct',
comment='My movie catalog',
name_short='Ctlg',
name_long='Video Catalog',
name_full='John's video catalog')
Then we start adding tables to the package: director, movie, dvd.
director = pkg_ctlg.table('director', name_short='Dir',name_long='Director', name_full='Director table')
For each table we can add columns with the functions column, intColumn, dateColumn, etc. Table's primary key is set with the function pkey.
dir_id = director.intColumn('id')
director.pkey('id')
A column can be related to another with the method ralate.
movie.column('director', name_short='Dir',name_long='Director').relate(dir_id)
Db creation
Now we have
