Compare commits
16 Commits
wasm-post-3.51
...
resfmt
| Author | SHA1 | Date | |
|---|---|---|---|
| 21769e1d85 | |||
| 22f8a84fda | |||
| ce3d809d62 | |||
| dcd722d204 | |||
| 15a064807f | |||
| 8e544ca512 | |||
| 8c8bd1969c | |||
| 861cb4c0b4 | |||
| 6e334ef616 | |||
| a1f32824e2 | |||
| 5b5b35f6d6 | |||
| a0b13abd34 | |||
| 377c4a8bfe | |||
| 7ebd8a8d85 | |||
| f39a16c595 | |||
| c02f8fdfe2 |
@@ -0,0 +1,325 @@
|
||||
/*
|
||||
** 2025-10-20
|
||||
**
|
||||
** The author disclaims copyright to this source code. In place of
|
||||
** a legal notice, here is a blessing:
|
||||
**
|
||||
** May you do good and not evil.
|
||||
** May you find forgiveness for yourself and forgive others.
|
||||
** May you share freely, never taking more than you give.
|
||||
**
|
||||
*************************************************************************
|
||||
** A simple command-line program for testing the Result-Format or "resfmt"
|
||||
** utility library for SQLite.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "sqlite3.h"
|
||||
#include "resfmt.h"
|
||||
|
||||
#define COUNT(X) (sizeof(X)/sizeof(X[0]))
|
||||
|
||||
/* Report out-of-memory and die if the argument is NULL */
|
||||
static void checkOOM(void *p){
|
||||
if( p==0 ){
|
||||
fprintf(stdout, "out-of-memory\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* A block of memory */
|
||||
typedef struct memblock memblock;
|
||||
struct memblock {
|
||||
memblock *pNext;
|
||||
};
|
||||
|
||||
/* List of all memory to be freed */
|
||||
static memblock *pToFree = 0;
|
||||
|
||||
/* Free all memory at exit */
|
||||
static void tempFreeAll(void){
|
||||
while( pToFree ){
|
||||
memblock *pNext = pToFree->pNext;
|
||||
sqlite3_free(pToFree);
|
||||
pToFree = pNext;
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate memory that will be freed all at once by freeall() */
|
||||
static void *tempMalloc(size_t n){
|
||||
memblock *p;
|
||||
if( n>0x10000000 ) checkOOM(0);
|
||||
p = sqlite3_malloc64( n+sizeof(memblock) );
|
||||
checkOOM(p);
|
||||
p->pNext = pToFree;
|
||||
pToFree = p;
|
||||
return (void*)&pToFree[1];
|
||||
}
|
||||
|
||||
/* Make a copy of a string using tempMalloc() */
|
||||
static char *tempStrdup(char *zIn){
|
||||
size_t n;
|
||||
char *z;
|
||||
if( zIn==0 ) zIn = "";
|
||||
n = strlen(zIn);
|
||||
if( n>0x10000000 ) checkOOM(0);
|
||||
z = tempMalloc( n+1 );
|
||||
checkOOM(z);
|
||||
memcpy(z, zIn, n+1);
|
||||
return z;
|
||||
}
|
||||
|
||||
/* Function used for writing to the console */
|
||||
static ssize_t testWriter(void *pContext, const unsigned char *p, size_t n){
|
||||
return fwrite(p,1,n,stdout);
|
||||
}
|
||||
|
||||
/* Render BLOB values as "(%d-byte-blob)". */
|
||||
static char *testBlobRender(void *pNotUsed, sqlite3_value *pVal){
|
||||
if( sqlite3_value_type(pVal)!=SQLITE_BLOB ) return 0;
|
||||
return sqlite3_mprintf("(%d-byte-blob)",sqlite3_value_bytes(pVal));
|
||||
}
|
||||
|
||||
int main(int argc, char **argv){
|
||||
char *zSrc;
|
||||
FILE *pSrc;
|
||||
sqlite3_str *pBuf;
|
||||
sqlite3 *db = 0;
|
||||
sqlite3_stmt *pStmt;
|
||||
int rc;
|
||||
int lineNum = 0;
|
||||
int bUseWriter = 1;
|
||||
sqlite3_resfmt_spec spec;
|
||||
char zLine[1000];
|
||||
|
||||
if( argc<2 ){
|
||||
zSrc = "<stdin>";
|
||||
pSrc = stdin;
|
||||
}else{
|
||||
zSrc = argv[1];
|
||||
pSrc = fopen(zSrc, "rb");
|
||||
if( pSrc==0 ){
|
||||
fprintf(stderr, "cannot open \"%s\" for reading\n", zSrc);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
memset(&spec, 0, sizeof(spec));
|
||||
spec.iVersion = 1;
|
||||
spec.eFormat = RESFMT_List;
|
||||
spec.xWrite = testWriter;
|
||||
pBuf = sqlite3_str_new(0);
|
||||
rc = sqlite3_open(":memory:", &db);
|
||||
if( rc ){
|
||||
fprintf(stderr, "unable to open an in-memory database: %s\n",
|
||||
sqlite3_errmsg(db));
|
||||
exit(1);
|
||||
}
|
||||
while( fgets(zLine, sizeof(zLine), pSrc) ){
|
||||
size_t n = strlen(zLine);
|
||||
lineNum++;
|
||||
while( n>0 && zLine[n-1]>0 && zLine[n-1]<=' ' ) n--;
|
||||
zLine[n] = 0;
|
||||
printf("%s\n", zLine);
|
||||
if( strncmp(zLine, "--open=", 7)==0 ){
|
||||
if( db ) sqlite3_close(db);
|
||||
db = 0;
|
||||
rc = sqlite3_open(&zLine[7], &db);
|
||||
if( rc!=SQLITE_OK ){
|
||||
fprintf(stderr, "%s:%d: cannot open \"%s\": %s\n",
|
||||
zSrc, lineNum, &zLine[7],
|
||||
sqlite3_errmsg(db));
|
||||
exit(1);
|
||||
}
|
||||
}else
|
||||
if( strcmp(zLine, "--go")==0 ){
|
||||
const char *zSql, *zTail;
|
||||
char *zErr = 0;
|
||||
int n;
|
||||
if( db==0 ){
|
||||
fprintf(stderr, "%s:%d: database not open\n", zSrc, lineNum);
|
||||
exit(1);
|
||||
}
|
||||
zSql = sqlite3_str_value(pBuf);
|
||||
pStmt = 0;
|
||||
while( zSql[0] ){
|
||||
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zTail);
|
||||
if( rc || pStmt==0 ){
|
||||
fprintf(stderr, "%s:%d: sqlite3_prepare() fails: %s\n",
|
||||
zSrc, lineNum, sqlite3_errmsg(db));
|
||||
sqlite3_finalize(pStmt);
|
||||
break;
|
||||
}
|
||||
zSql = sqlite3_sql(pStmt);
|
||||
while( isspace(zSql[0]) ) zSql++;
|
||||
n = (int)strlen(zSql);
|
||||
while( n>0 && isspace(zSql[n-1]) ) n--;
|
||||
if( n>0 ){
|
||||
char *zOut = 0;
|
||||
printf("/* %.*s */\n", n, zSql);
|
||||
if( bUseWriter ){
|
||||
spec.pzOutput = 0;
|
||||
spec.xWrite = testWriter;
|
||||
}else{
|
||||
spec.pzOutput = &zOut;
|
||||
spec.xWrite = 0;
|
||||
}
|
||||
rc = sqlite3_format_query_result(pStmt, &spec, &zErr);
|
||||
if( rc!=SQLITE_OK ){
|
||||
fprintf(stderr, "%s:%d: Error %d: %s\n", zSrc, lineNum,
|
||||
rc, zErr);
|
||||
}else{
|
||||
if( !bUseWriter && zOut ){
|
||||
fputs(zOut, stdout);
|
||||
sqlite3_free(zOut);
|
||||
}
|
||||
}
|
||||
sqlite3_free(zErr);
|
||||
}
|
||||
sqlite3_finalize(pStmt);
|
||||
pStmt = 0;
|
||||
zSql = zTail;
|
||||
}
|
||||
sqlite3_str_reset(pBuf);
|
||||
}else
|
||||
if( strncmp(zLine, "--eFormat=", 10)==0 ){
|
||||
const struct { const char *z; int e; } aFmt[] = {
|
||||
{ "box", RESFMT_Box, },
|
||||
{ "column", RESFMT_Column, },
|
||||
{ "count", RESFMT_Count, },
|
||||
{ "eqp", RESFMT_EQP, },
|
||||
{ "explain", RESFMT_Explain, },
|
||||
{ "html", RESFMT_Html, },
|
||||
{ "insert", RESFMT_Insert, },
|
||||
{ "json", RESFMT_Json, },
|
||||
{ "line", RESFMT_Line, },
|
||||
{ "list", RESFMT_List, },
|
||||
{ "markdown", RESFMT_Markdown, },
|
||||
{ "off", RESFMT_Off, },
|
||||
{ "table", RESFMT_Table, },
|
||||
{ "scanexp", RESFMT_ScanExp, },
|
||||
};
|
||||
int i;
|
||||
for(i=0; i<COUNT(aFmt); i++){
|
||||
if( strcmp(aFmt[i].z,&zLine[10])==0 ){
|
||||
spec.eFormat = aFmt[i].e;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( i>=COUNT(aFmt) ){
|
||||
sqlite3_str *pMsg = sqlite3_str_new(0);
|
||||
for(i=0; i<COUNT(aFmt); i++){
|
||||
sqlite3_str_appendf(pMsg, " %s", aFmt[i].z);
|
||||
}
|
||||
fprintf(stderr, "%s:%d: no such format: \"%s\"\nChoices: %s\n",
|
||||
zSrc, lineNum, &zLine[10], sqlite3_str_value(pMsg));
|
||||
sqlite3_free(sqlite3_str_finish(pMsg));
|
||||
}
|
||||
}else
|
||||
if( strncmp(zLine, "--eQuote=", 9)==0 ){
|
||||
const struct { const char *z; int e; } aQuote[] = {
|
||||
{ "csv", RESFMT_Q_Csv },
|
||||
{ "html", RESFMT_Q_Html },
|
||||
{ "json", RESFMT_Q_Json },
|
||||
{ "off", RESFMT_Q_Off },
|
||||
{ "sql", RESFMT_Q_Sql },
|
||||
{ "tcl", RESFMT_Q_Tcl },
|
||||
};
|
||||
int i;
|
||||
for(i=0; i<COUNT(aQuote); i++){
|
||||
if( strcmp(aQuote[i].z,&zLine[9])==0 ){
|
||||
spec.eQuote = aQuote[i].e;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( i>=COUNT(aQuote) ){
|
||||
sqlite3_str *pMsg = sqlite3_str_new(0);
|
||||
for(i=0; i<COUNT(aQuote); i++){
|
||||
sqlite3_str_appendf(pMsg, " %s", aQuote[i].z);
|
||||
}
|
||||
fprintf(stderr, "%s:%d: no such quoting style: \"%s\"\nChoices: %s\n",
|
||||
zSrc, lineNum, &zLine[9], sqlite3_str_value(pMsg));
|
||||
sqlite3_free(sqlite3_str_finish(pMsg));
|
||||
}
|
||||
}else
|
||||
if( strncmp(zLine, "--eBlob=", 8)==0 ){
|
||||
const struct { const char *z; int e; } aBlob[] = {
|
||||
{ "auto", RESFMT_B_Auto },
|
||||
{ "hex", RESFMT_B_Hex },
|
||||
{ "json", RESFMT_B_Json },
|
||||
{ "tcl", RESFMT_B_Tcl },
|
||||
{ "text", RESFMT_B_Text },
|
||||
{ "sql", RESFMT_B_Sql },
|
||||
};
|
||||
int i;
|
||||
for(i=0; i<COUNT(aBlob); i++){
|
||||
if( strcmp(aBlob[i].z,&zLine[8])==0 ){
|
||||
spec.eBlob = aBlob[i].e;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( i>=COUNT(aBlob) ){
|
||||
sqlite3_str *pMsg = sqlite3_str_new(0);
|
||||
for(i=0; i<COUNT(aBlob); i++){
|
||||
sqlite3_str_appendf(pMsg, " %s", aBlob[i].z);
|
||||
}
|
||||
fprintf(stderr, "%s:%d: no such blob style: \"%s\"\nChoices: %s\n",
|
||||
zSrc, lineNum, &zLine[8], sqlite3_str_value(pMsg));
|
||||
sqlite3_free(sqlite3_str_finish(pMsg));
|
||||
}
|
||||
}else
|
||||
if( strncmp(zLine, "--eEscape=", 10)==0 ){
|
||||
const struct { const char *z; int e; } aEscape[] = {
|
||||
{ "ascii", RESFMT_E_Ascii },
|
||||
{ "off", RESFMT_E_Off },
|
||||
{ "symbol", RESFMT_E_Symbol },
|
||||
};
|
||||
int i;
|
||||
for(i=0; i<COUNT(aEscape); i++){
|
||||
if( strcmp(aEscape[i].z,&zLine[10])==0 ){
|
||||
spec.eEscape = aEscape[i].e;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( i>=COUNT(aEscape) ){
|
||||
sqlite3_str *pMsg = sqlite3_str_new(0);
|
||||
for(i=0; i<COUNT(aEscape); i++){
|
||||
sqlite3_str_appendf(pMsg, " %s", aEscape[i].z);
|
||||
}
|
||||
fprintf(stderr, "%s:%d: no such escape mode: \"%s\"\nChoices: %s\n",
|
||||
zSrc, lineNum, &zLine[10], sqlite3_str_value(pMsg));
|
||||
sqlite3_free(sqlite3_str_finish(pMsg));
|
||||
}
|
||||
}else
|
||||
if( strncmp(zLine, "--bShowCNames=", 14)==0 ){
|
||||
spec.bShowCNames = atoi(&zLine[14])!=0;
|
||||
}else
|
||||
if( strncmp(zLine, "--zNull=", 8)==0 ){
|
||||
spec.zNull = tempStrdup(&zLine[8]);
|
||||
}else
|
||||
if( strncmp(zLine, "--zColumnSep=", 13)==0 ){
|
||||
spec.zColumnSep = tempStrdup(&zLine[13]);
|
||||
}else
|
||||
if( strncmp(zLine, "--zRowSep=", 10)==0 ){
|
||||
spec.zRowSep = tempStrdup(&zLine[10]);
|
||||
}else
|
||||
if( strcmp(zLine, "--exit")==0 ){
|
||||
break;
|
||||
}else
|
||||
if( strncmp(zLine, "--use-writer=",13)==0 ){
|
||||
bUseWriter = atoi(&zLine[13])!=0;
|
||||
}else
|
||||
if( strncmp(zLine, "--use-render=",13)==0 ){
|
||||
spec.xRender = (atoi(&zLine[13])!=0) ? testBlobRender : 0;
|
||||
}else
|
||||
{
|
||||
if( sqlite3_str_length(pBuf) ) sqlite3_str_append(pBuf, "\n", 1);
|
||||
sqlite3_str_appendall(pBuf, zLine);
|
||||
}
|
||||
}
|
||||
if( db ) sqlite3_close(db);
|
||||
sqlite3_free(sqlite3_str_finish(pBuf));
|
||||
tempFreeAll();
|
||||
if( pSrc!=stdin ) fclose(pSrc);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
/*
|
||||
** 2025-10-20
|
||||
**
|
||||
** The author disclaims copyright to this source code. In place of
|
||||
** a legal notice, here is a blessing:
|
||||
**
|
||||
** May you do good and not evil.
|
||||
** May you find forgiveness for yourself and forgive others.
|
||||
** May you share freely, never taking more than you give.
|
||||
**
|
||||
*************************************************************************
|
||||
** Implementation of the Result-Format or "resfmt" utility library for SQLite.
|
||||
** See the resfmt.md documentation for additional information.
|
||||
*/
|
||||
#include "resfmt.h"
|
||||
#include <string.h>
|
||||
|
||||
/*
|
||||
** Private state information. Subject to change from one release to the
|
||||
** next.
|
||||
*/
|
||||
typedef struct sqlite3_resfmt sqlite3_resfmt;
|
||||
struct sqlite3_resfmt {
|
||||
sqlite3_stmt *pStmt; /* The statement whose output is to be rendered */
|
||||
sqlite3 *db; /* The corresponding database connection */
|
||||
char **pzErr; /* Write error message here, if not NULL */
|
||||
sqlite3_str *pOut; /* Accumulated output */
|
||||
int iErr; /* Error code */
|
||||
int nCol; /* Number of output columns */
|
||||
sqlite3_int64 nRow; /* Number of rows handled so far */
|
||||
sqlite3_resfmt_spec spec; /* Copy of the original spec */
|
||||
};
|
||||
|
||||
/*
|
||||
** Set an error code and error message.
|
||||
*/
|
||||
static void resfmtError(
|
||||
sqlite3_resfmt *p, /* Query result state */
|
||||
int iCode, /* Error code */
|
||||
const char *zFormat, /* Message format (or NULL) */
|
||||
...
|
||||
){
|
||||
p->iErr = iCode;
|
||||
if( p->pzErr!=0 ){
|
||||
sqlite3_free(*p->pzErr);
|
||||
*p->pzErr = 0;
|
||||
if( zFormat ){
|
||||
va_list ap;
|
||||
va_start(ap, zFormat);
|
||||
*p->pzErr = sqlite3_vmprintf(zFormat, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Out-of-memory error.
|
||||
*/
|
||||
static void resfmtOom(sqlite3_resfmt *p){
|
||||
resfmtError(p, SQLITE_NOMEM, "out of memory");
|
||||
}
|
||||
|
||||
/*
|
||||
** If xWrite is defined, send all content of pOut to xWrite and
|
||||
** reset pOut.
|
||||
*/
|
||||
static void resfmtWrite(sqlite3_resfmt *p){
|
||||
int n;
|
||||
if( p->spec.xWrite && (n = sqlite3_str_length(p->pOut))>0 ){
|
||||
p->spec.xWrite(p->spec.pWriteArg,
|
||||
(const unsigned char*)sqlite3_str_value(p->pOut),
|
||||
(size_t)n);
|
||||
sqlite3_str_reset(p->pOut);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Escape the input string if it is needed and in accordance with
|
||||
** eEscape, which is either RESFMT_E_Ascii or RESFMT_E_Symbol.
|
||||
**
|
||||
** Escaping is needed if the string contains any control characters
|
||||
** other than \t, \n, and \r\n
|
||||
**
|
||||
** If no escaping is needed (the common case) then set *ppOut to NULL
|
||||
** and return 0. If escaping is needed, write the escaped string into
|
||||
** memory obtained from sqlite3_malloc64() and make *ppOut point to that
|
||||
** memory and return 0. If an error occurs, return non-zero.
|
||||
**
|
||||
** The caller is responsible for freeing *ppFree if it is non-NULL in order
|
||||
** to reclaim memory.
|
||||
*/
|
||||
static void resfmtEscape(
|
||||
int eEscape, /* RESFMT_E_Ascii or RESFMT_E_Symbol */
|
||||
sqlite3_str *pStr, /* String to be escaped */
|
||||
int iStart /* Begin escapding on this byte of pStr */
|
||||
){
|
||||
sqlite3_int64 i, j; /* Loop counters */
|
||||
sqlite3_int64 sz; /* Size of the string prior to escaping */
|
||||
sqlite3_int64 nCtrl = 0;/* Number of control characters to escape */
|
||||
unsigned char *zIn; /* Text to be escaped */
|
||||
unsigned char c; /* A single character of the text */
|
||||
unsigned char *zOut; /* Where to write the results */
|
||||
|
||||
/* Find the text to be escaped */
|
||||
zIn = (unsigned char*)sqlite3_str_value(pStr);
|
||||
if( zIn==0 ) return;
|
||||
zIn += iStart;
|
||||
|
||||
/* Count the control characters */
|
||||
for(i=0; (c = zIn[i])!=0; i++){
|
||||
if( c<=0x1f
|
||||
&& c!='\t'
|
||||
&& c!='\n'
|
||||
&& (c!='\r' || zIn[i+1]!='\n')
|
||||
){
|
||||
nCtrl++;
|
||||
}
|
||||
}
|
||||
if( nCtrl==0 ) return; /* Early out if no control characters */
|
||||
|
||||
/* Make space to hold the escapes. Copy the original text to the end
|
||||
** of the available space. */
|
||||
sz = sqlite3_str_length(pStr) - iStart;
|
||||
if( eEscape==RESFMT_E_Symbol ) nCtrl *= 2;
|
||||
sqlite3_str_appendchar(pStr, nCtrl, ' ');
|
||||
zOut = (unsigned char*)sqlite3_str_value(pStr);
|
||||
if( zOut==0 ) return;
|
||||
zOut += iStart;
|
||||
zIn = zOut + nCtrl;
|
||||
memmove(zIn,zOut,sz);
|
||||
|
||||
/* Convert the control characters */
|
||||
for(i=j=0; (c = zIn[i])!=0; i++){
|
||||
if( c>0x1f
|
||||
|| c=='\t'
|
||||
|| c=='\n'
|
||||
|| (c=='\r' && zIn[i+1]=='\n')
|
||||
){
|
||||
continue;
|
||||
}
|
||||
if( i>0 ){
|
||||
memmove(&zOut[j], zIn, i);
|
||||
j += i;
|
||||
}
|
||||
zIn += i+1;
|
||||
i = -1;
|
||||
if( eEscape==RESFMT_E_Symbol ){
|
||||
zOut[j++] = 0xe2;
|
||||
zOut[j++] = 0x90;
|
||||
zOut[j++] = 0x80+c;
|
||||
}else{
|
||||
zOut[j++] = '^';
|
||||
zOut[j++] = 0x40+c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** If a field contains any character identified by a 1 in the following
|
||||
** array, then the string must be quoted for CSV.
|
||||
*/
|
||||
static const char resfmtCsvQuote[] = {
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
};
|
||||
|
||||
/*
|
||||
** Encode text appropriately and append it to p->pOut.
|
||||
*/
|
||||
static void resfmtEncodeText(sqlite3_resfmt *p, const char *zTxt){
|
||||
int iStart = sqlite3_str_length(p->pOut);
|
||||
switch( p->spec.eQuote ){
|
||||
case RESFMT_Q_Sql: {
|
||||
sqlite3_str_appendf(p->pOut, "%Q", zTxt);
|
||||
break;
|
||||
}
|
||||
case RESFMT_Q_Csv: {
|
||||
unsigned int i;
|
||||
for(i=0; zTxt[i]; i++){
|
||||
if( resfmtCsvQuote[((const unsigned char*)zTxt)[i]] ){
|
||||
i = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( i==0 || strstr(zTxt, p->spec.zColumnSep)!=0 ){
|
||||
sqlite3_str_appendf(p->pOut, "\"%w\"", zTxt);
|
||||
}else{
|
||||
sqlite3_str_appendall(p->pOut, zTxt);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RESFMT_Q_Html: {
|
||||
const unsigned char *z = (const unsigned char*)zTxt;
|
||||
while( *z ){
|
||||
unsigned int i = 0;
|
||||
unsigned char c;
|
||||
while( (c=z[i])>'>'
|
||||
|| (c && c!='<' && c!='>' && c!='&' && c!='\"' && c!='\'')
|
||||
){
|
||||
i++;
|
||||
}
|
||||
if( i>0 ){
|
||||
sqlite3_str_append(p->pOut, (const char*)z, i);
|
||||
}
|
||||
switch( z[i] ){
|
||||
case '>': sqlite3_str_append(p->pOut, "<", 4); break;
|
||||
case '&': sqlite3_str_append(p->pOut, "&", 5); break;
|
||||
case '<': sqlite3_str_append(p->pOut, "<", 4); break;
|
||||
case '"': sqlite3_str_append(p->pOut, """, 6); break;
|
||||
case '\'': sqlite3_str_append(p->pOut, "'", 5); break;
|
||||
default: i--;
|
||||
}
|
||||
z += i + 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RESFMT_Q_Tcl:
|
||||
case RESFMT_Q_Json: {
|
||||
const unsigned char *z = (const unsigned char*)zTxt;
|
||||
sqlite3_str_append(p->pOut, "\"", 1);
|
||||
while( *z ){
|
||||
unsigned int i;
|
||||
for(i=0; z[i]>=0x20 && z[i]!='\\' && z[i]!='"'; i++){}
|
||||
if( i>0 ){
|
||||
sqlite3_str_append(p->pOut, (const char*)z, i);
|
||||
}
|
||||
switch( z[i] ){
|
||||
case '"': sqlite3_str_append(p->pOut, "\\\"", 2); break;
|
||||
case '\\': sqlite3_str_append(p->pOut, "\\\\", 2); break;
|
||||
case '\b': sqlite3_str_append(p->pOut, "\\b", 2); break;
|
||||
case '\f': sqlite3_str_append(p->pOut, "\\f", 2); break;
|
||||
case '\n': sqlite3_str_append(p->pOut, "\\n", 2); break;
|
||||
case '\r': sqlite3_str_append(p->pOut, "\\r", 2); break;
|
||||
case '\t': sqlite3_str_append(p->pOut, "\\t", 2); break;
|
||||
default: {
|
||||
if( p->spec.eQuote==RESFMT_Q_Json ){
|
||||
sqlite3_str_appendf(p->pOut, "\\u%04x", z[i]);
|
||||
}else{
|
||||
sqlite3_str_appendf(p->pOut, "\\%03o", z[i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
z += i + 1;
|
||||
}
|
||||
sqlite3_str_append(p->pOut, "\"", 1);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
sqlite3_str_appendall(p->pOut, zTxt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( p->spec.eEscape!=RESFMT_E_Off ){
|
||||
resfmtEscape(p->spec.eEscape, p->pOut, iStart);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Render value pVal into p->pOut
|
||||
*/
|
||||
static void resfmtRenderValue(sqlite3_resfmt *p, int iCol){
|
||||
if( p->spec.xRender ){
|
||||
sqlite3_value *pVal;
|
||||
char *z;
|
||||
pVal = sqlite3_value_dup(sqlite3_column_value(p->pStmt,iCol));
|
||||
z = p->spec.xRender(p->spec.pRenderArg, pVal);
|
||||
sqlite3_value_free(pVal);
|
||||
if( z ){
|
||||
sqlite3_str_appendall(p->pOut, z);
|
||||
sqlite3_free(z);
|
||||
return;
|
||||
}
|
||||
}
|
||||
switch( sqlite3_column_type(p->pStmt,iCol) ){
|
||||
case SQLITE_INTEGER: {
|
||||
sqlite3_str_appendf(p->pOut, "%lld", sqlite3_column_int64(p->pStmt,iCol));
|
||||
break;
|
||||
}
|
||||
case SQLITE_FLOAT: {
|
||||
if( p->spec.zFloatFmt ){
|
||||
double r = sqlite3_column_double(p->pStmt,iCol);
|
||||
sqlite3_str_appendf(p->pOut, p->spec.zFloatFmt, r);
|
||||
}else{
|
||||
const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol);
|
||||
sqlite3_str_appendall(p->pOut, zTxt);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SQLITE_BLOB: {
|
||||
switch( p->spec.eBlob ){
|
||||
case RESFMT_B_Hex:
|
||||
case RESFMT_B_Sql: {
|
||||
int iStart;
|
||||
int nBlob = sqlite3_column_bytes(p->pStmt,iCol);
|
||||
int i, j;
|
||||
char *zVal;
|
||||
const unsigned char *a = sqlite3_column_blob(p->pStmt,iCol);
|
||||
if( p->spec.eBlob==RESFMT_B_Sql ){
|
||||
sqlite3_str_append(p->pOut, "x'", 2);
|
||||
}
|
||||
iStart = sqlite3_str_length(p->pOut);
|
||||
sqlite3_str_appendchar(p->pOut, nBlob, ' ');
|
||||
sqlite3_str_appendchar(p->pOut, nBlob, ' ');
|
||||
if( p->spec.eBlob==RESFMT_B_Sql ){
|
||||
sqlite3_str_appendchar(p->pOut, 1, '\'');
|
||||
}
|
||||
if( sqlite3_str_errcode(p->pOut) ) return;
|
||||
zVal = sqlite3_str_value(p->pOut);
|
||||
for(i=0, j=iStart; i<nBlob; i++, j+=2){
|
||||
unsigned char c = a[i];
|
||||
zVal[j] = "0123456789abcdef"[(c>>4)&0xf];
|
||||
zVal[j+1] = "0123456789abcdef"[(c)&0xf];
|
||||
}
|
||||
break;
|
||||
}
|
||||
case RESFMT_B_Tcl:
|
||||
case RESFMT_B_Json: {
|
||||
int iStart;
|
||||
int nBlob = sqlite3_column_bytes(p->pStmt,iCol);
|
||||
int i, j;
|
||||
char *zVal;
|
||||
const unsigned char *a = sqlite3_column_blob(p->pStmt,iCol);
|
||||
int szC = p->spec.eBlob==RESFMT_B_Json ? 6 : 4;
|
||||
sqlite3_str_append(p->pOut, "\"", 1);
|
||||
iStart = sqlite3_str_length(p->pOut);
|
||||
for(i=szC; i>0; i--){
|
||||
sqlite3_str_appendchar(p->pOut, nBlob, ' ');
|
||||
}
|
||||
sqlite3_str_appendchar(p->pOut, 1, '"');
|
||||
if( sqlite3_str_errcode(p->pOut) ) return;
|
||||
zVal = sqlite3_str_value(p->pOut);
|
||||
for(i=0, j=iStart; i<nBlob; i++, j+=szC){
|
||||
unsigned char c = a[i];
|
||||
zVal[j] = '\\';
|
||||
if( szC==4 ){
|
||||
zVal[j+1] = '0' + ((c>>6)&3);
|
||||
zVal[j+2] = '0' + ((c>>3)&7);
|
||||
zVal[j+3] = '0' + (c&7);
|
||||
}else{
|
||||
zVal[j+1] = 'u';
|
||||
zVal[j+2] = '0';
|
||||
zVal[j+3] = '0';
|
||||
zVal[j+4] = "0123456789abcdef"[(c>>4)&0xf];
|
||||
zVal[j+5] = "0123456789abcdef"[(c)&0xf];
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol);
|
||||
resfmtEncodeText(p, zTxt);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SQLITE_NULL: {
|
||||
sqlite3_str_appendall(p->pOut, p->spec.zNull);
|
||||
break;
|
||||
}
|
||||
case SQLITE_TEXT: {
|
||||
const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol);
|
||||
resfmtEncodeText(p, zTxt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Initialize the internal sqlite3_resfmt object.
|
||||
*/
|
||||
static void resfmtInitialize(
|
||||
sqlite3_resfmt *p, /* State object to be initialized */
|
||||
sqlite3_stmt *pStmt, /* Query whose output to be formatted */
|
||||
const sqlite3_resfmt_spec *pSpec, /* Format specification */
|
||||
char **pzErr /* Write errors here */
|
||||
){
|
||||
size_t sz; /* Size of pSpec[], based on pSpec->iVersion */
|
||||
memset(p, 0, sizeof(*p));
|
||||
p->pzErr = pzErr;
|
||||
if( pSpec->iVersion!=1 ){
|
||||
resfmtError(p, SQLITE_ERROR,
|
||||
"unusable sqlite3_resfmt_spec.iVersion (%d)",
|
||||
pSpec->iVersion);
|
||||
return;
|
||||
}
|
||||
p->pStmt = pStmt;
|
||||
p->db = sqlite3_db_handle(pStmt);
|
||||
p->pOut = sqlite3_str_new(p->db);
|
||||
if( p->pOut==0 ){
|
||||
resfmtOom(p);
|
||||
return;
|
||||
}
|
||||
p->iErr = 0;
|
||||
p->nCol = sqlite3_column_count(p->pStmt);
|
||||
p->nRow = 0;
|
||||
sz = sizeof(sqlite3_resfmt_spec);
|
||||
memcpy(&p->spec, pSpec, sz);
|
||||
if( p->spec.zNull==0 ) p->spec.zNull = "";
|
||||
if( p->spec.eBlob==RESFMT_B_Auto ){
|
||||
switch( p->spec.eQuote ){
|
||||
case RESFMT_Q_Sql: p->spec.eBlob = RESFMT_B_Sql; break;
|
||||
case RESFMT_Q_Csv: p->spec.eBlob = RESFMT_B_Tcl; break;
|
||||
case RESFMT_Q_Tcl: p->spec.eBlob = RESFMT_B_Tcl; break;
|
||||
case RESFMT_Q_Json: p->spec.eBlob = RESFMT_B_Json; break;
|
||||
default: p->spec.eBlob = RESFMT_B_Text; break;
|
||||
}
|
||||
}
|
||||
switch( p->spec.eFormat ){
|
||||
case RESFMT_List: {
|
||||
if( p->spec.zColumnSep==0 ) p->spec.zColumnSep = "|";
|
||||
if( p->spec.zRowSep==0 ) p->spec.zRowSep = "\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Render a single row of output.
|
||||
*/
|
||||
static void resfmtDoOneRow(sqlite3_resfmt *p){
|
||||
int i;
|
||||
switch( p->spec.eFormat ){
|
||||
case RESFMT_Off:
|
||||
case RESFMT_Count: {
|
||||
/* No-op */
|
||||
break;
|
||||
}
|
||||
default: { /* RESFMT_List */
|
||||
if( p->nRow==0 && p->spec.bShowCNames ){
|
||||
for(i=0; i<p->nCol; i++){
|
||||
const char *zCName = sqlite3_column_name(p->pStmt, i);
|
||||
if( i>0 ) sqlite3_str_appendall(p->pOut, p->spec.zColumnSep);
|
||||
resfmtEncodeText(p, zCName);
|
||||
}
|
||||
sqlite3_str_appendall(p->pOut, p->spec.zRowSep);
|
||||
resfmtWrite(p);
|
||||
}
|
||||
for(i=0; i<p->nCol; i++){
|
||||
if( i>0 ) sqlite3_str_appendall(p->pOut, p->spec.zColumnSep);
|
||||
resfmtRenderValue(p, i);
|
||||
}
|
||||
sqlite3_str_appendall(p->pOut, p->spec.zRowSep);
|
||||
resfmtWrite(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
p->nRow++;
|
||||
}
|
||||
|
||||
/*
|
||||
** Finish rendering the results
|
||||
*/
|
||||
static void resfmtFinalize(sqlite3_resfmt *p){
|
||||
switch( p->spec.eFormat ){
|
||||
case RESFMT_Count: {
|
||||
sqlite3_str_appendf(p->pOut, "%lld\n", p->nRow);
|
||||
resfmtWrite(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( p->spec.pzOutput ){
|
||||
*p->spec.pzOutput = sqlite3_str_finish(p->pOut);
|
||||
}else if( p->pOut ){
|
||||
sqlite3_free(sqlite3_str_finish(p->pOut));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** Run the prepared statement pStmt and format the results according
|
||||
** to the specification provided in pSpec. Return an error code.
|
||||
** If pzErr is not NULL and if an error occurs, write an error message
|
||||
** into *pzErr.
|
||||
*/
|
||||
int sqlite3_format_query_result(
|
||||
sqlite3_stmt *pStmt, /* Statement to evaluate */
|
||||
const sqlite3_resfmt_spec *pSpec, /* Format specification */
|
||||
char **pzErr /* Write error message here */
|
||||
){
|
||||
sqlite3_resfmt fmt; /* The new sqlite3_resfmt being created */
|
||||
|
||||
if( pStmt==0 ) return SQLITE_OK; /* No-op */
|
||||
if( pSpec==0 ) return SQLITE_MISUSE;
|
||||
resfmtInitialize(&fmt, pStmt, pSpec, pzErr);
|
||||
while( fmt.iErr==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
|
||||
resfmtDoOneRow(&fmt);
|
||||
}
|
||||
if( fmt.iErr==SQLITE_OK ){
|
||||
int rc = sqlite3_reset(fmt.pStmt);
|
||||
if( rc!=SQLITE_OK ){
|
||||
resfmtError(&fmt, rc, "%s", sqlite3_errmsg(fmt.db));
|
||||
}
|
||||
}
|
||||
resfmtFinalize(&fmt);
|
||||
return fmt.iErr;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
** 2025-10-20
|
||||
**
|
||||
** The author disclaims copyright to this source code. In place of
|
||||
** a legal notice, here is a blessing:
|
||||
**
|
||||
** May you do good and not evil.
|
||||
** May you find forgiveness for yourself and forgive others.
|
||||
** May you share freely, never taking more than you give.
|
||||
**
|
||||
*************************************************************************
|
||||
** Header file for the Result-Format or "resfmt" utility library for SQLite.
|
||||
** See the resfmt.md documentation for additional information.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include "sqlite3.h"
|
||||
|
||||
/*
|
||||
** Specification used by clients to define the output format they want
|
||||
*/
|
||||
typedef struct sqlite3_resfmt_spec sqlite3_resfmt_spec;
|
||||
struct sqlite3_resfmt_spec {
|
||||
unsigned char iVersion; /* Version number of this structure */
|
||||
unsigned char eFormat; /* Output format */
|
||||
unsigned char bShowCNames; /* True to show column names */
|
||||
unsigned char eEscape; /* How to deal with control characters */
|
||||
unsigned char eQuote; /* Quoting style for text */
|
||||
unsigned char eBlob; /* Quoting style for BLOBs */
|
||||
unsigned char bWordWrap; /* Try to wrap on word boundaries */
|
||||
short int mxWidth; /* Maximum width of any column */
|
||||
int nWidth; /* Number of column width parameters */
|
||||
short int *aWidth; /* Column widths */
|
||||
const char *zColumnSep; /* Alternative column separator */
|
||||
const char *zRowSep; /* Alternative row separator */
|
||||
const char *zTableName; /* Output table name */
|
||||
const char *zNull; /* Rendering of NULL */
|
||||
const char *zFloatFmt; /* printf-style string for rendering floats */
|
||||
char *(*xRender)(void*,sqlite3_value*); /* Render a value */
|
||||
ssize_t (*xWrite)(void*,const unsigned char*,size_t); /* Write callback */
|
||||
void *pRenderArg; /* First argument to the xRender callback */
|
||||
void *pWriteArg; /* First argument to the xWrite callback */
|
||||
char **pzOutput; /* Storage location for output string */
|
||||
/* Additional fields may be added in the future */
|
||||
};
|
||||
|
||||
/*
|
||||
** Interfaces
|
||||
*/
|
||||
int sqlite3_format_query_result(
|
||||
sqlite3_stmt *pStmt, /* SQL statement to run */
|
||||
const sqlite3_resfmt_spec *pSpec, /* Result format specification */
|
||||
char **pzErr /* OUT: Write error message here */
|
||||
);
|
||||
|
||||
/*
|
||||
** Output styles:
|
||||
*/
|
||||
#define RESFMT_List 0 /* One record per line with a separator */
|
||||
#define RESFMT_Line 1 /* One column per line. */
|
||||
#define RESFMT_Html 2 /* Generate an XHTML table */
|
||||
#define RESFMT_Json 3 /* Output is a list of JSON objects */
|
||||
#define RESFMT_Insert 4 /* Generate SQL "insert" statements */
|
||||
#define RESFMT_Explain 5 /* EXPLAIN output */
|
||||
#define RESFMT_ScanExp 6 /* EXPLAIN output with vm stats */
|
||||
#define RESFMT_EQP 7 /* Converts EXPLAIN QUERY PLAN output into a graph */
|
||||
#define RESFMT_Markdown 8 /* Markdown formatting */
|
||||
#define RESFMT_Column 9 /* One record per line in neat columns */
|
||||
#define RESFMT_Table 10 /* MySQL-style table formatting */
|
||||
#define RESFMT_Box 11 /* Unicode box-drawing characters */
|
||||
#define RESFMT_Count 12 /* Output only a count of the rows of output */
|
||||
#define RESFMT_Off 13 /* No query output shown */
|
||||
|
||||
/*
|
||||
** Quoting styles for text.
|
||||
** Allowed values for sqlite3_resfmt_spec.eQuote
|
||||
*/
|
||||
#define RESFMT_Q_Off 0 /* Literal text */
|
||||
#define RESFMT_Q_Sql 1 /* Quote as an SQL literal */
|
||||
#define RESFMT_Q_Csv 2 /* CSV-style quoting */
|
||||
#define RESFMT_Q_Html 3 /* HTML-style quoting */
|
||||
#define RESFMT_Q_Tcl 4 /* C/Tcl quoting */
|
||||
#define RESFMT_Q_Json 5 /* JSON quoting */
|
||||
|
||||
/*
|
||||
** Quoting styles for BLOBs
|
||||
** Allowed values for sqlite3_resfmt_spec.eBlob
|
||||
*/
|
||||
#define RESFMT_B_Auto 0 /* Determine BLOB quoting using eQuote */
|
||||
#define RESFMT_B_Text 1 /* Display content exactly as it is */
|
||||
#define RESFMT_B_Sql 2 /* Quote as an SQL literal */
|
||||
#define RESFMT_B_Hex 3 /* Hexadecimal representation */
|
||||
#define RESFMT_B_Tcl 4 /* "\000" notation */
|
||||
#define RESFMT_B_Json 5 /* A JSON string */
|
||||
|
||||
/*
|
||||
** Control-character escape modes.
|
||||
** Allowed values for sqlite3_resfmt_spec.eEscape
|
||||
*/
|
||||
#define RESFMT_E_Off 0 /* Do not escape control characters */
|
||||
#define RESFMT_E_Ascii 1 /* Unix-style escapes. Ex: U+0007 shows ^G */
|
||||
#define RESFMT_E_Symbol 2 /* Unicode escapes. Ex: U+0007 shows U+2407 */
|
||||
@@ -0,0 +1,293 @@
|
||||
# SQLite Result Formatting Subsystem
|
||||
|
||||
The "resfmt" subsystem is a set of C-language subroutines that work
|
||||
together to format the output from an SQLite query. The output format
|
||||
is configurable. The application can request CSV, or a table, or
|
||||
any of several other formats, according to needs.
|
||||
|
||||
## 1.0 Overview Of Operation
|
||||
|
||||
Suppose `pStmt` is a pointer to an SQLite prepared statement
|
||||
(a pointer to an `sqlite3_stmt` object) that has been reset and
|
||||
bound and is ready to run. Then to format the output from this
|
||||
prepared statement, use code similar to the following:
|
||||
|
||||
> ~~~
|
||||
sqlite3_resfmt_spec spec; /* Formatter spec */
|
||||
sqlite3_resfmt *pFmt; /* Formatter object */
|
||||
int errCode; /* Error code */
|
||||
char *zErrMsg; /* Text error message (optional) */
|
||||
|
||||
memset(&spec, 0, sizeof(spec));
|
||||
// Additional spec initialization here
|
||||
pFmt = sqlite3_resfmt_begin(pStmt, &spec);
|
||||
while( SQLITE_ROW==sqlite3_step(pStmt) ){
|
||||
sqlite3_resfmt_row(pFmt, pStmt);
|
||||
}
|
||||
sqlite3_resfmt_finish(pFmt, &errCode, &zErrMsg);
|
||||
// Do something with errcode and zErrMsg
|
||||
sqlite3_free(zErrMsg);
|
||||
~~~
|
||||
|
||||
The `sqlite3_resfmt_spec` structure (defined below) describes the desired
|
||||
output format. The `pFmt` variable is a pointer to an opaque sqlite3_resfmt
|
||||
object that maintains the statement of the formatter.
|
||||
The pFmt object is used as the first parameter to two other
|
||||
routines, `sqlite3_resfmt_row()` and `sqlite3_resfmt_finish()`, and
|
||||
is not usable for any other purpose by the caller. The
|
||||
`sqlite3_resfmt_finish()` interface serves as a destructor for
|
||||
the pFmt object.
|
||||
|
||||
## 2.0 The `sqlite3_resfmt_spec` object
|
||||
|
||||
A pointer to an instance of the following structure is the second
|
||||
parameter to the `sqlite3_resfmt_begin()` interface. This structure
|
||||
defines how the rules of the statement are to be formatted.
|
||||
|
||||
> ~~~
|
||||
typedef struct sqlite3_resfmt_spec sqlite3_resfmt_spec;
|
||||
struct sqlite3_resfmt_spec {
|
||||
int iVersion; /* Version number of this structure */
|
||||
int eFormat; /* Output format */
|
||||
unsigned char bShowCNames; /* True to show column names */
|
||||
unsigned char eEscMode; /* How to deal with control characters */
|
||||
unsigned char bQuote; /* Quote output values as SQL literals */
|
||||
unsigned char bWordWrap; /* Try to wrap on word boundaries */
|
||||
int mxWidth; /* Maximum column width in columnar modes */
|
||||
const char *zColumnSep; /* Alternative column separator */
|
||||
const char *zRowSep; /* Alternative row separator */
|
||||
const char *zTableName; /* Output table name */
|
||||
const char *zNull; /* Rendering of NULL */
|
||||
const char *zFloatFmt; /* printf-style string for rendering floats */
|
||||
int nWidth; /* Number of column width parameters */
|
||||
short int *aWidth; /* Column widths */
|
||||
char *(*xRender)(void*,sqlite3_value*); /* Render a value */
|
||||
ssize_t (*xWrite)(void*,const unsigned char*,ssize_t); /* Write callback */
|
||||
void *pRenderArg; /* First argument to the xRender callback */
|
||||
void *pWriteArg; /* First argument to the xWrite callback */
|
||||
char **pzOutput; /* Storage location for output string */
|
||||
/* Additional fields may be added in the future */
|
||||
};
|
||||
~~~
|
||||
|
||||
The sqlite3_resfmt_spec object must be fully initialized prior
|
||||
to calling `sqlite3_resfmt_begin()` and its value must not change
|
||||
by the application until after the corresponding call to
|
||||
`sqlite3_resfmt_finish()`. Note that the result formatter itself
|
||||
might change values in the sqlite3_resfmt_spec object as it runs.
|
||||
But the application should not try to change or use any fields of
|
||||
the sqlite3_resfmt_spec object while the formatter is running.
|
||||
|
||||
### 2.1 Structure Version Number
|
||||
|
||||
The sqlite3_resfmt_spec.iVersion field must be 1. Future enhancements to this
|
||||
subsystem might add new fields onto the bottom of the sqlite3_resfmt_spec
|
||||
object. Those new fields will only be accessible if the iVersion is greater
|
||||
than 1. Thus the iVersion field is used to support upgradability.
|
||||
|
||||
### 2.2 Output Deposition
|
||||
|
||||
The formatted output can either be sent to a callback function
|
||||
or accumulated into an output buffer in memory obtained
|
||||
from system malloc(). If the sqlite3_resfmt_spec.xWrite column is not NULL,
|
||||
then that function is invoked (using sqlite3_resfmt_spec.xWriteArg as its
|
||||
first argument) to transmit the formatted output. Or, if
|
||||
sqlite3_resfmt_spec.pzOutput points to a pointer to a character, then that
|
||||
pointer is made to point to memory obtained from malloc() that
|
||||
contains the complete text of the formatted output.
|
||||
|
||||
When `sqlite3_resfmt_begin()` is called,
|
||||
one of sqlite3_resfmt_spec.xWrite and sqlite3_resfmt_spec.pzOutput must be
|
||||
non-NULL and the other must be NULL.
|
||||
|
||||
Output might be generated row by row, on each call to
|
||||
`sqlite3_resfmt_row()` or it might be written all at once
|
||||
on the final call to `sqlite3_resfmt_finish()`, depending
|
||||
on the output format.
|
||||
|
||||
### 2.3 Output Format
|
||||
|
||||
The sqlite3_resfmt_spec.eFormat field is an integer code that defines the
|
||||
specific output format that will be generated. See the
|
||||
output format describes below for additional detail.
|
||||
|
||||
### 2.4 Show Column Names
|
||||
|
||||
The sqlite3_resfmt_spec.bShowCNames field is a boolean. If true, then column
|
||||
names appear in the output. If false, column names are omitted.
|
||||
|
||||
### 2.5 Control Character Escapes
|
||||
|
||||
The sqlite3_resfmt_spec.eEscMode determines how ASCII control characters are
|
||||
formatted in the output. If this value is zero, then the control character
|
||||
with value X is displayed as ^Y where Y is X+0x40. Hence, a
|
||||
backspace character (U+0008) is shown as "^H". This is the default.
|
||||
If eEscMode is one, then control characters in the range of U+0001
|
||||
through U+001f are mapped into U+2401 through U+241f, respectively.
|
||||
If eEscMode is 2, then control characters are output directly, with
|
||||
no translation.
|
||||
|
||||
The TAB (U+0009), LF (U+000a) and CR-LF (U+000d,U+000a) character
|
||||
sequence are always output literally and are not mapped to alternative
|
||||
display values, regardless of this setting.
|
||||
|
||||
### 2.6 Render output as SQL literals
|
||||
|
||||
If the sqlite3_resfmt_spec.bQuote field is non-zero (true), then outputs
|
||||
are rendered as SQL literal values. String are written within `'...'`
|
||||
with appropriate escapes. BLOBs are of the form `x'...'`. NULL values
|
||||
come out as "NULL".
|
||||
|
||||
### 2.7 Word Wrapping In Columnar Modes
|
||||
|
||||
For output modes that attempt to display equal-width columns, the
|
||||
sqlite3_resfmt_spec.bWordWrap boolean determines whether long values
|
||||
are broken at word boundaries, or at arbitrary characters. The
|
||||
sqlite3_resfmt_spec.mxWidth determines the maximum width of an output column.
|
||||
|
||||
### 2.8 Row and Column Separator Strings
|
||||
|
||||
The sqlite3_resfmt_spec.zColumnSep and sqlite3_resfmt_spec.zRowSep strings
|
||||
are alternative column and row separator character sequences. If not
|
||||
specified (if these pointers are left as NULL) then appropriate defaults
|
||||
are used.
|
||||
|
||||
### 2.9 The Output Table Name
|
||||
|
||||
The sqlite3_resfmt_spec.zTableName value is the name of the output table
|
||||
when the MODE_Insert output mode is used.
|
||||
|
||||
### 2.10 The Rendering Of NULL
|
||||
|
||||
If a value is NULL and sqlite3_resfmt_spec.xRender() does not exist
|
||||
or declines to provide a rendering, then show the NULL using the string
|
||||
found in sqlite3_resfmt_spec.zNull. If zNull is itself a NULL pointer
|
||||
then NULL values are rendered as an empty string.
|
||||
|
||||
### 2.11 Floating Point Value Format
|
||||
|
||||
If a value is a floating-point number other than Infinity or NaN, then
|
||||
the value is rendered using the printf-style format given by the
|
||||
sqlite3_resfmt_spec.zFloatFmt string.
|
||||
|
||||
### 2.12 Column Widths And Alignments
|
||||
|
||||
The sqlite3_resfmt_spec.aWidth[] array, if specified, is an array of
|
||||
16-bit signed integers that specify the minimum column width and
|
||||
the alignment for all columns in columnar output modes. Negative
|
||||
values mean right-justify. The
|
||||
absolute value is the minimum of the corresponding column.
|
||||
These widths are deliberately stored in a 16-bit signed integer
|
||||
as no good can come from having column widths greater than 32767.
|
||||
|
||||
The sqlite3_resfmt_spec.nWidth field is the number of values in the aWidth[]
|
||||
array. Any column beyond the nWidth-th column are assumed to have
|
||||
a minimum width of 0.
|
||||
|
||||
### 2.13 Optional Value Rendering Callback
|
||||
|
||||
If the sqlite3_resfmt_spec.xRender field is not NULL, then each rendered
|
||||
sqlite3_value is first passed to the xRender function, giving it
|
||||
an opportunity to render the results. If xRender chooses to render,
|
||||
it should write the rendering into memory obtained from sqlite3_malloc()
|
||||
and return a pointer to that memory. The xRender function can decline
|
||||
to render (for example, based on the sqlite3_value_type() or other
|
||||
characteristics of the value) in which case it can simply return a
|
||||
NULL pointer and the usual default rendering will be used instead.
|
||||
|
||||
The result rendering subsystem will take responsibility for the
|
||||
returned string and will pass the returned string to sqlite3_free()
|
||||
when it has finished with it.
|
||||
|
||||
The bQuote and eEscMode settings above become no-ops if the xRender
|
||||
routine returns non-NULL. In other words, the application-supplied
|
||||
xRender routine is expected to do its own bQuote and eEscMode processing
|
||||
if it chooses to render values.
|
||||
|
||||
## 3.0 The `sqlite3_resfmt_begin()` Interface
|
||||
|
||||
Invoke the `sqlite3_resfmt_begin(P,S)` interface to begin formatting
|
||||
the output of prepared statement P using format specification S.
|
||||
This routine returns a pointer to an opaque sqlite3_resfmt object that is
|
||||
the current state of the formatter. The `sqlite3_resfmt_finish()`
|
||||
routine is the destructor for the sqlite3_resfmt object and must be called
|
||||
to prevent a memory leak.
|
||||
|
||||
If an out-of-memory fault occurs while allocating space for the
|
||||
sqlite3_resfmt object, then `sqlite3_resfmt_begin()` will return a NULL
|
||||
pointer. The application need not check for this case as the
|
||||
other routines that use a pointer to the sqlite3_resfmt object all
|
||||
interpret a NULL parameter in place of the sqlite3_resfmt pointer as
|
||||
a harmless no-op.
|
||||
|
||||
## 4.0 The `sqlite3_resfmt_step()` Interface
|
||||
|
||||
Invoke the `sqlite3_resfmt_step(F)` interface for each row
|
||||
in the prepared statement that is to be output.
|
||||
|
||||
The formatter might choose to output some content as each row
|
||||
is processed, or it might accumulate the output and send it all
|
||||
at once when `sqlite3_resfmt_finish()` is called. This is at
|
||||
the discretion of the output formatter. Generally, rows are
|
||||
output one-by-one on each call to `sqlite3_resfmt_row()` when the
|
||||
output format is such that the row can be computed without knowing
|
||||
the value of subsequence, such as in CSV output mode, and
|
||||
the output is accumulated and sent all at once in columnar output
|
||||
modes where the complete output content is needed to compute column
|
||||
widths.
|
||||
|
||||
### 5.0 The `sqlite3_resfmt_finish()` Interface
|
||||
|
||||
Invoke the `sqlite3_resfmt_finish(F,C,E)` interface to finish
|
||||
the formatting. C is an optional pointer to an integer. If C
|
||||
is not a NULL pointer, then any error code associated with the
|
||||
formatting operation is written into *C. E is an optional pointer
|
||||
to a human-readable text error message. If E is not a NULL pointer,
|
||||
then an error message held in memory obtained from sqlite3_malloc()
|
||||
is written into *E. It is the responsibility of the calling
|
||||
application to invoke sqlite3_free() on this error message to
|
||||
reclaim the space.
|
||||
|
||||
If the F argument to `sqlite3_resfmt_finish(F,C,E)` is a NULL pointer,
|
||||
then this interface is a harmless no-op.
|
||||
|
||||
### 6.0 Output Modes
|
||||
|
||||
The result formatter supports a variety of output modes. The
|
||||
set of supported output modes might increase in future versions.
|
||||
The following output modes are currently defined:
|
||||
|
||||
> ~~~
|
||||
#define RESFMT_Line 0 /* One column per line. */
|
||||
#define RESFMT_Column 1 /* One record per line in neat columns */
|
||||
#define RESFMT_List 2 /* One record per line with a separator */
|
||||
#define RESFMT_Html 3 /* Generate an XHTML table */
|
||||
#define RESFMT_Insert 4 /* Generate SQL "insert" statements */
|
||||
#define RESFMT_Tcl 5 /* Generate ANSI-C or TCL quoted elements */
|
||||
#define RESFMT_Csv 6 /* Quote strings, numbers are plain */
|
||||
#define RESFMT_Explain 7 /* Like RESFMT_Column, but do not truncate data */
|
||||
#define RESFMT_Pretty 8 /* Pretty-print schemas */
|
||||
#define RESFMT_EQP 9 /* Converts EXPLAIN QUERY PLAN output into a graph */
|
||||
#define RESFMT_Json 10 /* Output JSON */
|
||||
#define RESFMT_Markdown 11 /* Markdown formatting */
|
||||
#define RESFMT_Table 12 /* MySQL-style table formatting */
|
||||
#define RESFMT_Box 13 /* Unicode box-drawing characters */
|
||||
#define RESFMT_Count 14 /* Output only a count of the rows of output */
|
||||
#define RESFMT_Off 15 /* No query output shown */
|
||||
#define RESFMT_ScanExp 16 /* Like RESFMT_Explain, but for ".scanstats vm" */
|
||||
#define RESFMT_Www 17 /* Full web-page output */
|
||||
~~~
|
||||
|
||||
Additional detail about the meaning of each of these output modes
|
||||
is pending.
|
||||
|
||||
### 7.0 Source Code Files
|
||||
|
||||
The SQLite result formatter is implemented in three source code files:
|
||||
|
||||
* `resfmt.c` → The implementation, written in portable C99
|
||||
* `resfmt.h` → A header file defining interfaces
|
||||
* `resfmt.md` → This documentation, in Markdown
|
||||
|
||||
To use the SQLite result formatter, include the "`resfmt.h`" header file
|
||||
and link the application against the "`resfmt.c`" source file.
|
||||
@@ -2154,6 +2154,11 @@ threadtest5: sqlite3.c $(TOP)/test/threadtest5.c
|
||||
$(T.link) $(TOP)/test/threadtest5.c sqlite3.c -o $@ $(LDFLAGS.libsqlite3)
|
||||
xbin: threadtest5
|
||||
|
||||
resfmt-test: sqlite3.o $(TOP)/ext/misc/resfmt-tester.c $(TOP)/ext/misc/resfmt.c $(TOP)/ext/misc/resfmt.h
|
||||
$(T.link) -I$(TOP)/ext/misc -I. \
|
||||
$(TOP)/ext/misc/resfmt-tester.c $(TOP)/ext/misc/resfmt.c sqlite3.o \
|
||||
-o $@ $(LDFLAGS.libsqlite3)
|
||||
|
||||
#
|
||||
# STATIC_CLI_SHELL = 1 to statically link sqlite3$(T.exe), else
|
||||
# 0. Requires static versions of all requisite libraries. Primarily
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
C Reduce\sthe\smaximum\ssize\sof\sLIKE\sand\sGLOB\spatterns\sin\sthe\sossfuzz.c\stest\nmodule\sto\savoid\sfalse-positive\sinfinite\sloop\sreports\sfrom\sOSS-Fuzz\sbased\non\slong\sand\scomplex\sfuzzer-generated\sLIKE/GLOB\spatterns\sthat\sare\snot\sactually\ninfinite\sloops,\sbut\swhich\sdo\stake\sa\slong\stime\sto\sresolve.
|
||||
D 2025-10-19T17:30:07.855
|
||||
C Fix\sa\sva_list\smisuse.
|
||||
D 2025-10-22T12:38:39.500
|
||||
F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
@@ -389,6 +389,10 @@ F ext/misc/qpvtab.c fc189e127f68f791af90a487f4460ec91539a716daf45a0c357e963fd47c
|
||||
F ext/misc/randomjson.c ef835fc64289e76ac4873b85fe12f9463a036168d7683cf2b773e36e6262c4ed
|
||||
F ext/misc/regexp.c 548151f3e57506fda678e6a65e85a763f4eece653287e1ad44e167f9485e0c6b
|
||||
F ext/misc/remember.c add730f0f7e7436cd15ea3fd6a90fd83c3f706ab44169f7f048438b7d6baa69c
|
||||
F ext/misc/resfmt-tester.c 3188324e10f945429fd684c1d1a468a1bde43e1379af60f0d34c5411a04b0edc
|
||||
F ext/misc/resfmt.c fc9c8b26b4fb8958fb3fba85f0368a373fb2d1aaf707d57769f6eff791800b1c
|
||||
F ext/misc/resfmt.h ebb635f87324499a1487cdad812f64ab9f98a1b09accf0ba45fea0e274e2bd3b
|
||||
F ext/misc/resfmt.md 6f6cefd95fa11ce30e4f34ea84052e7a8291dd48b7e666352bd7cf2e22c22ec4
|
||||
F ext/misc/rot13.c 51ac5f51e9d5fd811db58a9c23c628ad5f333c173f1fc53c8491a3603d38556c
|
||||
F ext/misc/scrub.c 2a44b0d44c69584c0580ad2553f6290a307a49df4668941d2812135bfb96a946
|
||||
F ext/misc/series.c 22c6d8f00cc1b5089b1b37392e9097e9df9a5db53be86daf9a7669d95bb179f4
|
||||
@@ -658,7 +662,7 @@ F ext/wasm/tests/opfs/sahpool/index.html be736567fd92d3ecb9754c145755037cbbd2bca
|
||||
F ext/wasm/tests/opfs/sahpool/sahpool-pausing.js f264925cfc82155de38cecb3d204c36e0f6991460fff0cb7c15079454679a4e2
|
||||
F ext/wasm/tests/opfs/sahpool/sahpool-worker.js bd25a43fc2ab2d1bafd8f2854ad3943ef673f7c3be03e95ecf1612ff6e8e2a61
|
||||
F magic.txt 5ade0bc977aa135e79e3faaea894d5671b26107cc91e70783aa7dc83f22f3ba0
|
||||
F main.mk 00dd631c66c1f7922b2d691631163899eff1c3d1da780ff37a62f8d997b368e1
|
||||
F main.mk bf63775585a99e8e7158c4d67c30aeeae45b84572c474f9d41c7bba927788f46
|
||||
F mptest/config01.test 3c6adcbc50b991866855f1977ff172eb6d901271
|
||||
F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504
|
||||
F mptest/crash01.test 61e61469e257df0850df4293d7d4d6c2af301421
|
||||
@@ -2171,8 +2175,8 @@ F tool/version-info.c 33d0390ef484b3b1cb685d59362be891ea162123cea181cb8e6d2cf6dd
|
||||
F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
|
||||
F tool/warnings.sh 1ad0169b022b280bcaaf94a7fa231591be96b514230ab5c98fbf15cd7df842dd
|
||||
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
|
||||
P 6caf793b8f406f5089e3811591bc69384646df16ee18d63bd63760eaaa95d122
|
||||
R 69fa379b6b2c39c2868c3ce3729f3859
|
||||
U drh
|
||||
Z 34daa97b3470f94f840fcd2ff082eed1
|
||||
P 82dc13ec4887f90995bd984ede9c2670fb7512962ccceb1848a7e9aab7891c00
|
||||
R fa18e75fe233d98f1fe33dfd9aa9b399
|
||||
U stephan
|
||||
Z 380e5356f88927c6576fb8a521f933af
|
||||
# Remove this line to create a well-formed Fossil manifest.
|
||||
|
||||
+2
-2
@@ -1,2 +1,2 @@
|
||||
branch trunk
|
||||
tag trunk
|
||||
branch resfmt
|
||||
tag resfmt
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
53edea286b0a38f4f9c7017276f3a826037919144d517f8c89ef0f35b65af5f1
|
||||
3d044121768548df59a94bbbe4ee360d6464d1416a5cbe7437f4333b06496381
|
||||
|
||||
Reference in New Issue
Block a user