/* @(#)validate_schema.c 2.1 93/05/18 */ /******************************************************************************/ /* function : validate_schema.c */ /* */ /* subsystem : chgen */ /* */ /* input : (accesses tt and ta structures) */ /* */ /* output : (error messages) */ /* */ /* returns : void (exits upon fatal errors) */ /* */ /* author : Steve Smith / Craig Smith */ /* */ /* created : July, 1991 */ /* */ /* revisions : 93s5232: changed code to incorporate integer keys: */ /* strn(cmp/cpy) funcs need to use hcg_abbr_size, error */ /* messages need to output hcg_abbr_size chars from key. */ /* */ /* description : This routine will traverse the tt and ta structures, looking */ /* for various error and warning conditions. The conditions */ /* detected are: */ /* - duplicate table names */ /* - duplicate filed names (within each table) */ /* - forward references (fkey to table defined later in file) */ /* - undefined references (fkey to unknown table) */ /* */ /******************************************************************************/ #include #include #include "chgen_define.h" #include "chgen_externs.h" /* * This function performs some basic validations on the schema * which is now loaded into the internal tt/ta lists. */ void validate_schema() { static char rcsid[] = "$Id: validate_schema.c,v 1.2.4.1 1999/05/04 17:00:25 jkarner Exp $"; int num_errors = 0; int found_reference; int found_after; char temp_string[ABBREV_NAME_LENGTH]; /*********************************************************/ /* Search for duplicate table names. */ /*********************************************************/ tt_curr = tt; while (tt_curr != NULL) { tt_tmp = tt_curr->next_ptr; while (tt_tmp != NULL) { if (strcmp(tt_curr->TTabbr,tt_tmp->TTabbr) == 0) { printf("CHGEN-F-DUPTBLID, duplicate table-id (%s) found in schema\n",tt_curr->TTabbr); num_errors++; } if (strcmp(tt_curr->TableName,tt_tmp->TableName) == 0) { printf("CHGEN-F-DUPTBLNAME, duplicate table-name (%s) found in schema\n",tt_curr->TableName); num_errors++; } tt_tmp = tt_tmp->next_ptr; } tt_curr = tt_curr->next_ptr; } /*********************************************************/ /* For each table, check for duplicate field names. */ /*********************************************************/ tt_curr = tt; while (tt_curr != NULL) { ta_curr = tt_curr->ta_ptr; while (ta_curr != NULL) { ta_tmp = ta_curr->next_ptr; while (ta_tmp != NULL) { if (strcmp(ta_curr->FieldName,ta_tmp->FieldName) == 0) { printf("CHGEN-F-DUPTBLID, duplicate field-name (%s) found in table %s (%s)\n", ta_curr->FieldName,tt_curr->TableName,tt_curr->TTabbr); num_errors++; } ta_tmp = ta_tmp->next_ptr; } ta_curr = ta_curr->next_ptr; } tt_curr = tt_curr->next_ptr; } /*********************************************************/ /* For each table, check for forward references. */ /* to do this, loop over tables, and within each, loop */ /* over its fields. If a field is a foreign key (but */ /* not the primary key), then search the list of tables */ /* for it. If it is found before this table, ok. if it */ /* is found after this table its a forward reference. */ /* If it is not found at all, its a reference to an */ /* unknown table. */ /*********************************************************/ tt_curr = tt; while (tt_curr != NULL) { ta_curr = tt_curr->ta_ptr; while (ta_curr != NULL) { if ((ta_curr != tt_curr->ta_ptr) && (ta_curr->IsKey) && (strncmp(ta_curr->FieldName,tt_curr->TTabbr,hcg_abbr_size) != 0)) /* its a foreign key to some other table */ { found_reference = 0; found_after = 0; tt_tmp = tt; while (tt_tmp != NULL) { if ((tt_curr == tt_tmp) && (!found_reference)) found_after = 1; if (strncmp(ta_curr->FieldName,tt_tmp->TTabbr,hcg_abbr_size) == 0) { found_reference = 1; break; } tt_tmp = tt_tmp->next_ptr; } if (!found_reference) { strncpy_null(temp_string, ta_curr->FieldName, hcg_abbr_size); printf("CHGEN-F-UNKNOWNTABLEREF, unknown table %s referenced as a foreign key (%s) in table %s (%s)\n", temp_string,ta_curr->FieldName,tt_curr->TableName,tt_curr->TTabbr); num_errors++; } else if (found_after) { if (!cli_quiet) printf("CHGEN-I-FORWARDREF, forward reference to table %s (%s) found in table %s (%s) via foreign key field %s\n", tt_tmp->TableName,tt_tmp->TTabbr,tt_curr->TableName,tt_curr->TTabbr,ta_curr->FieldName); } } /* its a foreign key */ ta_curr = ta_curr->next_ptr; } tt_curr = tt_curr->next_ptr; } if (num_errors > 0) exit(1); return; }