Gossamer Forum
Home : Products : DBMan : Customization :

getting another database's field-names: a mod

Quote Reply
getting another database's field-names: a mod
JP Deni once wrote a small mod on how to import field-names from another database (http://webmagic.hypermart.net/dbman/multi12.txt).

I happen to use this mod quite often, and found it to have one disadvantage: You have to hard-code the list of fields that the other database has.

I now wrote a subroutine which gets that list of fields on the fly from the database configuration file. That way, if the field list happens to change, you always get the current version.

You can call this subroutine by passing on the name of the database setup file without the extension. For example, if I am currently using database db1 and want to access field names (and their values) from db2, I'd do this:

@db_cols = &get_db_cols("db2"); # get db2 field names

In the above mod from JPDeni, this line replaces the line starting with "@db_cols = ('Name', 'URL'".

Add the following subroutine somewhere to html.pl or db.cgi (db.pl):
----------------------------------
sub get_db_cols {

$configfile = "$_[0].cfg";
open(FILE, "<$configfile") || &cgierr("Cannot open $configfile.\n$!");
undef $/;

while (<FILE>)
{ if (/(%db_def\s+=\s+\(\s+)('.*?)(\))/s) { $fields = $2;}}
close(FILE);

while ($fields =~ /(\n')(\w+)(')/g) {push (@db_cols, $2); }
return (@db_cols);
}
---------------------------
Explanation:

What the code does is basically to open the cfg-file, look for the field list and extract the field names. It contains pattern searches based on how the field name list is written in the cfg-file: the assumption is that the field name list follows the string "%db_def = (" and is in turn followed by a closing bracket ")". Another assumption is that field-names are placed at the beginning of a new line, enclosed by single quotes. This is how the cfg-file comes "out of the box", so unless you've changed that, the sub should work fine.

I'd be glad to have suggestions to improve upon the above code (I'm not very confident in my coding skills!). Perhaps it could then be added to JPDeni's mod in the unofficial FAQ.

Cheers,

kellner
Subject Author Views Date
Thread getting another database's field-names: a mod kellner 7607 Aug 8, 2001, 11:50 AM
Thread Re: getting another database's field-names: a mod
Paul 7542 Aug 8, 2001, 1:17 PM
Thread Re: getting another database's field-names: a mod
kellner 7479 Aug 8, 2001, 3:11 PM
Thread Re: getting another database's field-names: a mod
Paul 7488 Aug 8, 2001, 3:30 PM
Post Re: getting another database's field-names: a mod
kellner 7466 Aug 8, 2001, 6:37 PM
Thread Re: [Paul] getting another database's field-names: a mod
delicia 7273 Jan 9, 2010, 3:04 PM
Thread Re: [delicia] getting another database's field-names: a mod
delicia 7255 Jan 12, 2010, 6:30 PM
Post Re: [delicia] getting another database's field-names: a mod
delicia 7229 Jan 19, 2010, 6:39 AM