ecbab12008-07-27Stephen R. van den Berg /* * Some pgsql utility functions. * They are kept here to avoid circular references. */
933a762014-11-10Stephen R. van den Berg //! The pgsql backend, shared between all connection instances.
3b31fa2014-11-14Stephen R. van den Berg //! It runs even in non-callback mode in a separate thread and makes sure //! that communication with the database is real-time and event driven //! at all times.
3a881c2014-11-10Stephen R. van den Berg //! //! @note //! Callbacks running from this backend directly determine the latency //! in reacting to communication with the database server; so it //! would be prudent not to block in these callbacks.
933a762014-11-10Stephen R. van den Berg 
ecbab12008-07-27Stephen R. van den Berg #pike __REAL_VERSION__
91e2382014-11-10Stephen R. van den Berg #require constant(Thread.Thread)
ecbab12008-07-27Stephen R. van den Berg  #include "pgsql.h"
3a881c2014-11-10Stephen R. van den Berg //! The instance of the pgsql dedicated backend.
fb8eb42014-10-31Stephen R. van den Berg final Pike.Backend local_backend = Pike.SmallBackend();
3a881c2014-11-10Stephen R. van den Berg 
6eb5942014-11-12Stephen R. van den Berg private Thread.Mutex backendmux = Thread.Mutex(); private int clientsregistered;
cff3a32014-09-12Stephen R. van den Berg 
771a162016-10-13Stephen R. van den Berg constant emptyarray = ({});
1c847c2017-06-27Stephen R. van den Berg constant describenodata
e2514a2017-06-28Stephen R. van den Berg  = (["datarowdesc":emptyarray, "datarowtypes":emptyarray, "datatypeoid":emptyarray]);
652b3c2014-11-19Stephen R. van den Berg final multiset censoroptions=(<"use_ssl","force_ssl", "cache_autoprepared_statements","reconnect","text_query","is_superuser", "server_encoding","server_version","integer_datetimes", "session_authorization">);
d99e6e2017-07-10Stephen R. van den Berg constant stdiobuftype = typeof(Stdio.Buffer());
bb4cfe2014-11-26Stephen R. van den Berg  /* Statements matching createprefix cause the prepared statement cache * to be flushed to prevent stale references to (temporary) tables */
02aba92014-11-26Stephen R. van den Berg final Regexp createprefix=iregexp("^\a*(CREATE|DROP)\a");
bb4cfe2014-11-26Stephen R. van den Berg  /* Statements matching dontcacheprefix never enter the cache */
02aba92014-11-26Stephen R. van den Berg private Regexp dontcacheprefix=iregexp("^\a*(FETCH|COPY)\a");
bb4cfe2014-11-26Stephen R. van den Berg  /* Statements not matching paralleliseprefix will cause the driver * to stall submission until all previously started statements have * run to completion */
02aba92014-11-26Stephen R. van den Berg private Regexp paralleliseprefix
a3a82e2017-06-17Stephen R. van den Berg  =iregexp("^\a*((SELEC|INSER)T|(UPDA|DELE)TE|(FETC|WIT)H)\a");
bb4cfe2014-11-26Stephen R. van den Berg 
bd47ec2017-06-25Stephen R. van den Berg  /* Statements matching transbeginprefix will cause the driver * insert a sync after the statement. * Failure to do so, will result in portal synchronisation errors * in the event of an ErrorResponse. */ final Regexp transbeginprefix
48e6cd2017-07-08Stephen R. van den Berg  =iregexp("^\a*(BEGIN|START)([; \t\f\r\n]|$)");
bd47ec2017-06-25Stephen R. van den Berg  /* Statements matching transendprefix will cause the driver * insert a sync after the statement. * Failure to do so, will result in portal synchronisation errors * in the event of an ErrorResponse. */ final Regexp transendprefix
48e6cd2017-07-08Stephen R. van den Berg  =iregexp("^\a*(COMMIT|ROLLBACK|END)([; \t\f\r\n]|$)");
bd47ec2017-06-25Stephen R. van den Berg 
bb4cfe2014-11-26Stephen R. van den Berg  /* For statements matching execfetchlimit the resultrows will not be
a7d3c22017-07-28Stephen R. van den Berg  * fetched in pieces. This heuristic will be sub-optimal whenever * either an UPDATE/DELETE/INSERT statement is prefixed by WITH, or * if there is a RETURNING with a *lot* of results. In those cases * the portal will be busy until all results have been fetched, and will * not be able to deliver results belonging to other parallel queries * running on the same filedescriptor. * * However, considering that the current heuristic increases query-speed * in the majority of the real-world cases, it would be considered a good * tradeoff.
bb4cfe2014-11-26Stephen R. van den Berg  */
6eb5942014-11-12Stephen R. van den Berg private Regexp execfetchlimit
02aba92014-11-26Stephen R. van den Berg  =iregexp("^\a*((UPDA|DELE)TE|INSERT)\a|\aLIMIT\a+[1-9][; \t\f\r\n]*$"); private Regexp iregexp(string expr) { Stdio.Buffer ret=Stdio.Buffer(); foreach(expr;;int c) if(c>='A'&&c<='Z') ret->add('[',c,c+'a'-'A',']'); else if(c=='\a') // Replace with generic whitespace ret->add("[ \t\f\r\n]"); else ret->add_int8(c); return Regexp(ret->read()); }
fb8eb42014-10-31Stephen R. van den Berg 
8675b02017-06-03Stephen R. van den Berg final void closestatement(bufcon|conxsess plugbuffer,string oldprep) {
fb8eb42014-10-31Stephen R. van den Berg  if(oldprep) { PD("Close statement %s\n",oldprep);
8675b02017-06-03Stephen R. van den Berg  CHAIN(plugbuffer)->add_int8('C')->add_hstring(({'S', oldprep, 0}), 4, 4);
fb8eb42014-10-31Stephen R. van den Berg  } }
6eb5942014-11-12Stephen R. van den Berg private void run_local_backend() {
cff3a32014-09-12Stephen R. van den Berg  Thread.MutexKey lock; int looponce; do { looponce=0; if(lock=backendmux->trylock()) { PD("Starting local backend\n");
49143b2016-10-12Stephen R. van den Berg  while (clientsregistered) { // Autoterminate when not needed mixed err; if (err = catch(local_backend(4096.0))) werror(describe_backtrace(err)); }
cff3a32014-09-12Stephen R. van den Berg  PD("Terminating local backend\n"); lock=0; looponce=clientsregistered; } } while(looponce); }
ecbab12008-07-27Stephen R. van den Berg 
3a881c2014-11-10Stephen R. van den Berg //! Registers yourself as a user of this backend. If the backend //! has not been started yet, it will be spawned automatically.
cff3a32014-09-12Stephen R. van den Berg final void register_backend() { if(!clientsregistered++) Thread.Thread(run_local_backend); }
ecbab12008-07-27Stephen R. van den Berg 
3a881c2014-11-10Stephen R. van den Berg //! Unregisters yourself as a user of this backend. If there are //! no longer any registered users, the backend will be terminated.
cff3a32014-09-12Stephen R. van den Berg final void unregister_backend() { --clientsregistered; }
4741cd2008-07-31Stephen R. van den Berg 
cff3a32014-09-12Stephen R. van den Berg final void throwdelayederror(object parent) { if(mixed err=parent._delayederror) { parent._delayederror=UNDEFINED; if(stringp(err)) err=({err,backtrace()[..<2]}); throw(err);
ecbab12008-07-27Stephen R. van den Berg  }
cff3a32014-09-12Stephen R. van den Berg }
ecbab12008-07-27Stephen R. van den Berg 
fb8eb42014-10-31Stephen R. van den Berg final int oidformat(int oid) { switch(oid) { case BOOLOID: case BYTEAOID: case CHAROID: case INT8OID: case INT2OID: case INT4OID: case TEXTOID: case OIDOID: case XMLOID: case MACADDROID: case BPCHAROID: case VARCHAROID: case CTIDOID: case UUIDOID: return 1; //binary } return 0; // text }
6eb5942014-11-12Stephen R. van den Berg private inline mixed callout(function(mixed ...:void) f,
0fed8c2014-11-10Stephen R. van den Berg  float|int delay,mixed ... args) { return local_backend->call_out(f,delay,@args); }
933a762014-11-10Stephen R. van den Berg // Some pgsql utility functions
ecbab12008-07-27Stephen R. van den Berg 
3b31fa2014-11-14Stephen R. van den Berg class bufcon {
cff3a32014-09-12Stephen R. van den Berg  inherit Stdio.Buffer;
feb8152017-11-09Stephen R. van den Berg  private int dirty;
ecbab12008-07-27Stephen R. van den Berg 
8675b02017-06-03Stephen R. van den Berg #ifdef PG_DEBUGRACE final bufcon `chain() { return this; } #endif
3b31fa2014-11-14Stephen R. van den Berg  private conxion realbuffer;
ecbab12008-07-27Stephen R. van den Berg 
3b31fa2014-11-14Stephen R. van den Berg  protected void create(conxion _realbuffer) {
cff3a32014-09-12Stephen R. van den Berg  realbuffer=_realbuffer;
ecbab12008-07-27Stephen R. van den Berg  }
d432822008-07-31Stephen R. van den Berg 
bfa2552016-10-15Stephen R. van den Berg  final int `stashcount() { return realbuffer->stashcount; }
3b31fa2014-11-14Stephen R. van den Berg  final bufcon start(void|int waitforreal) {
feb8152017-11-09Stephen R. van den Berg  dirty = 1;
cff3a32014-09-12Stephen R. van den Berg  realbuffer->stashcount++;
feb8152017-11-09Stephen R. van den Berg  dirty = 2;
cff3a32014-09-12Stephen R. van den Berg #ifdef PG_DEBUG if(waitforreal)
3b31fa2014-11-14Stephen R. van den Berg  error("pgsql.bufcon not allowed here\n");
d432822008-07-31Stephen R. van den Berg #endif
cff3a32014-09-12Stephen R. van den Berg  return this; }
0e7f332014-11-15Stephen R. van den Berg  final void sendcmd(int mode,void|sql_result portal) {
4e0be82014-11-18Stephen R. van den Berg  Thread.MutexKey lock=realbuffer->shortmux->lock();
04e6362017-07-03Stephen R. van den Berg  if (portal)
cff3a32014-09-12Stephen R. van den Berg  realbuffer->stashqueue->write(portal);
04e6362017-07-03Stephen R. van den Berg  if (mode == SYNCSEND) { add(PGSYNC); realbuffer->stashqueue->write(1); mode = SENDOUT; // Demote it to prevent an extra SYNC upon stashflush
1c847c2017-06-27Stephen R. van den Berg  }
cff3a32014-09-12Stephen R. van den Berg  realbuffer->stash->add(this);
04e6362017-07-03Stephen R. van den Berg  PD("%d>Stashed mode %d > %d\n",
3f39b52017-07-01Stephen R. van den Berg  realbuffer->socket->query_fd(), mode, realbuffer->stashflushmode); if (mode > realbuffer->stashflushmode) realbuffer->stashflushmode = mode;
feb8152017-11-09Stephen R. van den Berg  dirty = 1;
cff3a32014-09-12Stephen R. van den Berg  if(!--realbuffer->stashcount)
feb8152017-11-09Stephen R. van den Berg  dirty = 0, realbuffer->stashavail.signal(); else dirty = 0;
cff3a32014-09-12Stephen R. van den Berg  lock=0; this->clear(); if(lock=realbuffer->nostash->trylock(1)) {
8675b02017-06-03Stephen R. van den Berg #ifdef PG_DEBUGRACE conxsess sess = conxsess(realbuffer); realbuffer->started = lock; lock = 0; sess->sendcmd(SENDOUT); #else realbuffer->started = lock; lock = 0;
0e7f332014-11-15Stephen R. van den Berg  realbuffer->sendcmd(SENDOUT);
8675b02017-06-03Stephen R. van den Berg #endif
cff3a32014-09-12Stephen R. van den Berg  }
d432822008-07-31Stephen R. van den Berg  }
feb8152017-11-09Stephen R. van den Berg  protected void _destruct() { switch (dirty) { case 1: werror("FIXME: Race condition detected %s\n", describe_backtrace(({"", backtrace()[..<1]}))); if (!realbuffer->stashcount) break; case 2: Thread.MutexKey lock = realbuffer->shortmux->lock(2); if (!--realbuffer->stashcount) realbuffer->stashavail.signal(); lock = 0; } }
74abb02014-11-24Stephen R. van den Berg };
ecbab12008-07-27Stephen R. van den Berg 
c639252014-11-20Stephen R. van den Berg class conxiin {
cff3a32014-09-12Stephen R. van den Berg  inherit Stdio.Buffer:i;
c639252014-11-20Stephen R. van den Berg  final Thread.Condition fillread; final Thread.Mutex fillreadmux;
279d842014-12-16Stephen R. van den Berg  final int procmsg;
05b0852014-11-25Stephen R. van den Berg  private int didreadcb;
c639252014-11-20Stephen R. van den Berg  protected bool range_error(int howmuch) { #ifdef PG_DEBUG if(howmuch<=0) error("Out of range %d\n",howmuch); #endif if(fillread) { Thread.MutexKey lock=fillreadmux->lock();
05b0852014-11-25Stephen R. van den Berg  if(!didreadcb)
2d67a12014-11-25Stephen R. van den Berg  fillread.wait(lock);
05b0852014-11-25Stephen R. van den Berg  didreadcb=0;
c639252014-11-20Stephen R. van den Berg  lock=0; } else throw(MAGICTERMINATE); return true; } final int read_cb(mixed id,mixed b) {
08505c2017-07-01Stephen R. van den Berg  PD("Read callback %O\n", b && ((string)b)
8551f72016-10-12Stephen R. van den Berg #ifndef PG_DEBUGMORE [..255] #endif );
c639252014-11-20Stephen R. van den Berg  Thread.MutexKey lock=fillreadmux->lock();
c198712016-02-17Stephen R. van den Berg  if(procmsg&&id) procmsg=0,lock=0,Thread.Thread(id);
279d842014-12-16Stephen R. van den Berg  else if(fillread)
05b0852014-11-25Stephen R. van den Berg  didreadcb=1, fillread.signal();
c639252014-11-20Stephen R. van den Berg  lock=0; return 0; } protected void create() { i::create(); fillreadmux=Thread.Mutex(); fillread=Thread.Condition(); }
74abb02014-11-24Stephen R. van den Berg };
c639252014-11-20Stephen R. van den Berg 
57cfc62017-10-29Stephen R. van den Berg class sfile { inherit Stdio.File; int query_fd() { return is_open() ? ::query_fd() : -1; } };
c639252014-11-20Stephen R. van den Berg class conxion {
cff3a32014-09-12Stephen R. van den Berg  inherit Stdio.Buffer:o;
c639252014-11-20Stephen R. van den Berg  final conxiin i;
cff3a32014-09-12Stephen R. van den Berg 
6eb5942014-11-12Stephen R. van den Berg  private Thread.Queue qportals;
4e0be82014-11-18Stephen R. van den Berg  final Thread.Mutex shortmux;
fa4c472016-02-27Stephen R. van den Berg  private int closenext;
e7c7322016-02-24Stephen R. van den Berg 
57cfc62017-10-29Stephen R. van den Berg  final sfile socket;
279d842014-12-16Stephen R. van den Berg  private function(void|mixed:void) connectfail;
6eb5942014-11-12Stephen R. van den Berg  private int towrite;
3a1ed32015-12-23Stephen R. van den Berg  final multiset(function(void|mixed:void)) closecallbacks=(<>);
cff3a32014-09-12Stephen R. van den Berg 
a362e32014-11-03Stephen R. van den Berg  final Thread.Mutex nostash; final Thread.MutexKey started; final Thread.Queue stashqueue; final Thread.Condition stashavail; final Stdio.Buffer stash; final int stashflushmode; final int stashcount; final int synctransact;
8675b02017-06-03Stephen R. van den Berg #ifdef PG_DEBUGRACE final mixed nostrack; #endif
cff3a32014-09-12Stephen R. van den Berg #ifdef PG_DEBUG
a362e32014-11-03Stephen R. van den Berg  final int queueoutidx;
cbe52f2014-11-13Stephen R. van den Berg  final int queueinidx=-1;
ecbab12008-07-27Stephen R. van den Berg #endif
3b31fa2014-11-14Stephen R. van den Berg  private inline void queueup(sql_result portal) {
4ef8a32014-10-29Stephen R. van den Berg  qportals->write(portal); portal->_synctransact=synctransact;
bf75ce2014-11-15Stephen R. van den Berg  PD("%d>%O %d %d Queue portal %d bytes\n",socket->query_fd(), portal._portalname,++queueoutidx,synctransact,sizeof(this));
4ef8a32014-10-29Stephen R. van den Berg  }
8675b02017-06-03Stephen R. van den Berg  final bufcon|conxsess start(void|int waitforreal) {
cff3a32014-09-12Stephen R. van den Berg  Thread.MutexKey lock; if(lock=(waitforreal?nostash->lock:nostash->trylock)(1)) {
1c847c2017-06-27Stephen R. van den Berg  int mode;
8675b02017-06-03Stephen R. van den Berg #ifdef PG_DEBUGRACE conxsess sess = conxsess(this); #endif started = lock;
4e0be82014-11-18Stephen R. van den Berg  lock=shortmux->lock();
4ef8a32014-10-29Stephen R. van den Berg  if(stashcount)
abf9442014-12-01Stephen R. van den Berg  PT(stashavail.wait(lock));
1c847c2017-06-27Stephen R. van den Berg  mode = getstash(KEEP);
cff3a32014-09-12Stephen R. van den Berg  lock=0;
1c847c2017-06-27Stephen R. van den Berg  if (mode > KEEP) sendcmd(mode); // Force out stash to the server
8675b02017-06-03Stephen R. van den Berg #ifdef PG_DEBUGRACE return sess; #else
cff3a32014-09-12Stephen R. van den Berg  return this;
8675b02017-06-03Stephen R. van den Berg #endif
cff3a32014-09-12Stephen R. van den Berg  }
feb8152017-11-09Stephen R. van den Berg  return bufcon(this)->start();
ecbab12008-07-27Stephen R. van den Berg  }
6eb5942014-11-12Stephen R. van den Berg  private int write_cb() {
b982c02016-10-14Stephen R. van den Berg  Thread.MutexKey lock = shortmux->lock();
5a8c952016-10-15Stephen R. van den Berg  if (this) { // Guard against async destructs towrite -= output_to(socket, towrite); lock = 0; if (!i->fillread && !sizeof(this)) close(); }
cff3a32014-09-12Stephen R. van den Berg  return 0; }
1c847c2017-06-27Stephen R. van den Berg  private int getstash(int mode) { if (sizeof(stash)) { add(stash); stash->clear(); foreach (stashqueue->try_read_array();; int|sql_result portal) if (intp(portal)) qportals->write(synctransact++); else queueup(portal);
3f39b52017-07-01Stephen R. van den Berg  PD("%d>Got stash mode %d > %d\n", socket->query_fd(), stashflushmode, mode); if (stashflushmode > mode) mode = stashflushmode;
1c847c2017-06-27Stephen R. van den Berg  stashflushmode = KEEP; } return mode; }
771a162016-10-13Stephen R. van den Berg  final void sendcmd(void|int mode, void|sql_result portal) { Thread.MutexKey lock;
1c847c2017-06-27Stephen R. van den Berg  if (portal) queueup(portal); unfinalised:
6a5bed2014-11-13Stephen R. van den Berg  do {
771a162016-10-13Stephen R. van den Berg  switch (mode) {
6a5bed2014-11-13Stephen R. van den Berg  default:
1c847c2017-06-27Stephen R. van den Berg  break unfinalised;
0e7f332014-11-15Stephen R. van den Berg  case SYNCSEND:
bf75ce2014-11-15Stephen R. van den Berg  PD("%d>Sync %d %d Queue\n",
771a162016-10-13Stephen R. van den Berg  socket->query_fd(), synctransact, ++queueoutidx);
6a5bed2014-11-13Stephen R. van den Berg  add(PGSYNC);
771a162016-10-13Stephen R. van den Berg  mode = SENDOUT;
6a5bed2014-11-13Stephen R. van den Berg  break;
0e7f332014-11-15Stephen R. van den Berg  case FLUSHLOGSEND:
1c847c2017-06-27Stephen R. van den Berg  PD("%d>%O %d Queue simplequery %d bytes\n", socket->query_fd(), portal._portalname, ++queueoutidx, sizeof(this));
771a162016-10-13Stephen R. van den Berg  mode = FLUSHSEND;
6a5bed2014-11-13Stephen R. van den Berg  } qportals->write(synctransact++);
1c847c2017-06-27Stephen R. van den Berg  } while(0); lock = shortmux->lock(); mode = getstash(mode);
a362e32014-11-03Stephen R. van den Berg  catch {
4ef8a32014-10-29Stephen R. van den Berg outer:
a362e32014-11-03Stephen R. van den Berg  do { switch(mode) { default:
771a162016-10-13Stephen R. van den Berg  PD("%d>Skip flush %d Queue %O\n", socket->query_fd(), mode, (string)this);
a362e32014-11-03Stephen R. van den Berg  break outer;
0e7f332014-11-15Stephen R. van den Berg  case FLUSHSEND:
a362e32014-11-03Stephen R. van den Berg  PD("Flush\n"); add(PGFLUSH);
0e7f332014-11-15Stephen R. van den Berg  case SENDOUT:;
a362e32014-11-03Stephen R. van den Berg  } if(towrite=sizeof(this)) {
bf75ce2014-11-15Stephen R. van den Berg  PD("%d>Sendcmd %O\n",socket->query_fd(),((string)this)[..towrite-1]);
a362e32014-11-03Stephen R. van den Berg  towrite-=output_to(socket,towrite); } } while(0);
941f6c2014-11-19Stephen R. van den Berg  lock=started=0;
a362e32014-11-03Stephen R. van den Berg  return; };
941f6c2014-11-19Stephen R. van den Berg  lock=0;
279d842014-12-16Stephen R. van den Berg  catch(connectfail());
ecbab12008-07-27Stephen R. van den Berg  }
cff3a32014-09-12Stephen R. van den Berg  final int close() {
6c48822016-02-28Stephen R. van den Berg  if(!closenext && nostash) { closenext=1; Thread.MutexKey lock=i->fillreadmux->lock();
0983ca2016-02-17Stephen R. van den Berg  if(i->fillread) { // Delayed close() after flushing the output buffer i->fillread.signal(); i->fillread=0; } lock=0;
fe85ab2016-02-25Stephen R. van den Berg  PD("%d>Delayed close, flush write\n",socket->query_fd()); i->read_cb(socket->query_id(),0);
6c48822016-02-28Stephen R. van den Berg  return 0; } else return -1;
ecbab12008-07-27Stephen R. van den Berg  }
6eb5942014-11-12Stephen R. van den Berg  protected void destroy() {
b982c02016-10-14Stephen R. van den Berg  PD("%d>Close conxion %d\n", socket ? socket->query_fd() : -1, !!nostash);
1cb5e72016-12-06Stephen R. van den Berg  int|.pgsql_util.sql_result portal;
2434502017-07-01Stephen R. van den Berg  if (qportals) // CancelRequest does not use qportals while (portal = qportals->try_read()) if (objectp(portal)) portal->_purgeportal();
6c48822016-02-28Stephen R. van den Berg  if(nostash) { catch {
58d0b02016-04-01Stephen R. van den Berg  while(sizeof(closecallbacks)) foreach(closecallbacks;function(void|mixed:void) closecb;) closecb();
6c48822016-02-28Stephen R. van den Berg  destruct(nostash);
7925692016-10-15Stephen R. van den Berg  socket->set_nonblocking(); // Drop all callbacks
6c48822016-02-28Stephen R. van den Berg  PD("%d>Close socket\n",socket->query_fd()); socket->close(); }; }
279d842014-12-16Stephen R. van den Berg  connectfail=0;
ecbab12008-07-27Stephen R. van den Berg  }
279d842014-12-16Stephen R. van den Berg  final void connectloop(object pgsqlsess, int nossl) {
cff3a32014-09-12Stephen R. van den Berg  mixed err=catch { for(;;clear()) { socket->connect(pgsqlsess._host,pgsqlsess._port);
fc7f092014-06-01Martin Nilsson #if constant(SSL.File)
cff3a32014-09-12Stephen R. van den Berg  if(!nossl && !pgsqlsess->nossl
d743bf2014-11-13Stephen R. van den Berg  && (pgsqlsess._options.use_ssl || pgsqlsess._options.force_ssl)) {
cff3a32014-09-12Stephen R. van den Berg  PD("SSLRequest\n"); start()->add_int32(8)->add_int32(PG_PROTOCOL(1234,5679))
0e7f332014-11-15Stephen R. van den Berg  ->sendcmd(SENDOUT);
cff3a32014-09-12Stephen R. van den Berg  switch(read_int8()) { case 'S': object fcon=SSL.File(socket,SSL.Context()); if(fcon->connect()) { socket=fcon; break; }
1cb5e72016-12-06Stephen R. van den Berg  default: PD("%d>Close socket short\n", socket->query_fd());
bf75ce2014-11-15Stephen R. van den Berg  socket->close();
cff3a32014-09-12Stephen R. van den Berg  pgsqlsess.nossl=1; continue; case 'N':
d743bf2014-11-13Stephen R. van den Berg  if(pgsqlsess._options.force_ssl)
cff3a32014-09-12Stephen R. van den Berg  error("Encryption not supported on connection to %s:%d\n", pgsqlsess.host,pgsqlsess.port); } } #else
d743bf2014-11-13Stephen R. van den Berg  if(pgsqlsess._options.force_ssl)
cff3a32014-09-12Stephen R. van den Berg  error("Encryption library missing," " cannot establish connection to %s:%d\n", pgsqlsess.host,pgsqlsess.port); #endif break; }
1cb5e72016-12-06Stephen R. van den Berg  connectfail=pgsqlsess->_connectfail;
a362e32014-11-03Stephen R. van den Berg  if(!socket->is_open())
2056de2015-12-05Stephen R. van den Berg  error(strerror(socket->errno())+".\n");
cff3a32014-09-12Stephen R. van den Berg  socket->set_backend(local_backend);
c639252014-11-20Stephen R. van den Berg  socket->set_buffer_mode(i,0);
3811e32014-12-02Stephen R. van den Berg  socket->set_nonblocking(i->read_cb,write_cb,close);
8551f72016-10-12Stephen R. van den Berg  if (nossl != 2) { connectfail=pgsqlsess->_connectfail; Thread.Thread(pgsqlsess->_processloop,this); }
cff3a32014-09-12Stephen R. van den Berg  return; };
279d842014-12-16Stephen R. van den Berg  catch(connectfail(err));
0d1a5e2014-11-14Stephen R. van den Berg  } private string _sprintf(int type, void|mapping flags) { string res=UNDEFINED; switch(type) { case 'O':
bf9dab2016-02-17Stephen R. van den Berg  int fd=-1; if(socket) catch(fd=socket->query_fd());
3b31fa2014-11-14Stephen R. van den Berg  res=predef::sprintf("conxion fd: %d input queue: %d/%d "
8675b02017-06-03Stephen R. van den Berg  "queued portals: %d output queue: %d/%d\n"
feb8152017-11-09Stephen R. van den Berg  "started: %d stashcount: %d\n",
bf9dab2016-02-17Stephen R. van den Berg  fd,sizeof(i),i->_size_object(),
2434502017-07-01Stephen R. van den Berg  qportals && qportals->size(), sizeof(this), _size_object(),
feb8152017-11-09Stephen R. van den Berg  !!started, stashcount);
0d1a5e2014-11-14Stephen R. van den Berg  break; } return res;
cff3a32014-09-12Stephen R. van den Berg  }
279d842014-12-16Stephen R. van den Berg  protected void create(object pgsqlsess,Thread.Queue _qportals,int nossl) {
c639252014-11-20Stephen R. van den Berg  o::create();
cff3a32014-09-12Stephen R. van den Berg  qportals = _qportals;
4ef8a32014-10-29Stephen R. van den Berg  synctransact = 1;
57cfc62017-10-29Stephen R. van den Berg  socket=sfile();
c639252014-11-20Stephen R. van den Berg  i=conxiin();
efdef42014-11-18Stephen R. van den Berg  shortmux=Thread.Mutex();
941f6c2014-11-19Stephen R. van den Berg  nostash=Thread.Mutex();
fa4c472016-02-27Stephen R. van den Berg  closenext = 0;
941f6c2014-11-19Stephen R. van den Berg  stashavail=Thread.Condition(); stashqueue=Thread.Queue();
cff3a32014-09-12Stephen R. van den Berg  stash=Stdio.Buffer();
279d842014-12-16Stephen R. van den Berg  Thread.Thread(connectloop,pgsqlsess,nossl);
ecbab12008-07-27Stephen R. van den Berg  }
74abb02014-11-24Stephen R. van den Berg };
ecbab12008-07-27Stephen R. van den Berg 
8675b02017-06-03Stephen R. van den Berg #ifdef PG_DEBUGRACE class conxsess { final conxion chain; void create(conxion parent) { if (parent->started) werror("Overwriting conxsess %s %s\n", describe_backtrace(({"new ", backtrace()[..<1]})), describe_backtrace(({"old ", parent->nostrack}))); parent->nostrack = backtrace(); chain = parent; } final void sendcmd(int mode,void|sql_result portal) { chain->sendcmd(mode, portal); chain = 0; } void destroy() { if (chain) werror("Untransmitted conxsess %s\n", describe_backtrace(({"", backtrace()[..<1]}))); } }; #endif
f4c9d62009-02-15Stephen R. van den Berg //! The result object returned by @[Sql.pgsql()->big_query()], except for
0412962009-01-19Stephen R. van den Berg //! the noted differences it behaves the same as @[Sql.sql_result]. //! //! @seealso
f4c9d62009-02-15Stephen R. van den Berg //! @[Sql.sql_result], @[Sql.pgsql], @[Sql.Sql], @[Sql.pgsql()->big_query()]
3b31fa2014-11-14Stephen R. van den Berg class sql_result {
ecbab12008-07-27Stephen R. van den Berg 
6eb5942014-11-12Stephen R. van den Berg  private object pgsqlsess; private int eoffound;
3b31fa2014-11-14Stephen R. van den Berg  private conxion c;
c639252014-11-20Stephen R. van den Berg  private conxiin cr;
a362e32014-11-03Stephen R. van den Berg  final mixed _delayederror;
0e7f332014-11-15Stephen R. van den Berg  final int _state;
a362e32014-11-03Stephen R. van den Berg  final int _fetchlimit;
15d4e12014-11-18Stephen R. van den Berg  private int alltext;
a362e32014-11-03Stephen R. van den Berg  final int _forcetext;
f908742017-02-04Stephen R. van den Berg  private int syncparse;
bd47ec2017-06-25Stephen R. van den Berg  private int transtype;
a362e32014-11-03Stephen R. van den Berg  final string _portalname;
bf20922014-11-17Stephen R. van den Berg  private int rowsreceived; private int inflight; private int portalbuffersize; private Thread.Mutex closemux; private Thread.Queue datarows;
15d4e12014-11-18Stephen R. van den Berg  private array(mapping(string:mixed)) datarowdesc;
d9b3042014-11-25Stephen R. van den Berg  private array(int) datarowtypes; // types from datarowdesc
bf20922014-11-17Stephen R. van den Berg  private string statuscmdcomplete;
15d4e12014-11-18Stephen R. van den Berg  private int bytesreceived;
a362e32014-11-03Stephen R. van den Berg  final int _synctransact; final Thread.Condition _ddescribe; final Thread.Mutex _ddescribemux; final Thread.MutexKey _unnamedportalkey,_unnamedstatementkey; final array _params; final string _query; final string _preparedname; final mapping(string:mixed) _tprepared;
1611e42014-11-28Stephen R. van den Berg  private function(:void) gottimeout; private int timeout;
ecbab12008-07-27Stephen R. van den Berg 
6eb5942014-11-12Stephen R. van den Berg  private string _sprintf(int type, void|mapping flags) {
11b13b2014-08-16Martin Nilsson  string res=UNDEFINED;
cff3a32014-09-12Stephen R. van den Berg  switch(type) { case 'O':
bf9dab2016-02-17Stephen R. van den Berg  int fd=-1; if(c&&c->socket) catch(fd=c->socket->query_fd());
bf20922014-11-17Stephen R. van den Berg  res=sprintf("sql_result state: %d numrows: %d eof: %d inflight: %d\n"
cff3a32014-09-12Stephen R. van den Berg  "query: %O\n"
5600d22014-11-18Stephen R. van den Berg  "fd: %O portalname: %O datarows: %d"
bd47ec2017-06-25Stephen R. van den Berg  " synctransact: %d laststatus: %s\n",
bf20922014-11-17Stephen R. van den Berg  _state,rowsreceived,eoffound,inflight,
bf9dab2016-02-17Stephen R. van den Berg  _query,fd,_portalname,datarowtypes&&sizeof(datarowtypes),
bd47ec2017-06-25Stephen R. van den Berg  _synctransact,
bf20922014-11-17Stephen R. van den Berg  statuscmdcomplete||(_unnamedstatementkey?"*parsing*":""));
cff3a32014-09-12Stephen R. van den Berg  break;
11b13b2014-08-16Martin Nilsson  } return res;
ecbab12008-07-27Stephen R. van den Berg  }
3b31fa2014-11-14Stephen R. van den Berg  protected void create(object _pgsqlsess,conxion _c,string query,
1611e42014-11-28Stephen R. van den Berg  int _portalbuffersize,int alltyped,array params,int forcetext,
bd47ec2017-06-25Stephen R. van den Berg  int _timeout, int _syncparse, int _transtype) {
a362e32014-11-03Stephen R. van den Berg  pgsqlsess = _pgsqlsess;
c639252014-11-20Stephen R. van den Berg  cr = (c = _c)->i;
11b13b2014-08-16Martin Nilsson  _query = query;
bf20922014-11-17Stephen R. van den Berg  datarows = Thread.Queue();
fb8eb42014-10-31Stephen R. van den Berg  _ddescribe=Thread.Condition(); _ddescribemux=Thread.Mutex();
cff3a32014-09-12Stephen R. van den Berg  closemux=Thread.Mutex();
bf20922014-11-17Stephen R. van den Berg  portalbuffersize=_portalbuffersize;
15d4e12014-11-18Stephen R. van den Berg  alltext = !alltyped;
11b13b2014-08-16Martin Nilsson  _params = params; _forcetext = forcetext;
0e7f332014-11-15Stephen R. van den Berg  _state = PORTALINIT;
1611e42014-11-28Stephen R. van den Berg  timeout = _timeout;
f908742017-02-04Stephen R. van den Berg  syncparse = _syncparse;
1611e42014-11-28Stephen R. van den Berg  gottimeout = _pgsqlsess->cancelquery;
58d0b02016-04-01Stephen R. van den Berg  c->closecallbacks+=(<destroy>);
bd47ec2017-06-25Stephen R. van den Berg  transtype = _transtype;
11b13b2014-08-16Martin Nilsson  }
ecbab12008-07-27Stephen R. van den Berg 
11b13b2014-08-16Martin Nilsson  //! Returns the command-complete status for this query. //! //! @seealso //! @[affected_rows()] //! //! @note //! This function is PostgreSQL-specific, and thus it is not available //! through the generic SQL-interface.
74abb02014-11-24Stephen R. van den Berg  /*semi*/final string status_command_complete() {
bf20922014-11-17Stephen R. van den Berg  return statuscmdcomplete;
11b13b2014-08-16Martin Nilsson  }
ecbab12008-07-27Stephen R. van den Berg 
11b13b2014-08-16Martin Nilsson  //! Returns the number of affected rows by this query. //! //! @seealso //! @[status_command_complete()] //! //! @note //! This function is PostgreSQL-specific, and thus it is not available //! through the generic SQL-interface.
74abb02014-11-24Stephen R. van den Berg  /*semi*/final int affected_rows() {
11b13b2014-08-16Martin Nilsson  int rows;
bf20922014-11-17Stephen R. van den Berg  if(statuscmdcomplete) sscanf(statuscmdcomplete,"%*s %d",rows);
11b13b2014-08-16Martin Nilsson  return rows; }
ecbab12008-07-27Stephen R. van den Berg 
15d4e12014-11-18Stephen R. van den Berg  final void _storetiming() { if(_tprepared) { _tprepared.trun=gethrtime()-_tprepared.trunstart; m_delete(_tprepared,"trunstart"); _tprepared = UNDEFINED; } }
6eb5942014-11-12Stephen R. van den Berg  private void waitfordescribe() {
fb8eb42014-10-31Stephen R. van den Berg  Thread.MutexKey lock=_ddescribemux->lock();
192acd2014-11-22Stephen R. van den Berg  if(!datarowtypes)
abf9442014-12-01Stephen R. van den Berg  PT(_ddescribe->wait(lock));
fb8eb42014-10-31Stephen R. van den Berg  lock=0; }
11b13b2014-08-16Martin Nilsson  //! @seealso //! @[Sql.sql_result()->num_fields()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final int num_fields() {
192acd2014-11-22Stephen R. van den Berg  if(!datarowtypes)
fb8eb42014-10-31Stephen R. van den Berg  waitfordescribe(); trydelayederror();
192acd2014-11-22Stephen R. van den Berg  return sizeof(datarowtypes);
11b13b2014-08-16Martin Nilsson  }
ecbab12008-07-27Stephen R. van den Berg 
bbb4ce2014-11-24Stephen R. van den Berg  //! @note //! This method returns the number of rows already received from //! the database for the current query. Note that this number //! can still increase between subsequent calls if the results from //! the query are not complete yet. This function is only guaranteed to //! return the correct count after EOF has been reached.
11b13b2014-08-16Martin Nilsson  //! @seealso //! @[Sql.sql_result()->num_rows()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final int num_rows() {
fb8eb42014-10-31Stephen R. van den Berg  trydelayederror();
bf20922014-11-17Stephen R. van den Berg  return rowsreceived;
11b13b2014-08-16Martin Nilsson  }
ecbab12008-07-27Stephen R. van den Berg 
6eb5942014-11-12Stephen R. van den Berg  private inline void trydelayederror() {
cff3a32014-09-12Stephen R. van den Berg  if(_delayederror) throwdelayederror(this); }
11b13b2014-08-16Martin Nilsson  //! @seealso //! @[Sql.sql_result()->eof()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final int eof() {
cff3a32014-09-12Stephen R. van den Berg  trydelayederror();
11b13b2014-08-16Martin Nilsson  return eoffound; }
ecbab12008-07-27Stephen R. van den Berg 
11b13b2014-08-16Martin Nilsson  //! @seealso //! @[Sql.sql_result()->fetch_fields()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final array(mapping(string:mixed)) fetch_fields() {
192acd2014-11-22Stephen R. van den Berg  if(!datarowtypes)
fb8eb42014-10-31Stephen R. van den Berg  waitfordescribe();
cff3a32014-09-12Stephen R. van den Berg  trydelayederror();
a2b20d2014-11-25Stephen R. van den Berg  return datarowdesc+emptyarray;
15d4e12014-11-18Stephen R. van den Berg  } #ifdef PG_DEBUG
74abb02014-11-24Stephen R. van den Berg #define INTVOID int
15d4e12014-11-18Stephen R. van den Berg #else
74abb02014-11-24Stephen R. van den Berg #define INTVOID void
15d4e12014-11-18Stephen R. van den Berg #endif
74abb02014-11-24Stephen R. van den Berg  final INTVOID _decodedata(int msglen,string cenc) {
cfa2482017-06-18Stephen R. van den Berg  _storetiming(); _releasestatement();
15d4e12014-11-18Stephen R. van den Berg  string serror; bytesreceived+=msglen;
c639252014-11-20Stephen R. van den Berg  int cols=cr->read_int16();
15d4e12014-11-18Stephen R. van den Berg  array a=allocate(cols,!alltext&&Val.null); #ifdef PG_DEBUG msglen-=2+4*cols; #endif
192acd2014-11-22Stephen R. van den Berg  foreach(datarowtypes;int i;int typ) {
c639252014-11-20Stephen R. van den Berg  int collen=cr->read_sint(4);
15d4e12014-11-18Stephen R. van den Berg  if(collen>0) { #ifdef PG_DEBUG msglen-=collen; #endif mixed value;
192acd2014-11-22Stephen R. van den Berg  switch(typ) {
15d4e12014-11-18Stephen R. van den Berg  case FLOAT4OID: #if SIZEOF_FLOAT>=8 case FLOAT8OID: #endif if(!alltext) {
c639252014-11-20Stephen R. van den Berg  value=(float)cr->read(collen);
15d4e12014-11-18Stephen R. van den Berg  break; }
c639252014-11-20Stephen R. van den Berg  default:value=cr->read(collen);
15d4e12014-11-18Stephen R. van den Berg  break; case CHAROID:
c639252014-11-20Stephen R. van den Berg  value=alltext?cr->read(1):cr->read_int8();
15d4e12014-11-18Stephen R. van den Berg  break;
c639252014-11-20Stephen R. van den Berg  case BOOLOID:value=cr->read_int8();
15d4e12014-11-18Stephen R. van den Berg  switch(value) { case 'f':value=0; break; case 't':value=1; } if(alltext) value=value?"t":"f"; break; case TEXTOID: case BPCHAROID: case VARCHAROID:
c639252014-11-20Stephen R. van den Berg  value=cr->read(collen);
15d4e12014-11-18Stephen R. van den Berg  if(cenc==UTF8CHARSET && catch(value=utf8_to_string(value)) && !serror) serror=SERROR("%O contains non-%s characters\n", value,UTF8CHARSET); break; case INT8OID:case INT2OID: case OIDOID:case INT4OID: if(_forcetext) {
c639252014-11-20Stephen R. van den Berg  value=cr->read(collen);
15d4e12014-11-18Stephen R. van den Berg  if(!alltext) value=(int)value; } else { switch(typ) {
c639252014-11-20Stephen R. van den Berg  case INT8OID:value=cr->read_sint(8);
15d4e12014-11-18Stephen R. van den Berg  break;
c639252014-11-20Stephen R. van den Berg  case INT2OID:value=cr->read_sint(2);
15d4e12014-11-18Stephen R. van den Berg  break; case OIDOID:
c639252014-11-20Stephen R. van den Berg  case INT4OID:value=cr->read_sint(4);
15d4e12014-11-18Stephen R. van den Berg  } if(alltext) value=(string)value; } } a[i]=value; } else if(!collen) a[i]=""; } _processdataready(a); if(serror) error(serror); #ifdef PG_DEBUG return msglen; #endif
11b13b2014-08-16Martin Nilsson  }
ecbab12008-07-27Stephen R. van den Berg 
a2b20d2014-11-25Stephen R. van den Berg  final void _setrowdesc(array(mapping(string:mixed)) drowdesc, array(int) drowtypes) {
fb8eb42014-10-31Stephen R. van den Berg  Thread.MutexKey lock=_ddescribemux->lock();
a2b20d2014-11-25Stephen R. van den Berg  datarowdesc=drowdesc; datarowtypes=drowtypes;
fb8eb42014-10-31Stephen R. van den Berg  _ddescribe->broadcast(); lock=0; }
42fc8c2014-11-03Stephen R. van den Berg  final void _preparebind(array dtoid) {
fb8eb42014-10-31Stephen R. van den Berg  array(string|int) paramValues=_params?_params[2]:({}); if(sizeof(dtoid)!=sizeof(paramValues)) SUSERERROR("Invalid number of bindings, expected %d, got %d\n", sizeof(dtoid),sizeof(paramValues));
192acd2014-11-22Stephen R. van den Berg  Thread.MutexKey lock=_ddescribemux->lock();
2cdb902014-11-18Stephen R. van den Berg  if(!_portalname) { _portalname=(_unnamedportalkey=pgsqlsess._unnamedportalmux->trylock(1)) ? "" : PORTALPREFIX #ifdef PG_DEBUG +(string)(c->socket->query_fd())+"_" #endif +int2hex(pgsqlsess._pportalcount++); lock=0;
fb8eb42014-10-31Stephen R. van den Berg #ifdef PG_DEBUGMORE
2cdb902014-11-18Stephen R. van den Berg  PD("ParamValues to bind: %O\n",paramValues);
fb8eb42014-10-31Stephen R. van den Berg #endif
2cdb902014-11-18Stephen R. van den Berg  Stdio.Buffer plugbuffer=Stdio.Buffer();
78b2c52014-11-26Stephen R. van den Berg  { array dta=({sizeof(dtoid)}); plugbuffer->add(_portalname,0,_preparedname,0) ->add_ints(dta+map(dtoid,oidformat)+dta,2); }
2cdb902014-11-18Stephen R. van den Berg  string cenc=pgsqlsess._runtimeparameter[CLIENT_ENCODING]; foreach(paramValues;int i;mixed value) { if(undefinedp(value) || objectp(value)&&value->is_val_null) plugbuffer->add_int32(-1); // NULL else if(stringp(value) && !sizeof(value)) { int k=0; switch(dtoid[i]) { default: k=-1; // cast empty strings to NULL for non-string types case BYTEAOID: case TEXTOID: case XMLOID: case BPCHAROID: case VARCHAROID:; } plugbuffer->add_int32(k); } else switch(dtoid[i]) { case TEXTOID: case BPCHAROID: case VARCHAROID: { if(!value) { plugbuffer->add_int32(-1); break; } value=(string)value; switch(cenc) { case UTF8CHARSET: value=string_to_utf8(value); break; default: if(String.width(value)>8) { SUSERERROR("Don't know how to convert %O to %s encoding\n", value,cenc); value=""; } } plugbuffer->add_hstring(value,4);
fb8eb42014-10-31Stephen R. van den Berg  break; }
2cdb902014-11-18Stephen R. van den Berg  default: { if(!value) { plugbuffer->add_int32(-1);
fb8eb42014-10-31Stephen R. van den Berg  break;
2cdb902014-11-18Stephen R. van den Berg  }
d99e6e2017-07-10Stephen R. van den Berg  if (!objectp(value) || typeof(value) != stdiobuftype) /* * Like Oracle and SQLite, we accept literal binary values * from single-valued multisets. */ if (multisetp(value) && sizeof(value) == 1) value = indices(value)[0]; else { value = (string)value; if (String.width(value) > 8) if (dtoid[i] == BYTEAOID) /* * FIXME We should throw an error here, it would * have been correct, but for historical reasons and * as a DWIM convenience we autoconvert to UTF8 here. */ value = string_to_utf8(value); else { SUSERERROR( "Wide string %O not supported for type OID %d\n", value,dtoid[i]); value=""; } }
2cdb902014-11-18Stephen R. van den Berg  plugbuffer->add_hstring(value,4);
fb8eb42014-10-31Stephen R. van den Berg  break; }
f782e92014-11-25Stephen R. van den Berg  case BOOLOID:
2cdb902014-11-18Stephen R. van den Berg  do { int tval; if(stringp(value)) tval=value[0]; else if(!intp(value)) { value=!!value; // cast to boolean
fb8eb42014-10-31Stephen R. van den Berg  break;
2cdb902014-11-18Stephen R. van den Berg  } else tval=value; switch(tval) { case 'o':case 'O': catch { tval=value[1]; value=tval=='n'||tval=='N'; };
fb8eb42014-10-31Stephen R. van den Berg  break;
2cdb902014-11-18Stephen R. van den Berg  default: value=1; break; case 0:case '0':case 'f':case 'F':case 'n':case 'N': value=0; break; } } while(0);
f782e92014-11-25Stephen R. van den Berg  plugbuffer->add_int32(1)->add_int8(value);
2cdb902014-11-18Stephen R. van den Berg  break; case CHAROID: if(intp(value)) plugbuffer->add_hstring(value,4); else { value=(string)value; switch(sizeof(value)) { default: SUSERERROR( "\"char\" types must be 1 byte wide, got %O\n",value); case 0: plugbuffer->add_int32(-1); // NULL break; case 1: plugbuffer->add_hstring(value[0],4); }
fb8eb42014-10-31Stephen R. van den Berg  }
2cdb902014-11-18Stephen R. van den Berg  break; case INT8OID: plugbuffer->add_int32(8)->add_int((int)value,8); break; case OIDOID: case INT4OID: plugbuffer->add_int32(4)->add_int32((int)value); break; case INT2OID: plugbuffer->add_int32(2)->add_int16((int)value); break; } }
192acd2014-11-22Stephen R. van den Berg  if(!datarowtypes) { if(_tprepared && dontcacheprefix->match(_query))
2cdb902014-11-18Stephen R. van den Berg  m_delete(pgsqlsess->_prepareds,_query),_tprepared=0;
192acd2014-11-22Stephen R. van den Berg  waitfordescribe();
2cdb902014-11-18Stephen R. van den Berg  }
192acd2014-11-22Stephen R. van den Berg  if(_state>=CLOSING) lock=_unnamedstatementkey=0; else { plugbuffer->add_int16(sizeof(datarowtypes)); if(sizeof(datarowtypes))
a2b20d2014-11-25Stephen R. van den Berg  plugbuffer->add_ints(map(datarowtypes,oidformat),2);
a3a82e2017-06-17Stephen R. van den Berg  else if (syncparse < 0 && !pgsqlsess->_wasparallelisable
cfa2482017-06-18Stephen R. van den Berg  && pgsqlsess->_statementsinflight > 1) {
192acd2014-11-22Stephen R. van den Berg  lock=pgsqlsess->_shortmux->lock();
a3a82e2017-06-17Stephen R. van den Berg  // Decrement temporarily to account for ourselves
cfa2482017-06-18Stephen R. van den Berg  if(--pgsqlsess->_statementsinflight) {
192acd2014-11-22Stephen R. van den Berg  pgsqlsess->_waittocommit++;
cfa2482017-06-18Stephen R. van den Berg  PD("Commit waiting for statements to finish\n");
3a1ed32015-12-23Stephen R. van den Berg  catch(PT(pgsqlsess->_readyforcommit->wait(lock)));
192acd2014-11-22Stephen R. van den Berg  pgsqlsess->_waittocommit--; }
a3a82e2017-06-17Stephen R. van den Berg  // Increment again to account for ourselves
cfa2482017-06-18Stephen R. van den Berg  pgsqlsess->_statementsinflight++;
192acd2014-11-22Stephen R. van den Berg  } lock=0; PD("Bind portal %O statement %O\n",_portalname,_preparedname); _fetchlimit=pgsqlsess->_fetchlimit;
a3a82e2017-06-17Stephen R. van den Berg  _bindportal();
8675b02017-06-03Stephen R. van den Berg  conxsess bindbuffer = c->start();
192acd2014-11-22Stephen R. van den Berg  _unnamedstatementkey=0;
8675b02017-06-03Stephen R. van den Berg  CHAIN(bindbuffer)->add_int8('B')->add_hstring(plugbuffer, 4, 4);
192acd2014-11-22Stephen R. van den Berg  if(!_tprepared && sizeof(_preparedname))
8675b02017-06-03Stephen R. van den Berg  closestatement(CHAIN(bindbuffer), _preparedname);
192acd2014-11-22Stephen R. van den Berg  _sendexecute(_fetchlimit
bd47ec2017-06-25Stephen R. van den Berg  && !(transtype != NOTRANS
192acd2014-11-22Stephen R. van den Berg  || sizeof(_query)>=MINPREPARELENGTH && execfetchlimit->match(_query)) && _fetchlimit,bindbuffer); } } else lock=0;
fb8eb42014-10-31Stephen R. van den Berg  }
a2b20d2014-11-25Stephen R. van den Berg  final void _processrowdesc(array(mapping(string:mixed)) datarowdesc, array(int) datarowtypes) { _setrowdesc(datarowdesc,datarowtypes); if(_tprepared) {
bf20922014-11-17Stephen R. van den Berg  _tprepared.datarowdesc=datarowdesc;
a2b20d2014-11-25Stephen R. van den Berg  _tprepared.datarowtypes=datarowtypes; }
fb8eb42014-10-31Stephen R. van den Berg  }
a3a82e2017-06-17Stephen R. van den Berg  final void _parseportal() {
cfa2482017-06-18Stephen R. van den Berg  Thread.MutexKey lock = closemux->lock(); _state=PARSING; Thread.MutexKey lockc = pgsqlsess->_shortmux->lock();
a3a82e2017-06-17Stephen R. van den Berg  if (syncparse || syncparse < 0 && pgsqlsess->_wasparallelisable) {
cfa2482017-06-18Stephen R. van den Berg  if(pgsqlsess->_statementsinflight) {
a3a82e2017-06-17Stephen R. van den Berg  pgsqlsess->_waittocommit++;
cfa2482017-06-18Stephen R. van den Berg  PD("Commit waiting for statements to finish\n");
a3a82e2017-06-17Stephen R. van den Berg  // Do NOT put this in a function, it would require passing a lock // variable on the argumentstack to the function, which will cause // unpredictable lock release issues due to the extra copy on the stack
cfa2482017-06-18Stephen R. van den Berg  catch(PT(pgsqlsess->_readyforcommit->wait(lockc)));
a3a82e2017-06-17Stephen R. van den Berg  pgsqlsess->_waittocommit--; } }
cfa2482017-06-18Stephen R. van den Berg  pgsqlsess->_statementsinflight++; lockc = 0;
a3a82e2017-06-17Stephen R. van den Berg  lock=0; statuscmdcomplete=UNDEFINED; pgsqlsess->_wasparallelisable = paralleliseprefix->match(_query); }
cfa2482017-06-18Stephen R. van den Berg  final void _releasestatement(void|int nolock) { Thread.MutexKey lock; if (!nolock) lock = closemux->lock(); if (_state <= BOUND) { _state = COMMITTED; lock = pgsqlsess->_shortmux->lock(); if (!--pgsqlsess->_statementsinflight && pgsqlsess->_waittocommit) { PD("Signal no statements in flight\n"); catch(pgsqlsess->_readyforcommit->signal()); } } lock = 0; }
a3a82e2017-06-17Stephen R. van den Berg  final void _bindportal() {
cfa2482017-06-18Stephen R. van den Berg  Thread.MutexKey lock = closemux->lock();
0e7f332014-11-15Stephen R. van den Berg  _state=BOUND;
cfa2482017-06-18Stephen R. van den Berg  Thread.MutexKey lockc = pgsqlsess->_shortmux->lock(); pgsqlsess->_portalsinflight++; lockc = 0;
cff3a32014-09-12Stephen R. van den Berg  lock=0; }
fb8eb42014-10-31Stephen R. van den Berg  final void _purgeportal() {
a3a82e2017-06-17Stephen R. van den Berg  PD("Purge portal\n");
b4c7e92014-11-18Stephen R. van den Berg  datarows->write(1); // Signal EOF
fb8eb42014-10-31Stephen R. van den Berg  Thread.MutexKey lock=closemux->lock(); _fetchlimit=0; // disables further Executes switch(_state) {
0e7f332014-11-15Stephen R. van den Berg  case COPYINPROGRESS:
cfa2482017-06-18Stephen R. van den Berg  case COMMITTED:
0e7f332014-11-15Stephen R. van den Berg  case BOUND:
a362e32014-11-03Stephen R. van den Berg  --pgsqlsess->_portalsinflight;
fb8eb42014-10-31Stephen R. van den Berg  }
cfa2482017-06-18Stephen R. van den Berg  switch(_state) { case BOUND: case PARSING: --pgsqlsess->_statementsinflight; }
0e7f332014-11-15Stephen R. van den Berg  _state=CLOSED;
fb8eb42014-10-31Stephen R. van den Berg  lock=0;
e302b62014-11-14Stephen R. van den Berg  releaseconditions();
fb8eb42014-10-31Stephen R. van den Berg  }
8675b02017-06-03Stephen R. van den Berg  final int _closeportal(conxsess cs) { object plugbuffer = CHAIN(cs);
0e7f332014-11-15Stephen R. van den Berg  int retval=KEEP;
cff3a32014-09-12Stephen R. van den Berg  PD("%O Try Closeportal %d\n",_portalname,_state); Thread.MutexKey lock=closemux->lock(); _fetchlimit=0; // disables further Executes
cfa2482017-06-18Stephen R. van den Berg  switch(_state) { case PARSING: case BOUND: _releasestatement(1); }
cff3a32014-09-12Stephen R. van den Berg  switch(_state) {
0e7f332014-11-15Stephen R. van den Berg  case PORTALINIT:
cfa2482017-06-18Stephen R. van den Berg  case PARSING:
e302b62014-11-14Stephen R. van den Berg  _unnamedstatementkey=0;
b4c7e92014-11-18Stephen R. van den Berg  _state=CLOSING;
fb8eb42014-10-31Stephen R. van den Berg  break;
0e7f332014-11-15Stephen R. van den Berg  case COPYINPROGRESS:
fb8eb42014-10-31Stephen R. van den Berg  PD("CopyDone\n"); plugbuffer->add("c\0\0\0\4");
cfa2482017-06-18Stephen R. van den Berg  case COMMITTED:
0e7f332014-11-15Stephen R. van den Berg  case BOUND:
b4c7e92014-11-18Stephen R. van den Berg  _state=CLOSING;
cff3a32014-09-12Stephen R. van den Berg  lock=0; PD("Close portal %O\n",_portalname);
6172102017-06-21Stephen R. van den Berg  if (_portalname && sizeof(_portalname)) {
fb8eb42014-10-31Stephen R. van den Berg  plugbuffer->add_int8('C')->add_hstring(({'P',_portalname,0}),4,4);
0e7f332014-11-15Stephen R. van den Berg  retval=FLUSHSEND;
cff3a32014-09-12Stephen R. van den Berg  } else _unnamedportalkey=0;
4e0be82014-11-18Stephen R. van den Berg  Thread.MutexKey lockc=pgsqlsess->_shortmux->lock();
d743bf2014-11-13Stephen R. van den Berg  if(!--pgsqlsess->_portalsinflight) {
bd47ec2017-06-25Stephen R. van den Berg  if(!pgsqlsess->_waittocommit && !plugbuffer->stashcount && transtype != TRANSBEGIN)
bfa2552016-10-15Stephen R. van den Berg  /* * stashcount will be non-zero if a parse request has been queued * before the close was initiated. * It's a bit of a tricky race, but this check should be sufficient. */
0e7f332014-11-15Stephen R. van den Berg  pgsqlsess->_readyforquerycount++, retval=SYNCSEND;
a362e32014-11-03Stephen R. van den Berg  pgsqlsess->_pportalcount=0;
cff3a32014-09-12Stephen R. van den Berg  }
d743bf2014-11-13Stephen R. van den Berg  lockc=0;
cff3a32014-09-12Stephen R. van den Berg  } lock=0; return retval; }
15d4e12014-11-18Stephen R. van den Berg  final void _processdataready(array datarow,void|int msglen) { bytesreceived+=msglen;
bf20922014-11-17Stephen R. van den Berg  inflight--;
b4c7e92014-11-18Stephen R. van den Berg  if(_state<CLOSED) datarows->write(datarow);
15d4e12014-11-18Stephen R. van den Berg  if(++rowsreceived==1)
bf20922014-11-17Stephen R. van den Berg  PD("<%O _fetchlimit %d=min(%d||1,%d), inflight %d\n",_portalname,
15d4e12014-11-18Stephen R. van den Berg  _fetchlimit,(portalbuffersize>>1)*rowsreceived/bytesreceived,
bf20922014-11-17Stephen R. van den Berg  pgsqlsess._fetchlimit,inflight);
cff3a32014-09-12Stephen R. van den Berg  if(_fetchlimit) { _fetchlimit=
15d4e12014-11-18Stephen R. van den Berg  min((portalbuffersize>>1)*rowsreceived/bytesreceived||1,
a362e32014-11-03Stephen R. van den Berg  pgsqlsess._fetchlimit);
cff3a32014-09-12Stephen R. van den Berg  Thread.MutexKey lock=closemux->lock();
d899752014-11-22Stephen R. van den Berg  if(_fetchlimit && inflight<=(_fetchlimit-1)>>1)
cff3a32014-09-12Stephen R. van den Berg  _sendexecute(_fetchlimit); else if(!_fetchlimit)
bf20922014-11-17Stephen R. van den Berg  PD("<%O _fetchlimit %d, inflight %d, skip execute\n", _portalname,_fetchlimit,inflight);
cff3a32014-09-12Stephen R. van den Berg  lock=0;
ecbab12008-07-27Stephen R. van den Berg  }
cff3a32014-09-12Stephen R. van den Berg  }
e302b62014-11-14Stephen R. van den Berg  private void releaseconditions() {
3b899a2015-01-05Stephen R. van den Berg  _unnamedportalkey=_unnamedstatementkey=0;
e302b62014-11-14Stephen R. van den Berg  pgsqlsess=0;
192acd2014-11-22Stephen R. van den Berg  if(!datarowtypes) { Thread.MutexKey lock=_ddescribemux->lock();
a2b20d2014-11-25Stephen R. van den Berg  datarowtypes=emptyarray; datarowdesc=emptyarray;
5527d02014-11-17Stephen R. van den Berg  _ddescribe->broadcast();
192acd2014-11-22Stephen R. van den Berg  lock=0;
cbe52f2014-11-13Stephen R. van den Berg  }
ecbab12008-07-27Stephen R. van den Berg  }
bf20922014-11-17Stephen R. van den Berg  final void _releasesession(void|string statusccomplete) {
3a1ed32015-12-23Stephen R. van den Berg  c->closecallbacks-=(<destroy>);
bf20922014-11-17Stephen R. van den Berg  if(statusccomplete && !statuscmdcomplete) statuscmdcomplete=statusccomplete; inflight=0;
8675b02017-06-03Stephen R. van den Berg  conxsess plugbuffer; if (!catch(plugbuffer = c->start()))
3a1ed32015-12-23Stephen R. van den Berg  plugbuffer->sendcmd(_closeportal(plugbuffer));
b4c7e92014-11-18Stephen R. van den Berg  _state=CLOSED;
49e3302017-11-09Stephen R. van den Berg  datarows->write(1); // Signal EOF
e302b62014-11-14Stephen R. van den Berg  releaseconditions(); }
cff3a32014-09-12Stephen R. van den Berg  protected void destroy() { catch { // inside destructors, exceptions don't work _releasesession();
11b13b2014-08-16Martin Nilsson  }; }
ecbab12008-07-27Stephen R. van den Berg 
8675b02017-06-03Stephen R. van den Berg  final void _sendexecute(int fetchlimit,void|bufcon|conxsess plugbuffer) {
cff3a32014-09-12Stephen R. van den Berg  int flushmode;
1c847c2017-06-27Stephen R. van den Berg  PD("Execute portal %O fetchlimit %d transtype %d\n", _portalname, fetchlimit, transtype);
cff3a32014-09-12Stephen R. van den Berg  if(!plugbuffer) plugbuffer=c->start(1);
8675b02017-06-03Stephen R. van den Berg  CHAIN(plugbuffer)->add_int8('E')->add_hstring(({_portalname,0}), 4, 8)
78b2c52014-11-26Stephen R. van den Berg  ->add_int32(fetchlimit);
bd47ec2017-06-25Stephen R. van den Berg  if (!fetchlimit) { if (transtype != NOTRANS) pgsqlsess._intransaction = transtype == TRANSBEGIN; flushmode = _closeportal(plugbuffer) == SYNCSEND || transtype == TRANSEND ? SYNCSEND : FLUSHSEND; } else
bf20922014-11-17Stephen R. van den Berg  inflight+=fetchlimit, flushmode=FLUSHSEND;
cff3a32014-09-12Stephen R. van den Berg  plugbuffer->sendcmd(flushmode,this);
11b13b2014-08-16Martin Nilsson  }
ecbab12008-07-27Stephen R. van den Berg 
11b13b2014-08-16Martin Nilsson  //! @returns //! One result row at a time. //! //! When using COPY FROM STDOUT, this method returns one row at a time //! as a single string containing the entire row. //!
cff3a32014-09-12Stephen R. van den Berg  //! @seealso //! @[eof()], @[send_row()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final array(mixed) fetch_row() {
cff3a32014-09-12Stephen R. van den Berg  int|array datarow;
bf20922014-11-17Stephen R. van den Berg  if(arrayp(datarow=datarows->try_read()))
cff3a32014-09-12Stephen R. van den Berg  return datarow;
fb8eb42014-10-31Stephen R. van den Berg  if(!eoffound) {
1611e42014-11-28Stephen R. van den Berg  if(!datarow) { PD("%O Block for datarow\n",_portalname); array cid=callout(gottimeout,timeout);
abf9442014-12-01Stephen R. van den Berg  PT(datarow=datarows->read());
1611e42014-11-28Stephen R. van den Berg  local_backend->remove_call_out(cid); if(arrayp(datarow)) return datarow; }
fb8eb42014-10-31Stephen R. van den Berg  eoffound=1;
bf20922014-11-17Stephen R. van den Berg  datarows->write(1); // Signal EOF for other threads
fb8eb42014-10-31Stephen R. van den Berg  }
cff3a32014-09-12Stephen R. van den Berg  trydelayederror(); return 0; } //! @returns //! Multiple result rows at a time (at least one). //! //! When using COPY FROM STDOUT, this method returns one row at a time //! as a single string containing the entire row. //! //! @seealso //! @[eof()], @[fetch_row()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final array(array(mixed)) fetch_row_array() {
cff3a32014-09-12Stephen R. van den Berg  if(eoffound) return 0;
bf20922014-11-17Stephen R. van den Berg  array(array|int) datarow=datarows->try_read_array();
1611e42014-11-28Stephen R. van den Berg  if(!datarow) { array cid=callout(gottimeout,timeout);
abf9442014-12-01Stephen R. van den Berg  PT(datarow=datarows->read_array());
1611e42014-11-28Stephen R. van den Berg  local_backend->remove_call_out(cid); }
cff3a32014-09-12Stephen R. van den Berg  if(arrayp(datarow[-1])) return datarow; trydelayederror(); eoffound=1;
bf20922014-11-17Stephen R. van den Berg  datarows->write(1); // Signal EOF for other threads
cff3a32014-09-12Stephen R. van den Berg  return (datarow=datarow[..<1]); }
933a762014-11-10Stephen R. van den Berg  //! @param copydata
11b13b2014-08-16Martin Nilsson  //! When using COPY FROM STDIN, this method accepts a string or an //! array of strings to be processed by the COPY command; when sending //! the amount of data sent per call does not have to hit row or column //! boundaries. //! //! The COPY FROM STDIN sequence needs to be completed by either
cff3a32014-09-12Stephen R. van den Berg  //! explicitly or implicitly destroying the result object, or by passing no //! argument to this method.
11b13b2014-08-16Martin Nilsson  //! //! @seealso
cff3a32014-09-12Stephen R. van den Berg  //! @[fetch_row()], @[eof()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final void send_row(void|string|array(string) copydata) {
cff3a32014-09-12Stephen R. van den Berg  trydelayederror(); if(copydata) { PD("CopyData\n");
8675b02017-06-03Stephen R. van den Berg  object cs = c->start(); CHAIN(cs)->add_int8('d')->add_hstring(copydata, 4, 4); cs->sendcmd(SENDOUT);
cff3a32014-09-12Stephen R. van den Berg  } else _releasesession(); }
6eb5942014-11-12Stephen R. van den Berg  private void run_result_cb(
3b31fa2014-11-14Stephen R. van den Berg  function(sql_result, array(mixed), mixed ...:void) callback,
cff3a32014-09-12Stephen R. van den Berg  array(mixed) args) { int|array datarow;
1611e42014-11-28Stephen R. van den Berg  for(;;) { array cid=callout(gottimeout,timeout);
abf9442014-12-01Stephen R. van den Berg  PT(datarow=datarows->read());
1611e42014-11-28Stephen R. van den Berg  local_backend->remove_call_out(cid); if(!arrayp(datarow)) break;
0fed8c2014-11-10Stephen R. van den Berg  callout(callback, 0, this, datarow, @args);
1611e42014-11-28Stephen R. van den Berg  }
cff3a32014-09-12Stephen R. van den Berg  trydelayederror(); eoffound=1;
0fed8c2014-11-10Stephen R. van den Berg  callout(callback, 0, this, 0, @args);
cff3a32014-09-12Stephen R. van den Berg  } //! Sets up a callback for every row returned from the database. //! First argument passed is the resultobject itself, second argument //! is the result row (zero on EOF). //! //! @seealso //! @[fetch_row()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final void set_result_callback(
3b31fa2014-11-14Stephen R. van den Berg  function(sql_result, array(mixed), mixed ...:void) callback,
cff3a32014-09-12Stephen R. van den Berg  mixed ... args) { if(callback) Thread.Thread(run_result_cb,callback,args); }
6eb5942014-11-12Stephen R. van den Berg  private void run_result_array_cb(
3b31fa2014-11-14Stephen R. van den Berg  function(sql_result, array(array(mixed)), mixed ...:void) callback,
cff3a32014-09-12Stephen R. van den Berg  array(mixed) args) { array(array|int) datarow;
1611e42014-11-28Stephen R. van den Berg  for(;;) { array cid=callout(gottimeout,timeout);
abf9442014-12-01Stephen R. van den Berg  PT(datarow=datarows->read_array());
1611e42014-11-28Stephen R. van den Berg  local_backend->remove_call_out(cid); if(!datarow || !arrayp(datarow[-1])) break;
0fed8c2014-11-10Stephen R. van den Berg  callout(callback, 0, this, datarow, @args);
1611e42014-11-28Stephen R. van den Berg  }
cff3a32014-09-12Stephen R. van den Berg  trydelayederror(); eoffound=1; if(sizeof(datarow)>1)
0fed8c2014-11-10Stephen R. van den Berg  callout(callback, 0, this, datarow=datarow[..<1], @args); callout(callback, 0, this, 0, @args);
cff3a32014-09-12Stephen R. van den Berg  } //! Sets up a callback for sets of rows returned from the database. //! First argument passed is the resultobject itself, second argument //! is the array of result rows (zero on EOF). //! //! @seealso //! @[fetch_row()]
74abb02014-11-24Stephen R. van den Berg  /*semi*/final void set_result_array_callback(
3b31fa2014-11-14Stephen R. van den Berg  function(sql_result, array(array(mixed)), mixed ...:void) callback,
cff3a32014-09-12Stephen R. van den Berg  mixed ... args) { if(callback) Thread.Thread(run_result_array_cb,callback,args);
11b13b2014-08-16Martin Nilsson  }
ecbab12008-07-27Stephen R. van den Berg 
74abb02014-11-24Stephen R. van den Berg };