Chapter 7. Generic database access classes

main classes

This chapter discusses abstract two generic classes. An element and an aggregate class.

Database abstraction layer main classes

Figure 7-1. Database abstraction layer main classes schema

Database abstraction layer main classes are UserDb, DataSupplierDb, SmsProviderDb, ServiceDb, LoneuserDb and ServiceInstanceDb, all derived from GenericDb. They are one-to-one representations of corresponding tables, having all the fields from table as their attributes (see database description). In addition to this, they all have four public methods defined:

<classname>(p_ID=0) - constructor of the class. If given nonzero parameter, it initializes object to instance with given ID from database.

load() - loads object attributes from database, finding it by attribute ID. Returns ID if successful, otherwise 0.

save() - saves object to database. Returns ID if successful, otherwise returns false.

valid(p_attname) - If name of attribute given, returns true if attribute has valid value. With no parameters, returns true if values of all attributes are valid.

In addition to these four, they may have any number of fetchBy methods defined - to load object by something else than ID.

Method loadBySql(p_sql) is used by derived classes to easily extend their functionality with fetchBy() methods. It loads object from database using given part of SQL SELECT statement - starting with FROM clause until the end of SELECT. For example, to load user by name, loadBySql(“FROM user WHERE name = 'John'”) could be used. Method returns ID if exactly one record was selected, otherwise returns false.

ClientDb

[Object-ClientDb.png]

This class is associated with the AccountDb class. It provides operations for managing a client information record. Usually an object of class AccountDb is used to determine what client record to load.

Database abstraction layer list classes

Figure 7-2. Database abstraction layer list classes schema

These classes are used to retrieve lists of basic database objects.

Generic_list: Abstract class that other list classes derive from.

Attributes:

a_member - array of objects in this list.

cursor - cursor position in a_member, used by next() to retrieve next member.

Methods:

Generic_list() - constructor.

count() - returns number of objects in list.

next() - returns next element in list or 0 if beyond the end of list.

User_list: list of User objects.

Additional methods:

User_list() - constructor.

fetch_by_name(p_name_mask) - load all User objects, whose name matches regexp p_name_mask, “.*” by default.

fetch_by_supplier(p_supplier_ID, p_name_mask) - load all user objects for whom data supplier with p_supplier_ID has user_alias. Additionally filter with regexp p_name_mask, “.*” by default.

Service_instance_list: list of Service_instance objects.

Additional methods:

Service_instance_list() - constructor.

fetch_by_name(p_name_mask) - load all User objects, whose name matches regexp p_name_mask, “.*” by default.

fetch_by_user(p_user_ID, p_name_mask) - load all service instance objects for user p_user_ID. Additionally filter with regexp p_name_mask, “.*” by default.

Service_list: list of Service objects.

Additional methods:

Service_list() - constructor.

fetch_by_name(p_name_mask) - load all Service objects, whose name matches regexp p_name_mask, “.*” by default.