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

version» Context lines:

pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:12:   //! would be prudent not to block in these callbacks.      #pike __REAL_VERSION__   #require constant(Thread.Thread)      #include "pgsql.h"      //! The instance of the pgsql dedicated backend.   final Pike.Backend local_backend = Pike.SmallBackend();    - protected Thread.Mutex backendmux = Thread.Mutex(); - protected int clientsregistered; + private Thread.Mutex backendmux = Thread.Mutex(); + private int clientsregistered;      multiset cachealways=(<"BEGIN","begin","END","end","COMMIT","commit">);   Regexp createprefix    =Regexp("^[ \t\f\r\n]*[Cc][Rr][Ee][Aa][Tt][Ee][ \t\f\r\n]"); - protected Regexp dontcacheprefix + private Regexp dontcacheprefix    =Regexp("^[ \t\f\r\n]*([Ff][Ee][Tt][Cc][Hh]|[Cc][Oo][Pp][Yy])[ \t\f\r\n]"); - protected Regexp execfetchlimit + private Regexp execfetchlimit    =Regexp("^[ \t\f\r\n]*(([Uu][Pp][Dd][Aa]|[Dd][Ee][Ll][Ee])[Tt][Ee]|\   [Ii][Nn][Ss][Ee][Rr][Tt])[ \t\f\r\n]|\   [ \t\f\r\n][Ll][Ii][Mm][Ii][Tt][ \t\f\r\n]+[12][; \t\f\r\n]*$");      final void closestatement(PGplugbuffer|PGassist plugbuffer,string oldprep) {    if(oldprep) {    PD("Close statement %s\n",oldprep);    plugbuffer->add_int8('C')->add_hstring(({'S',oldprep,0}),4,4);    }   }    - protected void run_local_backend() { + private void run_local_backend() {    Thread.MutexKey lock;    int looponce;    do {    looponce=0;    if(lock=backendmux->trylock()) {    PD("Starting local backend\n");    while(clientsregistered) // Autoterminate when not needed    local_backend(4096.0);    PD("Terminating local backend\n");    lock=0;
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:91:    case MACADDROID:    case BPCHAROID:    case VARCHAROID:    case CTIDOID:    case UUIDOID:    return 1; //binary    }    return 0; // text   }    - protected sctype mergemode(PGassist realbuffer,sctype mode) { + private sctype mergemode(PGassist realbuffer,sctype mode) {    if(mode>realbuffer->stashflushmode)    realbuffer->stashflushmode=mode;    return realbuffer->stashflushmode;   }    - protected inline mixed callout(function(mixed ...:void) f, + private inline mixed callout(function(mixed ...:void) f,    float|int delay,mixed ... args) {    return local_backend->call_out(f,delay,@args);   }      // Some pgsql utility functions      class PGplugbuffer {    inherit Stdio.Buffer;    -  protected PGassist realbuffer; +  private PGassist realbuffer;       protected void create(PGassist _realbuffer) {    realbuffer=_realbuffer;    }       final PGplugbuffer start(void|int waitforreal) {    realbuffer->stashcount++;   #ifdef PG_DEBUG    if(waitforreal)    error("pgsql.PGplugbuffer not allowed here\n");   #endif    return this;    }    -  final -  void sendcmd(void|sctype mode,void|pgsql_result portal) { +  final void sendcmd(void|sctype mode,void|pgsql_result portal) {    Thread.MutexKey lock=realbuffer->stashupdate->lock();    if(portal)    realbuffer->stashqueue->write(portal);    realbuffer->stash->add(this);    mode=mergemode(realbuffer,mode);    if(!--realbuffer->stashcount)    realbuffer->stashavail.signal();    lock=0;    this->clear();    if(lock=realbuffer->nostash->trylock(1)) {
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:145:    realbuffer->sendcmd(sendout);    }    }      }      class PGassist {    inherit Stdio.Buffer:i;    inherit Stdio.Buffer:o;    -  protected Thread.Condition fillread; -  protected Thread.Mutex fillreadmux; -  protected Thread.Queue qportals; +  private Thread.Condition fillread; +  private Thread.Mutex fillreadmux; +  private Thread.Queue qportals;    final Stdio.File socket; -  protected object pgsqlsess; -  protected int towrite; +  private object pgsqlsess; +  private int towrite;       final function(:void) gottimeout;    final int timeout;    final Thread.Mutex nostash;    final Thread.MutexKey started;    final Thread.Mutex stashupdate;    final Thread.Queue stashqueue;    final Thread.Condition stashavail;    final Stdio.Buffer stash;    final int stashflushmode;    final int stashcount;    final int synctransact;   #ifdef PG_DEBUG    final int queueoutidx;    final int queueinidx;   #endif    -  inline void queueup(pgsql_result portal) { +  private inline void queueup(pgsql_result portal) {    qportals->write(portal); portal->_synctransact=synctransact;    PD(">%O %d %d Queue portal %d bytes\n",portal._portalname,++queueoutidx,    synctransact,sizeof(this));    }       final PGassist|PGplugbuffer start(void|int waitforreal) {    Thread.MutexKey lock;    if(lock=(waitforreal?nostash->lock:nostash->trylock)(1)) {    started=lock;    lock=stashupdate->lock();
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:209:    array cid=callout(gottimeout,timeout);    Thread.MutexKey lock=fillreadmux->lock();    fillread.wait(lock);    lock=0;    local_backend->remove_call_out(cid);    } else    throw(MAGICTERMINATE);    return true;    }    -  protected int read_cb(mixed id,mixed b) { +  private int read_cb(mixed id,mixed b) {    Thread.MutexKey lock=fillreadmux->lock();    if(fillread)    fillread.signal();    lock=0;    return 0;    }    -  protected int write_cb() { +  private int write_cb() {    towrite-=output_to(socket,towrite);    if(!fillread && !sizeof(this))    socket->close();    return 0;    }    -  inline final int consume(int w) { return i::consume(w); } -  inline final int unread(int w) { return i::unread(w); } -  inline final string read(int w) { return i::read(w); } -  inline final object read_buffer(int w) { return i::read_buffer(w); } -  inline final int read_sint(int w) { return i::read_sint(w); } -  inline final int read_int8() { return i::read_int8(); } -  inline final int read_int16() { return i::read_int16(); } -  inline final int read_int32() { return i::read_int32(); } -  inline final string read_cstring() { return i::read_cstring(); } +  final inline int consume(int w) { return i::consume(w); } +  final inline int unread(int w) { return i::unread(w); } +  final inline string read(int w) { return i::read(w); } +  final inline object read_buffer(int w) { return i::read_buffer(w); } +  final inline int read_sint(int w) { return i::read_sint(w); } +  final inline int read_int8() { return i::read_int8(); } +  final inline int read_int16() { return i::read_int16(); } +  final inline int read_int32() { return i::read_int32(); } +  final inline string read_cstring() { return i::read_cstring(); }    -  final -  void sendcmd(void|sctype mode,void|pgsql_result portal) { +  final void sendcmd(void|sctype mode,void|pgsql_result portal) {    if(portal)    queueup(portal);    if(mode==flushlogsend) {    mode=flushsend; qportals->write(synctransact++);    PD(">%O %d Queue simplequery %d bytes\n",portal._portalname,    ++queueoutidx,sizeof(this));    }    if(started) {    Thread.MutexKey lock=stashupdate->lock();    if(sizeof(stash)) {
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:288:    }       final void sendterminate() {    destruct(fillread); // Delayed close() after flushing the output buffer    }       final int close() {    return socket->close();    }    -  final void destroy() { +  protected void destroy() {    catch(close()); // Exceptions don't work inside destructors    pgsqlsess=0;    }       final void connectloop(int nossl) {    mixed err=catch {    for(;;clear()) {    socket->connect(pgsqlsess._host,pgsqlsess._port);   #if constant(SSL.File)    if(!nossl && !pgsqlsess->nossl
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:364:    }   }      //! The result object returned by @[Sql.pgsql()->big_query()], except for   //! the noted differences it behaves the same as @[Sql.sql_result].   //!   //! @seealso   //! @[Sql.sql_result], @[Sql.pgsql], @[Sql.Sql], @[Sql.pgsql()->big_query()]   class pgsql_result {    -  protected object pgsqlsess; -  protected int numrows; -  protected int eoffound; -  protected PGassist c; +  private object pgsqlsess; +  private int numrows; +  private int eoffound; +  private PGassist c;    final mixed _delayederror;    final portalstate _state;    final int _fetchlimit;    final int _alltext;    final int _forcetext;       final string _portalname;       final int _bytesreceived;    final int _rowsreceived;    final int _inflight;    final int _portalbuffersize;    final int _synctransact;    final Thread.Condition _ddescribe;    final Thread.Mutex _ddescribemux;    final Thread.MutexKey _unnamedportalkey,_unnamedstatementkey; -  protected Thread.Mutex closemux; +  private Thread.Mutex closemux;    final array _params;    final string _statuscmdcomplete;    final string _query;    final Thread.Queue _datarows;    final array(mapping(string:mixed)) _datarowdesc;    final int _oldpbpos; -  protected Stdio.Buffer prepbuffer; -  protected Thread.Condition prepbufferready; -  protected Thread.Mutex prepbuffermux; +  private Stdio.Buffer prepbuffer; +  private Thread.Condition prepbufferready; +  private Thread.Mutex prepbuffermux;    final string _preparedname;    final mapping(string:mixed) _tprepared;    -  protected string _sprintf(int type, void|mapping flags) { +  private string _sprintf(int type, void|mapping flags) {    string res=UNDEFINED;    switch(type) {    case 'O':    res=sprintf("pgsql_result numrows: %d eof: %d inflight: %d\n"   #ifdef PG_DEBUGMORE    "query: %O\n"   #endif    "portalname: %O datarows: %d"    " laststatus: %s\n",    numrows,eoffound,_inflight,
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:463:    //! @note    //! This function is PostgreSQL-specific, and thus it is not available    //! through the generic SQL-interface.    int affected_rows() {    int rows;    if(_statuscmdcomplete)    sscanf(_statuscmdcomplete,"%*s %d",rows);    return rows;    }    -  protected void waitfordescribe() { +  private void waitfordescribe() {    Thread.MutexKey lock=_ddescribemux->lock();    if(!_datarowdesc)    _ddescribe->wait(lock);    lock=0;    }       //! @seealso    //! @[Sql.sql_result()->num_fields()]    int num_fields() {    if(!_datarowdesc)
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:486:    return sizeof(_datarowdesc);    }       //! @seealso    //! @[Sql.sql_result()->num_rows()]    int num_rows() {    trydelayederror();    return _rowsreceived;    }    -  protected inline void trydelayederror() { +  private inline void trydelayederror() {    if(_delayederror)    throwdelayederror(this);    }       //! @seealso    //! @[Sql.sql_result()->eof()]    int eof() {    trydelayederror();    return eoffound;    }
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:666:       final void _processrowdesc(array(mapping(string:mixed)) datarowdesc) {    _setrowdesc(datarowdesc);    mapping(string:mixed) tp=_tprepared; // FIXME Is caching this worthwhile?    if(!tp || !tp.datarowdesc)    Thread.Thread(gotdatarowdesc); // Do not use callout, it deadlocks    if(tp)    tp.datarowdesc=datarowdesc;    }    -  protected void gotdatarowdesc() { +  private void gotdatarowdesc() {    if(!prepbuffer) {    Thread.MutexKey lock=prepbuffermux->lock();    prepbufferready->wait(lock);    lock=0;    }    Stdio.Buffer plugbuffer=prepbuffer;    prepbuffer=0;    plugbuffer->add_int16(sizeof(_datarowdesc));    foreach(_datarowdesc;;mapping col)    plugbuffer->add_int16(oidformat(col.type));
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:859:    //! @[fetch_row()], @[eof()]    void send_row(void|string|array(string) copydata) {    trydelayederror();    if(copydata) {    PD("CopyData\n");    c->start()->add_int8('d')->add_hstring(copydata,4,4)->sendcmd(sendout);    } else    _releasesession();    }    -  protected void run_result_cb( +  private void run_result_cb(    function(pgsql_result, array(mixed), mixed ...:void) callback,    array(mixed) args) {    int|array datarow;    while(arrayp(datarow=_datarows->read_array()))    callout(callback, 0, this, datarow, @args);    trydelayederror();    eoffound=1;    callout(callback, 0, this, 0, @args);    }   
pike.git/lib/modules/Sql.pmod/pgsql_util.pmod:883:    //!    //! @seealso    //! @[fetch_row()]    void set_result_callback(    function(pgsql_result, array(mixed), mixed ...:void) callback,    mixed ... args) {    if(callback)    Thread.Thread(run_result_cb,callback,args);    }    -  protected void run_result_array_cb( +  private void run_result_array_cb(    function(pgsql_result, array(array(mixed)), mixed ...:void) callback,    array(mixed) args) {    array(array|int) datarow;    while((datarow=_datarows->read_array()) && arrayp(datarow[-1]))    callout(callback, 0, this, datarow, @args);    trydelayederror();    eoffound=1;    if(sizeof(datarow)>1)    callout(callback, 0, this, datarow=datarow[..<1], @args);    callout(callback, 0, this, 0, @args);