?? pgsql.php
字號:
/** * Get the number of rows in a result set. * * @param $result resource PostgreSQL result identifier * * @return int the number of rows in $result */ function numRows($result) { $rows = @pg_numrows($result); if ($rows === null) { return $this->pgsqlRaiseError(); } return $rows; } // }}} // {{{ errorNative() /** * Get the native error code of the last error (if any) that * occured on the current connection. * * @return int native PostgreSQL error code */ function errorNative() { return pg_errormessage($this->connection); } // }}} // {{{ autoCommit() /** * Enable/disable automatic commits */ function autoCommit($onoff = false) { // XXX if $this->transaction_opcount > 0, we should probably // issue a warning here. $this->autocommit = $onoff ? true : false; return DB_OK; } // }}} // {{{ commit() /** * Commit the current transaction. */ function commit() { if ($this->transaction_opcount > 0) { // (disabled) hack to shut up error messages from libpq.a //@fclose(@fopen("php://stderr", "w")); $result = @pg_exec($this->connection, 'end;'); $this->transaction_opcount = 0; if (!$result) { return $this->pgsqlRaiseError(); } } return DB_OK; } // }}} // {{{ rollback() /** * Roll back (undo) the current transaction. */ function rollback() { if ($this->transaction_opcount > 0) { $result = @pg_exec($this->connection, 'abort;'); $this->transaction_opcount = 0; if (!$result) { return $this->pgsqlRaiseError(); } } return DB_OK; } // }}} // {{{ affectedRows() /** * Gets the number of rows affected by the last query. * if the last query was a select, returns 0. * * @return int number of rows affected by the last query or DB_ERROR */ function affectedRows() { return $this->affected; } // }}} // {{{ nextId() /** * Returns the next free id in a sequence * * @param string $seq_name name of the sequence * @param boolean $ondemand when true, the seqence is automatically * created if it does not exist * * @return int the next id number in the sequence. DB_Error if problem. * * @internal * @see DB_common::nextID() * @access public */ function nextId($seq_name, $ondemand = true) { $seqname = $this->getSequenceName($seq_name); $repeat = false; do { $this->pushErrorHandling(PEAR_ERROR_RETURN); $result =& $this->query("SELECT NEXTVAL('${seqname}')"); $this->popErrorHandling(); if ($ondemand && DB::isError($result) && $result->getCode() == DB_ERROR_NOSUCHTABLE) { $repeat = true; $this->pushErrorHandling(PEAR_ERROR_RETURN); $result = $this->createSequence($seq_name); $this->popErrorHandling(); if (DB::isError($result)) { return $this->raiseError($result); } } else { $repeat = false; } } while ($repeat); if (DB::isError($result)) { return $this->raiseError($result); } $arr = $result->fetchRow(DB_FETCHMODE_ORDERED); $result->free(); return $arr[0]; } // }}} // {{{ createSequence() /** * Create the sequence * * @param string $seq_name the name of the sequence * @return mixed DB_OK on success or DB error on error * @access public */ function createSequence($seq_name) { $seqname = $this->getSequenceName($seq_name); $result = $this->query("CREATE SEQUENCE ${seqname}"); return $result; } // }}} // {{{ dropSequence() /** * Drop a sequence * * @param string $seq_name the name of the sequence * @return mixed DB_OK on success or DB error on error * @access public */ function dropSequence($seq_name) { $seqname = $this->getSequenceName($seq_name); return $this->query("DROP SEQUENCE ${seqname}"); } // }}} // {{{ modifyLimitQuery() function modifyLimitQuery($query, $from, $count) { $query = $query . " LIMIT $count OFFSET $from"; return $query; } // }}} // {{{ pgsqlRaiseError() /** * Gather information about an error, then use that info to create a * DB error object and finally return that object. * * @param integer $errno PEAR error number (usually a DB constant) if * manually raising an error * @return object DB error object * @see errorNative() * @see errorCode() * @see DB_common::raiseError() */ function pgsqlRaiseError($errno = null) { $native = $this->errorNative(); if ($errno === null) { $err = $this->errorCode($native); } else { $err = $errno; } return $this->raiseError($err, null, null, null, $native); } // }}} // {{{ _pgFieldFlags() /** * Flags of a Field * * @param int $resource PostgreSQL result identifier * @param int $num_field the field number * * @return string The flags of the field ("not_null", "default_value", * "primary_key", "unique_key" and "multiple_key" * are supported). The default value is passed * through rawurlencode() in case there are spaces in it. * @access private */ function _pgFieldFlags($resource, $num_field, $table_name) { $field_name = @pg_fieldname($resource, $num_field); $result = @pg_exec($this->connection, "SELECT f.attnotnull, f.atthasdef FROM pg_attribute f, pg_class tab, pg_type typ WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid AND f.attname = '$field_name' AND tab.relname = '$table_name'"); if (@pg_numrows($result) > 0) { $row = @pg_fetch_row($result, 0); $flags = ($row[0] == 't') ? 'not_null ' : ''; if ($row[1] == 't') { $result = @pg_exec($this->connection, "SELECT a.adsrc FROM pg_attribute f, pg_class tab, pg_type typ, pg_attrdef a WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid AND f.attrelid = a.adrelid AND f.attname = '$field_name' AND tab.relname = '$table_name' AND f.attnum = a.adnum"); $row = @pg_fetch_row($result, 0); $num = preg_replace("/'(.*)'::\w+/", "\\1", $row[0]); $flags .= 'default_' . rawurlencode($num) . ' '; } } else { $flags = ''; } $result = @pg_exec($this->connection, "SELECT i.indisunique, i.indisprimary, i.indkey FROM pg_attribute f, pg_class tab, pg_type typ, pg_index i WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid AND f.attrelid = i.indrelid AND f.attname = '$field_name' AND tab.relname = '$table_name'"); $count = @pg_numrows($result); for ($i = 0; $i < $count ; $i++) { $row = @pg_fetch_row($result, $i); $keys = explode(' ', $row[2]); if (in_array($num_field + 1, $keys)) { $flags .= ($row[0] == 't' && $row[1] == 'f') ? 'unique_key ' : ''; $flags .= ($row[1] == 't') ? 'primary_key ' : ''; if (count($keys) > 1) $flags .= 'multiple_key '; } } return trim($flags); } // }}} // {{{ tableInfo() /** * Returns information about a table or a result set. * * NOTE: only supports 'table' and 'flags' if <var>$result</var> * is a table name. * * @param object|string $result DB_result object from a query or a * string containing the name of a table * @param int $mode a valid tableInfo mode * @return array an associative array with the information requested * or an error object if something is wrong * @access public * @internal * @see DB_common::tableInfo() */ function tableInfo($result, $mode = null) { if (isset($result->result)) { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->result; $got_string = false; } elseif (is_string($result)) { /* * Probably received a table name. * Create a result resource identifier. */ $id = @pg_exec($this->connection, "SELECT * FROM $result LIMIT 0"); $got_string = true; } else { /* * Probably received a result resource identifier. * Copy it. * Deprecated. Here for compatibility only. */ $id = $result; $got_string = false; } if (!is_resource($id)) { return $this->pgsqlRaiseError(DB_ERROR_NEED_MORE_DATA); } if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) { $case_func = 'strtolower'; } else { $case_func = 'strval'; } $count = @pg_numfields($id); // made this IF due to performance (one if is faster than $count if's) if (!$mode) { for ($i=0; $i<$count; $i++) { $res[$i]['table'] = $got_string ? $case_func($result) : ''; $res[$i]['name'] = $case_func(@pg_fieldname($id, $i)); $res[$i]['type'] = @pg_fieldtype($id, $i); $res[$i]['len'] = @pg_fieldsize($id, $i); $res[$i]['flags'] = $got_string ? $this->_pgFieldflags($id, $i, $result) : ''; } } else { // full $res['num_fields']= $count; for ($i=0; $i<$count; $i++) { $res[$i]['table'] = $got_string ? $case_func($result) : ''; $res[$i]['name'] = $case_func(@pg_fieldname($id, $i)); $res[$i]['type'] = @pg_fieldtype($id, $i); $res[$i]['len'] = @pg_fieldsize($id, $i); $res[$i]['flags'] = $got_string ? $this->_pgFieldFlags($id, $i, $result) : ''; if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } } // free the result only if we were called on a table if ($got_string) { @pg_freeresult($id); } return $res; } // }}} // {{{ getTablesQuery() /** * Returns the query needed to get some backend info * @param string $type What kind of info you want to retrieve * @return string The SQL query string */ function getSpecialQuery($type) { switch ($type) { case 'tables': return "SELECT c.relname as \"Name\" FROM pg_class c, pg_user u WHERE c.relowner = u.usesysid AND c.relkind = 'r' AND not exists (select 1 from pg_views where viewname = c.relname) AND c.relname !~ '^pg_' UNION SELECT c.relname as \"Name\" FROM pg_class c WHERE c.relkind = 'r' AND not exists (select 1 from pg_views where viewname = c.relname) AND not exists (select 1 from pg_user where usesysid = c.relowner) AND c.relname !~ '^pg_'"; case 'views': // Table cols: viewname | viewowner | definition return 'SELECT viewname FROM pg_views'; case 'users': // cols: usename |usesysid|usecreatedb|usetrace|usesuper|usecatupd|passwd |valuntil return 'SELECT usename FROM pg_user'; case 'databases': return 'SELECT datname FROM pg_database'; case 'functions': return 'SELECT proname FROM pg_proc'; default: return null; } } // }}}}/* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */?>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -