2010-04-21 12:01:32 +00:00
< ? php
2010-10-29 13:24:06 +00:00
$drivers [ " pgsql " ] = " PostgreSQL " ;
2010-04-21 12:01:32 +00:00
if ( isset ( $_GET [ " pgsql " ])) {
2010-10-29 13:24:06 +00:00
$possible_drivers = array ( " PgSQL " , " PDO_PgSQL " );
2010-04-21 12:01:32 +00:00
define ( " DRIVER " , " pgsql " );
if ( extension_loaded ( " pgsql " )) {
class Min_DB {
2018-03-10 16:16:07 +00:00
var $extension = " PgSQL " , $_link , $_result , $_string , $_database = true , $server_info , $affected_rows , $error , $timeout ;
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function _error ( $errno , $error ) {
if ( ini_bool ( " html_errors " )) {
$error = html_entity_decode ( strip_tags ( $error ));
}
2013-07-24 23:26:41 +00:00
$error = preg_replace ( '~^[^:]*: ~' , '' , $error );
2010-04-21 12:01:32 +00:00
$this -> error = $error ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function connect ( $server , $username , $password ) {
2011-07-19 15:58:44 +00:00
global $adminer ;
$db = $adminer -> database ();
2010-04-21 12:01:32 +00:00
set_error_handler ( array ( $this , '_error' ));
$this -> _string = " host=' " . str_replace ( " : " , " ' port=' " , addcslashes ( $server , " ' \\ " )) . " ' user=' " . addcslashes ( $username , " ' \\ " ) . " ' password=' " . addcslashes ( $password , " ' \\ " ) . " ' " ;
2012-02-24 05:47:26 +00:00
$this -> _link = @ pg_connect ( " $this->_string dbname=' " . ( $db != " " ? addcslashes ( $db , " ' \\ " ) : " postgres " ) . " ' " , PGSQL_CONNECT_FORCE_NEW );
2011-07-19 15:58:44 +00:00
if ( ! $this -> _link && $db != " " ) {
2010-04-21 12:01:32 +00:00
// try to connect directly with database for performance
$this -> _database = false ;
2012-02-24 05:47:26 +00:00
$this -> _link = @ pg_connect ( " $this->_string dbname='postgres' " , PGSQL_CONNECT_FORCE_NEW );
2010-04-21 12:01:32 +00:00
}
restore_error_handler ();
if ( $this -> _link ) {
$version = pg_version ( $this -> _link );
$this -> server_info = $version [ " server " ];
pg_set_client_encoding ( $this -> _link , " UTF8 " );
}
return ( bool ) $this -> _link ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function quote ( $string ) {
2018-02-06 14:42:14 +00:00
return " ' " . pg_escape_string ( $this -> _link , $string ) . " ' " ;
}
2018-02-06 15:05:39 +00:00
function value ( $val , $field ) {
return ( $field [ " type " ] == " bytea " ? pg_unescape_bytea ( $val ) : $val );
}
2018-02-06 14:42:14 +00:00
function quoteBinary ( $string ) {
return " ' " . pg_escape_bytea ( $this -> _link , $string ) . " ' " ;
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function select_db ( $database ) {
2011-07-19 15:58:44 +00:00
global $adminer ;
if ( $database == $adminer -> database ()) {
2010-04-21 12:01:32 +00:00
return $this -> _database ;
}
2010-08-03 13:08:14 +00:00
$return = @ pg_connect ( " $this->_string dbname=' " . addcslashes ( $database , " ' \\ " ) . " ' " , PGSQL_CONNECT_FORCE_NEW );
2010-05-14 13:51:54 +00:00
if ( $return ) {
$this -> _link = $return ;
2010-04-21 12:01:32 +00:00
}
2010-05-14 13:51:54 +00:00
return $return ;
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-22 23:02:28 +00:00
function close () {
2012-02-24 05:47:26 +00:00
$this -> _link = @ pg_connect ( " $this->_string dbname='postgres' " );
2010-04-22 23:02:28 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function query ( $query , $unbuffered = false ) {
$result = @ pg_query ( $this -> _link , $query );
2012-07-15 16:21:22 +00:00
$this -> error = " " ;
2010-04-21 12:01:32 +00:00
if ( ! $result ) {
$this -> error = pg_last_error ( $this -> _link );
2018-03-10 16:16:07 +00:00
$return = false ;
2010-04-21 12:01:32 +00:00
} elseif ( ! pg_num_fields ( $result )) {
$this -> affected_rows = pg_affected_rows ( $result );
2018-03-10 16:16:07 +00:00
$return = true ;
} else {
$return = new Min_Result ( $result );
}
if ( $this -> timeout ) {
$this -> timeout = 0 ;
$this -> query ( " RESET statement_timeout " );
2010-04-21 12:01:32 +00:00
}
2018-03-10 16:16:07 +00:00
return $return ;
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function multi_query ( $query ) {
return $this -> _result = $this -> query ( $query );
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function store_result () {
return $this -> _result ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function next_result () {
// PgSQL extension doesn't support multiple results
return false ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function result ( $query , $field = 0 ) {
$result = $this -> query ( $query );
2011-02-01 15:26:21 +00:00
if ( ! $result || ! $result -> num_rows ) {
2010-04-21 12:01:32 +00:00
return false ;
}
return pg_fetch_result ( $result -> _result , 0 , $field );
}
2018-02-01 11:00:34 +00:00
function warnings () {
return h ( pg_last_notice ( $this -> _link )); // second parameter is available since PHP 7.1.0
}
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
class Min_Result {
var $_result , $_offset = 0 , $num_rows ;
2013-07-24 23:26:41 +00:00
2015-08-15 15:04:21 +00:00
function __construct ( $result ) {
2010-04-21 12:01:32 +00:00
$this -> _result = $result ;
$this -> num_rows = pg_num_rows ( $result );
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function fetch_assoc () {
return pg_fetch_assoc ( $this -> _result );
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function fetch_row () {
return pg_fetch_row ( $this -> _result );
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function fetch_field () {
$column = $this -> _offset ++ ;
2010-05-14 13:51:54 +00:00
$return = new stdClass ;
2010-04-21 22:22:23 +00:00
if ( function_exists ( 'pg_field_table' )) {
2010-05-14 13:51:54 +00:00
$return -> orgtable = pg_field_table ( $this -> _result , $column );
2010-04-21 22:22:23 +00:00
}
2010-05-14 13:51:54 +00:00
$return -> name = pg_field_name ( $this -> _result , $column );
$return -> orgname = $return -> name ;
$return -> type = pg_field_type ( $this -> _result , $column );
$return -> charsetnr = ( $return -> type == " bytea " ? 63 : 0 ); // 63 - binary
return $return ;
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function __destruct () {
pg_free_result ( $this -> _result );
}
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
} elseif ( extension_loaded ( " pdo_pgsql " )) {
class Min_DB extends Min_PDO {
2018-03-10 16:16:07 +00:00
var $extension = " PDO_PgSQL " , $timeout ;
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function connect ( $server , $username , $password ) {
2011-07-19 15:58:44 +00:00
global $adminer ;
$db = $adminer -> database ();
2021-02-06 15:45:20 +00:00
$this -> dsn ( " pgsql:host=' " . str_replace ( " : " , " ' port=' " , addcslashes ( $server , " ' \\ " )) . " ' client_encoding=utf8 dbname=' " . ( $db != " " ? addcslashes ( $db , " ' \\ " ) : " postgres " ) . " ' " , $username , $password ); //! client_encoding is supported since 9.1 but we can't yet use min_version here
2010-04-21 12:01:32 +00:00
//! connect without DB in case of an error
return true ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function select_db ( $database ) {
2011-07-19 15:58:44 +00:00
global $adminer ;
return ( $adminer -> database () == $database );
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2018-02-06 14:42:14 +00:00
function quoteBinary ( $s ) {
return q ( $s );
}
2018-03-10 16:16:07 +00:00
function query ( $query , $unbuffered = false ) {
$return = parent :: query ( $query , $unbuffered );
if ( $this -> timeout ) {
$this -> timeout = 0 ;
parent :: query ( " RESET statement_timeout " );
}
return $return ;
}
2018-02-01 11:00:34 +00:00
function warnings () {
return '' ; // not implemented in PDO_PgSQL as of PHP 7.2.1
}
2010-04-22 23:02:28 +00:00
function close () {
}
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
}
2013-07-05 16:04:06 +00:00
class Min_Driver extends Min_SQL {
2013-07-24 23:26:41 +00:00
2013-07-09 18:34:12 +00:00
function insertUpdate ( $table , $rows , $primary ) {
2013-07-05 16:04:06 +00:00
global $connection ;
2013-07-09 18:34:12 +00:00
foreach ( $rows as $set ) {
$update = array ();
$where = array ();
foreach ( $set as $key => $val ) {
$update [] = " $key = $val " ;
if ( isset ( $primary [ idf_unescape ( $key )])) {
$where [] = " $key = $val " ;
}
}
if ( ! (( $where && queries ( " UPDATE " . table ( $table ) . " SET " . implode ( " , " , $update ) . " WHERE " . implode ( " AND " , $where )) && $connection -> affected_rows )
|| queries ( " INSERT INTO " . table ( $table ) . " ( " . implode ( " , " , array_keys ( $set )) . " ) VALUES ( " . implode ( " , " , $set ) . " ) " )
)) {
return false ;
2013-07-05 16:04:06 +00:00
}
}
2013-07-09 18:34:12 +00:00
return true ;
2013-07-05 16:04:06 +00:00
}
2013-07-24 23:26:41 +00:00
2018-03-09 17:06:19 +00:00
function slowQuery ( $query , $timeout ) {
2018-03-10 16:16:07 +00:00
$this -> _conn -> query ( " SET statement_timeout = " . ( 1000 * $timeout ));
$this -> _conn -> timeout = 1000 * $timeout ;
return $query ;
2018-03-09 17:06:19 +00:00
}
2018-02-06 12:39:44 +00:00
function convertSearch ( $idf , $val , $field ) {
2018-02-28 15:17:12 +00:00
return ( preg_match ( '~char|text'
2018-05-06 09:41:34 +00:00
. ( ! preg_match ( '~LIKE~' , $val [ " op " ]) ? '|date|time(stamp)?|boolean|uuid|' . number_type () : '' )
2018-03-11 15:41:04 +00:00
. '~' , $field [ " type " ])
2018-02-06 12:39:44 +00:00
? $idf
: " CAST( $idf AS text) "
);
}
2018-02-06 14:42:14 +00:00
function quoteBinary ( $s ) {
return $this -> _conn -> quoteBinary ( $s );
}
2018-02-01 10:50:15 +00:00
function warnings () {
2018-02-01 11:00:34 +00:00
return $this -> _conn -> warnings ();
2018-02-01 10:50:15 +00:00
}
2018-02-08 10:21:33 +00:00
function tableHelp ( $name ) {
$links = array (
" information_schema " => " infoschema " ,
" pg_catalog " => " catalog " ,
);
$link = $links [ $_GET [ " ns " ]];
if ( $link ) {
return " $link - " . str_replace ( " _ " , " - " , $name ) . " .html " ;
}
}
2013-07-05 16:04:06 +00:00
}
2010-04-21 12:01:32 +00:00
function idf_escape ( $idf ) {
return '"' . str_replace ( '"' , '""' , $idf ) . '"' ;
}
2010-05-11 14:45:04 +00:00
function table ( $idf ) {
return idf_escape ( $idf );
}
2010-04-21 12:01:32 +00:00
function connect () {
2017-04-08 17:59:13 +00:00
global $adminer , $types , $structured_types ;
2010-04-21 12:01:32 +00:00
$connection = new Min_DB ;
$credentials = $adminer -> credentials ();
if ( $connection -> connect ( $credentials [ 0 ], $credentials [ 1 ], $credentials [ 2 ])) {
2018-01-29 20:08:38 +00:00
if ( min_version ( 9 , 0 , $connection )) {
2011-08-23 12:23:48 +00:00
$connection -> query ( " SET application_name = 'Adminer' " );
2018-01-29 20:08:38 +00:00
if ( min_version ( 9.2 , 0 , $connection )) {
2017-04-08 17:59:13 +00:00
$structured_types [ lang ( 'Strings' )][] = " json " ;
$types [ " json " ] = 4294967295 ;
2018-01-29 20:08:38 +00:00
if ( min_version ( 9.4 , 0 , $connection )) {
2017-04-08 17:59:13 +00:00
$structured_types [ lang ( 'Strings' )][] = " jsonb " ;
$types [ " jsonb " ] = 4294967295 ;
}
}
2011-08-23 12:23:48 +00:00
}
2010-04-21 12:01:32 +00:00
return $connection ;
}
return $connection -> error ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function get_databases () {
2015-06-07 19:52:43 +00:00
return get_vals ( " SELECT datname FROM pg_database WHERE has_database_privilege(datname, 'CONNECT') ORDER BY datname " );
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-05-14 13:51:54 +00:00
function limit ( $query , $where , $limit , $offset = 0 , $separator = " " ) {
2012-05-14 06:54:07 +00:00
return " $query $where " . ( $limit !== null ? $separator . " LIMIT $limit " . ( $offset ? " OFFSET $offset " : " " ) : " " );
2010-04-21 12:01:32 +00:00
}
2018-02-01 17:53:53 +00:00
function limit1 ( $table , $query , $where , $separator = " \n " ) {
2018-02-01 17:47:57 +00:00
return ( preg_match ( '~^INTO~' , $query )
2018-02-01 17:53:53 +00:00
? limit ( $query , $where , 1 , 0 , $separator )
2018-03-11 15:41:04 +00:00
: " $query " . ( is_view ( table_status1 ( $table )) ? $where : " WHERE ctid = (SELECT ctid FROM " . table ( $table ) . $where . $separator . " LIMIT 1) " )
2018-02-01 17:47:57 +00:00
);
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function db_collation ( $db , $collations ) {
global $connection ;
2020-06-09 18:17:12 +00:00
return $connection -> result ( " SELECT datcollate FROM pg_database WHERE datname = " . q ( $db ));
2010-04-21 12:01:32 +00:00
}
function engines () {
return array ();
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function logged_user () {
global $connection ;
return $connection -> result ( " SELECT user " );
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function tables_list () {
2014-05-11 10:20:08 +00:00
$query = " SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = current_schema() " ;
if ( support ( 'materializedview' )) {
$query .= "
2014-05-10 01:57:35 +00:00
UNION ALL
SELECT matviewname , 'MATERIALIZED VIEW'
FROM pg_matviews
2015-01-06 10:20:40 +00:00
WHERE schemaname = current_schema () " ;
2014-05-11 10:20:08 +00:00
}
2015-01-06 10:20:40 +00:00
$query .= "
ORDER BY 1 " ;
2014-05-11 10:20:08 +00:00
return get_key_vals ( $query );
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function count_tables ( $databases ) {
return array (); // would require reconnect
}
function table_status ( $name = " " ) {
$return = array ();
2019-07-15 11:37:10 +00:00
foreach ( get_rows ( " SELECT c.relname AS \" Name \" , CASE c.relkind WHEN 'r' THEN 'table' WHEN 'm' THEN 'materialized view' ELSE 'view' END AS \" Engine \" , pg_relation_size(c.oid) AS \" Data_length \" , pg_total_relation_size(c.oid) - pg_relation_size(c.oid) AS \" Index_length \" , obj_description(c.oid, 'pg_class') AS \" Comment \" , " . ( min_version ( 12 ) ? " '' " : " CASE WHEN c.relhasoids THEN 'oid' ELSE '' END " ) . " AS \" Oid \" , c.reltuples as \" Rows \" , n.nspname
2014-07-10 10:34:08 +00:00
FROM pg_class c
JOIN pg_namespace n ON ( n . nspname = current_schema () AND n . oid = c . relnamespace )
2018-01-23 14:03:53 +00:00
WHERE relkind IN ( 'r' , 'm' , 'v' , 'f' )
2017-05-19 08:56:17 +00:00
" . ( $name != " " ? " AND relname = " . q( $name ) : " ORDER BY relname " )
2010-10-13 15:53:59 +00:00
) as $row ) { //! Index_length, Auto_increment
2010-04-21 12:01:32 +00:00
$return [ $row [ " Name " ]] = $row ;
}
return ( $name != " " ? $return [ $name ] : $return );
}
2013-07-24 23:26:41 +00:00
2010-05-27 11:31:08 +00:00
function is_view ( $table_status ) {
2014-05-10 01:57:35 +00:00
return in_array ( $table_status [ " Engine " ], array ( " view " , " materialized view " ));
2010-05-27 11:31:08 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function fk_support ( $table_status ) {
return true ;
}
2013-07-24 23:26:41 +00:00
2010-11-11 09:47:32 +00:00
function fields ( $table ) {
2010-04-21 12:01:32 +00:00
$return = array ();
2013-05-30 00:24:27 +00:00
$aliases = array (
'timestamp without time zone' => 'timestamp' ,
'timestamp with time zone' => 'timestamptz' ,
);
2018-10-20 17:34:28 +00:00
2020-04-29 10:03:23 +00:00
$identity_column = min_version ( 10 ) ? 'a.attidentity' : '0' ;
2018-10-20 17:34:28 +00:00
2019-10-18 17:12:57 +00:00
foreach ( get_rows ( " SELECT a.attname AS field, format_type(a.atttypid, a.atttypmod) AS full_type, pg_get_expr(d.adbin, d.adrelid) AS default, a.attnotnull::int, col_description(c.oid, a.attnum) AS comment, $identity_column AS identity
2010-05-21 13:07:59 +00:00
FROM pg_class c
JOIN pg_namespace n ON c . relnamespace = n . oid
JOIN pg_attribute a ON c . oid = a . attrelid
LEFT JOIN pg_attrdef d ON c . oid = d . adrelid AND a . attnum = d . adnum
2010-10-13 16:04:40 +00:00
WHERE c . relname = " . q( $table ) . "
2010-05-21 13:07:59 +00:00
AND n . nspname = current_schema ()
2010-05-21 14:23:44 +00:00
AND NOT a . attisdropped
2010-11-11 09:47:32 +00:00
AND a . attnum > 0
ORDER BY a . attnum "
2010-10-13 15:53:59 +00:00
) as $row ) {
//! collation, primary
2014-07-10 10:26:04 +00:00
preg_match ( '~([^([]+)(\((.*)\))?([a-z ]+)?((\[[0-9]*])*)$~' , $row [ " full_type " ], $match );
list (, $type , $length , $row [ " length " ], $addon , $array ) = $match ;
2013-08-06 22:48:45 +00:00
$row [ " length " ] .= $array ;
2014-07-10 10:26:04 +00:00
$check_type = $type . $addon ;
if ( isset ( $aliases [ $check_type ])) {
$row [ " type " ] = $aliases [ $check_type ];
$row [ " full_type " ] = $row [ " type " ] . $length . $array ;
} else {
$row [ " type " ] = $type ;
$row [ " full_type " ] = $row [ " type " ] . $length . $addon . $array ;
}
2020-04-29 10:03:23 +00:00
if ( in_array ( $row [ 'identity' ], array ( 'a' , 'd' ))) {
$row [ 'default' ] = 'GENERATED ' . ( $row [ 'identity' ] == 'd' ? 'BY DEFAULT' : 'ALWAYS' ) . ' AS IDENTITY' ;
2018-11-24 11:16:12 +00:00
}
2013-05-13 15:39:10 +00:00
$row [ " null " ] = ! $row [ " attnotnull " ];
2018-10-20 17:34:28 +00:00
$row [ " auto_increment " ] = $row [ 'identity' ] || preg_match ( '~^nextval\(~i' , $row [ " default " ]);
2010-10-13 15:53:59 +00:00
$row [ " privileges " ] = array ( " insert " => 1 , " select " => 1 , " update " => 1 );
2013-07-02 16:20:06 +00:00
if ( preg_match ( '~(.+)::[^)]+(.*)~' , $row [ " default " ], $match )) {
2018-01-17 08:50:45 +00:00
$row [ " default " ] = ( $match [ 1 ] == " NULL " ? null : (( $match [ 1 ][ 0 ] == " ' " ? idf_unescape ( $match [ 1 ]) : $match [ 1 ]) . $match [ 2 ]));
2011-05-18 14:52:23 +00:00
}
2010-10-13 15:53:59 +00:00
$return [ $row [ " field " ]] = $row ;
2010-04-21 12:01:32 +00:00
}
return $return ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function indexes ( $table , $connection2 = null ) {
global $connection ;
if ( ! is_object ( $connection2 )) {
$connection2 = $connection ;
}
$return = array ();
2012-06-29 21:59:04 +00:00
$table_oid = $connection2 -> result ( " SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema()) AND relname = " . q ( $table ));
2010-04-21 12:01:32 +00:00
$columns = get_key_vals ( " SELECT attnum, attname FROM pg_attribute WHERE attrelid = $table_oid AND attnum > 0 " , $connection2 );
2017-01-17 09:25:35 +00:00
foreach ( get_rows ( " SELECT relname, indisunique::int, indisprimary::int, indkey, indoption , (indpred IS NOT NULL)::int as indispartial FROM pg_index i, pg_class ci WHERE i.indrelid = $table_oid AND ci.oid = i.indexrelid " , $connection2 ) as $row ) {
2013-06-25 16:42:47 +00:00
$relname = $row [ " relname " ];
2014-07-10 10:34:08 +00:00
$return [ $relname ][ " type " ] = ( $row [ " indispartial " ] ? " INDEX " : ( $row [ " indisprimary " ] ? " PRIMARY " : ( $row [ " indisunique " ] ? " UNIQUE " : " INDEX " )));
2013-06-25 16:42:47 +00:00
$return [ $relname ][ " columns " ] = array ();
2010-04-21 12:01:32 +00:00
foreach ( explode ( " " , $row [ " indkey " ]) as $indkey ) {
2013-06-25 16:42:47 +00:00
$return [ $relname ][ " columns " ][] = $columns [ $indkey ];
2010-04-21 12:01:32 +00:00
}
2013-06-25 16:42:47 +00:00
$return [ $relname ][ " descs " ] = array ();
foreach ( explode ( " " , $row [ " indoption " ]) as $indoption ) {
2013-06-25 22:01:38 +00:00
$return [ $relname ][ " descs " ][] = ( $indoption & 1 ? '1' : null ); // 1 - INDOPTION_DESC
2013-06-25 16:42:47 +00:00
}
$return [ $relname ][ " lengths " ] = array ();
2010-04-21 12:01:32 +00:00
}
return $return ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function foreign_keys ( $table ) {
2011-08-02 15:34:21 +00:00
global $on_actions ;
2010-04-21 12:01:32 +00:00
$return = array ();
2014-07-10 10:34:08 +00:00
foreach ( get_rows ( " SELECT conname, condeferrable::int AS deferrable, pg_get_constraintdef(oid) AS definition
2011-08-01 08:47:45 +00:00
FROM pg_constraint
2011-12-22 05:59:24 +00:00
WHERE conrelid = ( SELECT pc . oid FROM pg_class AS pc INNER JOIN pg_namespace AS pn ON ( pn . oid = pc . relnamespace ) WHERE pc . relname = " . q( $table ) . " AND pn . nspname = current_schema ())
2011-08-01 08:47:45 +00:00
AND contype = 'f' :: char
ORDER BY conkey , conname " ) as $row ) {
if ( preg_match ( '~FOREIGN KEY\s*\((.+)\)\s*REFERENCES (.+)\((.+)\)(.*)$~iA' , $row [ 'definition' ], $match )) {
$row [ 'source' ] = array_map ( 'trim' , explode ( ',' , $match [ 1 ]));
2013-12-21 02:43:14 +00:00
if ( preg_match ( '~^(("([^"]|"")+"|[^"]+)\.)?"?("([^"]|"")+"|[^"]+)$~' , $match [ 2 ], $match2 )) {
$row [ 'ns' ] = str_replace ( '""' , '"' , preg_replace ( '~^"(.+)"$~' , '\1' , $match2 [ 2 ]));
$row [ 'table' ] = str_replace ( '""' , '"' , preg_replace ( '~^"(.+)"$~' , '\1' , $match2 [ 4 ]));
2011-08-02 15:36:12 +00:00
}
2011-08-01 08:47:45 +00:00
$row [ 'target' ] = array_map ( 'trim' , explode ( ',' , $match [ 3 ]));
2011-08-29 11:21:02 +00:00
$row [ 'on_delete' ] = ( preg_match ( " ~ON DELETE ( $on_actions )~ " , $match [ 4 ], $match2 ) ? $match2 [ 1 ] : 'NO ACTION' );
$row [ 'on_update' ] = ( preg_match ( " ~ON UPDATE ( $on_actions )~ " , $match [ 4 ], $match2 ) ? $match2 [ 1 ] : 'NO ACTION' );
2011-08-01 08:47:45 +00:00
$return [ $row [ 'conname' ]] = $row ;
2010-04-21 12:01:32 +00:00
}
}
return $return ;
}
2013-07-24 23:26:41 +00:00
2019-07-19 11:17:10 +00:00
function constraints ( $table ) {
global $on_actions ;
$return = array ();
foreach ( get_rows ( " SELECT conname, consrc
FROM pg_catalog . pg_constraint
INNER JOIN pg_catalog . pg_namespace ON pg_constraint . connamespace = pg_namespace . oid
INNER JOIN pg_catalog . pg_class ON pg_constraint . conrelid = pg_class . oid AND pg_constraint . connamespace = pg_class . relnamespace
WHERE pg_constraint . contype = 'c'
AND conrelid != 0 -- handle only CONSTRAINTs here , not TYPES
AND nspname = current_schema ()
AND relname = " . q( $table ) . "
ORDER BY connamespace , conname " ) as $row ) {
$return [ $row [ 'conname' ]] = $row [ 'consrc' ];
}
return $return ;
}
2010-04-21 12:01:32 +00:00
function view ( $name ) {
global $connection ;
2020-05-31 21:40:51 +00:00
return array ( " select " => trim ( $connection -> result ( " SELECT pg_get_viewdef( " . $connection -> result ( " SELECT oid FROM pg_class WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = current_schema()) AND relname = " . q ( $name )) . " ) " )));
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function collations () {
//! supported in CREATE DATABASE
return array ();
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function information_schema ( $db ) {
return ( $db == " information_schema " );
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function error () {
global $connection ;
$return = h ( $connection -> error );
2018-02-20 15:27:40 +00:00
if ( preg_match ( '~^(.*\n)?([^\n]*)\n( *)\^(\n.*)?$~s' , $return , $match )) {
$return = $match [ 1 ] . preg_replace ( '~((?:[^&]|&[^;]*;){' . strlen ( $match [ 3 ]) . '})(.*)~' , '\1<b>\2</b>' , $match [ 2 ]) . $match [ 4 ];
2010-04-21 12:01:32 +00:00
}
return nl_br ( $return );
}
2013-07-24 23:26:41 +00:00
2010-04-22 23:02:28 +00:00
function create_database ( $db , $collation ) {
return queries ( " CREATE DATABASE " . idf_escape ( $db ) . ( $collation ? " ENCODING " . idf_escape ( $collation ) : " " ));
}
2013-07-24 23:26:41 +00:00
2010-04-22 23:02:28 +00:00
function drop_databases ( $databases ) {
global $connection ;
$connection -> close ();
2010-05-17 16:18:32 +00:00
return apply_queries ( " DROP DATABASE " , $databases , 'idf_escape' );
2010-04-22 23:02:28 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function rename_database ( $name , $collation ) {
//! current database cannot be renamed
return queries ( " ALTER DATABASE " . idf_escape ( DB ) . " RENAME TO " . idf_escape ( $name ));
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function auto_increment () {
2021-02-06 17:01:24 +00:00
return ( min_version ( 11 ) ? " PRIMARY KEY " : " " );
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function alter_table ( $table , $name , $fields , $foreign , $comment , $engine , $collation , $auto_increment , $partitioning ) {
$alter = array ();
$queries = array ();
2019-07-15 10:21:17 +00:00
if ( $table != " " && $table != $name ) {
$queries [] = " ALTER TABLE " . table ( $table ) . " RENAME TO " . table ( $name );
}
2010-04-21 12:01:32 +00:00
foreach ( $fields as $field ) {
$column = idf_escape ( $field [ 0 ]);
$val = $field [ 1 ];
if ( ! $val ) {
$alter [] = " DROP $column " ;
} else {
$val5 = $val [ 5 ];
unset ( $val [ 5 ]);
2010-05-05 20:42:25 +00:00
if ( isset ( $val [ 6 ]) && $field [ 0 ] == " " ) { // auto_increment
2021-02-06 16:17:36 +00:00
$val [ 1 ] = ( $val [ 1 ] == " bigint " ? " big " : ( $val [ 1 ] == " smallint " ? " small " : " " )) . " serial " ;
2010-04-21 12:01:32 +00:00
}
if ( $field [ 0 ] == " " ) {
2010-05-07 16:10:35 +00:00
$alter [] = ( $table != " " ? " ADD " : " " ) . implode ( $val );
2010-04-21 12:01:32 +00:00
} else {
if ( $column != $val [ 0 ]) {
2019-07-15 10:21:17 +00:00
$queries [] = " ALTER TABLE " . table ( $name ) . " RENAME $column TO $val[0] " ;
2010-04-21 12:01:32 +00:00
}
2010-05-05 20:42:25 +00:00
$alter [] = " ALTER $column TYPE $val[1] " ;
2010-04-21 12:01:32 +00:00
if ( ! $val [ 6 ]) {
2013-07-02 16:20:06 +00:00
$alter [] = " ALTER $column " . ( $val [ 3 ] ? " SET $val[3] " : " DROP DEFAULT " );
2010-04-21 12:01:32 +00:00
$alter [] = " ALTER $column " . ( $val [ 2 ] == " NULL " ? " DROP NOT " : " SET " ) . $val [ 2 ];
}
}
2010-05-05 20:42:25 +00:00
if ( $field [ 0 ] != " " || $val5 != " " ) {
2019-07-15 10:21:17 +00:00
$queries [] = " COMMENT ON COLUMN " . table ( $name ) . " . $val[0] IS " . ( $val5 != " " ? substr ( $val5 , 9 ) : " '' " );
2010-04-21 12:01:32 +00:00
}
}
}
$alter = array_merge ( $alter , $foreign );
if ( $table == " " ) {
2010-05-11 14:45:04 +00:00
array_unshift ( $queries , " CREATE TABLE " . table ( $name ) . " ( \n " . implode ( " , \n " , $alter ) . " \n ) " );
2010-04-21 12:01:32 +00:00
} elseif ( $alter ) {
2010-05-11 14:45:04 +00:00
array_unshift ( $queries , " ALTER TABLE " . table ( $table ) . " \n " . implode ( " , \n " , $alter ));
2010-04-21 12:01:32 +00:00
}
if ( $table != " " || $comment != " " ) {
2010-10-13 16:04:40 +00:00
$queries [] = " COMMENT ON TABLE " . table ( $name ) . " IS " . q ( $comment );
2010-04-21 12:01:32 +00:00
}
if ( $auto_increment != " " ) {
2010-10-13 16:04:40 +00:00
//! $queries[] = "SELECT setval(pg_get_serial_sequence(" . q($name) . ", ), $auto_increment)";
2010-04-21 12:01:32 +00:00
}
foreach ( $queries as $query ) {
if ( ! queries ( $query )) {
return false ;
}
}
return true ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function alter_indexes ( $table , $alter ) {
$create = array ();
$drop = array ();
2013-06-25 00:37:59 +00:00
$queries = array ();
2010-04-21 12:01:32 +00:00
foreach ( $alter as $val ) {
if ( $val [ 0 ] != " INDEX " ) {
2013-06-25 16:42:47 +00:00
//! descending UNIQUE indexes results in syntax error
2011-07-13 13:13:00 +00:00
$create [] = ( $val [ 2 ] == " DROP "
? " \n DROP CONSTRAINT " . idf_escape ( $val [ 1 ])
2014-01-12 03:22:44 +00:00
: " \n ADD " . ( $val [ 1 ] != " " ? " CONSTRAINT " . idf_escape ( $val [ 1 ]) : " " ) . " $val[0] " . ( $val [ 0 ] == " PRIMARY " ? " KEY " : " " ) . " ( " . implode ( " , " , $val [ 2 ]) . " ) "
2011-07-13 13:13:00 +00:00
);
} elseif ( $val [ 2 ] == " DROP " ) {
$drop [] = idf_escape ( $val [ 1 ]);
2013-06-25 00:37:59 +00:00
} else {
2014-01-12 03:22:44 +00:00
$queries [] = " CREATE INDEX " . idf_escape ( $val [ 1 ] != " " ? $val [ 1 ] : uniqid ( $table . " _ " )) . " ON " . table ( $table ) . " ( " . implode ( " , " , $val [ 2 ]) . " ) " ;
2013-06-25 00:37:59 +00:00
}
}
if ( $create ) {
array_unshift ( $queries , " ALTER TABLE " . table ( $table ) . implode ( " , " , $create ));
}
if ( $drop ) {
array_unshift ( $queries , " DROP INDEX " . implode ( " , " , $drop ));
}
foreach ( $queries as $query ) {
if ( ! queries ( $query )) {
2010-04-21 12:01:32 +00:00
return false ;
}
}
2013-06-25 00:37:59 +00:00
return true ;
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function truncate_tables ( $tables ) {
2010-05-11 14:45:04 +00:00
return queries ( " TRUNCATE " . implode ( " , " , array_map ( 'table' , $tables )));
2010-04-21 12:01:32 +00:00
return true ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function drop_views ( $views ) {
2014-05-10 01:57:35 +00:00
return drop_tables ( $views );
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function drop_tables ( $tables ) {
2014-05-10 01:57:35 +00:00
foreach ( $tables as $table ) {
2014-07-10 10:34:08 +00:00
$status = table_status ( $table );
2014-05-10 01:57:35 +00:00
if ( ! queries ( " DROP " . strtoupper ( $status [ " Engine " ]) . " " . table ( $table ))) {
return false ;
}
}
return true ;
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-05-05 20:11:24 +00:00
function move_tables ( $tables , $views , $target ) {
2014-05-10 01:57:35 +00:00
foreach ( array_merge ( $tables , $views ) as $table ) {
$status = table_status ( $table );
if ( ! queries ( " ALTER " . strtoupper ( $status [ " Engine " ]) . " " . table ( $table ) . " SET SCHEMA " . idf_escape ( $target ))) {
2010-05-05 20:11:24 +00:00
return false ;
}
}
return true ;
}
2013-07-24 23:26:41 +00:00
2014-07-10 10:34:08 +00:00
function trigger ( $name , $table = null ) {
2011-08-08 16:18:16 +00:00
if ( $name == " " ) {
return array ( " Statement " => " EXECUTE PROCEDURE () " );
}
2014-07-10 10:34:08 +00:00
if ( $table === null ) {
$table = $_GET [ 'trigger' ];
}
$rows = get_rows ( 'SELECT t.trigger_name AS "Trigger", t.action_timing AS "Timing", (SELECT STRING_AGG(event_manipulation, \' OR \') FROM information_schema.triggers WHERE event_object_table = t.event_object_table AND trigger_name = t.trigger_name ) AS "Events", t.event_manipulation AS "Event", \'FOR EACH \' || t.action_orientation AS "Type", t.action_statement AS "Statement" FROM information_schema.triggers t WHERE t.event_object_table = ' . q ( $table ) . ' AND t.trigger_name = ' . q ( $name ));
2010-10-13 15:53:59 +00:00
return reset ( $rows );
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function triggers ( $table ) {
$return = array ();
2010-10-13 16:04:40 +00:00
foreach ( get_rows ( " SELECT * FROM information_schema.triggers WHERE event_object_table = " . q ( $table )) as $row ) {
2014-07-10 10:34:08 +00:00
$return [ $row [ " trigger_name " ]] = array ( $row [ " action_timing " ], $row [ " event_manipulation " ]);
2010-04-21 12:01:32 +00:00
}
return $return ;
}
2013-07-24 23:26:41 +00:00
2010-04-22 15:53:42 +00:00
function trigger_options () {
return array (
" Timing " => array ( " BEFORE " , " AFTER " ),
2014-03-15 17:58:24 +00:00
" Event " => array ( " INSERT " , " UPDATE " , " DELETE " ),
2010-04-22 19:43:36 +00:00
" Type " => array ( " FOR EACH ROW " , " FOR EACH STATEMENT " ),
2010-04-22 15:53:42 +00:00
);
}
2013-07-24 23:26:41 +00:00
2011-06-03 13:52:55 +00:00
function routine ( $name , $type ) {
2018-01-30 14:18:26 +00:00
$rows = get_rows ( ' SELECT routine_definition AS definition , LOWER ( external_language ) AS language , *
FROM information_schema . routines
WHERE routine_schema = current_schema () AND specific_name = ' . q ( $name ));
$return = $rows [ 0 ];
$return [ " returns " ] = array ( " type " => $return [ " type_udt_name " ]);
$return [ " fields " ] = get_rows ( ' SELECT parameter_name AS field , data_type AS type , character_maximum_length AS length , parameter_mode AS inout
FROM information_schema . parameters
WHERE specific_schema = current_schema () AND specific_name = ' . q($name) . '
ORDER BY ordinal_position ' );
return $return ;
}
2013-07-24 23:26:41 +00:00
2011-06-03 13:52:55 +00:00
function routines () {
2018-01-30 14:18:26 +00:00
return get_rows ( ' SELECT specific_name AS " SPECIFIC_NAME " , routine_type AS " ROUTINE_TYPE " , routine_name AS " ROUTINE_NAME " , type_udt_name AS " DTD_IDENTIFIER "
FROM information_schema . routines
WHERE routine_schema = current_schema ()
ORDER BY SPECIFIC_NAME ' );
2011-06-03 13:52:55 +00:00
}
2013-07-24 23:26:41 +00:00
2011-06-03 13:52:55 +00:00
function routine_languages () {
2018-01-30 14:18:26 +00:00
return get_vals ( " SELECT LOWER(lanname) FROM pg_catalog.pg_language " );
}
function routine_id ( $name , $row ) {
$return = array ();
foreach ( $row [ " fields " ] as $field ) {
$return [] = $field [ " type " ];
}
return idf_escape ( $name ) . " ( " . implode ( " , " , $return ) . " ) " ;
2011-06-03 13:52:55 +00:00
}
2013-07-24 23:26:41 +00:00
2010-05-12 16:07:46 +00:00
function last_id () {
return 0 ; // there can be several sequences
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function explain ( $connection , $query ) {
return $connection -> query ( " EXPLAIN $query " );
}
2013-07-24 23:26:41 +00:00
2011-07-29 15:08:06 +00:00
function found_rows ( $table_status , $where ) {
global $connection ;
2013-07-24 23:26:41 +00:00
if ( preg_match (
" ~ rows=([0-9]+)~ " ,
2011-07-29 15:08:06 +00:00
$connection -> result ( " EXPLAIN SELECT * FROM " . idf_escape ( $table_status [ " Name " ]) . ( $where ? " WHERE " . implode ( " AND " , $where ) : " " )),
$regs
)) {
return $regs [ 1 ];
}
return false ;
}
2013-07-24 23:26:41 +00:00
2010-05-21 14:07:22 +00:00
function types () {
return get_vals ( " SELECT typname
FROM pg_type
WHERE typnamespace = ( SELECT oid FROM pg_namespace WHERE nspname = current_schema ())
AND typtype IN ( 'b' , 'd' , 'e' )
AND typelem = 0 "
);
}
2013-07-24 23:26:41 +00:00
2010-05-05 16:30:55 +00:00
function schemas () {
2011-07-29 15:59:14 +00:00
return get_vals ( " SELECT nspname FROM pg_namespace ORDER BY nspname " );
2010-05-05 16:30:55 +00:00
}
2013-07-24 23:26:41 +00:00
2010-05-05 16:30:55 +00:00
function get_schema () {
global $connection ;
return $connection -> result ( " SELECT current_schema() " );
}
2013-07-24 23:26:41 +00:00
2019-11-11 11:10:46 +00:00
function set_schema ( $schema , $connection2 = null ) {
2010-05-21 13:07:59 +00:00
global $connection , $types , $structured_types ;
2019-11-11 11:10:46 +00:00
if ( ! $connection2 ) {
$connection2 = $connection ;
}
$return = $connection2 -> query ( " SET search_path TO " . idf_escape ( $schema ));
2010-05-21 14:07:22 +00:00
foreach ( types () as $type ) { //! get types from current_schemas('t')
2010-05-21 13:07:59 +00:00
if ( ! isset ( $types [ $type ])) {
$types [ $type ] = 0 ;
$structured_types [ lang ( 'User types' )][] = $type ;
}
}
2010-05-21 14:07:22 +00:00
return $return ;
2010-05-05 16:30:55 +00:00
}
2013-07-24 23:26:41 +00:00
2019-07-19 11:17:10 +00:00
// create_sql() produces CREATE TABLE without FK CONSTRAINTs
// foreign_keys_sql() produces all FK CONSTRAINTs as ALTER TABLE ... ADD CONSTRAINT
// so that all FKs can be added after all tables have been created, avoiding any need to reorder CREATE TABLE statements in order of their FK dependencies
function foreign_keys_sql ( $table ) {
$return = " " ;
$status = table_status ( $table );
$fkeys = foreign_keys ( $table );
ksort ( $fkeys );
foreach ( $fkeys as $fkey_name => $fkey ) {
$return .= " ALTER TABLE ONLY " . idf_escape ( $status [ 'nspname' ]) . " . " . idf_escape ( $status [ 'Name' ]) . " ADD CONSTRAINT " . idf_escape ( $fkey_name ) . " $fkey[definition] " . ( $fkey [ 'deferrable' ] ? 'DEFERRABLE' : 'NOT DEFERRABLE' ) . " ; \n " ;
}
return ( $return ? " $return\n " : $return );
}
2018-01-22 13:33:21 +00:00
function create_sql ( $table , $auto_increment , $style ) {
2014-07-10 10:34:08 +00:00
global $connection ;
$return = '' ;
$return_parts = array ();
$sequences = array ();
$status = table_status ( $table );
2020-01-30 16:13:50 +00:00
if ( is_view ( $status )) {
$view = view ( $table );
return rtrim ( " CREATE VIEW " . idf_escape ( $table ) . " AS $view[select] " , " ; " );
}
2014-07-10 10:34:08 +00:00
$fields = fields ( $table );
$indexes = indexes ( $table );
ksort ( $indexes );
2019-07-19 11:17:10 +00:00
$constraints = constraints ( $table );
2014-07-10 10:34:08 +00:00
if ( ! $status || empty ( $fields )) {
return false ;
}
$return = " CREATE TABLE " . idf_escape ( $status [ 'nspname' ]) . " . " . idf_escape ( $status [ 'Name' ]) . " ( \n " ;
// fields' definitions
foreach ( $fields as $field_name => $field ) {
$part = idf_escape ( $field [ 'field' ]) . ' ' . $field [ 'full_type' ]
2018-02-09 14:29:54 +00:00
. default_value ( $field )
2017-04-06 15:39:25 +00:00
. ( $field [ 'attnotnull' ] ? " NOT NULL " : " " );
2014-07-10 10:34:08 +00:00
$return_parts [] = $part ;
// sequences for fields
if ( preg_match ( '~nextval\(\'([^\']+)\'\)~' , $field [ 'default' ], $matches )) {
$sequence_name = $matches [ 1 ];
2018-02-09 13:43:19 +00:00
$sq = reset ( get_rows ( min_version ( 10 )
? " SELECT *, cache_size AS cache_value FROM pg_sequences WHERE schemaname = current_schema() AND sequencename = " . q ( $sequence_name )
: " SELECT * FROM $sequence_name "
));
2018-02-09 21:11:27 +00:00
$sequences [] = ( $style == " DROP+CREATE " ? " DROP SEQUENCE IF EXISTS $sequence_name ; \n " : " " )
2018-01-22 13:33:21 +00:00
. " CREATE SEQUENCE $sequence_name INCREMENT $sq[increment_by] MINVALUE $sq[min_value] MAXVALUE $sq[max_value] START " . ( $auto_increment ? $sq [ 'last_value' ] : 1 ) . " CACHE $sq[cache_value] ; " ;
2014-07-10 10:34:08 +00:00
}
}
// adding sequences before table definition
if ( ! empty ( $sequences )) {
$return = implode ( " \n \n " , $sequences ) . " \n \n $return " ;
}
// primary + unique keys
foreach ( $indexes as $index_name => $index ) {
switch ( $index [ 'type' ]) {
case 'UNIQUE' : $return_parts [] = " CONSTRAINT " . idf_escape ( $index_name ) . " UNIQUE ( " . implode ( ', ' , array_map ( 'idf_escape' , $index [ 'columns' ])) . " ) " ; break ;
case 'PRIMARY' : $return_parts [] = " CONSTRAINT " . idf_escape ( $index_name ) . " PRIMARY KEY ( " . implode ( ', ' , array_map ( 'idf_escape' , $index [ 'columns' ])) . " ) " ; break ;
}
}
2019-07-19 11:17:10 +00:00
foreach ( $constraints as $conname => $consrc ) {
$return_parts [] = " CONSTRAINT " . idf_escape ( $conname ) . " CHECK $consrc " ;
2014-07-10 10:34:08 +00:00
}
2018-02-01 15:45:31 +00:00
$return .= implode ( " , \n " , $return_parts ) . " \n ) WITH (oids = " . ( $status [ 'Oid' ] ? 'true' : 'false' ) . " ); " ;
2014-07-10 10:34:08 +00:00
// "basic" indexes after table definition
foreach ( $indexes as $index_name => $index ) {
if ( $index [ 'type' ] == 'INDEX' ) {
2018-10-27 16:14:00 +00:00
$columns = array ();
foreach ( $index [ 'columns' ] as $key => $val ) {
$columns [] = idf_escape ( $val ) . ( $index [ 'descs' ][ $key ] ? " DESC " : " " );
}
$return .= " \n \n CREATE INDEX " . idf_escape ( $index_name ) . " ON " . idf_escape ( $status [ 'nspname' ]) . " . " . idf_escape ( $status [ 'Name' ]) . " USING btree ( " . implode ( ', ' , $columns ) . " ); " ;
2014-07-10 10:34:08 +00:00
}
}
// coments for table & fields
if ( $status [ 'Comment' ]) {
$return .= " \n \n COMMENT ON TABLE " . idf_escape ( $status [ 'nspname' ]) . " . " . idf_escape ( $status [ 'Name' ]) . " IS " . q ( $status [ 'Comment' ]) . " ; " ;
}
foreach ( $fields as $field_name => $field ) {
if ( $field [ 'comment' ]) {
$return .= " \n \n COMMENT ON COLUMN " . idf_escape ( $status [ 'nspname' ]) . " . " . idf_escape ( $status [ 'Name' ]) . " . " . idf_escape ( $field_name ) . " IS " . q ( $field [ 'comment' ]) . " ; " ;
}
}
return rtrim ( $return , ';' );
}
2018-02-06 15:24:12 +00:00
function truncate_sql ( $table ) {
return " TRUNCATE " . table ( $table );
}
2018-01-22 13:14:42 +00:00
function trigger_sql ( $table ) {
2018-01-22 13:25:27 +00:00
$status = table_status ( $table );
2014-07-10 10:34:08 +00:00
$return = " " ;
2018-01-22 13:25:27 +00:00
foreach ( triggers ( $table ) as $trg_id => $trg ) {
$trigger = trigger ( $trg_id , $status [ 'Name' ]);
$return .= " \n CREATE TRIGGER " . idf_escape ( $trigger [ 'Trigger' ]) . " $trigger[Timing] $trigger[Events] ON " . idf_escape ( $status [ " nspname " ]) . " . " . idf_escape ( $status [ 'Name' ]) . " $trigger[Type] $trigger[Statement] ;; \n " ;
}
return $return ;
2014-07-10 10:34:08 +00:00
}
2010-04-21 22:37:16 +00:00
function use_sql ( $database ) {
return " \ connect " . idf_escape ( $database );
}
2013-07-24 23:26:41 +00:00
2010-05-05 16:31:39 +00:00
function show_variables () {
return get_key_vals ( " SHOW ALL " );
}
2011-05-11 09:48:51 +00:00
function process_list () {
2018-01-29 18:49:52 +00:00
return get_rows ( " SELECT * FROM pg_stat_activity ORDER BY " . ( min_version ( 9.2 ) ? " pid " : " procpid " ));
2011-05-11 09:48:51 +00:00
}
2013-07-24 23:26:41 +00:00
2010-08-03 15:15:45 +00:00
function show_status () {
}
2013-07-24 23:26:41 +00:00
2012-09-09 05:56:34 +00:00
function convert_field ( $field ) {
}
2013-07-24 23:26:41 +00:00
2012-09-09 05:56:34 +00:00
function unconvert_field ( $field , $return ) {
return $return ;
}
2013-07-24 23:26:41 +00:00
2010-04-21 12:01:32 +00:00
function support ( $feature ) {
2018-10-27 18:05:04 +00:00
return preg_match ( '~^(database|table|columns|sql|indexes|descidx|comment|view|' . ( min_version ( 9.3 ) ? 'materializedview|' : '' ) . 'scheme|routine|processlist|sequence|trigger|type|variables|drop_col|kill|dump)$~' , $feature );
2010-04-21 12:01:32 +00:00
}
2013-07-24 23:26:41 +00:00
2015-03-18 09:34:35 +00:00
function kill_process ( $val ) {
2014-07-10 10:34:08 +00:00
return queries ( " SELECT pg_terminate_backend( " . number ( $val ) . " ) " );
2015-03-18 09:34:35 +00:00
}
2017-01-16 16:03:02 +00:00
function connection_id (){
return " SELECT pg_backend_pid() " ;
}
2015-03-18 09:34:35 +00:00
function max_connections () {
global $connection ;
return $connection -> result ( " SHOW max_connections " );
}
2010-05-07 13:44:22 +00:00
$jush = " pgsql " ;
2010-04-21 12:01:32 +00:00
$types = array ();
$structured_types = array ();
foreach ( array ( //! arrays
lang ( 'Numbers' ) => array ( " smallint " => 5 , " integer " => 10 , " bigint " => 19 , " boolean " => 1 , " numeric " => 0 , " real " => 7 , " double precision " => 16 , " money " => 20 ),
2012-08-30 04:13:30 +00:00
lang ( 'Date and time' ) => array ( " date " => 13 , " time " => 17 , " timestamp " => 20 , " timestamptz " => 21 , " interval " => 0 ),
2010-04-21 12:01:32 +00:00
lang ( 'Strings' ) => array ( " character " => 0 , " character varying " => 0 , " text " => 0 , " tsquery " => 0 , " tsvector " => 0 , " uuid " => 0 , " xml " => 0 ),
lang ( 'Binary' ) => array ( " bit " => 0 , " bit varying " => 0 , " bytea " => 0 ),
lang ( 'Network' ) => array ( " cidr " => 43 , " inet " => 43 , " macaddr " => 17 , " txid_snapshot " => 0 ),
lang ( 'Geometry' ) => array ( " box " => 0 , " circle " => 0 , " line " => 0 , " lseg " => 0 , " path " => 0 , " point " => 0 , " polygon " => 0 ),
2010-05-21 13:07:59 +00:00
) as $key => $val ) { //! can be retrieved from pg_type
2010-04-21 12:01:32 +00:00
$types += $val ;
$structured_types [ $key ] = array_keys ( $val );
}
$unsigned = array ();
2018-06-27 07:20:06 +00:00
$operators = array ( " = " , " < " , " > " , " <= " , " >= " , " != " , " ~ " , " !~ " , " LIKE " , " LIKE %% " , " ILIKE " , " ILIKE %% " , " IN " , " IS NULL " , " NOT LIKE " , " NOT IN " , " IS NOT NULL " ); // no "SQL" to avoid CSRF
2010-04-21 12:01:32 +00:00
$functions = array ( " char_length " , " lower " , " round " , " to_hex " , " to_timestamp " , " upper " );
$grouping = array ( " avg " , " count " , " count distinct " , " max " , " min " , " sum " );
$edit_functions = array (
array (
" char " => " md5 " ,
" date|time " => " now " ,
), array (
2018-02-06 11:17:01 +00:00
number_type () => " +/- " ,
2010-04-21 12:01:32 +00:00
" date|time " => " + interval/- interval " , //! escape
" char|text " => " || " ,
)
);
}