Subject | RE: [firebird-php] php dump script |
---|---|
Author | Nigel Weeks |
Post date | 2005-08-28T23:08:15Z |
Ah blow it. It's been a year. People can handle it.
Here's the file again...
---------- BEGIN LISTING OF DATAPUMP.PHP ------------
#!/usr/local/bin/php -e
<?php
// Connect to the source database
//$src_db =
ibase_connect("203.26.213.102:c:/data/demo.fdb","sysdba","masterkey") or
$err = 1;
$src_db = ibase_connect("localhost:/u1/db/demo.fdb","sysdba","masterkey") or
$err = 1;
if($err == 1){
echo "Error:($err). Source Database connection failed\n";
exit;
}
/* ------------------------------------------------
Uncomment these if you want to transfer immediately
------------------------------------------------ */
/*
$tfer = 1;
// Connect to the destination database
$dest_db =
ibase_connect("203.26.213.98:/u1/db/test.gdb","sysdba","masterkey") or $err
= 2;
if($err == 2){
echo "Error:($err). Destination Database connection failed\n";
exit;
}
*/
// Set the following variable to 1 to produce insert statements
$ins = 1;
// Set the field delimiter here
$delim = "'";
// Set the field seperator here
$sep = ",";
// Set the single-quote escaper here
$esc = "'";
// Set the date formats here (// %H:%M:%S // %Y-%m-%d // %Y-%m-%d %H:%M:%S)
//ibase_timefmt("%H:%M:%S",IBASE_TIME);
//ibase_timefmt("%Y-%m-%d",IBASE_DATE);
//ibase_timefmt("%Y-%m-%d %H:%M:%S",IBASE_TIMESTAMP);
ini_set("ibase.timeformat","%H:%M");
ini_set("ibase.timestampformat","%Y/%m/%d %H:%M:%S");
ini_set("ibase.dateformat","%Y/%m/%d");
// Set the begin comment here ($bcomm = "/*"; $bcomm = "--";)
$bcomm = "/*";
// Set the end comment here ($ecomm = "*/"; $ecomm = "";)
$ecomm = "*/";
/* ---------------------------------------
Configuration complete
If you're changing things below this line,
you probably shouldn't be...
--------------------------------------- */
$tsql = "SELECT distinct(a.rdb\$relation_name) as str_tablename from
rdb\$relation_fields a where a.rdb\$relation_name NOT CONTAINING 'RDB\$'
order by 1";
$trec = ibase_query($src_db,$tsql) or die("Bad Table Query");
while($tobj = ibase_fetch_row($trec)){
//We have a table name. Let's pull the data
if($bcomm){echo "$bcomm --- ".trim($tobj[0])." --- $ecomm\n";}
$dsql = "SELECT * FROM ".trim($tobj[0]);
$drec = ibase_query($src_db,$dsql) or die("Bad Data Query");
// Echo out the field names
//echo $comm." (";
$col = 0;
$cols = ibase_num_fields($drec);
for($f=0;$f<$cols;$f++){
$info[$f] = ibase_field_info($drec,$f);
// Quotes management - some types need quotes
if($info[$f]['type'] == "VARYING"
|| $info[$f]['type'] == "VARCHAR"
|| $info[$f]['type'] == "TIMESTAMP"
|| $info[$f]['type'] == "DATE"
|| $info[$f]['type'] == "TIME"
){
$q[$f] = $delim;
} else {
$q[$f] = "";
}
// Null management - some fields can't handle empties...
if($info[$f]['type'] == "VARYING"
|| $info[$f]['type'] == "VARCHAR"
){
// Leave the empty value - it's ok
$n[$f] = 0;
} else {
// replace the empty value with a "null"
$n[$f] = 1;
}
// ISO format management - convert dates to a suitable format
if($info[$f]['type'] == "DATE"
){
$iso_d[$f] = 1;
} else {
$iso_d[$f] = 0;
}
}
//echo ") \n";
// Echo out the data
while($dobj = ibase_fetch_row($drec)){
// Output the row(if $ins is turned on, output insert statements)
if(!$ins){
$col = 0;
while($col < $cols){
if($dobj[$col] == "" && $n[$col]){
echo "null";
} else {
echo $q[$col].$dobj[$col].$q[$col];
}
if($col + 1 < $cols){
echo $sep;
} else {
echo "\n";
}
$col++;
}
} else {
$out = "INSERT INTO ".trim($tobj[0])." (";
for($col=0;$col < $cols;$col++){
$out .= $info[$col]['name'];
if($col + 1 < $cols){
$out .= $sep;
}
}
$out .= ") VALUES (";
for($col=0;$col < $cols;$col++){
if($dobj[$col] == "" && $n[$col]){
$out .= "null";
} else {
$prep = str_replace("'",$esc."'",$dobj[$col]);
$out .= $q[$col].$prep.$q[$col];
}
if($col + 1 < $cols){
$out .= $sep;
}
}
$out .= ");\n";
if($tfer){
ibase_query($dest_db,$out) or $err = 3;
if($err == "3"){
$dberr = ibase_errmsg();
echo "Error:(".$err."), \nDB Error Description: '".$dberrtext."'.
Destination Database insert failed\n";
}
} else {
echo $out;
}
}
}
}
?>
---------- END LISTING OF DATAPUMP.PHP -------------
Here's the file again...
---------- BEGIN LISTING OF DATAPUMP.PHP ------------
#!/usr/local/bin/php -e
<?php
// Connect to the source database
//$src_db =
ibase_connect("203.26.213.102:c:/data/demo.fdb","sysdba","masterkey") or
$err = 1;
$src_db = ibase_connect("localhost:/u1/db/demo.fdb","sysdba","masterkey") or
$err = 1;
if($err == 1){
echo "Error:($err). Source Database connection failed\n";
exit;
}
/* ------------------------------------------------
Uncomment these if you want to transfer immediately
------------------------------------------------ */
/*
$tfer = 1;
// Connect to the destination database
$dest_db =
ibase_connect("203.26.213.98:/u1/db/test.gdb","sysdba","masterkey") or $err
= 2;
if($err == 2){
echo "Error:($err). Destination Database connection failed\n";
exit;
}
*/
// Set the following variable to 1 to produce insert statements
$ins = 1;
// Set the field delimiter here
$delim = "'";
// Set the field seperator here
$sep = ",";
// Set the single-quote escaper here
$esc = "'";
// Set the date formats here (// %H:%M:%S // %Y-%m-%d // %Y-%m-%d %H:%M:%S)
//ibase_timefmt("%H:%M:%S",IBASE_TIME);
//ibase_timefmt("%Y-%m-%d",IBASE_DATE);
//ibase_timefmt("%Y-%m-%d %H:%M:%S",IBASE_TIMESTAMP);
ini_set("ibase.timeformat","%H:%M");
ini_set("ibase.timestampformat","%Y/%m/%d %H:%M:%S");
ini_set("ibase.dateformat","%Y/%m/%d");
// Set the begin comment here ($bcomm = "/*"; $bcomm = "--";)
$bcomm = "/*";
// Set the end comment here ($ecomm = "*/"; $ecomm = "";)
$ecomm = "*/";
/* ---------------------------------------
Configuration complete
If you're changing things below this line,
you probably shouldn't be...
--------------------------------------- */
$tsql = "SELECT distinct(a.rdb\$relation_name) as str_tablename from
rdb\$relation_fields a where a.rdb\$relation_name NOT CONTAINING 'RDB\$'
order by 1";
$trec = ibase_query($src_db,$tsql) or die("Bad Table Query");
while($tobj = ibase_fetch_row($trec)){
//We have a table name. Let's pull the data
if($bcomm){echo "$bcomm --- ".trim($tobj[0])." --- $ecomm\n";}
$dsql = "SELECT * FROM ".trim($tobj[0]);
$drec = ibase_query($src_db,$dsql) or die("Bad Data Query");
// Echo out the field names
//echo $comm." (";
$col = 0;
$cols = ibase_num_fields($drec);
for($f=0;$f<$cols;$f++){
$info[$f] = ibase_field_info($drec,$f);
// Quotes management - some types need quotes
if($info[$f]['type'] == "VARYING"
|| $info[$f]['type'] == "VARCHAR"
|| $info[$f]['type'] == "TIMESTAMP"
|| $info[$f]['type'] == "DATE"
|| $info[$f]['type'] == "TIME"
){
$q[$f] = $delim;
} else {
$q[$f] = "";
}
// Null management - some fields can't handle empties...
if($info[$f]['type'] == "VARYING"
|| $info[$f]['type'] == "VARCHAR"
){
// Leave the empty value - it's ok
$n[$f] = 0;
} else {
// replace the empty value with a "null"
$n[$f] = 1;
}
// ISO format management - convert dates to a suitable format
if($info[$f]['type'] == "DATE"
){
$iso_d[$f] = 1;
} else {
$iso_d[$f] = 0;
}
}
//echo ") \n";
// Echo out the data
while($dobj = ibase_fetch_row($drec)){
// Output the row(if $ins is turned on, output insert statements)
if(!$ins){
$col = 0;
while($col < $cols){
if($dobj[$col] == "" && $n[$col]){
echo "null";
} else {
echo $q[$col].$dobj[$col].$q[$col];
}
if($col + 1 < $cols){
echo $sep;
} else {
echo "\n";
}
$col++;
}
} else {
$out = "INSERT INTO ".trim($tobj[0])." (";
for($col=0;$col < $cols;$col++){
$out .= $info[$col]['name'];
if($col + 1 < $cols){
$out .= $sep;
}
}
$out .= ") VALUES (";
for($col=0;$col < $cols;$col++){
if($dobj[$col] == "" && $n[$col]){
$out .= "null";
} else {
$prep = str_replace("'",$esc."'",$dobj[$col]);
$out .= $q[$col].$prep.$q[$col];
}
if($col + 1 < $cols){
$out .= $sep;
}
}
$out .= ");\n";
if($tfer){
ibase_query($dest_db,$out) or $err = 3;
if($err == "3"){
$dberr = ibase_errmsg();
echo "Error:(".$err."), \nDB Error Description: '".$dberrtext."'.
Destination Database insert failed\n";
}
} else {
echo $out;
}
}
}
}
?>
---------- END LISTING OF DATAPUMP.PHP -------------