Skip to content
Snippets Groups Projects
Select Git revision
  • d0d8b8174750cbbcb05e6600c4857f93ba6cb73e
  • master default protected
  • 9.0
  • marcus/wix3
  • 8.0
  • nt-tools
  • 7.8
  • 7.6
  • 7.4
  • 7.2
  • 7.0
  • 0.6
  • rosuav/latex-markdown-renderer
  • rxnpatch/rxnpatch
  • marcus/gobject-introspection
  • rxnpatch/8.0
  • rosuav/pre-listening-ports
  • rosuav/async-annotations
  • rosuav/pgsql-ssl
  • rxnpatch/rxnpatch-broken/2023-10-06T094250
  • grubba/fdlib
  • v8.0.2020
  • v8.0.2018
  • v8.0.2016
  • v8.0.2014
  • v8.0.2012
  • v8.0.2008
  • v8.0.2006
  • v8.0.2004
  • v8.0.2002
  • v8.0.2000
  • v8.0.1998
  • v8.0.1996
  • v8.0.1994
  • v8.0.1992
  • v8.0.1990
  • v8.0.1988
  • v8.0.1986
  • rxnpatch/clusters/8.0/2025-04-29T124414
  • rxnpatch/2025-04-29T124414
  • v8.0.1984
41 results

rsql.pike

Blame
  • rsql.pike 4.67 KiB
    // Remote SQL server interface
    
    #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
    
    
    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)) ||
         losock->read(4) != "SQL!") {
        destruct(losock);
        throw(({"Initial handshake error on "+host+(port? ":"+port:"")+"\n",
    	    backtrace()}));    
      }
      sock = losock;
      if(!do_request('L', ({user,pw}), 1)) {
        sock = 0;
        if(losock)
          destruct(losock);
        throw(({"Login refused on "+host+(port? ":"+port:"")+"\n",
    	    backtrace()}));        
      }
    }
    
    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()}));
        } else
          low_reconnect();
      arg = (arg? encode_value(arg) : "");
      sock->write(sprintf("?<%c>%4c%4c%s", cmd, ++seqno, sizeof(arg), arg));
      string res;
      int rlen;
      if((res = sock->read(12)) && sizeof(res)==12 &&