programming.nbk: Home | Index | Next Page: Tcl: Control Structures | Previous Page: Tcl Mk4Tcl: Storing and Retrieving Data


 Tcl Mk4Tcl: Subviews

Subviews

Sometimes you will want to attach multiple values of the same property values to a row. For example, in an address book, you might want to record one or more phone numbers for each entry. There are several ways that you might accomplish this. A simple solution is to add properties like "homephone", "workphone", "mobilephone" etc. to the main view. This would give you a fixed number of phone numbers you could store with each entry, and for many entries most of these might be empty.

A second approach is to define a completely separate view, with each row in the new view containing a pointer to the corresponding entry in the address book. For example, if there is a unique "addressid" property in the main address book view, we might define a new view, add records, and search for all phone numbers for a particular address book entry:

    mk::view layout db.phones "addressid type number"
    ...
    mk::row append db.phones addressid $addressid type home number 123-4567
    mk::row append db.phones addressid $addressid type work number 987-6543
    ...
    set rows [mk::select db.phones addressid $addressid]

MetaKit provides a third option, using a subview of the main view. This allows you to store the phone numbers as a view within the same row you store the rest of the address information. Like normal views, subviews contain zero or more rows, each with the same set of properties, as defined in the mk::view layout command. You can refer to rows in a subview by extending the normal row syntax, "db.viewname!rownum.subview!subviewrownum.." Here's an example of how subviews can be used:

    # create the view, including a subview named phones
    mk::view layout db.addresses {name city {phones {type number}}

    # add a row (the first one, so it will be stored at index 0)
    mk::row append db.addresses name Mark city Ancaster

    # store two phone rows in the subview
    mk::row append db.addresses!0.phones type home number 123-4567
    mk::row append db.addresses!0.phones type work number 987-6543

You can use other commands like mk::select or mk::loop that operate on views to operate on subviews as well.

If all you're doing is storing and retrieving data in a subview that is associated with the parent row, subviews may be quicker and easier than using a separate table. But if you plan on doing other searches (e.g. print out all phone numbers), the separate table approach might be better.


programming.nbk: Home | Index | Next Page: Tcl: Control Structures | Previous Page: Tcl Mk4Tcl: Storing and Retrieving Data


Notebook exported on Monday, 7 July 2008, 18:56:06 PM Eastern Daylight Time