pike.git / lib / modules / Sql.pmod / pgsql.pike

version» Context lines:

pike.git/lib/modules/Sql.pmod/pgsql.pike:1:   /*    * This is the PostgreSQL direct network module for Pike.    */      //! This is an interface to the PostgreSQL database - //! server. This module is independent of external libraries. + //! server. This module is independent of any external libraries.   //! Note that you @b{do not@} need to have a   //! PostgreSQL server running on your host to use this module: you can   //! connect to the database over a TCP/IP socket.   //!   //! This module replaces the functionality of the older @[Sql.postgres]   //! and @[Postgres.postgres] modules.   //!   //! This module supports the following features:   //!   //! - PostgreSQL network protocol version 3, authentication methods   //! currently supported are: cleartext and MD5 (recommended).   //! - //! - Streaming queries which do not buffer the whole resulset in memory. + //! - Streaming queries which do not buffer the whole resultset in memory.   //!   //! - Automatic binary transfers to and from the database for most common   //! datatypes (amongst others: integer, text and bytea types).   //!   //! - SQL-injection protection by allowing just one statement per query   //! and ignoring anything after the first (unquoted) semicolon in the query.   //!   //! - COPY support for streaming up- and download.   //!   //! - Accurate error messages.
pike.git/lib/modules/Sql.pmod/pgsql.pike:31:   //! - Automatic precompilation of complex queries (session cache).   //!   //! - Multiple simultaneous queries on the same database connection.   //!   //! - Cancelling of long running queries by force or by timeout.   //!   //! - Event driven NOTIFY.   //!   //! - SSL encrypted connections (optional or forced).   //! - //! Refer to the PostgreSQL documentation for further details. + //! Check the PostgreSQL documentation for further details.   //!   //! @note   //! Multiple simultaneous queries on the same database connection is a   //! feature that none of the other database drivers for Pike support.   //! So, although it's efficient, its use will make switching database drivers   //! difficult.   //!   //! @seealso   //! @[Sql.Sql], @[Sql.postgres]   
pike.git/lib/modules/Sql.pmod/pgsql.pike:169:   //! to plain unencrypted   //! @value "force_ssl"   //! If the database supports and allows SSL connections, the session   //! will be SSL encrypted, if not, the connection will abort   //! @value "cache_autoprepared_statements"   //! If set to zero, it disables the automatic statement prepare and   //! cache logic; caching prepared statements can be problematic   //! when stored procedures and tables are redefined which leave stale   //! references in the already cached prepared statements   //! @value "client_encoding" - //! Character encoding for the client side, it defaults to use - //! database encoding, e.g.: "SQL_ASCII" + //! Character encoding for the client side, it defaults to using + //! the default encoding specified by the database, e.g.: "SQL_ASCII"   //! @value "standard_conforming_strings"   //! When on, backslashes in strings must not be escaped any longer, - //! @[quote] automatically adjusts quoting strategy accordingly + //! @[quote()] automatically adjusts quoting strategy accordingly   //! @value "escape_string_warning"   //! When on, a warning is issued if a backslash (\) appears in an   //! ordinary string literal and @[standard_conforming_strings] is off,   //! defaults to on   //! @endstring   //! For the numerous other options please check the PostgreSQL manual.   //!   //! @note   //! You need to have a database selected before using the sql-object,   //! otherwise you'll get exceptions when you try to query it. Also
pike.git/lib/modules/Sql.pmod/pgsql.pike:206:    options = _options || ([]);    if(has_value(host,":") && sscanf(_host,"%s:%d",host,port)!=2)    ERROR("Error in parsing the hostname argument\n");    if(!port)    port = PGSQL_DEFAULT_PORT;    _querymutex=Thread.Mutex();    _stealmutex=Thread.Mutex();    reconnect();   }    - //! @decl string error() - //! +    //! This function returns the textual description of the last   //! server-related error. Returns @expr{0@} if no error has occurred   //! yet. It is not cleared upon reading (can be invoked multiple   //! times, will return the same result until a new error occurs).   //!   //! During the execution of a statement, this function accumulates all   //! non-error messages (notices, warnings, etc.). If a statement does not   //! generate any errors, this function will return all collected messages   //! from the last statement.   //!   //! To clear the error, pass 1 as argument.   //!   //! @seealso - //! big_query + //! @[big_query]   string error(void|int clear) {    string s=lastmessage;    if(clear)    lastmessage="";    warningscollected=0;    return s;   }    - //! @decl string host_info() - //! +    //! This function returns a string describing what host are we talking to,   //! and how (TCP/IP or UNIX sockets).   //!   //! @seealso - //! server_info + //! @[server_info]   string host_info() {    return sprintf("fd:%d TCP/IP %s:%d PID %d",    _c?_c.query_fd():-1,host,port,backendpid);   }      final private object getsocket(void|int nossl) {    object lcon = Stdio.File();    if(!lcon.connect(host,port))    return UNDEFINED;   
pike.git/lib/modules/Sql.pmod/pgsql.pike:285:    fcon=.pgsql_util.PGconn(lcon,this);    return fcon;   }      //! Cancels the currently running query.   //!   //! This function is PostgreSQL-specific, and thus it is not available   //! through the generic SQL-interface.   //!   //! @seealso - //! reload + //! @[reload]   void cancelquery() {    if(qstate==inquery) {    qstate=cancelpending;    object lcon;    PD("CancelRequest\n");    if(!(lcon=getsocket(1)))    ERROR("Cancel connect failed\n");    lcon.write(({_c.plugint32(16),_c.plugint32(PG_PROTOCOL(1234,5678)),    _c.plugint32(backendpid),cancelsecret}));    lcon.close();
pike.git/lib/modules/Sql.pmod/pgsql.pike:1024:    }    return 1;   }      //! @decl void reload()   //!   //! Resets the connection to the database. Can be used for   //! a variety of reasons, for example to detect the status of a connection.   //!   //! @seealso - //! cancelquery + //! @[cancelquery]   void reload(void|int special) {    mixed err;    int didsync;    if(err = catch {    sendclose(1);    PD("Portalsinflight: %d\n",portalsinflight);    if(!portalsinflight) {    if(!earlyclose) {    PD("Sync\n");    _c.sendcmd(({"S",_c.plugint32(4)}),2);
pike.git/lib/modules/Sql.pmod/pgsql.pike:1059:    if(!reconnect(1))    ERROR(lastmessage);    }    else if(didsync && special==2)    _decodemsg(readyforquery);   #ifndef UNBUFFEREDIO    _c.set_read_callback(read_cb);   #endif   }    - //! @decl void select_db(string dbname) - //! +    //! This function allows you to connect to a database. Due to   //! restrictions of the Postgres frontend-backend protocol, you always   //! have to be connected to a database, so in fact this function just   //! allows you to connect to a different database on the same server.   //!   //! @note   //! This function @b{can@} raise exceptions if something goes wrong   //! (backend process not running, not enough permissions..)   //!   //! @seealso - //! create + //! @[create]   void select_db(string dbname) {    database=dbname;    reconnect();    reconnected=0;   }      //! With PostgreSQL you can LISTEN to NOTIFY events.   //! This function allows you to detect and handle such events.   //!   //! @param condition
pike.git/lib/modules/Sql.pmod/pgsql.pike:1140:    if((cb=notifylist[condition]||notifylist[""])    && (pid!=backendpid || sizeof(cb)>1 && cb[1]))    cb[0](pid,condition,extrainfo,@cb[2..]);   }      //! This function quotes magic characters inside strings embedded in a   //! textual query. Quoting must not be done for parameters passed in   //! bindings.   //!   //! @seealso - //! big_query, quotebinary, create + //! @[big_query], @[quotebinary], @[create]   string quote(string s) {    string r=runtimeparameter->standard_conforming_strings;    if(r && r=="on")    return replace(s, "'", "''");    return replace(s, ({ "'", "\\" }), ({ "''", "\\\\" }) );   }      //! This function quotes magic characters inside binaries (bytea) embedded in a   //! textual query. Quoting must not be done for parameters passed in   //! bindings.   //!   //! @seealso - //! big_query, quote + //! @[big_query], @[quote]   //!   //! This function is PostgreSQL-specific, and thus it is not available   //! through the generic SQL-interface.   string quotebinary(string s) {    return replace(s, ({ "'", "\\", "\0" }), ({ "''", "\\\\", "\\000" }) );   }      //! This function creates a new database with the given name (assuming we   //! have enough permissions to do this).   //!   //! @seealso - //! drop_db + //! @[drop_db]   void create_db(string db) {    big_query("CREATE DATABASE :db",([":db":db]));   }      //! This function destroys a database and all the data it contains (assuming   //! we have enough permissions to do so). It is not possible to delete   //! the database you're currently connected to. You can connect to database   //! @[template1] to avoid connecting to any live database.   //!   //! @seealso - //! create_db + //! @[create_db]   void drop_db(string db) {    big_query("DROP DATABASE :db",([":db":db]));   }      //! This function returns a string describing the server we are   //! talking to. It has the form @expr{"servername/serverversion"@}   //! (like the HTTP protocol description) and is most useful in   //! conjunction with the generic SQL-server module.   //!   //! @seealso - //! host_info + //! @[host_info]   string server_info () {    return DRIVERNAME"/"+(runtimeparameter->server_version||"unknown");   }      //! Lists all the databases available on the server.   //! If glob is specified, lists only those databases matching it.   array(string) list_dbs (void|string glob) {    array row,ret=({});    object res=big_query("SELECT d.datname "    "FROM pg_database d "
pike.git/lib/modules/Sql.pmod/pgsql.pike:1439:      final private void closestatement(array(string) plugbuf,mapping tp) {    string oldprep=tp->preparedname;    if(oldprep) {    PD("Close statement %s\n",oldprep);    plugbuf+=({"C",_c.plugint32(4+1+sizeof(oldprep)+1),    "S",oldprep,"\0"});    }   }    + //! @decl Sql.pgsql_util.pgsql_result big_query(string query) + //! @decl Sql.pgsql_util.pgsql_result big_query(string query, mapping bindings) + //!   //! This is the only provided interface which allows you to query the   //! database. If you wish to use the simpler "query" function, you need to   //! use the @[Sql.Sql] generic SQL-object.   //!   //! It returns a pgsql_result object (which conforms to the   //! @[Sql.sql_result] standard interface for accessing data). I   //! recommend using @[query()] for simpler queries (because it is   //! easier to handle, but stores all the result in memory), and   //! @[big_query()] for queries you expect to return huge amounts of   //! data (it's harder to handle, but fetches results on demand).