SQLite Forum

PSTOKEN extension
Login

PSTOKEN extension

(1) By anonymous on 2020-07-14 03:18:54 [source]

Here is a extension code for writing PostScript tokens as output. You might find this useful if you want to do graphics with the SQL data, possibly.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3ext.h"

static const sqlite3_api_routines*sqlite3_api;

static void fn_pstoken(sqlite3_context*cxt,int argc,sqlite3_value**argv) {
  sqlite3_str*str=sqlite3_str_new(sqlite3_context_db_handle(cxt));
  const unsigned char*c;
  int i,j,n;
  sqlite3_str_appendchar(str,1,'{');
  for(i=0;i<argc;i++) switch(sqlite3_value_type(argv[i])) {
    case SQLITE_NULL:
      sqlite3_str_append(str," //null",7);
      break;
    case SQLITE_FLOAT:
      sqlite3_str_appendf(str," %!f",sqlite3_value_double(argv[i]));
      break;
    case SQLITE_INTEGER:
      sqlite3_str_appendf(str," %lld",sqlite3_value_int64(argv[i]));
      break;
    case SQLITE_TEXT:
      c=sqlite3_value_text(argv[i]);
      n=sqlite3_value_bytes(argv[i]);
      sqlite3_str_append(str," (",2);
      for(j=0;j<n;j++) {
        if(c[j]=='(' || c[j]==')' || c[j]=='\\') sqlite3_str_appendchar(str,1,'\\');
        if(c[j]>=32 && c[j]<127) sqlite3_str_appendchar(str,1,c[j]); else sqlite3_str_appendf(str,"\\%03o",c[j]);
      }
      sqlite3_str_appendchar(str,1,')');
      break;
    case SQLITE_BLOB:
      c=sqlite3_value_blob(argv[i]);
      n=sqlite3_value_bytes(argv[i]);
      sqlite3_str_append(str," <",2);
      for(j=0;j<n;j++) sqlite3_str_appendf(str,"%02X",c[j]);
      sqlite3_str_appendchar(str,1,'>');
      break;
  }
  sqlite3_str_append(str," }",2);
  if(i=sqlite3_str_errcode(str)) {
    sqlite3_free(sqlite3_str_finish(str));
    if(i==SQLITE_TOOBIG) sqlite3_result_error_toobig(cxt);
    if(i==SQLITE_NOMEM) sqlite3_result_error_nomem(cxt);
  } else {
    sqlite3_result_text(cxt,sqlite3_str_finish(str),-1,sqlite3_free);
  }
}

int sqlite3_extension_init(sqlite3*db,const char**err,const struct sqlite3_api_routines*api) {
  sqlite3_api=api;
  sqlite3_create_function(db,"PSTOKEN",-1,SQLITE_UTF8|SQLITE_DETERMINISTIC,0,fn_pstoken,0,0);
  return SQLITE_OK;
}

(2) By anonymous on 2020-07-16 02:33:05 in reply to 1 [link] [source]

The command shell has many other formats (including CSV, JSON, etc), so why not PostScript? (Of course, PostScript is a full programming language like others, so just as well as you can parse JSON in programming languages other than JavaScript, so can you also parse CSV or JSON or the SQL quoted output in PostScript.) It seem that PostScript would be useful, since you might want to use it to draw pie charts and stuff like that. (Note that Ghostscript supports 64-bit integers as an extension. JSON does not support 64-bit integers, although modern versions of JavaScript do support arbitrarily long integers.) Also, the documentation for the output formats in the command line shell could be improved; some of them are not explained. The code I posted here is public domain. Also, it should be fixed so that you can change the separator in the "line" mode; some uses would want a colon instead of a equal sign.