📄 pgsql.php
字号:
*/ function dropSequence($seq_name) { return $this->query('DROP SEQUENCE ' . $this->getSequenceName($seq_name)); } // }}} // {{{ modifyLimitQuery() /** * Adds LIMIT clauses to a query string according to current DBMS standards * * @param string $query the query to modify * @param int $from the row to start to fetching (0 = the first row) * @param int $count the numbers of rows to fetch * @param mixed $params array, string or numeric data to be used in * execution of the statement. Quantity of items * passed must match quantity of placeholders in * query: meaning 1 placeholder for non-array * parameters or 1 placeholder per array element. * * @return string the query string with LIMIT clauses added * * @access protected */ function modifyLimitQuery($query, $from, $count, $params = array()) { return "$query LIMIT $count OFFSET $from"; } // }}} // {{{ pgsqlRaiseError() /** * Produces a DB_Error object regarding the current problem * * @param int $errno if the error is being manually raised pass a * DB_ERROR* constant here. If this isn't passed * the error information gathered from the DBMS. * * @return object the DB_Error object * * @see DB_common::raiseError(), * DB_pgsql::errorNative(), DB_pgsql::errorCode() */ function pgsqlRaiseError($errno = null) { $native = $this->errorNative(); if ($errno === null) { $errno = $this->errorCode($native); } return $this->raiseError($errno, null, null, null, $native); } // }}} // {{{ errorNative() /** * Gets the DBMS' native error message produced by the last query * * {@internal Error messages are used instead of error codes * in order to support older versions of PostgreSQL.}} * * @return string the DBMS' error message */ function errorNative() { return @pg_errormessage($this->connection); } // }}} // {{{ errorCode() /** * Determines PEAR::DB error code from the database's text error message. * * @param string $errormsg error message returned from the database * @return integer an error number from a DB error constant */ function errorCode($errormsg) { static $error_regexps; if (!isset($error_regexps)) { $error_regexps = array( '/(relation|sequence|table).*does not exist|class .* not found/i' => DB_ERROR_NOSUCHTABLE, '/index .* does not exist/' => DB_ERROR_NOT_FOUND, '/column .* does not exist/i' => DB_ERROR_NOSUCHFIELD, '/relation .* already exists/i' => DB_ERROR_ALREADY_EXISTS, '/(divide|division) by zero$/i' => DB_ERROR_DIVZERO, '/pg_atoi: error in .*: can\'t parse /i' => DB_ERROR_INVALID_NUMBER, '/invalid input syntax for( type)? (integer|numeric)/i' => DB_ERROR_INVALID_NUMBER, '/value .* is out of range for type \w*int/i' => DB_ERROR_INVALID_NUMBER, '/integer out of range/i' => DB_ERROR_INVALID_NUMBER, '/value too long for type character/i' => DB_ERROR_INVALID, '/attribute .* not found|relation .* does not have attribute/i' => DB_ERROR_NOSUCHFIELD, '/column .* specified in USING clause does not exist in (left|right) table/i' => DB_ERROR_NOSUCHFIELD, '/parser: parse error at or near/i' => DB_ERROR_SYNTAX, '/syntax error at/' => DB_ERROR_SYNTAX, '/column reference .* is ambiguous/i' => DB_ERROR_SYNTAX, '/permission denied/' => DB_ERROR_ACCESS_VIOLATION, '/violates not-null constraint/' => DB_ERROR_CONSTRAINT_NOT_NULL, '/violates [\w ]+ constraint/' => DB_ERROR_CONSTRAINT, '/referential integrity violation/' => DB_ERROR_CONSTRAINT, '/more expressions than target columns/i' => DB_ERROR_VALUE_COUNT_ON_ROW, ); } foreach ($error_regexps as $regexp => $code) { if (preg_match($regexp, $errormsg)) { return $code; } } // Fall back to DB_ERROR if there was no mapping. return DB_ERROR; } // }}} // {{{ 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. * While this also accepts a query result * resource identifier, this behavior is * deprecated. * @param int $mode a valid tableInfo mode * * @return array an associative array with the information requested. * A DB_Error object on failure. * * @see DB_common::tableInfo() */ function tableInfo($result, $mode = null) { if (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; } elseif (isset($result->result)) { /* * Probably received a result object. * Extract the result resource identifier. */ $id = $result->result; $got_string = false; } 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); $res = array(); if ($mode) { $res['num_fields'] = $count; } for ($i = 0; $i < $count; $i++) { $res[$i] = array( 'table' => $got_string ? $case_func($result) : '', 'name' => $case_func(@pg_fieldname($id, $i)), 'type' => @pg_fieldtype($id, $i), 'len' => @pg_fieldsize($id, $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; } // }}} // {{{ _pgFieldFlags() /** * Get a column's flags * * Supports "not_null", "default_value", "primary_key", "unique_key" * and "multiple_key". The default value is passed through * rawurlencode() in case there are spaces in it. * * @param int $resource the PostgreSQL result identifier * @param int $num_field the field number * * @return string the flags * * @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); } // }}} // {{{ getSpecialQuery() /** * Obtains the query string needed for listing a given type of objects * * @param string $type the kind of objects you want to retrieve * * @return string the SQL query string or null if the driver doesn't * support the object type requested * * @access protected * @see DB_common::getListOf() */ 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_|sql_)'" . ' 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 'schema.tables': return "SELECT schemaname || '.' || tablename" . ' AS "Name"' . ' FROM pg_catalog.pg_tables' . ' WHERE schemaname NOT IN' . " ('pg_catalog', 'information_schema', 'pg_toast')"; case 'views': // Table cols: viewname | viewowner | definition return 'SELECT viewname from pg_views WHERE schemaname' . " NOT IN ('information_schema', 'pg_catalog')"; 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': case 'procedures': return 'SELECT proname FROM pg_proc WHERE proowner <> 1'; default: return null; } } // }}}}/* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -