Implemented quote and backup_data methods in firebird driver

This commit is contained in:
Timothy Warren 2012-02-28 20:44:14 -05:00
parent 451d883242
commit 105a086603
4 changed files with 71 additions and 4 deletions

View File

@ -303,6 +303,24 @@ SQL;
return $this->execute($args); return $this->execute($args);
} }
// --------------------------------------------------------------------------
/**
* Method to emulate PDO->quote
*
* @param string $str
* @return string
*/
public function quote($str)
{
if(is_numeric($str))
{
return $str;
}
return "'".str_replace("'", "''", $str)."'";
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -342,8 +360,48 @@ SQL;
*/ */
public function backup_data() public function backup_data()
{ {
// @todo Implement Backup function $tables = $this->get_tables();
return '';
$output_sql = '';
// Get the data for each object
foreach($table as $r)
{
$sql = 'SELECT * FROM "'.$r['name'].'"';
$res = $this->query($sql);
$obj_res = $res->fetchAll(PDO::FETCH_ASSOC);
unset($res);
// Nab the column names by getting the keys of the first row
$columns = array_keys($obj_res[0]);
$insert_rows = array();
// Create the insert statements
foreach($obj_res as $row)
{
$row = array_values($row);
// Quote values as needed by type
for($i=0, $icount=count($row); $i<$icount; $i++)
{
$row[$i] = (is_numeric($row[$i])) ? $row[$i] : $this->quote($row[$i]);
}
$row_string = 'INSERT INTO "'.$r['name'].'" ("'.implode('","', $columns).'") VALUES ('.implode(',', $row).');';
unset($row);
$insert_rows[] = $row_string;
}
unset($obj_res);
$output_sql .= "\n\n".implode("\n", $insert_rows);
}
return $output_sql;
} }
} }
// End of firebird.php // End of firebird.php

View File

@ -171,12 +171,19 @@ SQL;
/** /**
* Create an SQL backup file for the current database's data * Create an SQL backup file for the current database's data
* *
* @param array $excluded
* @return string * @return string
*/ */
public function backup_data() public function backup_data($excluded=array())
{ {
// Get a list of all the objects // Get a list of all the objects
$sql = 'SELECT "name" FROM "sqlite_master"'; $sql = 'SELECT "name" FROM "sqlite_master"';
if( ! empty($excluded))
{
$sql .= ' WHERE NOT IN("'.implode('","', $excluded).'")';
}
$res = $this->query($sql); $res = $this->query($sql);
$result = $res->fetchAll(PDO::FETCH_ASSOC); $result = $res->fetchAll(PDO::FETCH_ASSOC);
@ -218,7 +225,7 @@ SQL;
unset($obj_res); unset($obj_res);
$output_sql = implode("\n", $insert_rows); $output_sql .= "\n\n".implode("\n", $insert_rows);
} }
return $output_sql; return $output_sql;

View File

@ -118,6 +118,8 @@ SQL;
function TestDeleteTable() function TestDeleteTable()
{ {
?><pre><?= $this->db->backup_data(); ?></pre><?php
//Attempt to delete the table //Attempt to delete the table
$sql = $this->db->manip->delete_table('create_test'); $sql = $this->db->manip->delete_table('create_test');
$this->db->query($sql); $this->db->query($sql);

Binary file not shown.