PostgreSQL functions. LXXX. PostgreSQL Features Database Size

Standard Template Library ( Standard Template Library , STL) is included in the standard library of the C++ language. It includes implementations of the most commonly used containers and algorithms, which saves programmers from the routine of rewriting them over and over again. When developing containers and the algorithms applied to them (such as removing identical elements, sorting, searching, etc.), either versatility or performance often have to be sacrificed. However, the STL developers set themselves the task of making the library both efficient and universal. To solve this problem, such universal means of the C++ language as templates and operator overloading were used. In the following presentation we will rely on the STL implementation supplied by Microsoft along with the Visual C++ 6.0 compiler. However, most of the above will also be true for STL implementations by other compilers.

The main concepts in STL are the concepts of container (container), algorithm (algorithm) and iterator (iterator).

Containeris a store of objects (both built-in and user-defined types). Typically, containers are implemented as class templates. The simplest types of containers (static and dynamic arrays) are built directly into the C++ language. In addition, the standard library includes implementations of containers such as vector, list, queue, map, set, and some others.

Algorithmis a function for manipulating objects contained in a container. Typical examples of algorithms are sorting and searching. The STL implements about 60 algorithms that can be applied to various containers, including arrays built into the C++ language.

Iteratoris an abstraction of a pointer, that is, an object that can refer to other objects contained in the container. The main functions of an iterator are to provide access to the object it refers to (dereferencing) and to move from one container element to another (iteration, hence the name iterator). For built-in containers, regular pointers are used as iterators. In the case of more complex containers, iterators are implemented as classes with a set of overloaded operators.

In addition to the marked elements in the STL there are a number auxiliary concepts ; You should also get to know some of them.

Allocator(allocator) is an object responsible for allocating memory for container elements. An allocator is associated with each standard container (its type is passed as one of the template parameters). If some algorithm needs to allocate memory for elements, it must do this through an allocator. In this case, you can be sure that distributed objects will be destroyed correctly.

The STL includes a standard allocator class (described in the xmemory file). This is what all containers implemented in the STL use by default. However, the user can implement his own class. This is very rarely necessary, but can sometimes be done for efficiency or debugging purposes.

Let us dwell in more detail on the consideration of the introduced concepts.

Containers . Each container provides a strictly defined interface through which algorithms will interact with it. Container-specific iterators provide this interface. It is important to emphasize that no additional member functions are used to interact between algorithms and containers. This is done because standard algorithms must work, including the built-in containers of the C++ language, which have iterators (pointers) but nothing else. Thus, when creating your own container, implementing an iterator is the bare minimum.

Each container implements a specific type of iterator. This selects the most functional iterator type that can be effectively implemented for a given container. "Efficient" means that the speed of performing operations on the iterator should not depend on the number of elements in the container. For example, a random access iterator is implemented for a vector, and a bidirectional iterator is implemented for a list. Because the speed of a list operation is linear with its length, a random access iterator for a list is not implemented.

Regardless of the actual organization of the container (vector, list, tree), the elements stored in it can be considered as a sequence. The iterator of the first element in this sequence is returned by the begin() function, and the iterator of the element following the last is returned by the end() function. This is very important, since all algorithms in STL work precisely with sequences specified by start and end iterators.

In addition to regular iterators in the STL, there are reverse iterators ( reverse iterator ). A reverse iterator is different in that it iterates through the sequence of elements in a container in reverse order. In other words, the operations + and - are reversed. This allows the algorithms to be applied to both direct and reverse sequences of elements. For example, using the find function, you can search for elements from both the “beginning” and “the end” of a container.

In STL, containers are divided into three main groups (Table 2): sequence containers, associative containers, and container adapters. The first two groups are combined into first class containers.

table 2

Container class STL

Description

Sequence containers

vector

Dynamic array

deque

Bidirectional queue

list

Bidirectional linear list

Associative Containers

Associative container with unique keys

multiset

Associative container allowing duplicate keys

Associative container for sets of unique elements

multimap

Associative container for sets with duplicate elements

Container adapters

stack

Standard stack

queue

Standard queue

priority_queue

Priority queue

