Gossamer Forum
Home : Products : DBMan : Customization :

have dbman write to two files

Quote Reply
have dbman write to two files
Hi, this sounds like an easy one....famous last words....can I have dbman write two files when records are added/changed? Right now I change the first file, default.db by using dbman as usual......then I run a cron job at 12.01 that copies that file to a different directory for a different use....and yes, I would like to maintain the two files seperately, and I also need updates right away instead of over night.......so what I would like is to just insert a little extra code for when dbman writes data.....now it writes to the usual file plus the second one....in the case of the second one it can just copy the default file over the second file. I searched a bit but didn't find a solution....
Quote Reply
Re: [steven99] have dbman write to two files In reply to
Looking at one solution in the following thread:

Subject \n as delimeter
ozozgur 29-May-00

================

Is it possible to add a sub, which will work after each add, modify or delete, and will open the .db, replace all pipes with \n and save it as a different file - which will be used by my java?
I think this will help me, right? The problem: I dont know how to add such a sub! :))
----------------------------------------------------------
Response: JPDeni

Yes, you could do that. It will slow things down quite a bit, but you could do it.

Add the following to db.cgi

sub create_mirror_file {
#-------------------------------------------------------------
$output = '';
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
@lines = <DB>;
close DB;
foreach $line (@lines) {
$line =~ s/\|/\n/g;
$output .= "$line\n";
}
open (MIRROR, ">$db_mirror_name") or &cgierr("unable to open $db_mirror_name. Reason: $!");
if ($db_use_flock) {
flock(MIRROR, 2) or &cgierr("unable to get exclusive lock on $db_mirror_name.\nReason: $!");
}
print MIRROR $output;
close MIRROR;
}

You'll also need to add a line to your .cfg file like

$db_mirror_name = $db_script_path . "/mirror.db";

Create an empty file -- mirror.db -- and set the permissions to 666.

In each of the subroutines that change the .db file -- sub add_record, sub modify_record and sub delete_records
-- just before the line that sends the script to the "success" page, add the following:

&create_mirror_file;

======

If you are creating a copy in a differnt directory you will need to modify the line:

$db_mirror_name = $db_script_path . "/mirror.db";

to include the full PATH (not url) to that directory.


$db_mirror_name = '/full/path/to/mirror.db";


Also this person was adjusting the output for the second file so I'm not sure exactly what wuld need to be changed for this portion:

foreach $line (@lines) {
$line =~ s/\|/\n/g; ### this line could be deleted?
$output .= "$line\n";
}

Perhaps Watts will come along and verifty whether he thinks this will work for you or suggest any needed changes.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] have dbman write to two files In reply to
That looks like it'd work - you could remark out this line:

$line =~ s/\|/\n/g;

or change the \n to a comma
($line =~ s/\|/\,/g;)

or something else to make it comma delimited - you'd have to play around with \, or just a comma , by itself.
Quote Reply
Re: [LoisC] have dbman write to two files In reply to
thank you LoisC and Watts, I'll try this this week. Hopefully I won't mess it up and everything will work. I don't want to change the data at all so I'll just remark out that line that Watts pointed out....and see where that takes me. Regards. Steven99

ps. LoisC, thanks for your help several months ago on another problem, I think I forgot to say thanks.
Quote Reply
Re: [steven99] have dbman write to two files In reply to
The suggested changes worked and the program now creates two seperate, duplicate files. Thanks Guys.
Quote Reply
Re: [Watts] have dbman write to two files In reply to
In Reply To:
That looks like it'd work - you could remark out this line:

$line =~ s/\|/\n/g;

or change the \n to a comma
($line =~ s/\|/\,/g;)

or something else to make it comma delimited - you'd have to play around with \, or just a comma , by itself.


I used the following to produce a mirror copy of data in the second field:

close DB;
foreach $line (@lines) {
# $line =~ s/\|/\n/g;
$output .= "$line";
}