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

version» Context lines:

pike.git/lib/modules/Sql.pmod/rsql.pike:1:   // Remote SQL server interface      #pike __REAL_VERSION__      #define RSQL_PORT 3994   #define RSQL_VERSION 1    -  +    #if constant(thread_create)   #define LOCK object key=mutex->lock()   #define UNLOCK destruct(key)   static private object(Thread.Mutex) mutex = Thread.Mutex();   #else   #define LOCK   #define UNLOCK   #endif    -  + #define ERROR(X ...) predef::error(X)      static object(Stdio.File) sock;   static int seqno = 0;      static private string host, user, pw;   static private int port;      static void low_reconnect()   {    object losock = Stdio.File();    if(sock)    destruct(sock);    if(!losock->connect(host, port|RSQL_PORT)) -  throw(({"Can't connect to "+host+(port? ":"+port:"")+": "+ -  strerror(losock->errno())+"\n", backtrace()})); -  if(8!=losock->write(sprintf("RSQL%4c", RSQL_VERSION)) || +  ERROR("Can't connect to "+host+(port? ":"+port:"")+": "+ +  strerror(losock->errno())+"\n"); +  if(8!=losock->write("RSQL%4c", RSQL_VERSION) ||    losock->read(4) != "SQL!") {    destruct(losock); -  throw(({"Initial handshake error on "+host+(port? ":"+port:"")+"\n", -  backtrace()})); +  ERROR("Initial handshake error on "+host+(port? ":"+port:"")+"\n");    }    sock = losock;    if(!do_request('L', ({user,pw}), 1)) {    sock = 0;    if(losock)    destruct(losock); -  throw(({"Login refused on "+host+(port? ":"+port:"")+"\n", -  backtrace()})); +  ERROR("Login refused on "+host+(port? ":"+port:"")+"\n");    }   }      static void low_connect(string the_host, int the_port, string the_user,    string the_pw)   {    host = the_host;    port = the_port;    user = the_user;    pw = the_pw;    low_reconnect();   }      static mixed do_request(int cmd, mixed|void arg, int|void noreconnect)   {    LOCK;    if(!sock)    if(noreconnect) {    UNLOCK; -  throw(({"No connection\n", backtrace()})); +  ERROR("No connection\n");    } else    low_reconnect();    arg = (arg? encode_value(arg) : ""); -  sock->write(sprintf("?<%c>%4c%4c%s", cmd, ++seqno, sizeof(arg), arg)); +  sock->write("?<%c>%4c%4c%s", cmd, ++seqno, sizeof(arg), arg);    string res;    int rlen;    if((res = sock->read(12)) && sizeof(res)==12 &&    res[1..7]==sprintf("<%c>%4c", cmd, seqno) &&    sscanf(res[8..11], "%4c", rlen)==1 && (res[0]=='.' || res[0]=='!')) {    mixed rdat = (rlen? sock->read(rlen) : "");    if((!rdat) || sizeof(rdat)!=rlen) {    destruct(sock);    UNLOCK;    if(noreconnect) -  throw(({"RSQL Phase error, disconnected\n", backtrace()})); +  ERROR("RSQL Phase error, disconnected\n");    else return do_request(cmd, arg, 1);    }    UNLOCK;    rdat = (sizeof(rdat)? decode_value(rdat):0);    switch(res[0]) {    case '.': return rdat; -  case '!': throw(rdat); +  case '!': ERROR(rdat);    } -  throw(({"Internal error\n", backtrace()})); +  ERROR("Internal error\n");    } else {    destruct(sock);    UNLOCK;    if(noreconnect) -  throw(({"RSQL Phase error, disconnected\n", backtrace()})); +  ERROR("RSQL Phase error, disconnected\n");    else return do_request(cmd, arg, 1);    }   }      void select_db(string db)   {    do_request('D', db);   }      int|string error()