Postmodern Examples Table of Contents Doquery
Postmodern allows you to have a relatively simple but straight forward matching of clos classes to a database table. A sample is as follows:
Note that the class needs to be modified slightly. Three specific points here. First, to specify that a slot refers to a column, it needs a :col-type option. Second, the class definition includes a metaclass reference to postmodern:dao-class. Third, the table-name and primary keys are specified.
Notice the slot named something-with-null. Instead of having an :initform, it has a :col-default :null specification. This ensures that the default in the database for this field is null, but it does not bound the slot to a default form. Thus, making an instance of the class without initializing this slot will leave it in an unbound state.
You can have multiple primary keys (a composite key) in postgresql/postmodern.
If you need to specify the schema for the table name in the dao definition, notice that the table name is really a string. The proper specification would be like this:
Assuming you have a class whose instances will contain other classes which themselves have separate tables, you don't include the :col-type in the definition.
Postmodern allows you to return daos that match classes and tables. Given a class as described above, the standard call just uses the name of the table and the primary key to get the class instance out of the table. More on daos later.
Here are two simple examples of the calls to the database. First, get-dao returns a single instance of a countries class from the database:
Second, select-dao return instances of the countries class out of the database query of 'id greater than 200, sorted by name.
NOTE: if you have added fields to the database table without updating the class definition, get-dao and select-dao will throw errors.
This may cause your application to appear to hang unless you have the necessary condition handling in your code.
Assume that you want to pull out all the data as classes, the following will pull that as a list of daos, sorted by name, with id greater than 0.
If for some reason, you wanted the list in reverse alphabetical order, then:
Finally, the following adds a second condition, that the name be greater than "Turkmenistan" and shows it in context, printing out a list of the names of the activty-types daos returned:
If you want to display fields in a record which matches a dao class that you have set up, you can call get-dao with the name of table and the primary key. In this example, the table is "countries and the primary key happens to be the field "id" with a value of 1.
For example, assume we pull a dao object out of our countries table for Australia:
Notice that the region-id field has an integer value. This works. But assume it has a slot of region-id, which refers to an id in the table "regions" and you want the name of the region displayed rather than the region-id. There is a hack using with-column-writers which essentially writes the name into the link slot. Now, we write a function that uses the with-column-writers macro and pull in the actual region name from the regions table.
Normally calling the accessor region-id would return an integer, but now it is returning the name of the region. if you are using the dao as a simple way to get the relevant data out of the database and you are just going to display this value, this saves you from having to make additional database calls. Otherwise, you would have to make an additional call to get the information from all the foreign tables.
Postmodern examples >