| 
     
      
      
      From: Brian B. <ca...@um...> - 2000-04-22 21:29:59
      
     
   | 
Carl,
Sorry to have taken so long with this, been very busy.
This patch doesn't apply cleanly, can you double check it? the part with
"void do_first () {" looks like something is missing there.
Brian
On Mon, 10 Apr 2000, Carl Seutter wrote:
> Ok, here's the initial cut at generating foreign keys.  To accomplish
> this, I basically set about reading the MSysRelationships table using
> existing routines.  I made the following changes.  There were 4 files
> that I made changes to include/mdbtools.h, libmdb/mem.c,
> libmdb/backend.c, and util/schema.c.
> 
> In include/mdbtools.h:
>   --  I added this to the MdbHandle structure
>             char            *backend_name;
>   -- I added this to the backend.c declarations
>            extern char *mdb_get_relationships(MdbHandle *mdb);
> 
> In libmdb/mem.c:
>    -- I added to the mdb_free_handle function
>              if (mdb->backend_name) free(mdb->backend_name);
> 
> In util/schema.c
>    -- I added the variable declaration of
>            char *the_relation;
>    -- I placed this code just before the call to mdb_free_handle
>             fprintf (stdout, "\n\n");
>             fprintf (stdout, "-- CREATE ANY Relationships ...\n");
>             fprintf (stdout, "\n");
>             the_relation=mdb_get_relationships(mdb);
>             while (the_relation[0] != '\0') {
>                   fprintf(stdout,"%s\n",the_relation);
>                  the_relation=mdb_get_relationships(mdb);
>              }
> 
> In libmdb/schema.c:
>   -- I added these variables to the beginning:
>             char *bound_values[256];
>              char *relationships[4];
>              MdbColumn *col;
>              MdbCatalogEntry entry;
>              MdbTableDef *table;
>              int did_first;
> 
>   -- I added this code to mdb_set_default_backend before the return 1
>                 mdb->backend_name = (char *)
> malloc(strlen(backend_name)+1);
>                 strcpy(mdb->backend_name,backend_name);
>                 did_first = 0;
> 
>  -- I added this function:
> char *mdb_get_relationships(MdbHandle *mdb) {
> 
> int   i, j, k;
> static char text[255];
>   void do_first () {
>     mdb_read_catalog (mdb, MDB_TABLE);
> 
>     /* loop over each entry in the catalog */
>     for (i=0; i < mdb->num_catalog; i++) {
>       entry = g_array_index (mdb->catalog, MdbCatalogEntry, i);
>       if ((entry.object_type == MDB_TABLE) &&
>             (strncmp (entry.object_name, "MSysRelationships", 17) == 0))
> {
>     table = mdb_read_table (&entry);
>            if ( table->num_rows > 0 ) {
>              mdb_read_columns(table);
>              mdb_rewind_table(table);
>              for (k=0;k<table->num_cols;k++) {
>                bound_values[k] = (char *) malloc(256);
>                bound_values[k][0] = '\0';
>                mdb_bind_column(table,k+1,bound_values[k]);
>              }
>              relationships[0] = (char *) malloc(256); /* child column */
> 
>              relationships[1] = (char *) malloc(256); /* child table */
>              relationships[2] = (char *) malloc(256); /* parent column
> */
>              relationships[3] = (char *) malloc(256); /* parent table */
> 
>           }
>           did_first = 1;
>           return;
>       }
>     }
>   }
> /*
>  * generate relationships by "reading" the MSysRelationships table
>  *   szColumn contains the column name of the child table
>  *   szObject contains the table name of the child table
>  *   szReferencedColumn contains the column name of the parent table
>  *   szReferencedObject contains the table name of the parent table
>  */
>   sprintf(text,"%c",0);
>   if ( did_first == 0)
>     do_first();
>   if (table->cur_row < table->num_rows) {
>     if (mdb_fetch_row(table)) {
>        relationships[0][0] = '\0';
>        relationships[1][0] = '\0';
>        relationships[2][0] = '\0';
>        relationships[3][0] = '\0';
>        for (k=0;k<table->num_cols;k++) {
>           col=g_ptr_array_index(table->columns,k);
>           if (strncmp(col->name,"szColumn",8) == 0)
>              strcpy(relationships[0],bound_values[k]);
>           else if (strncmp(col->name,"szObject",8) == 0)
>              strcpy(relationships[1],bound_values[k]);
>           else if (strncmp(col->name,"szReferencedColumn",18) == 0)
>              strcpy(relationships[2],bound_values[k]);
>           else if (strncmp(col->name,"szReferencedObject",18) == 0)
>              strcpy(relationships[3],bound_values[k]);
>        }
>        if (strncmp(mdb->backend_name,"oracle",6) == 0)
>          sprintf(text,"alter table %s add constraint %s_%s foreign key
> (%s) references %s(%s)",
> 
> relationships[1],relationships[3],relationships[1],
> 
> relationships[0],relationships[3],relationships[2]);
>         else
>           sprintf(text,"relationships are not supported for
> %s",mdb->backend_name);
>     } /* got a row */
>   }
>   else {
>     for (k=0;k<table->num_cols;k++) {
>        free(bound_values[k]);
>     }
>     free(relationships[0]);
>     free(relationships[1]);
>     free(relationships[2]);
>     free(relationships[3]);
>     did_first = 0;
>   }
>   return text;
> }
> 
> 
> 
> _______________________________________________
> mdbtools-dev mailing list
> mdb...@li...
> http://lists.sourceforge.net/mailman/listinfo/mdbtools-dev
> 
 |