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

version» Context lines:

pike.git/lib/modules/Sql.pmod/pgsql.pike:59:   int _closesent;   int _fetchlimit=FETCHLIMIT;   private int unnamedportalinuse;   private int portalsinflight;      object _c;   private string SSauthdata,cancelsecret;   private int backendpid;   private int backendstatus;   private mapping(string:mixed) options; - private string lastmessage=""; + private array(string) lastmessage=({});   private int clearmessage;   private int earlyclose;   private mapping(string:array(mixed)) notifylist=([]);   private mapping(string:string) runtimeparameter;   state _mstate;   private enum querystate {queryidle,inquery,cancelpending,canceled};   private querystate qstate;   private mapping(string:mapping(string:mixed)) prepareds=([]);   private mapping(string:mixed) tprepared;   private int pstmtcount;
pike.git/lib/modules/Sql.pmod/pgsql.pike:99:   private int port;   private object createprefix    =Regexp("^[ \t\f\r\n]*[Cc][Rr][Ee][Aa][Tt][Ee][ \t\f\r\n]");   private object dontcacheprefix    =Regexp("^[ \t\f\r\n]*([Ff][Ee][Tt][Cc][Hh]|[Cc][Oo][Pp][Yy])[ \t\f\r\n]");   private object limitpostfix    =Regexp("[ \t\f\r\n][Ll][Ii][Mm][Ii][Tt][ \t\f\r\n]+[12][; \t\f\r\n]*$");   Thread.Mutex _querymutex;   Thread.Mutex _stealmutex;    - #define USERERROR(msg) throw(({msg, backtrace()[..<1]})) + #define USERERROR(msg) throw(({(msg), backtrace()[..<1]}))      protected string _sprintf(int type, void|mapping flags) {    string res=UNDEFINED;    switch(type) {    case 'O':    res=sprintf(DRIVERNAME"(%s@%s:%d/%s,%d)",    user,host,port,database,backendpid);    break;    }    return res;
pike.git/lib/modules/Sql.pmod/pgsql.pike:216:   //! 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.   //! + //! The string returned is not newline-terminated. + //!   //! To clear the error, pass 1 as argument.   //!   //! @seealso   //! @[big_query]   string error(void|int clear) { -  string s=lastmessage; +  string s=lastmessage*"\n";    if(clear) -  lastmessage=""; +  lastmessage=({});    warningscollected=0;    return sizeof(s) && s;   }      //! This function returns a string describing what host are we talking to,   //! and how (TCP/IP or UNIX sockets).   //!   //! @seealso   //! @[server_info]   string host_info() {
pike.git/lib/modules/Sql.pmod/pgsql.pike:487:    _fetchlimit=newfetchlimit;    return oldfetchlimit;   }      final private string glob2reg(string glob) {    if (!glob||!sizeof(glob))    return "%";    return replace(glob,({"*","?","\\","%","_"}),({"%","_","\\\\","\\%","\\_"}));   }    - final private string addnlifpresent(void|string msg) { -  return msg?msg+"\n":""; + final private string a2nls(array(string) msg) { +  return msg*"\n"+"\n";   }      final private string pinpointerror(void|string query,void|string offset) {    if(!query)    return "";    int k=(int)offset;    if(k<=0)    return MARKSTART+query+MARKEND;    return MARKSTART+(k>1?query[..k-2]:"")+MARKERROR+query[k-1..]+MARKEND;   }
pike.git/lib/modules/Sql.pmod/pgsql.pike:833:    case 'c':PD("CopyDone\n");    msglen-=4;    break;    case 'E':PD("ErrorResponse\n");    { mapping(string:string) msgresponse;    msgresponse=getresponse();    warningsdropcount+=warningscollected;    warningscollected=0;    switch(msgresponse->C) {    case "P0001": -  lastmessage=sprintf("%s: %s\n",msgresponse->S,msgresponse->M); -  USERERROR(sprintf("%s%s\n", -  lastmessage,pinpointerror(_c.portal->query,msgresponse->P))); +  lastmessage=({sprintf("%s: %s",msgresponse->S,msgresponse->M)}); +  USERERROR(a2nls(lastmessage +  +({pinpointerror(_c.portal->query,msgresponse->P)})));    break;    case "08P01":case "42P05":    errtype=protocolerror;    case "XX000":case "42883":case "42P01":    invalidatecache=1;    default: -  lastmessage=sprintf("%s %s:%s %s\n (%s:%s:%s)\n%s%s%s%s\n%s", +  lastmessage=({sprintf("%s %s:%s %s\n (%s:%s:%s)",    msgresponse->S,msgresponse->C,msgresponse->P||"",msgresponse->M, -  msgresponse->F||"",msgresponse->R||"",msgresponse->L||"", -  addnlifpresent(msgresponse->D),addnlifpresent(msgresponse->H), -  pinpointerror(_c.portal&&_c.portal->query,msgresponse->P), -  pinpointerror(msgresponse->q,msgresponse->p), -  addnlifpresent(msgresponse->W)); +  msgresponse->F||"",msgresponse->R||"",msgresponse->L||"")}); +  if(msgresponse->D) +  lastmessage+=({msgresponse->D}); +  if(msgresponse->H) +  lastmessage+=({msgresponse->H}); +  lastmessage+=({ +  pinpointerror(_c.portal&&_c.portal->query,msgresponse->P)+ +  pinpointerror(msgresponse->q,msgresponse->p)}); +  if(msgresponse->W) +  lastmessage+=({msgresponse->W});    switch(msgresponse->S) { -  case "PANIC":werror(lastmessage); +  case "PANIC":werror(a2nls(lastmessage));    } -  USERERROR(lastmessage); +  USERERROR(a2nls(lastmessage));    }    break;    }    case 'N':PD("NoticeResponse\n");    { mapping(string:string) msgresponse;    msgresponse=getresponse();    if(clearmessage) {    warningsdropcount+=warningscollected;    clearmessage=warningscollected=0; -  lastmessage=""; +  lastmessage=({});    }    warningscollected++; -  lastmessage=sprintf("%s%s %s: %s\n", -  lastmessage,msgresponse->S,msgresponse->C,msgresponse->M); +  lastmessage=({sprintf("%s %s: %s", +  msgresponse->S,msgresponse->C,msgresponse->M)});    break;    }    case 'A':PD("NotificationResponse\n");    { msglen-=4+4;    int pid=_c.getint32();    string condition,extrainfo=UNDEFINED;    { array(string) ts=getstrings();    switch(sizeof(ts)) {    case 0:errtype=protocolerror;    break;
pike.git/lib/modules/Sql.pmod/pgsql.pike:893:    runcallback(pid,condition,extrainfo);    break;    }    default:    if(msgtype!=-1) {    PD("Unknown message received %c\n",msgtype);    msglen-=4;PD("%O\n",_c.getstring(msglen));msglen=0;    errtype=protocolunsupported;    }    else { -  string msg=lastmessage; +  array(string) msg=lastmessage;    if(!reconnect(1)) {    sleep(RECONNECTDELAY);    if(!reconnect(1)) {    sleep(RECONNECTBACKOFF);    reconnect(1);    }    } -  ERROR("%s%sConnection lost to database %s@%s:%d/%s %d\n", -  msg,lastmessage,user,host,port,database,backendpid); +  msg+=lastmessage; +  string s=sizeof(msg)?a2nls(msg):""; +  ERROR("%sConnection lost to database %s@%s:%d/%s %d\n", +  s,user,host,port,database,backendpid);    }    break;    }    if(msglen)    errtype=protocolerror;    switch(errtype) {    case protocolunsupported:    ERROR("Unsupported servermessage received %c\n",msgtype);    break;    case protocolerror: -  string msg=lastmessage; -  lastmessage=""; +  array(string) msg=lastmessage; +  lastmessage=({});    reconnect(1); -  ERROR("%s%sProtocol error with database %s\n", -  msg,lastmessage,host_info()); +  msg+=lastmessage; +  string s=sizeof(msg)?a2nls(msg):""; +  ERROR("%sProtocol error with database %s\n",s,host_info());    break;    case noerror:    break;    }    if(zero_type(waitforstate))    break;    }    PD("Found state %O\n",_mstate);    return _mstate;   }
pike.git/lib/modules/Sql.pmod/pgsql.pike:971:   #endif    if(!force)    _c.sendterminate();    _c.close(); _c=0;    foreach(prepareds;;mapping tp)    m_delete(tp,"preparedname");    if(!(connectmtxkey = _stealmutex.trylock(2)))    return 0; // Recursive reconnect, bailing out    }    if(!(_c=getsocket())) { -  string msg=sprintf("Couldn't connect to database on %s:%d\n",host,port); +  string msg=sprintf("Couldn't connect to database on %s:%d",host,port);    if(force) { -  lastmessage+=msg; +  lastmessage+=({msg});    return 0;    }    else -  ERROR(msg); +  ERROR(msg+"\n");    }    _closesent=0;    _mstate=unauthenticated;    qstate=queryidle;    runtimeparameter=([]);    array(string) plugbuf=({"",_c.plugint32(PG_PROTOCOL(3,0))});    if(user)    plugbuf+=({"user\0",user,"\0"});    if(database)    plugbuf+=({"database\0",database,"\0"});
pike.git/lib/modules/Sql.pmod/pgsql.pike:1007:    PD("%O\n",plugbuf);    { mixed err=catch(_decodemsg(readyforquery));    if(err)    if(force)    throw(err);    else    return 0;    }    PD("%O\n",runtimeparameter);    if(force) { -  lastmessage=sprintf("%sReconnected to database %s\n", -  lastmessage,host_info()); +  lastmessage+=({sprintf("Reconnected to database %s",host_info())});    runcallback(backendpid,"_reconnect","");    }    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.   //!
pike.git/lib/modules/Sql.pmod/pgsql.pike:1046:    m_delete(tp,"datatypeoid");    m_delete(tp,"datarowdesc");    }    }    }    earlyclose=0;    }) {    earlyclose=0;    PD("%O\n",err);    if(!reconnect(1)) -  ERROR(lastmessage); +  ERROR(a2nls(lastmessage));    }    else if(didsync && special==2)    _decodemsg(readyforquery);   #ifndef UNBUFFEREDIO    _c.set_read_callback(read_cb);   #endif   }      //! This function allows you to connect to a database. Due to   //! restrictions of the Postgres frontend-backend protocol, you always