Each container class implemented in STL , describes the set of types associated with the container. You should follow this same practice when writing your own containers. Here is a list of the most important types:

value_type - element type;

size_type - a type for storing the number of elements (usually size_t);

iterator - iterator for container elements;

key_type - key type (in an associative container).

In addition to types, we can distinguish a set of functions that almost every container in STL implements (Table 3). They are not required for interaction with algorithms, but their implementation improves the interchangeability of containers in the program. STL is designed so that containers provide similar functionality.

Table 3

Common methods of all STL containers

Description

default constructor

Default constructor. Typically a container has several designers

copy constructor

Copy constructor

destructor

Destructor

empty

Returns true if there are no elements in the container, false otherwise

max_size

Returns the maximum number of elements for a container

size

Returns the number of elements in the container at the current time

operator =

Assigns one container to another

operator<

Returns true if the first container is smaller than the second, false otherwise

operator<=

Returns true if the first container is not larger than the second, false otherwise

operator >

Returns true if the first container is larger than the second, false otherwise

operator >=

Returns true if the first container is not smaller than the second, false otherwise

operator ==

Returns true if the compared containers are equal, false otherwise

operator !=

Returns true if the compared containers are not equal, false otherwise

swap

Swaps elements of two containers

Features available only in first class containers

begin

Two versions of this function return either an iterator or a const_iterator, which refers to the first element of the container

Two versions of this function return either an iterator or a const_iterator, which refers to the next position after the end of the container

rbegin

Two versions of this function return either reverse_iterator or reverse_const_iterator , which refers to the last element of the container

rend

Two versions of this function return either a reverse_iterator or a reverse_const_iterator, which refers to the position before the first element of the container

insert, erase,

Allows you to insert or remove element(s) in the middle of a sequence

End table 3

clear

Removes all elements from a container

front, back

push_back, pop_back

Allows you to add or remove the last element in a sequence

push_front, pop_front

Allows you to add or remove the first element in a sequence

Iterators are typically created as friends of the classes they operate on, allowing direct access to the private data of those classes. A single container can have multiple iterators associated with it, each of which maintains its own “positional information” (Table 4).

Table 4

Iterator type

Access

Dereferencing

Iteration

Comparison

Output Iterator

(output iterator)

Recording only

Input Iterator

(input iterator)

Only reading

*, ->

==, !=

Forward iterator

(forward iterator)

Read and Write

*, ->

==, !=

Bidirectional iterator(bidirectional iterator)

Read and Write

*, ->

++, --

==, !=

Random access iterator

(random - access iterator)

Read and Write

*, ->,

++, --, +, -, +=, -=

==, !=, <, <=, >, >=

pg_update (resource $connection , string $table_name , array $data , array $condition [, int $options = PGSQL_DML_EXEC ])

pg_update() replaces records in the table that meet the conditions condition data data. If an argument is given options, the data will be transformed by the function pg_convert(), to which the parameters from this argument will be passed.

List of parameters

PostgreSQL database connection resource.

Table_name

The name of the table in which the records are updated.

Array( array), the keys of which correspond to the names of the columns of the table_name table, and the values ​​will replace the data in these columns.

Condition

Array( array), the keys of which correspond to the names of the columns of the table table_name . Only those rows whose field values ​​match the array values ​​will be updated.

Options

One of the constants PGSQL_CONV_OPTS, PGSQL_DML_NO_CONV, PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC or PGSQL_DML_STRING, or a combination of them. If options contains PGSQL_DML_STRING, the function will return a string. If installed PGSQL_DML_NO_CONV or PGSQL_DML_ESCAPE, then the function pg_convert() not called internally.

Return values

Returns TRUE upon successful completion or FALSE in case of an error. The function will return the string ( string), if constant PGSQL_DML_STRING contained in options.

Examples

Example #1 Usage example pg_update()

$db = pg_connect("dbname=foo" );
$data = array("field1" => "AA" , "field2" => "BB" );

// This is safe since $_POST is automatically converted
$res = pg_update ($db, "post_log", $_POST, $data);
if ($res) (
echo "Data updated:$res\n" ;
) else (
echo "Invalid data must have been sent\n";
}
?>

