📄 ug_ch5c.htm
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"><meta http-equiv="content-type" content="text/html; charset=us-ascii"><meta name="Generator" content="Microsoft Word 97"><title>db.* User's Guide Chapter 5</title></head><body><h2><a name="Creation" id="Creation"></a>5.5 Data Creation</h2><p><font size="2">The <b><i>db.*</i></b> functions used to createrecord and key occurrences and to make set connections are listedin Table 5-5 below.</font></p><p align="center"><b>Table 5-5 Data Creation Functions</b></p><table cellspacing="0" border="0" cellpadding="7" width="542"><tr><td width="39%" valign="top"><p><font size="2"><b>Record/Key Creation Functions</b></font></p></td><td width="61%" valign="top"><p><font size="2"><b>Description</b></font></p></td></tr><tr><td width="39%" valign="top"><p><font size="2"><b>d_fillnew</b>(<i>REC</i>, <i>val, task,dbn</i>)</font></p></td><td width="61%" valign="top"><p><font size="2">Create and fill contents of new recordoccurrence.</font></p></td></tr><tr><td width="39%" valign="top"><p><font size="2"><b>d_setkey</b>(<i>FLD</i>, <i>val</i>, <i>task,dbn</i>)</font></p></td><td width="61%" valign="top"><p><font size="2">Set key field value for new record.</font></p></td></tr><tr><td width="39%" valign="top"><p><font size="2"><b>d_makenew</b>(<i>REC</i>, <i>task,dbn</i>)</font></p></td><td width="61%" valign="top"><p><font size="2">Make a new record occurrence slot and storeassociated keys.</font></p></td></tr><tr><td width="39%" valign="top"><p><font size="2"><b>d_crwrite</b>(<i>FLD</i>, <i>val</i>, <i>task,dbn</i>)</font></p></td><td width="61%" valign="top"><p><font size="2">Write data to specified field of new recordoccurrence.</font></p></td></tr><tr><td width="39%" valign="top"><p><font size="2"><b>d_keystore</b>(<i>FLD</i>, <i>task,dbn</i>)</font></p></td><td width="61%" valign="top"><p><font size="2">Create optional key entry.</font></p></td></tr><tr><td valign="top" colspan="2"> </td></tr><tr><td valign="top" colspan="2"><p><font size="2"><b>Set Creation Function</b></font></p></td></tr><tr><td width="39%" valign="top"><p><font size="2"><b>d_connect</b>(<i>SET</i>, <i>task,dbn</i>)</font></p></td><td width="61%" valign="top"><p><font size="2">Connect new record to <i>SET</i>.</font></p></td></tr></table><p><font size="2">New record occurrences are entered into thedatabase by using either the <b>d_fillnew</b> or <b>d_makenew</b>function. To <b>d_fillnew</b> is passed the record type of therecord to be created and a pointer to the record's value. Thispointer usually points to a variable of that record's <b>struct</b>type, as declared in file <b><i>dbname</i>.h</b>. Function<b>d_makenew</b> creates an (almost) empty record occurrence, inwhich the field values will be stored later, usually throughindividual calls to <b>d_crwrite</b>. However, <b>d_makenew</b>must store the key fields in the record and create the key fileentries at the time of the call. Prior to the call to<b>d_makenew</b>, function <b>d_setkey</b> must be called for each(non-optional) key field, to save the values of each key field for<b>d_makenew</b>. Generally, it is simplest to use only<b>d_fillnew</b>, which automatically creates the key entries foryou without a call to <b>d_setkey</b>. Function <b>d_makenew</b> isuseful when a record is to be created before the contents of thefields are known, or for creation of records that have no fields(such as an empty intersection record of a many-to-manyset).</font></p><p>Function <b>d_keystore</b> is used to create the key fileentries for optional keys in the current record. Optional keys areoften used to defer key creation until non-peak system load times,in order to maximize data entry performance.</p><p>The data creation process involves not only record creation, butset creation as well. After a record has been created, it oftenneeds to be connected to appropriate sets. This may involve dataretrieval to set up the currency tables properly.</p><p>Entry of an <b>info</b> record into the <b>tims</b> databaseprovides a good illustration of what is typically involved increating <b><i>db.*</i></b> data.</p><p>Three functions are presented below. Function <b>ent_info</b>enters the <b>info</b> record and, if necessary, the <b>author</b>record, and makes the proper set connections. Function<b>enter_key_words</b> is called by <b>enter_info</b> to read eachkey word from the user and set up the many-to-many relationshipwith the entered <b>info</b> record. Function <b>enter_abstract</b>is called to read from the user each line of the abstract andconnect it to the <b>info</b> record through set abstract. Onefunction that is called but not shown is function <b>get_info</b>,which reads from the user, the author name, and <b>info</b> recordfields.</p><pre><font color="#0000FF">#include <stdio.h>#include "db.star.h"#include "tims.h"static struct info irec; /* info record variable */static struct author arec; /* author record variable */extern DB_TASK *task;/* Enter technical information records into TIMS database */ent_info(){ char s[SIZEOF_NAME]; /* generic string variable */ int status; /* enter tech info into TIMS database */ while (get_info() != EOF) { /* see if author exists */ for ( status = d_findfm(AUTHOR_LIST, task, CURR_DB); status == S_OKAY; status = d_findnm(AUTHOR_LIST, task, CURR_DB)) { d_crread(NAME, s, task, CURR_DB); if (strcmp(arec.name, s) == 0) break; /* author record on file */ } if (status == S_EOS) { /* author not on file -- create record and connect to author list */ d_fillnew(AUTHOR, &arec, task, CURR_DB); d_connect(AUTHOR_LIST, task, CURR_DB); } /* make author current owner of has_published set */ d_setor(HAS_PUBLISHED, task, CURR_DB); /* create new tech. info record */ if (d_fillnew(INFO, &irec, task, CURR_DB) == S_DUPLICATE) printf("duplicate id_code: %s\n", irec.id_code); else { /* connect to author record */ d_connect(HAS_PUBLISHED, task, CURR_DB); /* set current owner for key words and abstract */ d_setor(INFO_TO_KEY, task, CURR_DB); d_setor(ABSTRACT, task, CURR_DB); enter_key_words(); enter_abstract(); } }}</font></pre><p><font size="2">After the data has been collected from the user(<b>get_info</b>), function <b>ent_info</b> is used to scan the<b>author_list</b> set for the user-specified author name. If nomatch is found (status == S_EOS), an <b>author</b> record iscreated and connected to <b>author_list</b> (the current owner of<b>author_list</b> is always the <b>system</b> record). The foundor newly created <b>author</b> record is set as the current ownerof set <b>has_published</b>, using <b>d_setor</b>. The <b>info</b>record is created and if its <b>id_code</b> is not a duplicate, theid is connected to its <b>author</b> record. The new <b>info</b>record occurrence is then made the current owner of sets<b>info_to_key</b> and <b>abstract</b>. Key words and abstract textwill be connected to it by the function calls thatfollow.</font></p><pre><font color="#0000FF">/* Enter any key words */static enter_key_words(){ char s[SIZEOF_KWORD]; for ( ; ; ) { printf("key word: "); if ((getstring(s,SIZEOF_KWORD) == NULL) || (s[0] == '\0')) break; /* see if key word record exists */ if (d_keyfind(KWORD, s, task, CURR_DB) == S_NOTFOUND) { /* create new key word record */ d_setkey(KWORD, s, task, CURR_DB); d_makenew(KEY_WORD, task, CURR_DB); } /* create empty intersection record */ d_setor(KEY_TO_INFO, task, CURR_DB); d_makenew(INTERSECT, task, CURR_DB); d_crwrite(INT_TYPE, &irec.info_type, task, CURR_DB); d_connect(KEY_TO_INFO, task, CURR_DB); d_connect(INFO_TO_KEY, task, CURR_DB); }}</font></pre><p><font size="2">Function <b>enter_key_words</b> shows how tocreate many-to-many relationships. For each key word entered by theuser, <b>d_keyfind</b> is called to check if it is already on file.If not, a new <b>key_word</b> record is created. The found (or new)<b>key_word</b> is then set as the current owner of the<b>key_to_info</b> set, and an <b>intersect</b> record is created,which is then connected to both the <b>key_word</b> record throughset <b>key_to_info</b> and the <b>info</b> record through set<b>info_to_key</b>.</font></p><pre><font color="#0000FF">/* Enter abstract description */static enter_abstract(){ struct infotext infotext; for ( ; ; ) { printf("abstract: "); if ((getstring(infotext.line, SIZEOF_LINE) == NULL) || infotext.line[0] == '\0') return; d_fillnew(INFOTEXT, &infotext.line, task, CURR_DB); d_connect(ABSTRACT, task, CURR_DB); }}</font></pre><p><font size="2">Function <b>enter_abstract</b> is very simple. Aseach line of abstract text is entered, the text record is createdand connected to the <b>info</b> record, which is the current ownerof set <b>abstract</b>.</font></p><h2><a name="Modification" id="Modification"></a>5.6 DataModification</h2><p><font size="2">The functions used to modify fields, records, andsets are shown in Table 5-6.</font></p><p align="center"><b>Table 5-6 Modification Functions</b></p><table cellspacing="0" border="0" cellpadding="7" width="542"><tr><td valign="top" colspan="2"><p><font size="2"><b>Record/Key ModificationFunctions</b></font></p></td></tr><tr><td width="42%" valign="top"><p><font size="2"><b>d_recwrite</b>(<i>val</i>, <i>task,dbn</i>)</font></p></td><td width="58%" valign="top"><p><font size="2">Write contents of current record.</font></p></td></tr><tr><td width="42%" valign="top"><p><font size="2"><b>d_crwrite</b>(<i>FLD</i>, <i>val</i>, <i>task,dbn</i>)</font></p></td><td width="58%" valign="top"><p><font size="2">Write data in field of current record.</font></p></td></tr><tr><td width="42%" valign="top"><p><font size="2"><b>d_csmwrite</b>(<i>SET</i>, <i>FLD</i>,<i>val</i>, <i>task, dbn</i>)</font></p></td><td width="58%" valign="top"><p><font size="2">Write data in field of current member ofset.</font></p></td></tr><tr><td width="42%" valign="top"><p><font size="2"><b>d_csowrite</b>(<i>SET</i>, <i>FLD</i>,<i>val</i>, <i>task, dbn</i>)</font></p></td><td width="58%" valign="top"><p><font size="2">Write data in field of current owner ofset.</font></p></td></tr><tr><td valign="top" colspan="2"> </td></tr><tr><td valign="top" colspan="2"><p><font size="2"><b>Set Modification Functions</b></font></p></td></tr><tr><td width="42%" valign="top"><p><font size="2"><b>d_discon</b>(<i>SET</i>, <i>task,dbn</i>)</font></p></td><td width="58%" valign="top"><p><font size="2">Disconnect current member from set.</font></p></td></tr><tr><td width="42%" valign="top"><p><font size="2"><b>d_connect</b>(<i>SET</i>, <i>task,dbn</i>)</font></p></td><td width="58%" valign="top"><p><font size="2">Connect current record to set.</font></p></td></tr></table><p><font size="2">Modification of data is very straightforward. Tomodify a record or field, you first retrieve the record, read therecord or field contents, make the desired changes and then writeout the updated data using a record modificationfunction.</font></p><p>Set modifications often involve disconnecting a record from oneset and connecting it to another.</p><blockquote>Note<b><i>:</i></b> If two or more fields in a record
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -