function DumpDatabase($arguments) { if (!isset($arguments["Output"])) { return "it was not specified a valid output function"; } $output = $arguments["Output"]; $eol = isset($arguments["EndOfLine"]) ? $arguments["EndOfLine"] : "\n"; $dump_definition = isset($arguments["Definition"]); $sequences = array(); if (isset($this->database_definition["SEQUENCES"])) { for ($error = "", Reset($this->database_definition["SEQUENCES"]), $sequence = 0; $sequence < count($this->database_definition["SEQUENCES"]); Next($this->database_definition["SEQUENCES"]), $sequence++) { $sequence_name = Key($this->database_definition["SEQUENCES"]); if (isset($this->database_definition["SEQUENCES"][$sequence_name]["on"])) { $table = $this->database_definition["SEQUENCES"][$sequence_name]["on"]["table"]; } else { $table = ""; } $sequences[$table][] = $sequence_name; } } $previous_database_name = strcmp($this->database_definition["name"], "") ? MetabaseSetDatabase($this->database, $this->database_definition["name"]) : ""; $output("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>{$eol}"); $output("<database>{$eol}{$eol} <name>" . $this->database_definition["name"] . "</name>{$eol} <create>" . $this->database_definition["create"] . "</create>{$eol}"); for ($error = "", Reset($this->database_definition["TABLES"]), $table = 0; $table < count($this->database_definition["TABLES"]); Next($this->database_definition["TABLES"]), $table++) { $table_name = Key($this->database_definition["TABLES"]); $output("{$eol} <table>{$eol}{$eol} <name>{$table_name}</name>{$eol}"); $output("{$eol} <declaration>{$eol}"); $fields = $this->database_definition["TABLES"][$table_name]["FIELDS"]; for (Reset($fields), $field_number = 0; $field_number < count($fields); $field_number++, Next($fields)) { $field_name = Key($fields); $field = $fields[$field_name]; if (!isset($field["type"])) { return "it was not specified the type of the field \"{$field_name}\" of the table \"{$table_name}\""; } $output("{$eol} <field>{$eol} <name>{$field_name}</name>{$eol} <type>" . $field["type"] . "</type>{$eol}"); switch ($field["type"]) { case "integer": if (isset($field["unsigned"])) { $output(" <unsigned>1</unsigned>{$eol}"); } break; case "text": case "clob": case "blob": if (isset($field["length"])) { $output(" <length>" . $field["length"] . "</length>{$eol}"); } break; case "boolean": case "date": case "timestamp": case "time": case "float": case "decimal": break; default: return "type \"" . $field["type"] . "\" is not yet supported"; } if (isset($field["notnull"])) { $output(" <notnull>1</notnull>{$eol}"); } if (isset($field["default"])) { $output(" <default>" . $this->EscapeSpecialCharacters($field["default"]) . "</default>{$eol}"); } $output(" </field>{$eol}"); } if (isset($this->database_definition["TABLES"][$table_name]["INDEXES"])) { $indexes = $this->database_definition["TABLES"][$table_name]["INDEXES"]; for (Reset($indexes), $index_number = 0; $index_number < count($indexes); $index_number++, Next($indexes)) { $index_name = Key($indexes); $index = $indexes[$index_name]; $output("{$eol} <index>{$eol} <name>{$index_name}</name>{$eol}"); if (isset($indexes[$index_name]["unique"])) { $output(" <unique>1</unique>{$eol}"); } for (Reset($index["FIELDS"]), $field_number = 0; $field_number < count($index["FIELDS"]); $field_number++, Next($index["FIELDS"])) { $field_name = Key($index["FIELDS"]); $field = $index["FIELDS"][$field_name]; $output(" <field>{$eol} <name>{$field_name}</name>{$eol}"); if (isset($field["sorting"])) { $output(" <sorting>" . $field["sorting"] . "</sorting>{$eol}"); } $output(" </field>{$eol}"); } $output(" </index>{$eol}"); } } $output("{$eol} </declaration>{$eol}"); if ($dump_definition) { if (isset($this->database_definition["TABLES"][$table_name]["initialization"])) { $output("{$eol} <initialization>{$eol}"); $instructions = $this->database_definition["TABLES"][$table_name]["initialization"]; for (Reset($instructions), $instruction = 0; $instruction < count($instructions); $instruction++, Next($instructions)) { switch ($instructions[$instruction]["type"]) { case "insert": $output("{$eol} <insert>{$eol}"); $fields = $instructions[$instruction]["FIELDS"]; for (Reset($fields), $field_number = 0; $field_number < count($fields); $field_number++, Next($fields)) { $field_name = Key($fields); $output("{$eol} <field>{$eol} <name>{$field_name}</name>{$eol} <value>" . $this->EscapeSpecialCharacters($fields[$field_name]) . "</value>{$eol} </field>{$eol}"); } $output("{$eol} </insert>{$eol}"); break; } } $output("{$eol} </initialization>{$eol}"); } } else { if (count($this->database_definition["TABLES"][$table_name]["FIELDS"]) == 0) { return "the definition of the table \"{$table_name}\" does not contain any fields"; } if (strcmp($error = $this->GetFields($table_name, $query_fields), "")) { return $error; } if ($support_summary_functions = MetabaseSupport($this->database, "SummaryFunctions")) { if (($result = MetabaseQuery($this->database, "SELECT COUNT(*) FROM {$table_name}")) == 0) { return MetabaseError($this->database); } $rows = MetabaseFetchResult($this->database, $result, 0, 0); MetabaseFreeResult($this->database, $result); } if (($result = MetabaseQuery($this->database, "SELECT {$query_fields} FROM {$table_name}")) == 0) { return MetabaseError($this->database); } if (!$support_summary_functions) { $rows = MetabaseNumberOfRows($this->database, $result); } if ($rows > 0) { $output("{$eol} <initialization>{$eol}"); for ($row = 0; $row < $rows; $row++) { $output("{$eol} <insert>{$eol}"); for (Reset($fields), $field_number = 0; $field_number < count($fields); $field_number++, Next($fields)) { $field_name = Key($fields); if (!MetabaseResultIsNull($this->database, $result, $row, $field_name)) { $field = $fields[$field_name]; $output("{$eol} <field>{$eol} <name>{$field_name}</name>{$eol} <value>"); switch ($field["type"]) { case "integer": case "text": $output($this->EscapeSpecialCharacters(MetabaseFetchResult($this->database, $result, $row, $field_name))); break; case "clob": if (!($lob = MetabaseFetchCLOBResult($this->database, $result, $row, $field_name))) { return MetabaseError($this->database); } while (!MetabaseEndOfLOB($lob)) { if (MetabaseReadLOB($lob, $data, 8000) < 0) { return MetabaseLOBError($lob); } $output($this->EscapeSpecialCharacters($data)); } MetabaseDestroyLOB($lob); break; case "blob": if (!($lob = MetabaseFetchBLOBResult($this->database, $result, $row, $field_name))) { return MetabaseError($this->database); } while (!MetabaseEndOfLOB($lob)) { if (MetabaseReadLOB($lob, $data, 8000) < 0) { return MetabaseLOBError($lob); } $output(bin2hex($data)); } MetabaseDestroyLOB($lob); break; case "float": $output($this->EscapeSpecialCharacters(MetabaseFetchFloatResult($this->database, $result, $row, $field_name))); break; case "decimal": $output($this->EscapeSpecialCharacters(MetabaseFetchDecimalResult($this->database, $result, $row, $field_name))); break; case "boolean": $output($this->EscapeSpecialCharacters(MetabaseFetchBooleanResult($this->database, $result, $row, $field_name))); break; case "date": $output($this->EscapeSpecialCharacters(MetabaseFetchDateResult($this->database, $result, $row, $field_name))); break; case "timestamp": $output($this->EscapeSpecialCharacters(MetabaseFetchTimestampResult($this->database, $result, $row, $field_name))); break; case "time": $output($this->EscapeSpecialCharacters(MetabaseFetchTimeResult($this->database, $result, $row, $field_name))); break; default: return "type \"" . $field["type"] . "\" is not yet supported"; } $output("</value>{$eol} </field>{$eol}"); } } $output("{$eol} </insert>{$eol}"); } $output("{$eol} </initialization>{$eol}"); } MetabaseFreeResult($this->database, $result); } $output("{$eol} </table>{$eol}"); if (isset($sequences[$table_name])) { for ($sequence = 0; $sequence < count($sequences[$table_name]); $sequence++) { if (!$this->DumpSequence($sequences[$table_name][$sequence], $output, $eol, $dump_definition)) { return MetabaseError($this->database); } } } } if (isset($sequences[""])) { for ($sequence = 0; $sequence < count($sequences[""]); $sequence++) { if (!$this->DumpSequence($sequences[""][$sequence], $output, $eol, $dump_definition)) { return MetabaseError($this->database); } } } $output("{$eol}</database>{$eol}"); if (strcmp($previous_database_name, "")) { MetabaseSetDatabase($this->database, $previous_database_name); } return $error; }
function GetBLOBFieldValue($prepared_query, $parameter, $blob, &$value) { for ($value = "'"; !MetabaseEndOfLOB($blob);) { if (!MetabaseReadLOB($blob, $data, $this->lob_buffer_length)) { $value = ""; return $this->SetError("Get BLOB field value", MetabaseLOBError($blob)); } $value .= AddSlashes($data); } $value .= "'"; return 1; }
function GetLOBFieldValue($prepared_query, $parameter, $lob, &$value, $binary) { if (!$this->Connect(0)) { return 0; } for ($blob_data = ""; !MetabaseEndOfLOB($lob);) { if (MetabaseReadLOB($lob, $data, $this->lob_buffer_length) < 0) { return $this->SetError("Get LOB field value", MetabaseLOBError($lob)); } $blob_data .= $data; } if (!($blob = ifx_create_blob($binary ? 0 : 1, 0, $blob_data))) { return $this->SetIFXError("Get LOB field value", "Could not create a blob"); } if (!isset($this->query_parameters[$prepared_query])) { $this->query_parameters[$prepared_query] = $this->query_parameter_values[$prepared_query] = array(); } $query_parameter = count($this->query_parameters[$prepared_query]); $this->query_parameter_values[$prepared_query][$parameter] = $query_parameter; $this->query_parameters[$prepared_query][$query_parameter] = $blob; $value = "?"; return 1; }
function GetLOBFieldValue($prepared_query, $parameter, $lob, &$value) { if (!$this->Connect()) { return 0; } $success = 1; if ($blob = ibase_blob_create($this->auto_commit ? $this->connection : $this->transaction_id)) { while (!MetabaseEndOfLOB($lob)) { if (MetabaseReadLOB($lob, $data, $this->lob_buffer_length) < 0) { $this->SetError("Get LOB field value", MetabaseLOBError($lob)); $success = 0; break; } if (!ibase_blob_add($blob, $data)) { $this->SetError("Get LOB field value", "Could not add data to a large object: " . ibase_errmsg()); $success = 0; break; } } if ($success) { if (GetType($value = ibase_blob_close($blob))) { if (!isset($this->query_parameters[$prepared_query])) { $this->query_parameters[$prepared_query] = array(0, ""); $this->query_parameter_values[$prepared_query] = array(); } $query_parameter = count($this->query_parameters[$prepared_query]); $this->query_parameter_values[$prepared_query][$parameter] = $query_parameter; $this->query_parameters[$prepared_query][$query_parameter] = $value; $value = "?"; } else { $success = 0; } } if (!$success) { ibase_blob_cancel($blob); } } else { $this->SetError("Get LOB field value", "Could not create a large object: " . ibase_errmsg()); $success = 0; } return $success; }
function GetLOBFieldValue($prepared_query, $parameter, $lob, &$value) { if (!$this->Connect()) { return 0; } if ($this->auto_commit && !@pg_Exec($this->connection, "BEGIN")) { return 0; } $success = 1; if ($lo = pg_locreate($this->connection)) { if ($handle = pg_loopen($this->connection, $lo, "w")) { while (!MetabaseEndOfLOB($lob)) { if (MetabaseReadLOB($lob, $data, $this->lob_buffer_length) < 0) { $this->SetError("Get LOB field value", MetabaseLOBError($lob)); $success = 0; break; } if (!pg_lowrite($handle, $data)) { $this->SetError("Get LOB field value", pg_ErrorMessage($this->connection)); $success = 0; break; } } pg_loclose($handle); if ($success) { $value = strval($lo); } } else { $this->SetError("Get LOB field value", pg_ErrorMessage($this->connection)); $success = 0; } if (!$success) { pg_lounlink($this->connection, $lo); } } else { $this->SetError("Get LOB field value", pg_ErrorMessage($this->connection)); $success = 0; } if ($this->auto_commit) { @pg_Exec($this->connection, "END"); } return $success; }
fclose($file); } else { $success = 0; $error = "could not reopen the character lob data file"; } } if ($success) { if (strcmp($value, $character_data)) { $pass = 0; echo "FAILED!{$eol}"; $failed++; echo "Test {$test}: retrieved character LOB value (\"" . $value . "\") is different from what was stored (\"" . $character_data . "\"){$eol}"; } else { $binary_lob = array("Type" => "outputfile", "Database" => $database, "Result" => $result, "Row" => 0, "Field" => "picture", "Binary" => 1, "Error" => "", "FileName" => $binary_data_file, "BufferLength" => 32); if ($success = MetabaseCreateLOB($binary_lob, $blob)) { if (MetabaseReadLOB($blob, $data, 0) < 0) { $error = MetabaseLOBError($clob); $success = 0; } MetabaseDestroyLOB($blob); if ($success) { if ($file = fopen($binary_data_file, "rb")) { if (GetType($value = fread($file, filesize($binary_data_file))) != "string") { $success = 0; $error = "could not read from the binary lob data file"; } fclose($file); } else { $success = 0; $error = "could not reopen the binary lob data file"; }
function ReadLOB(&$data, $length) { $buffer_length = $length == 0 ? $this->buffer_length : min($this->buffer_length, $length); for ($written = 0; !MetabaseEndOfLOB($this->input_lob) && ($length == 0 || $written < $buffer_length); $written += $read) { if (MetabaseReadLOB($this->input_lob, $buffer, $buffer_length) == -1) { $this->error = MetabaseLOBError($this->input_lob); return -1; } $read = strlen($buffer); if (@fwrite($this->file, $buffer, $read) != $read) { $this->error = "could not write to the output file"; return -1; } } return $written; }
function DoQuery($query, $first = 0, $limit = 0, $prepared_query = 0) { $lobs = 0; $success = 1; $result = 0; $descriptors = array(); if ($prepared_query) { $columns = ""; $variables = ""; for (Reset($this->clobs[$prepared_query]), $clob = 0; $clob < count($this->clobs[$prepared_query]); $clob++, Next($this->clobs[$prepared_query])) { $position = Key($this->clobs[$prepared_query]); if (GetType($descriptors[$position] = OCINewDescriptor($this->connection, OCI_D_LOB)) != "object") { $this->SetError("Do query", "Could not create descriptor for clob parameter"); $success = 0; break; } $columns .= ($lobs == 0 ? " RETURNING " : ",") . $this->prepared_queries[$prepared_query - 1]["Fields"][$position - 1]; $variables .= ($lobs == 0 ? " INTO " : ",") . ":clob" . $position; $lobs++; } if ($success) { for (Reset($this->blobs[$prepared_query]), $blob = 0; $blob < count($this->blobs[$prepared_query]); $blob++, Next($this->blobs[$prepared_query])) { $position = Key($this->blobs[$prepared_query]); if (GetType($descriptors[$position] = OCINewDescriptor($this->connection, OCI_D_LOB)) != "object") { $this->SetError("Do query", "Could not create descriptor for blob parameter"); $success = 0; break; } $columns .= ($lobs == 0 ? " RETURNING " : ",") . $this->prepared_queries[$prepared_query - 1]["Fields"][$position - 1]; $variables .= ($lobs == 0 ? " INTO " : ",") . ":blob" . $position; $lobs++; } $query .= $columns . $variables; } } if ($success) { if ($statement = OCIParse($this->connection, $query)) { if ($lobs) { for (Reset($this->clobs[$prepared_query]), $clob = 0; $clob < count($this->clobs[$prepared_query]); $clob++, Next($this->clobs[$prepared_query])) { $position = Key($this->clobs[$prepared_query]); if (!OCIBindByName($statement, ":clob" . $position, $descriptors[$position], -1, OCI_B_CLOB)) { $this->SetOCIError("Do query", "Could not bind clob upload descriptor", OCIError($statement)); $success = 0; break; } } if ($success) { for (Reset($this->blobs[$prepared_query]), $blob = 0; $blob < count($this->blobs[$prepared_query]); $blob++, Next($this->blobs[$prepared_query])) { $position = Key($this->blobs[$prepared_query]); if (!OCIBindByName($statement, ":blob" . $position, $descriptors[$position], -1, OCI_B_BLOB)) { $this->SetOCIError("Do query", "Could not bind blob upload descriptor", OCIError($statement)); $success = 0; break; } } } } if ($success) { if ($result = @OCIExecute($statement, $lobs == 0 && $this->auto_commit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT)) { if ($lobs) { for (Reset($this->clobs[$prepared_query]), $clob = 0; $clob < count($this->clobs[$prepared_query]); $clob++, Next($this->clobs[$prepared_query])) { $position = Key($this->clobs[$prepared_query]); $clob_stream = $this->prepared_queries[$prepared_query - 1]["Values"][$position - 1]; for ($value = ""; !MetabaseEndOfLOB($clob_stream);) { if (MetabaseReadLOB($clob_stream, $data, $this->lob_buffer_length) < 0) { $this->SetError("Do query", MetabaseLOBError($clob)); $success = 0; break; } $value .= $data; } if ($success && !$descriptors[$position]->save($value)) { $this->SetOCIError("Do query", "Could not upload clob data", OCIError($statement)); $success = 0; } } if ($success) { for (Reset($this->blobs[$prepared_query]), $blob = 0; $blob < count($this->blobs[$prepared_query]); $blob++, Next($this->blobs[$prepared_query])) { $position = Key($this->blobs[$prepared_query]); $blob_stream = $this->prepared_queries[$prepared_query - 1]["Values"][$position - 1]; for ($value = ""; !MetabaseEndOfLOB($blob_stream);) { if (MetabaseReadLOB($blob_stream, $data, $this->lob_buffer_length) < 0) { $this->SetError("Do query", MetabaseLOBError($blob)); $success = 0; break; } $value .= $data; } if ($success && !$descriptors[$position]->save($value)) { $this->SetOCIError("Do query", "Could not upload blob data", OCIError($statement)); $success = 0; } } } } if ($this->auto_commit) { if ($lobs) { if ($success) { if (!OCICommit($this->connection)) { $this->SetOCIError("Do query", "Could not commit pending LOB updating transaction", OCIError()); $success = 0; } } else { if (!OCIRollback($this->connection)) { $this->SetOCIError("Do query", $this->Error() . " and then could not rollback LOB updating transaction", OCIError()); } } } } else { $this->uncommitedqueries++; } if ($success) { switch (OCIStatementType($statement)) { case "SELECT": $result_value = intval($statement); $this->current_row[$result_value] = -1; if ($limit > 0) { $this->limits[$result_value] = array($first, $limit, 0); } $this->highest_fetched_row[$result_value] = -1; break; default: $this->affected_rows = OCIRowCount($statement); OCIFreeCursor($statement); break; } $result = $statement; } } else { $this->SetOCIError("Do query", "Could not execute query", OCIError($statement)); } } } else { $this->SetOCIError("Do query", "Could not parse query", OCIError($statement)); } } for (Reset($descriptors), $descriptor = 0; $descriptor < count($descriptors); $descriptor++, Next($descriptors)) { @OCIFreeDesc($descriptors[Key($descriptors)]); } return $result; }
function GetLOBFieldValue($prepared_query, $parameter, $lob, &$value) { if (!isset($this->query_parameters[$prepared_query])) { $this->query_parameters[$prepared_query] = $this->query_parameter_values[$prepared_query] = array(); } $query_parameter = count($this->query_parameters[$prepared_query]); $this->query_parameter_values[$prepared_query][$parameter] = $query_parameter; for ($this->query_parameters[$prepared_query][$query_parameter] = ""; !MetabaseEndOfLOB($lob);) { if (MetabaseReadLOB($lob, $data, $this->lob_buffer_length) < 0) { $this->FreeLOBValue($prepared_query, $lob, $value, 0); return $this->SetError("Get LOB field value", MetabaseLOBError($clob)); } $this->query_parameters[$prepared_query][$query_parameter] .= $data; } $value = "?"; return 1; }