pg_update (resource $connection , string $table_name , array $data , array $condition [, int $options = PGSQL_DML_EXEC ])

pg_update() replaces records in the table that meet the conditions condition data data. If an argument is given options, the data will be transformed by the function pg_convert(), to which the parameters from this argument will be passed.

List of parameters

PostgreSQL database connection resource.

Table_name

The name of the table in which the records are updated.

Array( array), the keys of which correspond to the names of the columns of the table_name table, and the values ​​will replace the data in these columns.

Condition

Array( array), the keys of which correspond to the names of the columns of the table table_name . Only those rows whose field values ​​match the array values ​​will be updated.

Options

One of the constants PGSQL_CONV_OPTS, PGSQL_DML_NO_CONV, PGSQL_DML_ESCAPE, PGSQL_DML_EXEC, PGSQL_DML_ASYNC or PGSQL_DML_STRING, or a combination of them. If options contains PGSQL_DML_STRING, the function will return a string. If installed PGSQL_DML_NO_CONV or PGSQL_DML_ESCAPE, then the function pg_convert() not called internally.

Return values

Returns TRUE upon successful completion or FALSE in case of an error. The function will return the string ( string), if constant PGSQL_DML_STRING contained in options.

Examples

Example #1 Usage example pg_update()

$db = pg_connect("dbname=foo" );
$data = array("field1" => "AA" , "field2" => "BB" );

// This is safe since $_POST is automatically converted
$res = pg_update ($db, "post_log", $_POST, $data);
if ($res) (
echo "Data updated:$res\n" ;
) else (
echo "Invalid data must have been sent\n";
}
?>

Postgres, originally developed by the UC Berkeley Computer Science Department, pioneered many of the object-oriented concepts now available in some commercial databases. It provides support for the SQL92/SQL99 language, transaction integrity, and type extensibility. PostgreSQL is an open source descendant of the original Berkeley code.

PostgreSQL is a product available for free. To use PostgreSQL support, you need PostgreSQL 6.5 or later. PostgreSQL 7.0 or later - for all PostgreSQL module features. PostgreSQL supports many character encodings, including multibyte character encodings. The current version and information about PostgreSQL can be found at http://www.postgresql.org/.

To enable PostgreSQL support, the --with-pgsql[=DIR] option is required when compiling PHP. If the shared objects module is available, the PostgreSQL module can be loaded using the extension directive in the php.ini file or the function dl(). Supported ini directives are described in the php.ini-dist file supplied with the distribution source code.

Table 1. Function name changes
Old nameNew name
pg_exec() pg_query()
pg_getlastoid() pg_last_oid()
pg_cmdtuples() pg_affected_rows()
pg_numrows() pg_num_rows()
pg_numfields() pg_num_fields()
pg_fieldname() pg_field_name()
pg_fieldsize() pg_field_size()
pg_fieldnum() pg_field_num()
pg_fieldprtlen() pg_field_prtlen()
pg_fieldisnull() pg_field_is_null()
pg_freeresult() pg_free_result()
pg_result() pg_fetch_result()
pg_loreadall() pg_lo_read_all()
pg_locreate() pg_lo_create()
pg_lounlink() pg_lo_unlink()
pg_loopen() pg_lo_open()
pg_loclose() pg_lo_close()
pg_loread() pg_lo_read()
pg_lowrite() pg_lo_write()
pg_loimport() pg_lo_import()
pg_loexport() pg_lo_export()

Not all features are supported in all builds. This depends on the version of your libpq (The PostgreSQL C Client interface) and how libpq is compiled. If there is a missing function, libpq does not support the capabilities required by that function.

It is also important that you use a libpq that is newer than the PostgreSQL Server you are connecting to. If you are using a libpq that is older than PostgreSQL Server expects, you will have problems.

Since version 6.3 (02/03/1998), PostgreSQL uses a unix domain socket by default. The TCP port is NOT opened by default. The table describes these new connection options. This socket can be found in /tmp/.s.PGSQL.5432. This option can be enabled with the "-i" flag for postmaster, and its value will be: "listen to TCP/IP sockets as well as Unix domain sockets."

Table 2. Postmaster and PHP

