本文整理汇总了PHP中pg_end_copy函数的典型用法代码示例。如果您正苦于以下问题:PHP pg_end_copy函数的具体用法?PHP pg_end_copy怎么用?PHP pg_end_copy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pg_end_copy函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sqlLine
public function sqlLine($sql)
{
if (!$this->putline) {
return parent::sqlLine($sql);
}
if ($sql == '\\.') {
$this->putline = false;
pg_put_line($this->connection, $sql . "\n");
pg_end_copy($this->connection);
pg_close($this->connection);
} else {
pg_put_line($this->connection, $sql . "\n");
}
return true;
}
示例2: main
//.........这里部分代码省略.........
$close = 'gzclose';
$fgetd = 'fgetd';
break;
}
switch ($db->get_sql_layer()) {
case 'mysql':
case 'mysql4':
case 'mysqli':
case 'sqlite':
case 'sqlite3':
while (($sql = $fgetd($fp, ";\n", $read, $seek, $eof)) !== false) {
$db->sql_query($sql);
}
break;
case 'postgres':
$delim = ";\n";
while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false) {
$query = trim($sql);
if (substr($query, 0, 13) == 'CREATE DOMAIN') {
list(, , $domain) = explode(' ', $query);
$sql = "SELECT domain_name\n\t\t\t\t\t\t\t\t\t\t\t\tFROM information_schema.domains\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE domain_name = '{$domain}';";
$result = $db->sql_query($sql);
if (!$db->sql_fetchrow($result)) {
$db->sql_query($query);
}
$db->sql_freeresult($result);
} else {
$db->sql_query($query);
}
if (substr($query, 0, 4) == 'COPY') {
while (($sub = $fgetd($fp, "\n", $read, $seek, $eof)) !== '\\.') {
if ($sub === false) {
trigger_error($user->lang['RESTORE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
}
pg_put_line($db->get_db_connect_id(), $sub . "\n");
}
pg_put_line($db->get_db_connect_id(), "\\.\n");
pg_end_copy($db->get_db_connect_id());
}
}
break;
case 'oracle':
while (($sql = $fgetd($fp, "/\n", $read, $seek, $eof)) !== false) {
$db->sql_query($sql);
}
break;
case 'mssql':
case 'mssql_odbc':
case 'mssqlnative':
while (($sql = $fgetd($fp, "GO\n", $read, $seek, $eof)) !== false) {
$db->sql_query($sql);
}
break;
}
$close($fp);
// Purge the cache due to updated data
$cache->purge();
$phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_DB_RESTORE');
trigger_error($user->lang['RESTORE_SUCCESS'] . adm_back_link($this->u_action));
break;
} else {
if (!$download) {
confirm_box(false, $user->lang['RESTORE_SELECTED_BACKUP'], build_hidden_fields(array('file' => $file)));
}
}
}
default:
$methods = array('sql');
$available_methods = array('sql.gz' => 'zlib', 'sql.bz2' => 'bz2');
foreach ($available_methods as $type => $module) {
if (!@extension_loaded($module)) {
continue;
}
$methods[] = $type;
}
$dir = $phpbb_root_path . 'store/';
$dh = @opendir($dir);
$backup_files = array();
if ($dh) {
while (($file = readdir($dh)) !== false) {
if (preg_match('#^backup_(\\d{10,})_[a-z\\d]{16}\\.(sql(?:\\.(?:gz|bz2))?)$#', $file, $matches)) {
if (in_array($matches[2], $methods)) {
$backup_files[(int) $matches[1]] = $file;
}
}
}
closedir($dh);
}
if (!empty($backup_files)) {
krsort($backup_files);
foreach ($backup_files as $name => $file) {
$template->assign_block_vars('files', array('FILE' => $file, 'NAME' => $user->format_date($name, 'd-m-Y H:i:s', true), 'SUPPORTED' => true));
}
}
$template->assign_vars(array('U_ACTION' => $this->u_action . '&action=submit'));
break;
}
break;
}
}
示例3: _batchInsertPgSQL
/**
* Performs a batch insert using plain INSERTs
*
* @see OA_Dal::batchInsert()
*
* @param string $tableName The unquoted table name
* @param array $aFields The array of unquoted field names
* @param array $aValues The array of data to be inserted
* @param bool $replace Should the primary key be replaced when already present?
* @return int The number of rows inserted or PEAR_Error on failure
*/
private function _batchInsertPgSQL($qTableName, $fieldList, $aValues, $replace, $primaryKey)
{
$oDbh = OA_DB::singleton();
$delim = "\t";
$eol = "\n";
$null = '\\N';
// Disable error handler
RV::disableErrorHandling();
// we start by manually deleting conflicting unique rows
foreach ($aValues as $aRow) {
// because Postgresql doesn't have the REPLACE keyword,
// we manually delete the rows with the primary key first
if ($replace) {
$where = '';
foreach ($primaryKey as $fieldName) {
$where .= $fieldName . ' = \'' . $aRow[$fieldName] . '\' AND ';
}
$where = substr($where, 0, strlen($where) - 5);
$oDbh->query('DELETE FROM ' . $qTableName . ' WHERE ' . $where);
}
}
$pg = $oDbh->getConnection();
$result = $oDbh->exec("\n COPY\n {$qTableName} {$fieldList}\n FROM\n STDIN\n ");
if (PEAR::isError($result)) {
return MAX::raiseError('Error issuing the COPY query for the batch INSERTs.', PEAR_ERROR_RETURN);
}
foreach ($aValues as $aRow) {
// Stringify row
$row = '';
foreach ($aRow as $value) {
if (!isset($value) || $value === false) {
$row .= $null . $delim;
} else {
$row .= $value . $delim;
}
}
// Replace delim with eol
$row[strlen($row) - 1] = $eol;
// Send line
$ret = pg_put_line($pg, $row);
if (!$ret) {
return MAX::raiseError('Error COPY-ing data: ' . pg_errormessage($pg), PEAR_ERROR_RETURN);
}
}
$result = pg_put_line($pg, '\\.' . $eol) && pg_end_copy($pg);
$result = $result ? count($aValues) : new PEAR_Error('Error at the end of the COPY: ' . pg_errormessage($pg));
// Enable error handler again
RV::enableErrorHandling();
return $result;
}
示例4: endCopy
public function endCopy()
{
return pg_end_copy($this->_pg);
}
示例5: main
//.........这里部分代码省略.........
}
break;
case 'firebird':
$delim = ";\n";
while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
{
$query = trim($sql);
if (substr($query, 0, 8) === 'SET TERM')
{
$delim = $query[9] . "\n";
continue;
}
$db->sql_query($query);
}
break;
case 'postgres':
while (($sql = $fgetd($fp, $delim, $read, $seek, $eof)) !== false)
{
$query = trim($sql);
$db->sql_query($query);
if (substr($query, 0, 4) == 'COPY')
{
while (($sub = $fgetd($fp, "\n", $read, $seek, $eof)) !== '\.')
{
if ($sub === false)
{
trigger_error($user->lang['RESTORE_FAILURE'] . adm_back_link($this->u_action), E_USER_WARNING);
}
pg_put_line($db->db_connect_id, $sub . "\n");
}
pg_put_line($db->db_connect_id, "\\.\n");
pg_end_copy($db->db_connect_id);
}
}
break;
case 'oracle':
while (($sql = $fgetd($fp, "/\n", $read, $seek, $eof)) !== false)
{
$db->sql_query($sql);
}
break;
case 'mssql':
case 'mssql_odbc':
while (($sql = $fgetd($fp, "GO\n", $read, $seek, $eof)) !== false)
{
$db->sql_query($sql);
}
break;
}
$close($fp);
add_log('admin', 'LOG_DB_RESTORE');
trigger_error($user->lang['RESTORE_SUCCESS'] . adm_back_link($this->u_action));
break;
}
default:
$methods = array('sql');
$available_methods = array('sql.gz' => 'zlib', 'sql.bz2' => 'bz2');
foreach ($available_methods as $type => $module)
示例6: executeScript
//.........这里部分代码省略.........
} else {
if (substr($line, $i, 1) == ')' && $paren_level > 0) {
$paren_level--;
} else {
if (substr($line, $i, 1) == ';' && !$bslash_count && !$paren_level) {
$subline = substr(substr($line, 0, $i), $query_start);
/* is there anything else on the line? */
if (strspn($subline, " \t\n\r") != strlen($subline)) {
/*
* insert a cosmetic newline, if this is not the first
* line in the buffer
*/
if (strlen($query_buf) > 0) {
$query_buf .= "\n";
}
/* append the line to the query buffer */
$query_buf .= $subline;
$query_buf .= ';';
// Execute the query. PHP cannot execute
// empty queries, unlike libpq
$res = @pg_query($conn, $query_buf);
// Call the callback function for display
if ($callback !== null) {
$callback($query_buf, $res, $lineno);
}
// Check for COPY request
if (pg_result_status($res) == 4) {
// 4 == PGSQL_COPY_FROM
while (!feof($fd)) {
$copy = fgets($fd, 32768);
$lineno++;
pg_put_line($conn, $copy);
if ($copy == "\\.\n" || $copy == "\\.\r\n") {
pg_end_copy($conn);
break;
}
}
}
}
$query_buf = null;
$query_start = $i + $thislen;
} else {
if (preg_match('/^[_[:alpha:]]$/', substr($line, $i, 1))) {
$sub = substr($line, $i, $thislen);
while (preg_match('/^[\\$_A-Za-z0-9]$/', $sub)) {
/* keep going while we still have identifier chars */
$this->advance_1($i, $prevlen, $thislen);
$sub = substr($line, $i, $thislen);
}
// Since we're now over the next character to be examined, it is necessary
// to move back one space.
$i -= $prevlen;
}
}
}
}
}
}
}
}
}
}
}
}
// end for
/* Put the rest of the line in the query buffer. */
示例7: query
public function query($type, $sql, $as_object = FALSE, array $params = NULL)
{
$this->_connection or $this->connect();
if (Kohana::$profiling) {
// Benchmark this query for the current instance
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
}
try {
if ($type === Database::INSERT and $this->_config['primary_key']) {
$sql .= ' RETURNING ' . $this->quote_identifier($this->_config['primary_key']);
}
try {
$result = pg_query($this->_connection, $sql);
} catch (Exception $e) {
throw new Database_Exception(':error [ :query ]', array(':error' => pg_last_error($this->_connection), ':query' => $sql));
}
if (!$result) {
throw new Database_Exception(':error [ :query ]', array(':error' => pg_last_error($this->_connection), ':query' => $sql));
}
// Check the result for errors
switch (pg_result_status($result)) {
case PGSQL_COMMAND_OK:
$rows = pg_affected_rows($result);
break;
case PGSQL_TUPLES_OK:
$rows = pg_num_rows($result);
break;
case PGSQL_BAD_RESPONSE:
case PGSQL_NONFATAL_ERROR:
case PGSQL_FATAL_ERROR:
throw new Database_Exception(':error [ :query ]', array(':error' => pg_result_error($result), ':query' => $sql));
case PGSQL_COPY_OUT:
case PGSQL_COPY_IN:
pg_end_copy($this->_connection);
throw new Database_Exception('PostgreSQL COPY operations not supported [ :query ]', array(':query' => $sql));
default:
$rows = 0;
}
if (isset($benchmark)) {
Profiler::stop($benchmark);
}
$this->last_query = $sql;
if ($type === Database::SELECT) {
return new Database_PostgreSQL_Result($result, $sql, $as_object, $params, $rows);
}
if ($type === Database::INSERT) {
if ($this->_config['primary_key']) {
// Fetch the first column of the last row
$insert_id = pg_fetch_result($result, $rows - 1, 0);
} elseif ($insert_id = pg_send_query($this->_connection, 'SELECT LASTVAL()')) {
if ($result = pg_get_result($this->_connection) and pg_result_status($result) === PGSQL_TUPLES_OK) {
$insert_id = pg_fetch_result($result, 0);
}
}
return array($insert_id, $rows);
}
return $rows;
} catch (Exception $e) {
if (isset($benchmark)) {
Profiler::delete($benchmark);
}
throw $e;
}
}
示例8: bulkInsert
public function bulkInsert($table, array $params, $extra = null)
{
# Ensure we have a connection to run this query on
$this->connect();
if ($output = $this->output) {
$this->output = false;
echo "BULK INSERT INTO " . $table . " (" . count($params) . " rows)...\n";
}
switch ($this->mode) {
case "mysql":
case "redshift":
case "odbc":
$fields = "";
$first = reset($params);
foreach ($first as $key => $val) {
if ($fields) {
$fields .= ",";
}
$fields .= $this->quoteField($key);
}
$newParams = [];
$noParams = false;
if ($this->mode == "redshift" && count($params) * count($first) > 32767) {
$noParams = true;
}
$values = "";
foreach ($params as $row) {
if ($values) {
$values .= ",";
}
$values .= "(";
$first = true;
foreach ($row as $key => $val) {
if ($first) {
$first = false;
} else {
$values .= ",";
}
if ($noParams) {
$values .= "'" . pg_escape_string($val) . "'";
} else {
$values .= "?";
$newParams[] = $val;
}
}
$values .= ")";
}
$tableName = $this->getTableName($table);
if ($extra === self::INSERT_REPLACE) {
$query = "REPLACE ";
} elseif ($extra === self::INSERT_IGNORE) {
$query = "INSERT IGNORE ";
} else {
$query = "INSERT ";
}
$query .= "INTO " . $tableName . " (" . $fields . ") VALUES " . $values;
$result = $this->query($query, $newParams);
break;
case "postgres":
$fields = "";
$first = reset($params);
foreach ($first as $key => $val) {
if ($fields) {
$fields .= ",";
}
$fields .= $this->quoteField($key);
}
$tableName = $this->getTableName($table);
$this->query("COPY " . $tableName . " (" . $fields . ") FROM STDIN");
foreach ($params as $row) {
if (!pg_put_line($this->server, implode("\t", $row) . "\n")) {
$this->error();
}
}
if (pg_put_line($this->server, "\\.\n")) {
$this->error();
}
$result = new Result(pg_end_copy($this->server), $this->mode);
break;
default:
$result = true;
foreach ($params as $newParams) {
if (!$this->insert($table, $newParams)) {
$result = false;
break;
}
}
}
if (!$result) {
$this->error();
}
if ($output) {
$this->output = true;
}
return $result;
}