Programming with SBME Databases
Essentially, working with SBME databases from a programmatic standpoint is virtually no different
to working with PPCS databases. For a fairly in-depth description please see
the section called “Object-Oriented Database Access” and Chapter 12, Using Databases in SIMPOL. The only significant
differences arise in the creation and opening of SBME databases. To open an SBME database, the
new()
method of the sbme1 is called. In that call the name of
the database file and the action to take must be specified. The action can be one of the following
characters or combinations of characters:
O — open
C — create
R — replace
OC — open if exists otherwise create
RC — replace if exists otherwise create
Once the file has been opened the sbme1 object can be used to retrieve the list of tables
and if the table name is known the opentable()
method can be used to retrieve
a reference to the table. From that point onwards things work identically to the way PPCS
works for accessing fields, indexes, and records other than a difference in terms of the types of
information available for the actual objects (properties and methods for things like readonly, required,
etc.).
Creating an SBME database is done by first opening or creating an
sbm
file and then by calling the newtable()
method. The following function is part of the db1util.sml
library that is included
in the lib
directory. The entire source code to the project can
be found in the projects\libs\db1util
directory.
function create_sbme1table_from_db1table(type(db1table) dbSrc, \ sbme1 sbmFile, string sNewTableName) export integer iResult, iErrnum string sTablename type(db1field) fld sbme1newtable sbmnt sbme1newfield sbmnfld sbme1newindex sbmnidx iResult = iIMEX_ERR_SUCCESS if dbSrc =@= .nul iResult = iIMEX_ERR_PPCSOBJNUL else if sbmFile =@= .nul iResult = iIMEX_ERR_SBMEOBJNUL else if sNewTableName > "" sTablename = sNewTableName else iErrnum = 0 if dbSrc.type =@= ppcstype1file sTablename = dbSrc.filename else if dbSrc.type =@= sbme1table sTablename = dbSrc.tablename end if end if if sTablename > "" iErrnum = 0 sbmFile.lock("shared", iErrnum) if iErrnum != 0 iResult = iIMEX_ERR_SBMEOBJLOCKED else sbmnt =@ sbmFile.newtable(sTablename) if sbmnt =@= .nul iResult = iIMEX_ERR_SBMETABLECREATEFAILED else fld =@ dbSrc.firstfield while sbmnfld =@ sbmnt.newfield(fld.name, fld.datatype) if fld.index !@= .nul and fld.datatype !@= number sbmnidx =@ sbmnt.newindex(sbmnfld, 100, "") end if fld =@ fld.next end while fld =@= dbSrc.firstfield sbmnt.create(iErrnum) if iErrnum != iSIMPOL_ERR_SUCCESS iResult = iIMEX_ERR_SBMETABLECOMMITFAILED end if sbmFile.commit() end if iErrnum = 0 sbmFile.unlock(iErrnum) end if end if end if end function iResult
From the preceding program code, specifically following the statement if sTablename > ""
the order of events when creating a new table is:
Get at least a shared lock on the sbme1 object using the
lock()
methodCreate a new table with the desired name using the
newtable()
method, which returns a sbme1newtable objectFor each field desired create a new field using the
newfield()
method of the sbme1newtable objectIf a field should also be indexed then create an index on the new field using the
newindex()
method of the sbme1newtable object and passing the reference to the sbme1newfield objectCreate the table by calling the
create()
method of the sbme1newtable objectCall the
commit()
method of the sbme1 object to write the changes back to the fileCall the
unlock()
method of the sbme1 object
There is also a number of tools and library modules being created that are intended to make importing
data and creating files easier. Watch the projects
directory for
ongoing changes.