A connection to the PostgreSQL server can be established with the following pairs of values ​​on the command line: $conn = pg_connect("host=myHost port=myPort tty=myTTY options=myOptions dbname=myDB user=myUser password=myPassword ");

Previous syntax: $conn = pg_connect("host", "port", "options", "tty", "dbname") is no longer recommended.

Environment variables affect the behavior of PostgreSQL server/client. For example, the PostgreSQL module will look for the PGHOST environment variable if hostname is not in the connection string. Supported environment variables differ between versions. See PostgreSQL Programmer's Manual (libpq - Environment Variables) for details.

Make sure you set the environment variables for the appropriate user. Use $_ENV or getenv() to check which environment variables are available to the current process.

Starting with PostgreSQL 7.1.0, you can store 1GB in a text field. Older versions may have block size restrictions (the default was 8KB, the maximum was 32KB, determined at compile time).

To use the large object (lo) interface, you must include lo functions inside the transaction block. A transaction block begins with a SQL statement BEGIN, and if the transaction was correct, ends COMMIT or END. If a transaction fails, it must be closed with ROLLBACK or ABORT.

You should not close the connection to the PostgreSQL server before closing the large object.

Content pg_affeded_rows - returns the number of entries involved (steam/tuples) pg_cancel_query - cancels the async request pg_client_encoding - receives customer coding pg_close - closes the postgreSQL connection PG_ConNNECT - opens posteg Rescl connection PG_ConNECTION_BUSY - receives whether the connection is closed or not pg_connection_Reset - restores the connection (reconnect) pg_connection_status - gets connection status pg_convert - converts the value of an associative array into a value suitable for the SQL statement pg_copy_from - inserts records into a table from an array pg_copy_to - copies a table into an array pg_dbname - gets the database name pg_delete - deletes records pg_end_copy - synchronizes with PostgreSQL backend pg_escape_bytea - mnemonizes /escape binary for bytea type pg_escape_string - mnemonizes string for type text/char pg_fetch_array - retrieves a row as an array pg_fetch_object - retrieves a row as an object pg_fetch_result - returns values ​​from the resulting resource pg_fetch_row - retrieves a row as an enumerable array pg_field_is_null - checks whether a field is NULL pg_field_name - returns the name of the field pg_field_num - returns the number of the named field pg_field_prtlen - returns the printable size pg_field_size - returns the internal storage size of the named field pg_field_type - returns the type name for the corresponding field number

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

pg_convert — Converts the values ​​of an associative array into acceptable values ​​for use in SQL queries

Description

pg_convert (resource $connection , string $table_name , array $assoc_array [, int $options = 0]) : array

pg_convert() checks and converts values ​​from assoc_array into those acceptable to the SQL server. The table table_name must exist, and the number of columns in it must not be less than the values ​​in the assoc_array array. The column names in the table_name table must match the keys of the assoc_array array, and the data types of the array values ​​must also match the data types of the corresponding columns. If the conversion is successful, the function returns an array of converted values, otherwise it returns FALSE.

Comment:

As of PHP 5.6.0, boolean values ​​are allowed. They are converted to the PostgreSQL boolean type. String representations of a boolean value are also supported. NULL is converted to PostgreSQL NULL.

Before PHP 5.6.0, if the table table_name contains boolean fields, do not use a constant TRUE as the value of the table_name array for these fields. It will be converted to the string "TRUE", which is an invalid value for boolean fields in PostgreSQL. Use the values ​​"t", "true", "1", "y", "yes".

List of parameters

PostgreSQL database connection resource.

Table_name

Database table name.

Assoc_array

Data to convert.

Options

One of the constants PGSQL_CONV_IGNORE_DEFAULT, PGSQL_CONV_FORCE_NULL or PGSQL_CONV_IGNORE_NOT_NULL, or a combination of them.

Return values

Array( array), containing the converted data, or FALSE in case of error.

Examples

Example #1 Usage example pg_convert()

$dbconn = pg_connect ( "dbname=foo" );

$tmp = array(
"author" => "Joe Thackery" ,
"year" => 2005,
"title" => "My Life, by Joe Thackery"
);

$vals = pg_convert ($dbconn, "authors", $tmp);
?>