| 1 |
/* |
| 2 |
** 2025-10-20 |
| 3 |
** |
| 4 |
** The author disclaims copyright to this source code. In place of |
| 5 |
** a legal notice, here is a blessing: |
| 6 |
** |
| 7 |
** May you do good and not evil. |
| 8 |
** May you find forgiveness for yourself and forgive others. |
| 9 |
** May you share freely, never taking more than you give. |
| 10 |
** |
| 11 |
************************************************************************* |
| 12 |
** Implementation of the Query Result-Format or "qrf" utility library for |
| 13 |
** SQLite. See the README.md documentation for additional information. |
| 14 |
*/ |
| 15 |
#ifndef SQLITE_QRF_H |
| 16 |
#include "qrf.h" |
| 17 |
#endif |
| 18 |
#include <string.h> |
| 19 |
#include <assert.h> |
| 20 |
#include <stdint.h> |
| 21 |
|
| 22 |
#ifndef SQLITE_AMALGAMATION |
| 23 |
typedef sqlite3_int64 i64; |
| 24 |
#endif |
| 25 |
|
| 26 |
/* A single line in the EQP output */ |
| 27 |
typedef struct qrfEQPGraphRow qrfEQPGraphRow; |
| 28 |
struct qrfEQPGraphRow { |
| 29 |
int iEqpId; /* ID for this row */ |
| 30 |
int iParentId; /* ID of the parent row */ |
| 31 |
qrfEQPGraphRow *pNext; /* Next row in sequence */ |
| 32 |
char zText[1]; /* Text to display for this row */ |
| 33 |
}; |
| 34 |
|
| 35 |
/* All EQP output is collected into an instance of the following */ |
| 36 |
typedef struct qrfEQPGraph qrfEQPGraph; |
| 37 |
struct qrfEQPGraph { |
| 38 |
qrfEQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ |
| 39 |
qrfEQPGraphRow *pLast; /* Last element of the pRow list */ |
| 40 |
int nWidth; /* Width of the graph */ |
| 41 |
char zPrefix[400]; /* Graph prefix */ |
| 42 |
}; |
| 43 |
|
| 44 |
/* |
| 45 |
** Private state information. Subject to change from one release to the |
| 46 |
** next. |
| 47 |
*/ |
| 48 |
typedef struct Qrf Qrf; |
| 49 |
struct Qrf { |
| 50 |
sqlite3_stmt *pStmt; /* The statement whose output is to be rendered */ |
| 51 |
sqlite3 *db; /* The corresponding database connection */ |
| 52 |
sqlite3_stmt *pJTrans; /* JSONB to JSON translator statement */ |
| 53 |
char **pzErr; /* Write error message here, if not NULL */ |
| 54 |
sqlite3_str *pOut; /* Accumulated output */ |
| 55 |
int iErr; /* Error code */ |
| 56 |
int nCol; /* Number of output columns */ |
| 57 |
int expMode; /* Original sqlite3_stmt_isexplain() plus 1 */ |
| 58 |
int mxWidth; /* Screen width */ |
| 59 |
int mxHeight; /* nLineLimit */ |
| 60 |
union { |
| 61 |
struct { /* Content for QRF_STYLE_Line */ |
| 62 |
int mxColWth; /* Maximum display width of any column */ |
| 63 |
char **azCol; /* Names of output columns (MODE_Line) */ |
| 64 |
} sLine; |
| 65 |
qrfEQPGraph *pGraph; /* EQP graph (Eqp, Stats, and StatsEst) */ |
| 66 |
struct { /* Content for QRF_STYLE_Explain */ |
| 67 |
int nIndent; /* Slots allocated for aiIndent */ |
| 68 |
int iIndent; /* Current slot */ |
| 69 |
int *aiIndent; /* Indentation for each opcode */ |
| 70 |
} sExpln; |
| 71 |
unsigned int nIns; /* Bytes used for current INSERT stmt */ |
| 72 |
} u; |
| 73 |
sqlite3_int64 nRow; /* Number of rows handled so far */ |
| 74 |
int *actualWidth; /* Actual width of each column */ |
| 75 |
sqlite3_qrf_spec spec; /* Copy of the original spec */ |
| 76 |
}; |
| 77 |
|
| 78 |
/* |
| 79 |
** Data for substitute ctype.h functions. Used for x-platform |
| 80 |
** consistency and so that '_' is counted as an alphabetic |
| 81 |
** character. |
| 82 |
** |
| 83 |
** 0x01 - space |
| 84 |
** 0x02 - digit |
| 85 |
** 0x04 - alphabetic, including '_' |
| 86 |
*/ |
| 87 |
static const char qrfCType[] = { |
| 88 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, |
| 89 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 90 |
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 91 |
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, |
| 92 |
0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 93 |
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 4, |
| 94 |
0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 95 |
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, |
| 96 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 97 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 98 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 99 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 100 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 101 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 102 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 103 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 104 |
}; |
| 105 |
#define qrfSpace(x) ((qrfCType[(unsigned char)x]&1)!=0) |
| 106 |
#define qrfDigit(x) ((qrfCType[(unsigned char)x]&2)!=0) |
| 107 |
#define qrfAlpha(x) ((qrfCType[(unsigned char)x]&4)!=0) |
| 108 |
#define qrfAlnum(x) ((qrfCType[(unsigned char)x]&6)!=0) |
| 109 |
|
| 110 |
#ifndef deliberate_fall_through |
| 111 |
/* Quiet some compilers about some of our intentional code. */ |
| 112 |
# if defined(GCC_VERSION) && GCC_VERSION>=7000000 |
| 113 |
# define deliberate_fall_through __attribute__((fallthrough)); |
| 114 |
# else |
| 115 |
# define deliberate_fall_through |
| 116 |
# endif |
| 117 |
#endif |
| 118 |
|
| 119 |
/* |
| 120 |
** Set an error code and error message. |
| 121 |
*/ |
| 122 |
static void qrfError( |
| 123 |
Qrf *p, /* Query result state */ |
| 124 |
int iCode, /* Error code */ |
| 125 |
const char *zFormat, /* Message format (or NULL) */ |
| 126 |
... |
| 127 |
){ |
| 128 |
p->iErr = iCode; |
| 129 |
if( p->pzErr!=0 ){ |
| 130 |
sqlite3_free(*p->pzErr); |
| 131 |
*p->pzErr = 0; |
| 132 |
if( zFormat ){ |
| 133 |
va_list ap; |
| 134 |
va_start(ap, zFormat); |
| 135 |
*p->pzErr = sqlite3_vmprintf(zFormat, ap); |
| 136 |
va_end(ap); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* |
| 142 |
** Out-of-memory error. |
| 143 |
*/ |
| 144 |
static void qrfOom(Qrf *p){ |
| 145 |
qrfError(p, SQLITE_NOMEM, "out of memory"); |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
** Transfer any error in pStr over into p. |
| 150 |
*/ |
| 151 |
static void qrfStrErr(Qrf *p, sqlite3_str *pStr){ |
| 152 |
int rc = pStr ? sqlite3_str_errcode(pStr) : 0; |
| 153 |
if( rc ){ |
| 154 |
qrfError(p, rc, sqlite3_errstr(rc)); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/* |
| 160 |
** Add a new entry to the EXPLAIN QUERY PLAN data |
| 161 |
*/ |
| 162 |
static void qrfEqpAppend(Qrf *p, int iEqpId, int p2, const char *zText){ |
| 163 |
qrfEQPGraphRow *pNew; |
| 164 |
sqlite3_int64 nText; |
| 165 |
if( zText==0 ) return; |
| 166 |
if( p->u.pGraph==0 ){ |
| 167 |
p->u.pGraph = sqlite3_malloc64( sizeof(qrfEQPGraph) ); |
| 168 |
if( p->u.pGraph==0 ){ |
| 169 |
qrfOom(p); |
| 170 |
return; |
| 171 |
} |
| 172 |
memset(p->u.pGraph, 0, sizeof(qrfEQPGraph) ); |
| 173 |
} |
| 174 |
nText = strlen(zText); |
| 175 |
pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); |
| 176 |
if( pNew==0 ){ |
| 177 |
qrfOom(p); |
| 178 |
return; |
| 179 |
} |
| 180 |
pNew->iEqpId = iEqpId; |
| 181 |
pNew->iParentId = p2; |
| 182 |
memcpy(pNew->zText, zText, nText+1); |
| 183 |
pNew->pNext = 0; |
| 184 |
if( p->u.pGraph->pLast ){ |
| 185 |
p->u.pGraph->pLast->pNext = pNew; |
| 186 |
}else{ |
| 187 |
p->u.pGraph->pRow = pNew; |
| 188 |
} |
| 189 |
p->u.pGraph->pLast = pNew; |
| 190 |
} |
| 191 |
|
| 192 |
/* |
| 193 |
** Free and reset the EXPLAIN QUERY PLAN data that has been collected |
| 194 |
** in p->u.pGraph. |
| 195 |
*/ |
| 196 |
static void qrfEqpReset(Qrf *p){ |
| 197 |
qrfEQPGraphRow *pRow, *pNext; |
| 198 |
if( p->u.pGraph ){ |
| 199 |
for(pRow = p->u.pGraph->pRow; pRow; pRow = pNext){ |
| 200 |
pNext = pRow->pNext; |
| 201 |
sqlite3_free(pRow); |
| 202 |
} |
| 203 |
sqlite3_free(p->u.pGraph); |
| 204 |
p->u.pGraph = 0; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after |
| 209 |
** pOld, or return the first such line if pOld is NULL |
| 210 |
*/ |
| 211 |
static qrfEQPGraphRow *qrfEqpNextRow(Qrf *p, int iEqpId, qrfEQPGraphRow *pOld){ |
| 212 |
qrfEQPGraphRow *pRow = pOld ? pOld->pNext : p->u.pGraph->pRow; |
| 213 |
while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; |
| 214 |
return pRow; |
| 215 |
} |
| 216 |
|
| 217 |
/* Render a single level of the graph that has iEqpId as its parent. Called |
| 218 |
** recursively to render sublevels. |
| 219 |
*/ |
| 220 |
static void qrfEqpRenderLevel(Qrf *p, int iEqpId){ |
| 221 |
qrfEQPGraphRow *pRow, *pNext; |
| 222 |
i64 n = strlen(p->u.pGraph->zPrefix); |
| 223 |
char *z; |
| 224 |
for(pRow = qrfEqpNextRow(p, iEqpId, 0); pRow; pRow = pNext){ |
| 225 |
pNext = qrfEqpNextRow(p, iEqpId, pRow); |
| 226 |
z = pRow->zText; |
| 227 |
sqlite3_str_appendf(p->pOut, "%s%s%s\n", p->u.pGraph->zPrefix, |
| 228 |
pNext ? "|--" : "`--", z); |
| 229 |
if( n<(i64)sizeof(p->u.pGraph->zPrefix)-7 ){ |
| 230 |
memcpy(&p->u.pGraph->zPrefix[n], pNext ? "| " : " ", 4); |
| 231 |
qrfEqpRenderLevel(p, pRow->iEqpId); |
| 232 |
p->u.pGraph->zPrefix[n] = 0; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
** Render the 64-bit value N in a more human-readable format into |
| 239 |
** pOut. |
| 240 |
** |
| 241 |
** + Only show the first three significant digits. |
| 242 |
** + Append suffixes K, M, G, T, P, and E for 1e3, 1e6, ... 1e18 |
| 243 |
*/ |
| 244 |
static void qrfApproxInt64(sqlite3_str *pOut, i64 N){ |
| 245 |
static const char aSuffix[] = { 'K', 'M', 'G', 'T', 'P', 'E' }; |
| 246 |
int i; |
| 247 |
if( N<0 ){ |
| 248 |
N = N==INT64_MIN ? INT64_MAX : -N; |
| 249 |
sqlite3_str_append(pOut, "-", 1); |
| 250 |
} |
| 251 |
if( N<10000 ){ |
| 252 |
sqlite3_str_appendf(pOut, "%4lld ", N); |
| 253 |
return; |
| 254 |
} |
| 255 |
for(i=1; i<=18; i++){ |
| 256 |
N = (N+5)/10; |
| 257 |
if( N<10000 ){ |
| 258 |
int n = (int)N; |
| 259 |
switch( i%3 ){ |
| 260 |
case 0: |
| 261 |
sqlite3_str_appendf(pOut, "%d.%02d", n/1000, (n%1000)/10); |
| 262 |
break; |
| 263 |
case 1: |
| 264 |
sqlite3_str_appendf(pOut, "%2d.%d", n/100, (n%100)/10); |
| 265 |
break; |
| 266 |
case 2: |
| 267 |
sqlite3_str_appendf(pOut, "%4d", n/10); |
| 268 |
break; |
| 269 |
} |
| 270 |
sqlite3_str_append(pOut, &aSuffix[i/3], 1); |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/* |
| 277 |
** Display and reset the EXPLAIN QUERY PLAN data |
| 278 |
*/ |
| 279 |
static void qrfEqpRender(Qrf *p, i64 nCycle){ |
| 280 |
qrfEQPGraphRow *pRow; |
| 281 |
if( p->u.pGraph!=0 && (pRow = p->u.pGraph->pRow)!=0 ){ |
| 282 |
if( pRow->zText[0]=='-' ){ |
| 283 |
if( pRow->pNext==0 ){ |
| 284 |
qrfEqpReset(p); |
| 285 |
return; |
| 286 |
} |
| 287 |
sqlite3_str_appendf(p->pOut, "%s\n", pRow->zText+3); |
| 288 |
p->u.pGraph->pRow = pRow->pNext; |
| 289 |
sqlite3_free(pRow); |
| 290 |
}else if( nCycle>0 ){ |
| 291 |
int nSp = p->u.pGraph->nWidth - 2; |
| 292 |
if( p->spec.eStyle==QRF_STYLE_StatsEst ){ |
| 293 |
sqlite3_str_appendchar(p->pOut, nSp, ' '); |
| 294 |
sqlite3_str_appendall(p->pOut, |
| 295 |
"Cycles Loops (est) Rows (est)\n"); |
| 296 |
sqlite3_str_appendchar(p->pOut, nSp, ' '); |
| 297 |
sqlite3_str_appendall(p->pOut, |
| 298 |
"---------- ------------ ------------\n"); |
| 299 |
}else{ |
| 300 |
sqlite3_str_appendchar(p->pOut, nSp, ' '); |
| 301 |
sqlite3_str_appendall(p->pOut, |
| 302 |
"Cycles Loops Rows \n"); |
| 303 |
sqlite3_str_appendchar(p->pOut, nSp, ' '); |
| 304 |
sqlite3_str_appendall(p->pOut, |
| 305 |
"---------- ----- -----\n"); |
| 306 |
} |
| 307 |
sqlite3_str_appendall(p->pOut, "QUERY PLAN"); |
| 308 |
sqlite3_str_appendchar(p->pOut, nSp - 10, ' '); |
| 309 |
qrfApproxInt64(p->pOut, nCycle); |
| 310 |
sqlite3_str_appendall(p->pOut, " 100%\n"); |
| 311 |
}else{ |
| 312 |
sqlite3_str_appendall(p->pOut, "QUERY PLAN\n"); |
| 313 |
} |
| 314 |
p->u.pGraph->zPrefix[0] = 0; |
| 315 |
qrfEqpRenderLevel(p, 0); |
| 316 |
qrfEqpReset(p); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 321 |
/* |
| 322 |
** Helper function for qrfExpStats(). |
| 323 |
** |
| 324 |
*/ |
| 325 |
static int qrfStatsHeight(sqlite3_stmt *p, int iEntry){ |
| 326 |
int iPid = 0; |
| 327 |
int ret = 1; |
| 328 |
sqlite3_stmt_scanstatus_v2(p, iEntry, |
| 329 |
SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid |
| 330 |
); |
| 331 |
while( iPid!=0 ){ |
| 332 |
int ii; |
| 333 |
for(ii=0; 1; ii++){ |
| 334 |
int iId; |
| 335 |
int res; |
| 336 |
res = sqlite3_stmt_scanstatus_v2(p, ii, |
| 337 |
SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId |
| 338 |
); |
| 339 |
if( res ) break; |
| 340 |
if( iId==iPid ){ |
| 341 |
sqlite3_stmt_scanstatus_v2(p, ii, |
| 342 |
SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid |
| 343 |
); |
| 344 |
} |
| 345 |
} |
| 346 |
ret++; |
| 347 |
} |
| 348 |
return ret; |
| 349 |
} |
| 350 |
#endif /* SQLITE_ENABLE_STMT_SCANSTATUS */ |
| 351 |
|
| 352 |
|
| 353 |
/* |
| 354 |
** Generate ".scanstatus est" style of EQP output. |
| 355 |
*/ |
| 356 |
static void qrfEqpStats(Qrf *p){ |
| 357 |
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS |
| 358 |
qrfError(p, SQLITE_ERROR, "not available in this build"); |
| 359 |
#else |
| 360 |
static const int f = SQLITE_SCANSTAT_COMPLEX; |
| 361 |
sqlite3_stmt *pS = p->pStmt; |
| 362 |
int i = 0; |
| 363 |
i64 nTotal = 0; |
| 364 |
int nWidth = 0; |
| 365 |
int prevPid = -1; /* Previous iPid */ |
| 366 |
double rEstCum = 1.0; /* Cumulative row estimate */ |
| 367 |
sqlite3_str *pLine = sqlite3_str_new(p->db); |
| 368 |
sqlite3_str *pStats = sqlite3_str_new(p->db); |
| 369 |
qrfEqpReset(p); |
| 370 |
|
| 371 |
for(i=0; 1; i++){ |
| 372 |
const char *z = 0; |
| 373 |
int n = 0; |
| 374 |
if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){ |
| 375 |
break; |
| 376 |
} |
| 377 |
n = (int)strlen(z) + qrfStatsHeight(pS,i)*3; |
| 378 |
if( n>nWidth ) nWidth = n; |
| 379 |
} |
| 380 |
nWidth += 2; |
| 381 |
|
| 382 |
sqlite3_stmt_scanstatus_v2(pS,-1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal); |
| 383 |
for(i=0; 1; i++){ |
| 384 |
i64 nLoop = 0; |
| 385 |
i64 nRow = 0; |
| 386 |
i64 nCycle = 0; |
| 387 |
int iId = 0; |
| 388 |
int iPid = 0; |
| 389 |
const char *zo = 0; |
| 390 |
const char *zName = 0; |
| 391 |
double rEst = 0.0; |
| 392 |
|
| 393 |
if( sqlite3_stmt_scanstatus_v2(pS,i,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){ |
| 394 |
break; |
| 395 |
} |
| 396 |
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid); |
| 397 |
if( iPid!=prevPid ){ |
| 398 |
prevPid = iPid; |
| 399 |
rEstCum = 1.0; |
| 400 |
} |
| 401 |
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_EST,f,(void*)&rEst); |
| 402 |
rEstCum *= rEst; |
| 403 |
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop); |
| 404 |
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow); |
| 405 |
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle); |
| 406 |
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId); |
| 407 |
sqlite3_stmt_scanstatus_v2(pS,i, SQLITE_SCANSTAT_NAME,f,(void*)&zName); |
| 408 |
|
| 409 |
if( nCycle>=0 || nLoop>=0 || nRow>=0 ){ |
| 410 |
int nSp = 0; |
| 411 |
sqlite3_str_reset(pStats); |
| 412 |
if( nCycle>=0 && nTotal>0 ){ |
| 413 |
qrfApproxInt64(pStats, nCycle); |
| 414 |
sqlite3_str_appendf(pStats, " %3d%%", |
| 415 |
((nCycle*100)+nTotal/2) / nTotal |
| 416 |
); |
| 417 |
nSp = 2; |
| 418 |
} |
| 419 |
if( nLoop>=0 ){ |
| 420 |
if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' '); |
| 421 |
qrfApproxInt64(pStats, nLoop); |
| 422 |
nSp = 2; |
| 423 |
if( p->spec.eStyle==QRF_STYLE_StatsEst ){ |
| 424 |
sqlite3_str_appendf(pStats, " "); |
| 425 |
qrfApproxInt64(pStats, (i64)(rEstCum/rEst)); |
| 426 |
} |
| 427 |
} |
| 428 |
if( nRow>=0 ){ |
| 429 |
if( nSp ) sqlite3_str_appendchar(pStats, nSp, ' '); |
| 430 |
qrfApproxInt64(pStats, nRow); |
| 431 |
nSp = 2; |
| 432 |
if( p->spec.eStyle==QRF_STYLE_StatsEst ){ |
| 433 |
sqlite3_str_appendf(pStats, " "); |
| 434 |
qrfApproxInt64(pStats, (i64)rEstCum); |
| 435 |
} |
| 436 |
} |
| 437 |
sqlite3_str_appendf(pLine, |
| 438 |
"% *s %s", -1*(nWidth-qrfStatsHeight(pS,i)*3), zo, |
| 439 |
sqlite3_str_value(pStats) |
| 440 |
); |
| 441 |
sqlite3_str_reset(pStats); |
| 442 |
qrfEqpAppend(p, iId, iPid, sqlite3_str_value(pLine)); |
| 443 |
sqlite3_str_reset(pLine); |
| 444 |
}else{ |
| 445 |
qrfEqpAppend(p, iId, iPid, zo); |
| 446 |
} |
| 447 |
} |
| 448 |
if( p->u.pGraph ) p->u.pGraph->nWidth = nWidth; |
| 449 |
qrfStrErr(p, pLine); |
| 450 |
sqlite3_free(sqlite3_str_finish(pLine)); |
| 451 |
qrfStrErr(p, pStats); |
| 452 |
sqlite3_free(sqlite3_str_finish(pStats)); |
| 453 |
#endif |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
/* |
| 458 |
** Reset the prepared statement. |
| 459 |
*/ |
| 460 |
static void qrfResetStmt(Qrf *p){ |
| 461 |
int rc = sqlite3_reset(p->pStmt); |
| 462 |
if( rc!=SQLITE_OK && p->iErr==SQLITE_OK ){ |
| 463 |
qrfError(p, rc, "%s", sqlite3_errmsg(p->db)); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
/* |
| 468 |
** If xWrite is defined, send all content of pOut to xWrite and |
| 469 |
** reset pOut. |
| 470 |
*/ |
| 471 |
static void qrfWrite(Qrf *p){ |
| 472 |
int n; |
| 473 |
if( p->spec.xWrite && (n = sqlite3_str_length(p->pOut))>0 ){ |
| 474 |
int rc = p->spec.xWrite(p->spec.pWriteArg, |
| 475 |
sqlite3_str_value(p->pOut), |
| 476 |
(sqlite3_int64)n); |
| 477 |
sqlite3_str_reset(p->pOut); |
| 478 |
if( rc ){ |
| 479 |
qrfError(p, rc, "Failed to write %d bytes of output", n); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/* Lookup table to estimate the number of columns consumed by a Unicode |
| 485 |
** character. |
| 486 |
*/ |
| 487 |
static const struct { |
| 488 |
unsigned char w; /* Width of the character in columns */ |
| 489 |
int iFirst; /* First character in a span having this width */ |
| 490 |
} aQrfUWidth[] = { |
| 491 |
/* {1, 0x00000}, */ |
| 492 |
{0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488}, |
| 493 |
{1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0}, |
| 494 |
{0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7}, |
| 495 |
{1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616}, |
| 496 |
{0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6}, |
| 497 |
{1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee}, |
| 498 |
{0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730}, |
| 499 |
{1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4}, |
| 500 |
{0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941}, |
| 501 |
{1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955}, |
| 502 |
{0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc}, |
| 503 |
{1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce}, |
| 504 |
{0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c}, |
| 505 |
{1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49}, |
| 506 |
{0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81}, |
| 507 |
{1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6}, |
| 508 |
{0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2}, |
| 509 |
{1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d}, |
| 510 |
{0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d}, |
| 511 |
{1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83}, |
| 512 |
{0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e}, |
| 513 |
{1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e}, |
| 514 |
{0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf}, |
| 515 |
{1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce}, |
| 516 |
{0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d}, |
| 517 |
{1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5}, |
| 518 |
{0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34}, |
| 519 |
{1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2}, |
| 520 |
{0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8}, |
| 521 |
{1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36}, |
| 522 |
{0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71}, |
| 523 |
{1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88}, |
| 524 |
{0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6}, |
| 525 |
{1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033}, |
| 526 |
{0, 0x01036}, {1, 0x0103b}, {0, 0x01058}, |
| 527 |
{1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f}, |
| 528 |
{1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735}, |
| 529 |
{0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4}, |
| 530 |
{1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7}, |
| 531 |
{0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b}, |
| 532 |
{1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923}, |
| 533 |
{0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939}, |
| 534 |
{1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04}, |
| 535 |
{0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c}, |
| 536 |
{1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74}, |
| 537 |
{0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b}, |
| 538 |
{1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064}, |
| 539 |
{0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329}, |
| 540 |
{1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f}, |
| 541 |
{2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806}, |
| 542 |
{1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827}, |
| 543 |
{2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e}, |
| 544 |
{1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20}, |
| 545 |
{1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00}, |
| 546 |
{1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc}, |
| 547 |
{0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c}, |
| 548 |
{1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40}, |
| 549 |
{0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185}, |
| 550 |
{1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245}, |
| 551 |
{2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001}, |
| 552 |
{1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0} |
| 553 |
}; |
| 554 |
|
| 555 |
/* |
| 556 |
** Return an estimate of the width, in columns, for the single Unicode |
| 557 |
** character c. For normal characters, the answer is always 1. But the |
| 558 |
** estimate might be 0 or 2 for zero-width and double-width characters. |
| 559 |
** |
| 560 |
** Different display devices display unicode using different widths. So |
| 561 |
** it is impossible to know that true display width with 100% accuracy. |
| 562 |
** Inaccuracies in the width estimates might cause columns to be misaligned. |
| 563 |
** Unfortunately, there is nothing we can do about that. |
| 564 |
*/ |
| 565 |
int sqlite3_qrf_wcwidth(int c){ |
| 566 |
int iFirst, iLast; |
| 567 |
|
| 568 |
/* Fast path for common characters */ |
| 569 |
if( c<0x300 ) return 1; |
| 570 |
|
| 571 |
/* The general case */ |
| 572 |
iFirst = 0; |
| 573 |
iLast = sizeof(aQrfUWidth)/sizeof(aQrfUWidth[0]) - 1; |
| 574 |
while( iFirst<iLast-1 ){ |
| 575 |
int iMid = (iFirst+iLast)/2; |
| 576 |
int cMid = aQrfUWidth[iMid].iFirst; |
| 577 |
if( cMid < c ){ |
| 578 |
iFirst = iMid; |
| 579 |
}else if( cMid > c ){ |
| 580 |
iLast = iMid - 1; |
| 581 |
}else{ |
| 582 |
return aQrfUWidth[iMid].w; |
| 583 |
} |
| 584 |
} |
| 585 |
if( aQrfUWidth[iLast].iFirst > c ) return aQrfUWidth[iFirst].w; |
| 586 |
return aQrfUWidth[iLast].w; |
| 587 |
} |
| 588 |
|
| 589 |
/* |
| 590 |
** Compute the value and length of a multi-byte UTF-8 character that |
| 591 |
** begins at z[0]. Return the length. Write the Unicode value into *pU. |
| 592 |
** |
| 593 |
** This routine only works for *multi-byte* UTF-8 characters. It does |
| 594 |
** not attempt to detect illegal characters. |
| 595 |
*/ |
| 596 |
int sqlite3_qrf_decode_utf8(const unsigned char *z, int *pU){ |
| 597 |
if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){ |
| 598 |
*pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f); |
| 599 |
return 2; |
| 600 |
} |
| 601 |
if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){ |
| 602 |
*pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f); |
| 603 |
return 3; |
| 604 |
} |
| 605 |
if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 |
| 606 |
&& (z[3] & 0xc0)==0x80 |
| 607 |
){ |
| 608 |
*pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6 |
| 609 |
| (z[3] & 0x3f); |
| 610 |
return 4; |
| 611 |
} |
| 612 |
*pU = 0; |
| 613 |
return 1; |
| 614 |
} |
| 615 |
|
| 616 |
/* |
| 617 |
** Check to see if z[] is a valid VT100 escape. If it is, then |
| 618 |
** return the number of bytes in the escape sequence. Return 0 if |
| 619 |
** z[] is not a VT100 escape. |
| 620 |
** |
| 621 |
** This routine assumes that z[0] is \033 (ESC). |
| 622 |
*/ |
| 623 |
static int qrfIsVt100(const unsigned char *z){ |
| 624 |
int i; |
| 625 |
if( z[1]!='[' ) return 0; |
| 626 |
i = 2; |
| 627 |
while( z[i]>=0x30 && z[i]<=0x3f ){ i++; } |
| 628 |
while( z[i]>=0x20 && z[i]<=0x2f ){ i++; } |
| 629 |
if( z[i]<0x40 || z[i]>0x7e ) return 0; |
| 630 |
return i+1; |
| 631 |
} |
| 632 |
|
| 633 |
/* |
| 634 |
** Return the length of a string in display characters. |
| 635 |
** |
| 636 |
** Most characters of the input string count as 1, including |
| 637 |
** multi-byte UTF8 characters. However, zero-width unicode |
| 638 |
** characters and VT100 escape sequences count as zero, and |
| 639 |
** double-width characters count as two. |
| 640 |
** |
| 641 |
** The definition of "zero-width" and "double-width" characters |
| 642 |
** is not precise. It depends on the output device, to some extent, |
| 643 |
** and it varies according to the Unicode version. This routine |
| 644 |
** makes the best guess that it can. |
| 645 |
*/ |
| 646 |
size_t sqlite3_qrf_wcswidth(const char *zIn){ |
| 647 |
const unsigned char *z = (const unsigned char*)zIn; |
| 648 |
size_t n = 0; |
| 649 |
while( *z ){ |
| 650 |
if( z[0]<' ' ){ |
| 651 |
int k; |
| 652 |
if( z[0]=='\033' && (k = qrfIsVt100(z))>0 ){ |
| 653 |
z += k; |
| 654 |
}else{ |
| 655 |
z++; |
| 656 |
} |
| 657 |
}else if( (0x80&z[0])==0 ){ |
| 658 |
n++; |
| 659 |
z++; |
| 660 |
}else{ |
| 661 |
int u = 0; |
| 662 |
int len = sqlite3_qrf_decode_utf8(z, &u); |
| 663 |
z += len; |
| 664 |
n += sqlite3_qrf_wcwidth(u); |
| 665 |
} |
| 666 |
} |
| 667 |
return n; |
| 668 |
} |
| 669 |
|
| 670 |
/* |
| 671 |
** Return the display width of the longest line of text |
| 672 |
** in the (possibly) multi-line input string zIn[0..nByte]. |
| 673 |
** zIn[] is not necessarily zero-terminated. Take |
| 674 |
** into account tab characters, zero- and double-width |
| 675 |
** characters, CR and NL, and VT100 escape codes. |
| 676 |
** |
| 677 |
** Write the number of newlines into *pnNL. So, *pnNL will |
| 678 |
** return 0 if everything fits on one line, or positive it |
| 679 |
** it will need to be split. |
| 680 |
*/ |
| 681 |
static int qrfDisplayWidth(const char *zIn, sqlite3_int64 nByte, int *pnNL){ |
| 682 |
const unsigned char *z; |
| 683 |
const unsigned char *zEnd; |
| 684 |
int mx = 0; |
| 685 |
int n = 0; |
| 686 |
int nNL = 0; |
| 687 |
if( zIn==0 ) zIn = ""; |
| 688 |
z = (const unsigned char*)zIn; |
| 689 |
zEnd = &z[nByte]; |
| 690 |
while( z<zEnd ){ |
| 691 |
if( z[0]<' ' ){ |
| 692 |
int k; |
| 693 |
if( z[0]=='\033' && (k = qrfIsVt100(z))>0 ){ |
| 694 |
z += k; |
| 695 |
}else{ |
| 696 |
if( z[0]=='\t' ){ |
| 697 |
n = (n+8)&~7; |
| 698 |
}else if( z[0]=='\n' || z[0]=='\r' ){ |
| 699 |
nNL++; |
| 700 |
if( n>mx ) mx = n; |
| 701 |
n = 0; |
| 702 |
} |
| 703 |
z++; |
| 704 |
} |
| 705 |
}else if( (0x80&z[0])==0 ){ |
| 706 |
n++; |
| 707 |
z++; |
| 708 |
}else{ |
| 709 |
int u = 0; |
| 710 |
int len = sqlite3_qrf_decode_utf8(z, &u); |
| 711 |
z += len; |
| 712 |
n += sqlite3_qrf_wcwidth(u); |
| 713 |
} |
| 714 |
} |
| 715 |
if( mx>n ) n = mx; |
| 716 |
if( pnNL ) *pnNL = nNL; |
| 717 |
return n; |
| 718 |
} |
| 719 |
|
| 720 |
/* |
| 721 |
** Escape the input string if it is needed and in accordance with |
| 722 |
** eEsc, which is either QRF_ESC_Ascii or QRF_ESC_Symbol. |
| 723 |
** |
| 724 |
** Escaping is needed if the string contains any control characters |
| 725 |
** other than \t, \n, and \r\n |
| 726 |
** |
| 727 |
** If no escaping is needed (the common case) then set *ppOut to NULL |
| 728 |
** and return 0. If escaping is needed, write the escaped string into |
| 729 |
** memory obtained from sqlite3_malloc64() and make *ppOut point to that |
| 730 |
** memory and return 0. If an error occurs, return non-zero. |
| 731 |
** |
| 732 |
** The caller is responsible for freeing *ppFree if it is non-NULL in order |
| 733 |
** to reclaim memory. |
| 734 |
*/ |
| 735 |
static void qrfEscape( |
| 736 |
int eEsc, /* QRF_ESC_Ascii or QRF_ESC_Symbol */ |
| 737 |
sqlite3_str *pStr, /* String to be escaped */ |
| 738 |
int iStart /* Begin escapding on this byte of pStr */ |
| 739 |
){ |
| 740 |
sqlite3_int64 i, j; /* Loop counters */ |
| 741 |
sqlite3_int64 sz; /* Size of the string prior to escaping */ |
| 742 |
sqlite3_int64 nCtrl = 0;/* Number of control characters to escape */ |
| 743 |
unsigned char *zIn; /* Text to be escaped */ |
| 744 |
unsigned char c; /* A single character of the text */ |
| 745 |
unsigned char *zOut; /* Where to write the results */ |
| 746 |
|
| 747 |
/* Find the text to be escaped */ |
| 748 |
zIn = (unsigned char*)sqlite3_str_value(pStr); |
| 749 |
if( zIn==0 ) return; |
| 750 |
zIn += iStart; |
| 751 |
|
| 752 |
/* Count the control characters */ |
| 753 |
for(i=0; (c = zIn[i])!=0; i++){ |
| 754 |
if( c<=0x1f |
| 755 |
&& c!='\t' |
| 756 |
&& c!='\n' |
| 757 |
&& (c!='\r' || zIn[i+1]!='\n') |
| 758 |
){ |
| 759 |
nCtrl++; |
| 760 |
} |
| 761 |
} |
| 762 |
if( nCtrl==0 ) return; /* Early out if no control characters */ |
| 763 |
|
| 764 |
/* Make space to hold the escapes. Copy the original text to the end |
| 765 |
** of the available space. */ |
| 766 |
sz = sqlite3_str_length(pStr) - iStart; |
| 767 |
if( eEsc==QRF_ESC_Symbol ) nCtrl *= 2; |
| 768 |
sqlite3_str_appendchar(pStr, nCtrl, ' '); |
| 769 |
zOut = (unsigned char*)sqlite3_str_value(pStr); |
| 770 |
if( zOut==0 ) return; |
| 771 |
zOut += iStart; |
| 772 |
zIn = zOut + nCtrl; |
| 773 |
memmove(zIn,zOut,sz); |
| 774 |
|
| 775 |
/* Convert the control characters */ |
| 776 |
for(i=j=0; (c = zIn[i])!=0; i++){ |
| 777 |
if( c>0x1f |
| 778 |
|| c=='\t' |
| 779 |
|| c=='\n' |
| 780 |
|| (c=='\r' && zIn[i+1]=='\n') |
| 781 |
){ |
| 782 |
continue; |
| 783 |
} |
| 784 |
if( i>0 ){ |
| 785 |
memmove(&zOut[j], zIn, i); |
| 786 |
j += i; |
| 787 |
} |
| 788 |
zIn += i+1; |
| 789 |
i = -1; |
| 790 |
if( eEsc==QRF_ESC_Symbol ){ |
| 791 |
zOut[j++] = 0xe2; |
| 792 |
zOut[j++] = 0x90; |
| 793 |
zOut[j++] = 0x80+c; |
| 794 |
}else{ |
| 795 |
zOut[j++] = '^'; |
| 796 |
zOut[j++] = 0x40+c; |
| 797 |
} |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
/* |
| 802 |
** Determine if the string z[] can be shown as plain text. Return true |
| 803 |
** if z[] is unambiguously text. Return false if z[] needs to be |
| 804 |
** quoted. |
| 805 |
** |
| 806 |
** All of the following must be true in order for z[] to be relaxable: |
| 807 |
** |
| 808 |
** (1) z[] does not begin or end with ' or whitespace |
| 809 |
** (2) z[] is not the same as the NULL rendering |
| 810 |
** (3) z[] does not looks like a numeric literal |
| 811 |
*/ |
| 812 |
static int qrfRelaxable(Qrf *p, const char *z){ |
| 813 |
size_t i, n; |
| 814 |
if( z[0]=='\'' || qrfSpace(z[0]) ) return 0; |
| 815 |
if( z[0]==0 ){ |
| 816 |
return (p->spec.zNull!=0 && p->spec.zNull[0]!=0); |
| 817 |
} |
| 818 |
n = strlen(z); |
| 819 |
if( n==0 || z[n-1]=='\'' || qrfSpace(z[n-1]) ) return 0; |
| 820 |
if( p->spec.zNull && strcmp(p->spec.zNull,z)==0 ) return 0; |
| 821 |
i = (z[0]=='-' || z[0]=='+'); |
| 822 |
if( strcmp(z+i,"Inf")==0 ) return 0; |
| 823 |
if( !qrfDigit(z[i]) ) return 1; |
| 824 |
i++; |
| 825 |
while( qrfDigit(z[i]) ){ i++; } |
| 826 |
if( z[i]==0 ) return 0; |
| 827 |
if( z[i]=='.' ){ |
| 828 |
i++; |
| 829 |
while( qrfDigit(z[i]) ){ i++; } |
| 830 |
if( z[i]==0 ) return 0; |
| 831 |
} |
| 832 |
if( z[i]=='e' || z[i]=='E' ){ |
| 833 |
i++; |
| 834 |
if( z[i]=='+' || z[i]=='-' ){ i++; } |
| 835 |
if( !qrfDigit(z[i]) ) return 1; |
| 836 |
i++; |
| 837 |
while( qrfDigit(z[i]) ){ i++; } |
| 838 |
} |
| 839 |
return z[i]!=0; |
| 840 |
} |
| 841 |
|
| 842 |
/* |
| 843 |
** If a field contains any character identified by a 1 in the following |
| 844 |
** array, then the string must be quoted for CSV. |
| 845 |
*/ |
| 846 |
static const char qrfCsvQuote[] = { |
| 847 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 848 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 849 |
1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, |
| 850 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 851 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 852 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 853 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 854 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, |
| 855 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 856 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 857 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 858 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 859 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 860 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 861 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 862 |
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 863 |
}; |
| 864 |
|
| 865 |
/* |
| 866 |
** Encode text appropriately and append it to pOut. |
| 867 |
*/ |
| 868 |
static void qrfEncodeText(Qrf *p, sqlite3_str *pOut, const char *zTxt){ |
| 869 |
int iStart = sqlite3_str_length(pOut); |
| 870 |
switch( p->spec.eText ){ |
| 871 |
case QRF_TEXT_Relaxed: |
| 872 |
if( qrfRelaxable(p, zTxt) ){ |
| 873 |
sqlite3_str_appendall(pOut, zTxt); |
| 874 |
break; |
| 875 |
} |
| 876 |
deliberate_fall_through; /* FALLTHRU */ |
| 877 |
case QRF_TEXT_Sql: { |
| 878 |
if( p->spec.eEsc==QRF_ESC_Off ){ |
| 879 |
sqlite3_str_appendf(pOut, "%Q", zTxt); |
| 880 |
}else{ |
| 881 |
sqlite3_str_appendf(pOut, "%#Q", zTxt); |
| 882 |
} |
| 883 |
break; |
| 884 |
} |
| 885 |
case QRF_TEXT_Csv: { |
| 886 |
unsigned int i; |
| 887 |
for(i=0; zTxt[i]; i++){ |
| 888 |
if( qrfCsvQuote[((const unsigned char*)zTxt)[i]] ){ |
| 889 |
i = 0; |
| 890 |
break; |
| 891 |
} |
| 892 |
} |
| 893 |
if( i==0 || strstr(zTxt, p->spec.zColumnSep)!=0 ){ |
| 894 |
sqlite3_str_appendf(pOut, "\"%w\"", zTxt); |
| 895 |
}else{ |
| 896 |
sqlite3_str_appendall(pOut, zTxt); |
| 897 |
} |
| 898 |
break; |
| 899 |
} |
| 900 |
case QRF_TEXT_Html: { |
| 901 |
const unsigned char *z = (const unsigned char*)zTxt; |
| 902 |
while( *z ){ |
| 903 |
unsigned int i = 0; |
| 904 |
unsigned char c; |
| 905 |
while( (c=z[i])>'>' |
| 906 |
|| (c && c!='<' && c!='>' && c!='&' && c!='\"' && c!='\'') |
| 907 |
){ |
| 908 |
i++; |
| 909 |
} |
| 910 |
if( i>0 ){ |
| 911 |
sqlite3_str_append(pOut, (const char*)z, i); |
| 912 |
} |
| 913 |
switch( z[i] ){ |
| 914 |
case '>': sqlite3_str_append(pOut, ">", 4); break; |
| 915 |
case '&': sqlite3_str_append(pOut, "&", 5); break; |
| 916 |
case '<': sqlite3_str_append(pOut, "<", 4); break; |
| 917 |
case '"': sqlite3_str_append(pOut, """, 6); break; |
| 918 |
case '\'': sqlite3_str_append(pOut, "'", 5); break; |
| 919 |
default: i--; |
| 920 |
} |
| 921 |
z += i + 1; |
| 922 |
} |
| 923 |
break; |
| 924 |
} |
| 925 |
case QRF_TEXT_Tcl: |
| 926 |
case QRF_TEXT_Json: { |
| 927 |
const unsigned char *z = (const unsigned char*)zTxt; |
| 928 |
sqlite3_str_append(pOut, "\"", 1); |
| 929 |
while( *z ){ |
| 930 |
unsigned int i; |
| 931 |
for(i=0; z[i]>=0x20 && z[i]!='\\' && z[i]!='"'; i++){} |
| 932 |
if( i>0 ){ |
| 933 |
sqlite3_str_append(pOut, (const char*)z, i); |
| 934 |
} |
| 935 |
if( z[i]==0 ) break; |
| 936 |
switch( z[i] ){ |
| 937 |
case '"': sqlite3_str_append(pOut, "\\\"", 2); break; |
| 938 |
case '\\': sqlite3_str_append(pOut, "\\\\", 2); break; |
| 939 |
case '\b': sqlite3_str_append(pOut, "\\b", 2); break; |
| 940 |
case '\f': sqlite3_str_append(pOut, "\\f", 2); break; |
| 941 |
case '\n': sqlite3_str_append(pOut, "\\n", 2); break; |
| 942 |
case '\r': sqlite3_str_append(pOut, "\\r", 2); break; |
| 943 |
case '\t': sqlite3_str_append(pOut, "\\t", 2); break; |
| 944 |
default: { |
| 945 |
if( p->spec.eText==QRF_TEXT_Json ){ |
| 946 |
sqlite3_str_appendf(pOut, "\\u%04x", z[i]); |
| 947 |
}else{ |
| 948 |
sqlite3_str_appendf(pOut, "\\%03o", z[i]); |
| 949 |
} |
| 950 |
break; |
| 951 |
} |
| 952 |
} |
| 953 |
z += i + 1; |
| 954 |
} |
| 955 |
sqlite3_str_append(pOut, "\"", 1); |
| 956 |
break; |
| 957 |
} |
| 958 |
default: { |
| 959 |
sqlite3_str_appendall(pOut, zTxt); |
| 960 |
break; |
| 961 |
} |
| 962 |
} |
| 963 |
if( p->spec.eEsc!=QRF_ESC_Off ){ |
| 964 |
qrfEscape(p->spec.eEsc, pOut, iStart); |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
/* |
| 969 |
** Do a quick sanity check to see aBlob[0..nBlob-1] is valid JSONB |
| 970 |
** return true if it is and false if it is not. |
| 971 |
** |
| 972 |
** False positives are possible, but not false negatives. |
| 973 |
*/ |
| 974 |
static int qrfJsonbQuickCheck(unsigned char *aBlob, int nBlob){ |
| 975 |
unsigned char x; /* Payload size half-byte */ |
| 976 |
int i; /* Loop counter */ |
| 977 |
int n; /* Bytes in the payload size integer */ |
| 978 |
sqlite3_uint64 sz; /* value of the payload size integer */ |
| 979 |
|
| 980 |
if( nBlob==0 ) return 0; |
| 981 |
x = aBlob[0]>>4; |
| 982 |
if( x<=11 ) return nBlob==(1+x); |
| 983 |
n = x<14 ? x-11 : 4*(x-13); |
| 984 |
if( nBlob<1+n ) return 0; |
| 985 |
sz = aBlob[1]; |
| 986 |
for(i=1; i<n; i++) sz = (sz<<8) + aBlob[i+1]; |
| 987 |
return sz+n+1==(sqlite3_uint64)nBlob; |
| 988 |
} |
| 989 |
|
| 990 |
/* |
| 991 |
** The current iCol-th column of p->pStmt is known to be a BLOB. Check |
| 992 |
** to see if that BLOB is really a JSONB blob. If it is, then translate |
| 993 |
** it into a text JSON representation and return a pointer to that text JSON. |
| 994 |
** If the BLOB is not JSONB, then return a NULL pointer. |
| 995 |
** |
| 996 |
** The memory used to hold the JSON text is managed internally by the |
| 997 |
** "p" object and is overwritten and/or deallocated upon the next call |
| 998 |
** to this routine (with the same p argument) or when the p object is |
| 999 |
** finailized. |
| 1000 |
*/ |
| 1001 |
static const char *qrfJsonbToJson(Qrf *p, int iCol){ |
| 1002 |
int nByte; |
| 1003 |
const void *pBlob; |
| 1004 |
int rc; |
| 1005 |
nByte = sqlite3_column_bytes(p->pStmt, iCol); |
| 1006 |
pBlob = sqlite3_column_blob(p->pStmt, iCol); |
| 1007 |
if( qrfJsonbQuickCheck((unsigned char*)pBlob, nByte)==0 ){ |
| 1008 |
return 0; |
| 1009 |
} |
| 1010 |
if( p->pJTrans==0 ){ |
| 1011 |
sqlite3 *db; |
| 1012 |
rc = sqlite3_open(":memory:",&db); |
| 1013 |
if( rc ){ |
| 1014 |
sqlite3_close(db); |
| 1015 |
return 0; |
| 1016 |
} |
| 1017 |
rc = sqlite3_prepare_v2(db, "SELECT json(?1)", -1, &p->pJTrans, 0); |
| 1018 |
if( rc ){ |
| 1019 |
sqlite3_finalize(p->pJTrans); |
| 1020 |
p->pJTrans = 0; |
| 1021 |
sqlite3_close(db); |
| 1022 |
return 0; |
| 1023 |
} |
| 1024 |
}else{ |
| 1025 |
sqlite3_reset(p->pJTrans); |
| 1026 |
} |
| 1027 |
sqlite3_bind_blob(p->pJTrans, 1, (void*)pBlob, nByte, SQLITE_STATIC); |
| 1028 |
rc = sqlite3_step(p->pJTrans); |
| 1029 |
if( rc==SQLITE_ROW ){ |
| 1030 |
return (const char*)sqlite3_column_text(p->pJTrans, 0); |
| 1031 |
}else{ |
| 1032 |
return 0; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* |
| 1037 |
** Adjust the input string zIn[] such that it is no more than N display |
| 1038 |
** characters wide. If it is wider than that, then truncate and add |
| 1039 |
** ellipsis. Or if zIn[] contains a \r or \n, truncate at that point, |
| 1040 |
** adding ellipsis. Embedded tabs in zIn[] are converted into ordinary |
| 1041 |
** spaces. |
| 1042 |
** |
| 1043 |
** Return this display width of the modified title string. |
| 1044 |
*/ |
| 1045 |
static int qrfTitleLimit(char *zIn, int N){ |
| 1046 |
unsigned char *z = (unsigned char*)zIn; |
| 1047 |
int n = 0; |
| 1048 |
unsigned char *zEllipsis = 0; |
| 1049 |
while( 1 /*exit-by-break*/ ){ |
| 1050 |
if( z[0]<' ' ){ |
| 1051 |
int k; |
| 1052 |
if( z[0]==0 ){ |
| 1053 |
zEllipsis = 0; |
| 1054 |
break; |
| 1055 |
}else if( z[0]=='\033' && (k = qrfIsVt100(z))>0 ){ |
| 1056 |
z += k; |
| 1057 |
}else if( z[0]=='\t' ){ |
| 1058 |
z[0] = ' '; |
| 1059 |
}else if( z[0]=='\n' || z[0]=='\r' ){ |
| 1060 |
z[0] = ' '; |
| 1061 |
}else{ |
| 1062 |
z++; |
| 1063 |
} |
| 1064 |
}else if( (0x80&z[0])==0 ){ |
| 1065 |
if( n>=(N-3) && zEllipsis==0 ) zEllipsis = z; |
| 1066 |
if( n==N ){ z[0] = 0; break; } |
| 1067 |
n++; |
| 1068 |
z++; |
| 1069 |
}else{ |
| 1070 |
int u = 0; |
| 1071 |
int len = sqlite3_qrf_decode_utf8(z, &u); |
| 1072 |
if( n+len>(N-3) && zEllipsis==0 ) zEllipsis = z; |
| 1073 |
if( n+len>N ){ z[0] = 0; break; } |
| 1074 |
z += len; |
| 1075 |
n += sqlite3_qrf_wcwidth(u); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
if( zEllipsis && N>=3 ) memcpy(zEllipsis,"...",4); |
| 1079 |
return n; |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
/* |
| 1084 |
** Render value pVal into pOut |
| 1085 |
*/ |
| 1086 |
static void qrfRenderValue(Qrf *p, sqlite3_str *pOut, int iCol){ |
| 1087 |
#if SQLITE_VERSION_NUMBER>=3052000 |
| 1088 |
int iStartLen = sqlite3_str_length(pOut); |
| 1089 |
#endif |
| 1090 |
if( p->spec.xRender ){ |
| 1091 |
sqlite3_value *pVal; |
| 1092 |
char *z; |
| 1093 |
pVal = sqlite3_value_dup(sqlite3_column_value(p->pStmt,iCol)); |
| 1094 |
z = p->spec.xRender(p->spec.pRenderArg, pVal); |
| 1095 |
sqlite3_value_free(pVal); |
| 1096 |
if( z ){ |
| 1097 |
sqlite3_str_appendall(pOut, z); |
| 1098 |
sqlite3_free(z); |
| 1099 |
return; |
| 1100 |
} |
| 1101 |
} |
| 1102 |
switch( sqlite3_column_type(p->pStmt,iCol) ){ |
| 1103 |
case SQLITE_INTEGER: { |
| 1104 |
sqlite3_str_appendf(pOut, "%lld", sqlite3_column_int64(p->pStmt,iCol)); |
| 1105 |
break; |
| 1106 |
} |
| 1107 |
case SQLITE_FLOAT: { |
| 1108 |
const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol); |
| 1109 |
sqlite3_str_appendall(pOut, zTxt); |
| 1110 |
break; |
| 1111 |
} |
| 1112 |
case SQLITE_BLOB: { |
| 1113 |
if( p->spec.bTextJsonb==QRF_Yes ){ |
| 1114 |
const char *zJson = qrfJsonbToJson(p, iCol); |
| 1115 |
if( zJson ){ |
| 1116 |
if( p->spec.eText==QRF_TEXT_Sql ){ |
| 1117 |
sqlite3_str_append(pOut,"jsonb(",6); |
| 1118 |
qrfEncodeText(p, pOut, zJson); |
| 1119 |
sqlite3_str_append(pOut,")",1); |
| 1120 |
}else{ |
| 1121 |
qrfEncodeText(p, pOut, zJson); |
| 1122 |
} |
| 1123 |
break; |
| 1124 |
} |
| 1125 |
} |
| 1126 |
switch( p->spec.eBlob ){ |
| 1127 |
case QRF_BLOB_Hex: |
| 1128 |
case QRF_BLOB_Sql: { |
| 1129 |
int iStart; |
| 1130 |
int nBlob = sqlite3_column_bytes(p->pStmt,iCol); |
| 1131 |
int i, j; |
| 1132 |
char *zVal; |
| 1133 |
const unsigned char *a = sqlite3_column_blob(p->pStmt,iCol); |
| 1134 |
if( p->spec.eBlob==QRF_BLOB_Sql ){ |
| 1135 |
sqlite3_str_append(pOut, "x'", 2); |
| 1136 |
} |
| 1137 |
iStart = sqlite3_str_length(pOut); |
| 1138 |
sqlite3_str_appendchar(pOut, nBlob, ' '); |
| 1139 |
sqlite3_str_appendchar(pOut, nBlob, ' '); |
| 1140 |
if( p->spec.eBlob==QRF_BLOB_Sql ){ |
| 1141 |
sqlite3_str_appendchar(pOut, 1, '\''); |
| 1142 |
} |
| 1143 |
if( sqlite3_str_errcode(pOut) ) return; |
| 1144 |
zVal = sqlite3_str_value(pOut); |
| 1145 |
for(i=0, j=iStart; i<nBlob; i++, j+=2){ |
| 1146 |
unsigned char c = a[i]; |
| 1147 |
zVal[j] = "0123456789abcdef"[(c>>4)&0xf]; |
| 1148 |
zVal[j+1] = "0123456789abcdef"[(c)&0xf]; |
| 1149 |
} |
| 1150 |
break; |
| 1151 |
} |
| 1152 |
case QRF_BLOB_Tcl: |
| 1153 |
case QRF_BLOB_Json: { |
| 1154 |
int iStart; |
| 1155 |
int nBlob = sqlite3_column_bytes(p->pStmt,iCol); |
| 1156 |
int i, j; |
| 1157 |
char *zVal; |
| 1158 |
const unsigned char *a = sqlite3_column_blob(p->pStmt,iCol); |
| 1159 |
int szC = p->spec.eBlob==QRF_BLOB_Json ? 6 : 4; |
| 1160 |
sqlite3_str_append(pOut, "\"", 1); |
| 1161 |
iStart = sqlite3_str_length(pOut); |
| 1162 |
for(i=szC; i>0; i--){ |
| 1163 |
sqlite3_str_appendchar(pOut, nBlob, ' '); |
| 1164 |
} |
| 1165 |
sqlite3_str_appendchar(pOut, 1, '"'); |
| 1166 |
if( sqlite3_str_errcode(pOut) ) return; |
| 1167 |
zVal = sqlite3_str_value(pOut); |
| 1168 |
for(i=0, j=iStart; i<nBlob; i++, j+=szC){ |
| 1169 |
unsigned char c = a[i]; |
| 1170 |
zVal[j] = '\\'; |
| 1171 |
if( szC==4 ){ |
| 1172 |
zVal[j+1] = '0' + ((c>>6)&3); |
| 1173 |
zVal[j+2] = '0' + ((c>>3)&7); |
| 1174 |
zVal[j+3] = '0' + (c&7); |
| 1175 |
}else{ |
| 1176 |
zVal[j+1] = 'u'; |
| 1177 |
zVal[j+2] = '0'; |
| 1178 |
zVal[j+3] = '0'; |
| 1179 |
zVal[j+4] = "0123456789abcdef"[(c>>4)&0xf]; |
| 1180 |
zVal[j+5] = "0123456789abcdef"[(c)&0xf]; |
| 1181 |
} |
| 1182 |
} |
| 1183 |
break; |
| 1184 |
} |
| 1185 |
case QRF_BLOB_Size: { |
| 1186 |
int nBlob = sqlite3_column_bytes(p->pStmt,iCol); |
| 1187 |
sqlite3_str_appendf(pOut, "(%d-byte blob)", nBlob); |
| 1188 |
break; |
| 1189 |
} |
| 1190 |
default: { |
| 1191 |
const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol); |
| 1192 |
qrfEncodeText(p, pOut, zTxt); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
break; |
| 1196 |
} |
| 1197 |
case SQLITE_NULL: { |
| 1198 |
sqlite3_str_appendall(pOut, p->spec.zNull); |
| 1199 |
break; |
| 1200 |
} |
| 1201 |
case SQLITE_TEXT: { |
| 1202 |
const char *zTxt = (const char*)sqlite3_column_text(p->pStmt,iCol); |
| 1203 |
qrfEncodeText(p, pOut, zTxt); |
| 1204 |
break; |
| 1205 |
} |
| 1206 |
} |
| 1207 |
#if SQLITE_VERSION_NUMBER>=3052000 |
| 1208 |
if( p->spec.nCharLimit>0 |
| 1209 |
&& (sqlite3_str_length(pOut) - iStartLen) > p->spec.nCharLimit |
| 1210 |
){ |
| 1211 |
const unsigned char *z; |
| 1212 |
int ii = 0, w = 0, limit = p->spec.nCharLimit; |
| 1213 |
z = (const unsigned char*)sqlite3_str_value(pOut) + iStartLen; |
| 1214 |
if( limit<4 ) limit = 4; |
| 1215 |
while( 1 ){ |
| 1216 |
if( z[ii]<' ' ){ |
| 1217 |
int k; |
| 1218 |
if( z[ii]=='\033' && (k = qrfIsVt100(z+ii))>0 ){ |
| 1219 |
ii += k; |
| 1220 |
}else if( z[ii]==0 ){ |
| 1221 |
break; |
| 1222 |
}else{ |
| 1223 |
ii++; |
| 1224 |
} |
| 1225 |
}else if( (0x80&z[ii])==0 ){ |
| 1226 |
w++; |
| 1227 |
if( w>limit ) break; |
| 1228 |
ii++; |
| 1229 |
}else{ |
| 1230 |
int u = 0; |
| 1231 |
int len = sqlite3_qrf_decode_utf8(&z[ii], &u); |
| 1232 |
w += sqlite3_qrf_wcwidth(u); |
| 1233 |
if( w>limit ) break; |
| 1234 |
ii += len; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
if( w>limit ){ |
| 1238 |
sqlite3_str_truncate(pOut, iStartLen+ii); |
| 1239 |
sqlite3_str_append(pOut, "...", 3); |
| 1240 |
} |
| 1241 |
} |
| 1242 |
#endif |
| 1243 |
} |
| 1244 |
|
| 1245 |
/* Trim spaces of the end if pOut |
| 1246 |
*/ |
| 1247 |
static void qrfRTrim(sqlite3_str *pOut){ |
| 1248 |
#if SQLITE_VERSION_NUMBER>=3052000 |
| 1249 |
int nByte = sqlite3_str_length(pOut); |
| 1250 |
const char *zOut = sqlite3_str_value(pOut); |
| 1251 |
while( nByte>0 && zOut[nByte-1]==' ' ){ nByte--; } |
| 1252 |
sqlite3_str_truncate(pOut, nByte); |
| 1253 |
#endif |
| 1254 |
} |
| 1255 |
|
| 1256 |
/* |
| 1257 |
** Store string zUtf to pOut as w characters. If w is negative, |
| 1258 |
** then right-justify the text. W is the width in display characters, not |
| 1259 |
** in bytes. Double-width unicode characters count as two characters. |
| 1260 |
** VT100 escape sequences count as zero. And so forth. |
| 1261 |
*/ |
| 1262 |
static void qrfWidthPrint(Qrf *p, sqlite3_str *pOut, int w, const char *zUtf){ |
| 1263 |
const unsigned char *a = (const unsigned char*)zUtf; |
| 1264 |
static const int mxW = 10000000; |
| 1265 |
unsigned char c; |
| 1266 |
int i = 0; |
| 1267 |
int n = 0; |
| 1268 |
int k; |
| 1269 |
int aw; |
| 1270 |
(void)p; |
| 1271 |
if( w<-mxW ){ |
| 1272 |
w = -mxW; |
| 1273 |
}else if( w>mxW ){ |
| 1274 |
w= mxW; |
| 1275 |
} |
| 1276 |
aw = w<0 ? -w : w; |
| 1277 |
if( a==0 ) a = (const unsigned char*)""; |
| 1278 |
while( (c = a[i])!=0 ){ |
| 1279 |
if( (c&0xc0)==0xc0 ){ |
| 1280 |
int u; |
| 1281 |
int len = sqlite3_qrf_decode_utf8(a+i, &u); |
| 1282 |
int x = sqlite3_qrf_wcwidth(u); |
| 1283 |
if( x+n>aw ){ |
| 1284 |
break; |
| 1285 |
} |
| 1286 |
i += len; |
| 1287 |
n += x; |
| 1288 |
}else if( c==0x1b && (k = qrfIsVt100(&a[i]))>0 ){ |
| 1289 |
i += k; |
| 1290 |
}else if( n>=aw ){ |
| 1291 |
break; |
| 1292 |
}else{ |
| 1293 |
n++; |
| 1294 |
i++; |
| 1295 |
} |
| 1296 |
} |
| 1297 |
if( n>=aw ){ |
| 1298 |
sqlite3_str_append(pOut, zUtf, i); |
| 1299 |
}else if( w<0 ){ |
| 1300 |
if( aw>n ) sqlite3_str_appendchar(pOut, aw-n, ' '); |
| 1301 |
sqlite3_str_append(pOut, zUtf, i); |
| 1302 |
}else{ |
| 1303 |
sqlite3_str_append(pOut, zUtf, i); |
| 1304 |
if( aw>n ) sqlite3_str_appendchar(pOut, aw-n, ' '); |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
/* |
| 1309 |
** (*pz)[] is a line of text that is to be displayed the box or table or |
| 1310 |
** similar tabular formats. z[] contain newlines or might be too wide |
| 1311 |
** to fit in the columns so will need to be split into multiple line. |
| 1312 |
** |
| 1313 |
** This routine determines: |
| 1314 |
** |
| 1315 |
** * How many bytes of z[] should be shown on the current line. |
| 1316 |
** * How many character positions those bytes will cover. |
| 1317 |
** * The byte offset to the start of the next line. |
| 1318 |
*/ |
| 1319 |
static void qrfWrapLine( |
| 1320 |
const char *zIn, /* Input text to be displayed */ |
| 1321 |
int w, /* Column width in characters (not bytes) */ |
| 1322 |
int bWrap, /* True if we should do word-wrapping */ |
| 1323 |
int *pnThis, /* OUT: How many bytes of z[] for the current line */ |
| 1324 |
int *pnWide, /* OUT: How wide is the text of this line */ |
| 1325 |
int *piNext /* OUT: Offset into z[] to start of the next line */ |
| 1326 |
){ |
| 1327 |
int i; /* Input bytes consumed */ |
| 1328 |
int k; /* Bytes in a VT100 code */ |
| 1329 |
int n; /* Output column number */ |
| 1330 |
const unsigned char *z = (const unsigned char*)zIn; |
| 1331 |
unsigned char c = 0; |
| 1332 |
|
| 1333 |
if( z[0]==0 ){ |
| 1334 |
*pnThis = 0; |
| 1335 |
*pnWide = 0; |
| 1336 |
*piNext = 0; |
| 1337 |
return; |
| 1338 |
} |
| 1339 |
n = 0; |
| 1340 |
for(i=0; n<=w; i++){ |
| 1341 |
c = z[i]; |
| 1342 |
if( c>=0xc0 ){ |
| 1343 |
int u; |
| 1344 |
int len = sqlite3_qrf_decode_utf8(&z[i], &u); |
| 1345 |
int wcw = sqlite3_qrf_wcwidth(u); |
| 1346 |
if( wcw+n>w ) break; |
| 1347 |
i += len-1; |
| 1348 |
n += wcw; |
| 1349 |
continue; |
| 1350 |
} |
| 1351 |
if( c>=' ' ){ |
| 1352 |
if( n==w ) break; |
| 1353 |
n++; |
| 1354 |
continue; |
| 1355 |
} |
| 1356 |
if( c==0 || c=='\n' ) break; |
| 1357 |
if( c=='\r' && z[i+1]=='\n' ){ c = z[++i]; break; } |
| 1358 |
if( c=='\t' ){ |
| 1359 |
int wcw = 8 - (n&7); |
| 1360 |
if( n+wcw>w ) break; |
| 1361 |
n += wcw; |
| 1362 |
continue; |
| 1363 |
} |
| 1364 |
if( c==0x1b && (k = qrfIsVt100(&z[i]))>0 ){ |
| 1365 |
i += k-1; |
| 1366 |
}else if( n==w ){ |
| 1367 |
break; |
| 1368 |
}else{ |
| 1369 |
n++; |
| 1370 |
} |
| 1371 |
} |
| 1372 |
if( c==0 ){ |
| 1373 |
*pnThis = i; |
| 1374 |
*pnWide = n; |
| 1375 |
*piNext = i; |
| 1376 |
return; |
| 1377 |
} |
| 1378 |
if( c=='\n' ){ |
| 1379 |
*pnThis = i; |
| 1380 |
*pnWide = n; |
| 1381 |
*piNext = i+1; |
| 1382 |
return; |
| 1383 |
} |
| 1384 |
|
| 1385 |
/* If we get this far, that means the current line will end at some |
| 1386 |
** point that is neither a "\n" or a 0x00. Figure out where that |
| 1387 |
** split should occur |
| 1388 |
*/ |
| 1389 |
if( bWrap && z[i]!=0 && !qrfSpace(z[i]) && qrfAlnum(c)==qrfAlnum(z[i]) ){ |
| 1390 |
/* Perhaps try to back up to a better place to break the line */ |
| 1391 |
for(k=i-1; k>=i/2; k--){ |
| 1392 |
if( qrfSpace(z[k]) ) break; |
| 1393 |
} |
| 1394 |
if( k<i/2 && i/2>0 ){ |
| 1395 |
for(k=i; k>=i/2; k--){ |
| 1396 |
if( qrfAlnum(z[k-1])!=qrfAlnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; |
| 1397 |
} |
| 1398 |
} |
| 1399 |
if( k>=i/2 ){ |
| 1400 |
i = k; |
| 1401 |
n = qrfDisplayWidth((const char*)z, k, 0); |
| 1402 |
} |
| 1403 |
} |
| 1404 |
*pnThis = i; |
| 1405 |
*pnWide = n; |
| 1406 |
while( zIn[i]==' ' || zIn[i]=='\t' || zIn[i]=='\r' ){ i++; } |
| 1407 |
*piNext = i; |
| 1408 |
} |
| 1409 |
|
| 1410 |
/* |
| 1411 |
** Append nVal bytes of text from zVal onto the end of pOut. |
| 1412 |
** Convert tab characters in zVal to the appropriate number of |
| 1413 |
** spaces. |
| 1414 |
*/ |
| 1415 |
static void qrfAppendWithTabs( |
| 1416 |
sqlite3_str *pOut, /* Append text here */ |
| 1417 |
const char *zVal, /* Text to append */ |
| 1418 |
int nVal /* Use only the first nVal bytes of zVal[] */ |
| 1419 |
){ |
| 1420 |
int i = 0; |
| 1421 |
unsigned int col = 0; |
| 1422 |
unsigned char *z = (unsigned char *)zVal; |
| 1423 |
while( i<nVal ){ |
| 1424 |
unsigned char c = z[i]; |
| 1425 |
if( c<' ' ){ |
| 1426 |
int k; |
| 1427 |
sqlite3_str_append(pOut, (const char*)z, i); |
| 1428 |
nVal -= i; |
| 1429 |
z += i; |
| 1430 |
i = 0; |
| 1431 |
if( c=='\033' && (k = qrfIsVt100(z))>0 ){ |
| 1432 |
sqlite3_str_append(pOut, (const char*)z, k); |
| 1433 |
z += k; |
| 1434 |
nVal -= k; |
| 1435 |
}else if( c=='\t' ){ |
| 1436 |
k = 8 - (col&7); |
| 1437 |
sqlite3_str_appendchar(pOut, k, ' '); |
| 1438 |
col += k; |
| 1439 |
z++; |
| 1440 |
nVal--; |
| 1441 |
}else if( c=='\r' && nVal==1 ){ |
| 1442 |
z++; |
| 1443 |
nVal--; |
| 1444 |
}else{ |
| 1445 |
char zCtrlPik[4]; |
| 1446 |
col++; |
| 1447 |
zCtrlPik[0] = 0xe2; |
| 1448 |
zCtrlPik[1] = 0x90; |
| 1449 |
zCtrlPik[2] = 0x80+c; |
| 1450 |
sqlite3_str_append(pOut, zCtrlPik, 3); |
| 1451 |
z++; |
| 1452 |
nVal--; |
| 1453 |
} |
| 1454 |
}else if( (0x80&c)==0 ){ |
| 1455 |
i++; |
| 1456 |
col++; |
| 1457 |
}else{ |
| 1458 |
int u = 0; |
| 1459 |
int len = sqlite3_qrf_decode_utf8(&z[i], &u); |
| 1460 |
i += len; |
| 1461 |
col += sqlite3_qrf_wcwidth(u); |
| 1462 |
} |
| 1463 |
} |
| 1464 |
sqlite3_str_append(pOut, (const char*)z, i); |
| 1465 |
} |
| 1466 |
|
| 1467 |
/* |
| 1468 |
** GCC does not define the offsetof() macro so we'll have to do it |
| 1469 |
** ourselves. |
| 1470 |
*/ |
| 1471 |
#ifndef offsetof |
| 1472 |
# define offsetof(ST,M) ((size_t)((char*)&((ST*)0)->M - (char*)0)) |
| 1473 |
#endif |
| 1474 |
|
| 1475 |
/* |
| 1476 |
** Data for columnar layout, collected into a single object so |
| 1477 |
** that it can be more easily passed into subroutines. |
| 1478 |
*/ |
| 1479 |
typedef struct qrfColData qrfColData; |
| 1480 |
struct qrfColData { |
| 1481 |
Qrf *p; /* The QRF instance */ |
| 1482 |
int nCol; /* Number of columns in the table */ |
| 1483 |
unsigned char bMultiRow; /* One or more cells will span multiple lines */ |
| 1484 |
unsigned char nMargin; /* Width of column margins */ |
| 1485 |
sqlite3_int64 nRow; /* Number of rows */ |
| 1486 |
sqlite3_int64 nAlloc; /* Number of cells allocated */ |
| 1487 |
sqlite3_int64 n; /* Number of cells. nCol*nRow */ |
| 1488 |
char **az; /* Content of all cells */ |
| 1489 |
int *aiWth; /* Width of each cell */ |
| 1490 |
unsigned char *abNum; /* True for each numeric cell */ |
| 1491 |
struct qrfPerCol { /* Per-column data */ |
| 1492 |
char *z; /* Cache of text for current row */ |
| 1493 |
int w; /* Computed width of this column */ |
| 1494 |
int mxW; /* Maximum natural (unwrapped) width */ |
| 1495 |
unsigned char e; /* Alignment */ |
| 1496 |
unsigned char fx; /* Width is fixed */ |
| 1497 |
unsigned char bNum; /* True if is numeric */ |
| 1498 |
} *a; /* One per column */ |
| 1499 |
}; |
| 1500 |
|
| 1501 |
/* |
| 1502 |
** Output horizontally justified text into pOut. The text is the |
| 1503 |
** first nVal bytes of zVal. Include nWS bytes of whitespace, either |
| 1504 |
** split between both sides, or on the left, or on the right, depending |
| 1505 |
** on eAlign. |
| 1506 |
*/ |
| 1507 |
static void qrfPrintAligned( |
| 1508 |
sqlite3_str *pOut, /* Append text here */ |
| 1509 |
struct qrfPerCol *pCol, /* Information about the text to print */ |
| 1510 |
int nVal, /* Use only the first nVal bytes of zVal[] */ |
| 1511 |
int nWS /* Whitespace for horizonal alignment */ |
| 1512 |
){ |
| 1513 |
unsigned char eAlign = pCol->e & QRF_ALIGN_HMASK; |
| 1514 |
if( eAlign==QRF_Auto && pCol->bNum ) eAlign = QRF_ALIGN_Right; |
| 1515 |
if( eAlign==QRF_ALIGN_Center ){ |
| 1516 |
/* Center the text */ |
| 1517 |
sqlite3_str_appendchar(pOut, nWS/2, ' '); |
| 1518 |
qrfAppendWithTabs(pOut, pCol->z, nVal); |
| 1519 |
sqlite3_str_appendchar(pOut, nWS - nWS/2, ' '); |
| 1520 |
}else if( eAlign==QRF_ALIGN_Right ){ |
| 1521 |
/* Right justify the text */ |
| 1522 |
sqlite3_str_appendchar(pOut, nWS, ' '); |
| 1523 |
qrfAppendWithTabs(pOut, pCol->z, nVal); |
| 1524 |
}else{ |
| 1525 |
/* Left justify the text */ |
| 1526 |
qrfAppendWithTabs(pOut, pCol->z, nVal); |
| 1527 |
sqlite3_str_appendchar(pOut, nWS, ' '); |
| 1528 |
} |
| 1529 |
} |
| 1530 |
|
| 1531 |
/* |
| 1532 |
** Free all the memory allocates in the qrfColData object |
| 1533 |
*/ |
| 1534 |
static void qrfColDataFree(qrfColData *p){ |
| 1535 |
sqlite3_int64 i; |
| 1536 |
for(i=0; i<p->n; i++) sqlite3_free(p->az[i]); |
| 1537 |
sqlite3_free(p->az); |
| 1538 |
sqlite3_free(p->aiWth); |
| 1539 |
sqlite3_free(p->abNum); |
| 1540 |
sqlite3_free(p->a); |
| 1541 |
memset(p, 0, sizeof(*p)); |
| 1542 |
} |
| 1543 |
|
| 1544 |
/* |
| 1545 |
** Allocate space for more cells in the qrfColData object. |
| 1546 |
** Return non-zero if a memory allocation fails. |
| 1547 |
*/ |
| 1548 |
static int qrfColDataEnlarge(qrfColData *p){ |
| 1549 |
char **azData; |
| 1550 |
int *aiWth; |
| 1551 |
unsigned char *abNum; |
| 1552 |
p->nAlloc = 2*p->nAlloc + 10*p->nCol; |
| 1553 |
azData = sqlite3_realloc64(p->az, p->nAlloc*sizeof(char*)); |
| 1554 |
if( azData==0 ){ |
| 1555 |
qrfOom(p->p); |
| 1556 |
qrfColDataFree(p); |
| 1557 |
return 1; |
| 1558 |
} |
| 1559 |
p->az = azData; |
| 1560 |
aiWth = sqlite3_realloc64(p->aiWth, p->nAlloc*sizeof(int)); |
| 1561 |
if( aiWth==0 ){ |
| 1562 |
qrfOom(p->p); |
| 1563 |
qrfColDataFree(p); |
| 1564 |
return 1; |
| 1565 |
} |
| 1566 |
p->aiWth = aiWth; |
| 1567 |
abNum = sqlite3_realloc64(p->abNum, p->nAlloc); |
| 1568 |
if( abNum==0 ){ |
| 1569 |
qrfOom(p->p); |
| 1570 |
qrfColDataFree(p); |
| 1571 |
return 1; |
| 1572 |
} |
| 1573 |
p->abNum = abNum; |
| 1574 |
return 0; |
| 1575 |
} |
| 1576 |
|
| 1577 |
/* |
| 1578 |
** Print a markdown or table-style row separator using ascii-art |
| 1579 |
*/ |
| 1580 |
static void qrfRowSeparator(sqlite3_str *pOut, qrfColData *p, char cSep){ |
| 1581 |
int i; |
| 1582 |
if( p->nCol>0 ){ |
| 1583 |
int useBorder = p->p->spec.bBorder!=QRF_No; |
| 1584 |
if( useBorder ){ |
| 1585 |
sqlite3_str_append(pOut, &cSep, 1); |
| 1586 |
} |
| 1587 |
sqlite3_str_appendchar(pOut, p->a[0].w+p->nMargin, '-'); |
| 1588 |
for(i=1; i<p->nCol; i++){ |
| 1589 |
sqlite3_str_append(pOut, &cSep, 1); |
| 1590 |
sqlite3_str_appendchar(pOut, p->a[i].w+p->nMargin, '-'); |
| 1591 |
} |
| 1592 |
if( useBorder ){ |
| 1593 |
sqlite3_str_append(pOut, &cSep, 1); |
| 1594 |
} |
| 1595 |
} |
| 1596 |
sqlite3_str_append(pOut, "\n", 1); |
| 1597 |
} |
| 1598 |
|
| 1599 |
/* |
| 1600 |
** UTF8 box-drawing characters. Imagine box lines like this: |
| 1601 |
** |
| 1602 |
** 1 |
| 1603 |
** | |
| 1604 |
** 4 --+-- 2 |
| 1605 |
** | |
| 1606 |
** 3 |
| 1607 |
** |
| 1608 |
** Each box characters has between 2 and 4 of the lines leading from |
| 1609 |
** the center. The characters are here identified by the numbers of |
| 1610 |
** their corresponding lines. |
| 1611 |
*/ |
| 1612 |
#define BOX_24 "\342\224\200" /* U+2500 --- */ |
| 1613 |
#define BOX_13 "\342\224\202" /* U+2502 | */ |
| 1614 |
#define BOX_23 "\342\224\214" /* U+250c ,- */ |
| 1615 |
#define BOX_34 "\342\224\220" /* U+2510 -, */ |
| 1616 |
#define BOX_12 "\342\224\224" /* U+2514 '- */ |
| 1617 |
#define BOX_14 "\342\224\230" /* U+2518 -' */ |
| 1618 |
#define BOX_123 "\342\224\234" /* U+251c |- */ |
| 1619 |
#define BOX_134 "\342\224\244" /* U+2524 -| */ |
| 1620 |
#define BOX_234 "\342\224\254" /* U+252c -,- */ |
| 1621 |
#define BOX_124 "\342\224\264" /* U+2534 -'- */ |
| 1622 |
#define BOX_1234 "\342\224\274" /* U+253c -|- */ |
| 1623 |
|
| 1624 |
/* Rounded corners: */ |
| 1625 |
#define BOX_R12 "\342\225\260" /* U+2570 '- */ |
| 1626 |
#define BOX_R23 "\342\225\255" /* U+256d ,- */ |
| 1627 |
#define BOX_R34 "\342\225\256" /* U+256e -, */ |
| 1628 |
#define BOX_R14 "\342\225\257" /* U+256f -' */ |
| 1629 |
|
| 1630 |
/* Doubled horizontal lines: */ |
| 1631 |
#define DBL_24 "\342\225\220" /* U+2550 === */ |
| 1632 |
#define DBL_123 "\342\225\236" /* U+255e |= */ |
| 1633 |
#define DBL_134 "\342\225\241" /* U+2561 =| */ |
| 1634 |
#define DBL_1234 "\342\225\252" /* U+256a =|= */ |
| 1635 |
|
| 1636 |
/* Draw horizontal line N characters long using unicode box |
| 1637 |
** characters |
| 1638 |
*/ |
| 1639 |
static void qrfBoxLine(sqlite3_str *pOut, int N, int bDbl){ |
| 1640 |
const char *azDash[2] = { |
| 1641 |
BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24, |
| 1642 |
DBL_24 DBL_24 DBL_24 DBL_24 DBL_24 DBL_24 DBL_24 DBL_24 DBL_24 DBL_24 |
| 1643 |
};/* 0 1 2 3 4 5 6 7 8 9 */ |
| 1644 |
const int nDash = 30; |
| 1645 |
N *= 3; |
| 1646 |
while( N>nDash ){ |
| 1647 |
sqlite3_str_append(pOut, azDash[bDbl], nDash); |
| 1648 |
N -= nDash; |
| 1649 |
} |
| 1650 |
sqlite3_str_append(pOut, azDash[bDbl], N); |
| 1651 |
} |
| 1652 |
|
| 1653 |
/* |
| 1654 |
** Draw a horizontal separator for a QRF_STYLE_Box table. |
| 1655 |
*/ |
| 1656 |
static void qrfBoxSeparator( |
| 1657 |
sqlite3_str *pOut, |
| 1658 |
qrfColData *p, |
| 1659 |
const char *zSep1, |
| 1660 |
const char *zSep2, |
| 1661 |
const char *zSep3, |
| 1662 |
int bDbl |
| 1663 |
){ |
| 1664 |
int i; |
| 1665 |
if( p->nCol>0 ){ |
| 1666 |
int useBorder = p->p->spec.bBorder!=QRF_No; |
| 1667 |
if( useBorder ){ |
| 1668 |
sqlite3_str_appendall(pOut, zSep1); |
| 1669 |
} |
| 1670 |
qrfBoxLine(pOut, p->a[0].w+p->nMargin, bDbl); |
| 1671 |
for(i=1; i<p->nCol; i++){ |
| 1672 |
sqlite3_str_appendall(pOut, zSep2); |
| 1673 |
qrfBoxLine(pOut, p->a[i].w+p->nMargin, bDbl); |
| 1674 |
} |
| 1675 |
if( useBorder ){ |
| 1676 |
sqlite3_str_appendall(pOut, zSep3); |
| 1677 |
} |
| 1678 |
} |
| 1679 |
sqlite3_str_append(pOut, "\n", 1); |
| 1680 |
} |
| 1681 |
|
| 1682 |
/* |
| 1683 |
** Load into pData the default alignment for the body of a table. |
| 1684 |
*/ |
| 1685 |
static void qrfLoadAlignment(qrfColData *pData, Qrf *p){ |
| 1686 |
sqlite3_int64 i; |
| 1687 |
for(i=0; i<pData->nCol; i++){ |
| 1688 |
pData->a[i].e = p->spec.eDfltAlign; |
| 1689 |
if( i<p->spec.nAlign ){ |
| 1690 |
unsigned char ax = p->spec.aAlign[i]; |
| 1691 |
if( (ax & QRF_ALIGN_HMASK)!=0 ){ |
| 1692 |
pData->a[i].e = (ax & QRF_ALIGN_HMASK) | |
| 1693 |
(pData->a[i].e & QRF_ALIGN_VMASK); |
| 1694 |
} |
| 1695 |
}else if( i<p->spec.nWidth ){ |
| 1696 |
if( p->spec.aWidth[i]<0 ){ |
| 1697 |
pData->a[i].e = QRF_ALIGN_Right | |
| 1698 |
(pData->a[i].e & QRF_ALIGN_VMASK); |
| 1699 |
} |
| 1700 |
} |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
/* |
| 1705 |
** If the single column in pData->a[] with pData->n entries can be |
| 1706 |
** laid out as nCol columns with a 2-space gap between each such |
| 1707 |
** that all columns fit within nSW, then return a pointer to an array |
| 1708 |
** of integers which is the width of each column from left to right. |
| 1709 |
** |
| 1710 |
** If the layout is not possible, return a NULL pointer. |
| 1711 |
** |
| 1712 |
** Space to hold the returned array is from sqlite_malloc64(). |
| 1713 |
*/ |
| 1714 |
static int *qrfValidLayout( |
| 1715 |
qrfColData *pData, /* Collected query results */ |
| 1716 |
Qrf *p, /* On which to report an OOM */ |
| 1717 |
int nCol, /* Attempt this many columns */ |
| 1718 |
int nSW /* Screen width */ |
| 1719 |
){ |
| 1720 |
int i; /* Loop counter */ |
| 1721 |
int nr; /* Number of rows */ |
| 1722 |
int w = 0; /* Width of the current column */ |
| 1723 |
int t; /* Total width of all columns */ |
| 1724 |
int *aw; /* Array of individual column widths */ |
| 1725 |
|
| 1726 |
aw = sqlite3_malloc64( sizeof(int)*nCol ); |
| 1727 |
if( aw==0 ){ |
| 1728 |
qrfOom(p); |
| 1729 |
return 0; |
| 1730 |
} |
| 1731 |
nr = (pData->n + nCol - 1)/nCol; |
| 1732 |
for(i=0; i<pData->n; i++){ |
| 1733 |
if( (i%nr)==0 ){ |
| 1734 |
if( i>0 ) aw[i/nr-1] = w; |
| 1735 |
w = pData->aiWth[i]; |
| 1736 |
}else if( pData->aiWth[i]>w ){ |
| 1737 |
w = pData->aiWth[i]; |
| 1738 |
} |
| 1739 |
} |
| 1740 |
aw[nCol-1] = w; |
| 1741 |
for(t=i=0; i<nCol; i++) t += aw[i]; |
| 1742 |
t += 2*(nCol-1); |
| 1743 |
if( t>nSW ){ |
| 1744 |
sqlite3_free(aw); |
| 1745 |
return 0; |
| 1746 |
} |
| 1747 |
return aw; |
| 1748 |
} |
| 1749 |
|
| 1750 |
/* |
| 1751 |
** The output is single-column and the bSplitColumn flag is set. |
| 1752 |
** Check to see if the single-column output can be split into multiple |
| 1753 |
** columns that appear side-by-side. Adjust pData appropriately. |
| 1754 |
*/ |
| 1755 |
static void qrfSplitColumn(qrfColData *pData, Qrf *p){ |
| 1756 |
int nCol = 1; |
| 1757 |
int *aw = 0; |
| 1758 |
char **az = 0; |
| 1759 |
int *aiWth = 0; |
| 1760 |
unsigned char *abNum = 0; |
| 1761 |
int nColNext = 2; |
| 1762 |
int w; |
| 1763 |
struct qrfPerCol *a = 0; |
| 1764 |
sqlite3_int64 nRow = 1; |
| 1765 |
sqlite3_int64 i; |
| 1766 |
while( 1/*exit-by-break*/ ){ |
| 1767 |
int *awNew = qrfValidLayout(pData, p, nColNext, p->spec.nScreenWidth); |
| 1768 |
if( awNew==0 ) break; |
| 1769 |
sqlite3_free(aw); |
| 1770 |
aw = awNew; |
| 1771 |
nCol = nColNext; |
| 1772 |
nRow = (pData->n + nCol - 1)/nCol; |
| 1773 |
if( nRow==1 ) break; |
| 1774 |
nColNext++; |
| 1775 |
while( (pData->n + nColNext - 1)/nColNext == nRow ) nColNext++; |
| 1776 |
} |
| 1777 |
if( nCol==1 ){ |
| 1778 |
sqlite3_free(aw); |
| 1779 |
return; /* Cannot do better than 1 column */ |
| 1780 |
} |
| 1781 |
az = sqlite3_malloc64( nRow*nCol*sizeof(char*) ); |
| 1782 |
if( az==0 ){ |
| 1783 |
qrfOom(p); |
| 1784 |
return; |
| 1785 |
} |
| 1786 |
aiWth = sqlite3_malloc64( nRow*nCol*sizeof(int) ); |
| 1787 |
if( aiWth==0 ){ |
| 1788 |
sqlite3_free(az); |
| 1789 |
qrfOom(p); |
| 1790 |
return; |
| 1791 |
} |
| 1792 |
a = sqlite3_malloc64( nCol*sizeof(struct qrfPerCol) ); |
| 1793 |
if( a==0 ){ |
| 1794 |
sqlite3_free(az); |
| 1795 |
sqlite3_free(aiWth); |
| 1796 |
qrfOom(p); |
| 1797 |
return; |
| 1798 |
} |
| 1799 |
abNum = sqlite3_malloc64( nRow*nCol ); |
| 1800 |
if( abNum==0 ){ |
| 1801 |
sqlite3_free(az); |
| 1802 |
sqlite3_free(aiWth); |
| 1803 |
sqlite3_free(a); |
| 1804 |
qrfOom(p); |
| 1805 |
return; |
| 1806 |
} |
| 1807 |
for(i=0; i<pData->n; i++){ |
| 1808 |
sqlite3_int64 j = (i%nRow)*nCol + (i/nRow); |
| 1809 |
az[j] = pData->az[i]; |
| 1810 |
abNum[j]= pData->abNum[i]; |
| 1811 |
pData->az[i] = 0; |
| 1812 |
aiWth[j] = pData->aiWth[i]; |
| 1813 |
} |
| 1814 |
while( i<nRow*nCol ){ |
| 1815 |
sqlite3_int64 j = (i%nRow)*nCol + (i/nRow); |
| 1816 |
az[j] = sqlite3_mprintf(""); |
| 1817 |
if( az[j]==0 ) qrfOom(p); |
| 1818 |
aiWth[j] = 0; |
| 1819 |
abNum[j] = 0; |
| 1820 |
i++; |
| 1821 |
} |
| 1822 |
for(i=0; i<nCol; i++){ |
| 1823 |
a[i].fx = a[i].mxW = a[i].w = aw[i]; |
| 1824 |
a[i].e = pData->a[0].e; |
| 1825 |
} |
| 1826 |
sqlite3_free(pData->az); |
| 1827 |
sqlite3_free(pData->aiWth); |
| 1828 |
sqlite3_free(pData->a); |
| 1829 |
sqlite3_free(pData->abNum); |
| 1830 |
sqlite3_free(aw); |
| 1831 |
pData->az = az; |
| 1832 |
pData->aiWth = aiWth; |
| 1833 |
pData->a = a; |
| 1834 |
pData->abNum = abNum; |
| 1835 |
pData->nCol = nCol; |
| 1836 |
pData->n = pData->nAlloc = nRow*nCol; |
| 1837 |
for(i=w=0; i<nCol; i++) w += a[i].w; |
| 1838 |
pData->nMargin = (p->spec.nScreenWidth - w)/(nCol - 1); |
| 1839 |
if( pData->nMargin>5 ) pData->nMargin = 5; |
| 1840 |
} |
| 1841 |
|
| 1842 |
/* |
| 1843 |
** Adjust the layout for the screen width restriction |
| 1844 |
*/ |
| 1845 |
static void qrfRestrictScreenWidth(qrfColData *pData, Qrf *p){ |
| 1846 |
int sepW; /* Width of all box separators and margins */ |
| 1847 |
int sumW; /* Total width of data area over all columns */ |
| 1848 |
int targetW; /* Desired total data area */ |
| 1849 |
int i; /* Loop counters */ |
| 1850 |
int nCol; /* Number of columns */ |
| 1851 |
|
| 1852 |
pData->nMargin = 2; /* Default to normal margins */ |
| 1853 |
if( p->spec.nScreenWidth==0 ) return; |
| 1854 |
if( p->spec.eStyle==QRF_STYLE_Column ){ |
| 1855 |
sepW = pData->nCol*2 - 2; |
| 1856 |
}else{ |
| 1857 |
sepW = pData->nCol*3 + 1; |
| 1858 |
if( p->spec.bBorder==QRF_No ) sepW -= 2; |
| 1859 |
} |
| 1860 |
nCol = pData->nCol; |
| 1861 |
for(i=sumW=0; i<nCol; i++) sumW += pData->a[i].w; |
| 1862 |
if( p->spec.nScreenWidth >= sumW+sepW ) return; |
| 1863 |
|
| 1864 |
/* First thing to do is reduce the separation between columns */ |
| 1865 |
pData->nMargin = 0; |
| 1866 |
if( p->spec.eStyle==QRF_STYLE_Column ){ |
| 1867 |
sepW = pData->nCol - 1; |
| 1868 |
}else{ |
| 1869 |
sepW = pData->nCol + 1; |
| 1870 |
if( p->spec.bBorder==QRF_No ) sepW -= 2; |
| 1871 |
} |
| 1872 |
targetW = p->spec.nScreenWidth - sepW; |
| 1873 |
|
| 1874 |
#define MIN_SQUOZE 8 |
| 1875 |
#define MIN_EX_SQUOZE 16 |
| 1876 |
/* Reduce the width of the widest eligible column. A column is |
| 1877 |
** eligible for narrowing if: |
| 1878 |
** |
| 1879 |
** * It is not a fixed-width column (a[0].fx is false) |
| 1880 |
** * The current width is more than MIN_SQUOZE |
| 1881 |
** * Either: |
| 1882 |
** + The current width is more then MIN_EX_SQUOZE, or |
| 1883 |
** + The current width is more than half the max width (a[].mxW) |
| 1884 |
** |
| 1885 |
** Keep making reductions until either no more reductions are |
| 1886 |
** possible or until the size target is reached. |
| 1887 |
*/ |
| 1888 |
while( sumW > targetW ){ |
| 1889 |
int gain, w; |
| 1890 |
int ix = -1; |
| 1891 |
int mx = 0; |
| 1892 |
for(i=0; i<nCol; i++){ |
| 1893 |
if( pData->a[i].fx==0 |
| 1894 |
&& (w = pData->a[i].w)>mx |
| 1895 |
&& w>MIN_SQUOZE |
| 1896 |
&& (w>MIN_EX_SQUOZE || w*2>pData->a[i].mxW) |
| 1897 |
){ |
| 1898 |
ix = i; |
| 1899 |
mx = w; |
| 1900 |
} |
| 1901 |
} |
| 1902 |
if( ix<0 ) break; |
| 1903 |
if( mx>=MIN_SQUOZE*2 ){ |
| 1904 |
gain = mx/2; |
| 1905 |
}else{ |
| 1906 |
gain = mx - MIN_SQUOZE; |
| 1907 |
} |
| 1908 |
if( sumW - gain < targetW ){ |
| 1909 |
gain = sumW - targetW; |
| 1910 |
} |
| 1911 |
sumW -= gain; |
| 1912 |
pData->a[ix].w -= gain; |
| 1913 |
pData->bMultiRow = 1; |
| 1914 |
} |
| 1915 |
} |
| 1916 |
|
| 1917 |
/* |
| 1918 |
** Columnar modes require that the entire query be evaluated first, with |
| 1919 |
** results written into memory, so that we can compute appropriate column |
| 1920 |
** widths. |
| 1921 |
*/ |
| 1922 |
static void qrfColumnar(Qrf *p){ |
| 1923 |
sqlite3_int64 i, j; /* Loop counters */ |
| 1924 |
const char *colSep = 0; /* Column separator text */ |
| 1925 |
const char *rowSep = 0; /* Row terminator text */ |
| 1926 |
const char *rowStart = 0; /* Row start text */ |
| 1927 |
int szColSep, szRowSep, szRowStart; /* Size in bytes of previous 3 */ |
| 1928 |
int rc; /* Result code */ |
| 1929 |
int nColumn = p->nCol; /* Number of columns */ |
| 1930 |
int bWW; /* True to do word-wrap */ |
| 1931 |
sqlite3_str *pStr; /* Temporary rendering */ |
| 1932 |
qrfColData data; /* Columnar layout data */ |
| 1933 |
int bRTrim; /* Trim trailing space */ |
| 1934 |
|
| 1935 |
rc = sqlite3_step(p->pStmt); |
| 1936 |
if( rc!=SQLITE_ROW || nColumn==0 ){ |
| 1937 |
return; /* No output */ |
| 1938 |
} |
| 1939 |
|
| 1940 |
/* Initialize the data container */ |
| 1941 |
memset(&data, 0, sizeof(data)); |
| 1942 |
data.nCol = p->nCol; |
| 1943 |
data.p = p; |
| 1944 |
data.a = sqlite3_malloc64( nColumn*sizeof(struct qrfPerCol) ); |
| 1945 |
if( data.a==0 ){ |
| 1946 |
qrfOom(p); |
| 1947 |
return; |
| 1948 |
} |
| 1949 |
memset(data.a, 0, nColumn*sizeof(struct qrfPerCol) ); |
| 1950 |
if( qrfColDataEnlarge(&data) ) return; |
| 1951 |
assert( data.az!=0 ); |
| 1952 |
|
| 1953 |
/* Load the column header names and all cell content into data */ |
| 1954 |
if( p->spec.bTitles==QRF_Yes ){ |
| 1955 |
unsigned char saved_eText = p->spec.eText; |
| 1956 |
p->spec.eText = p->spec.eTitle; |
| 1957 |
memset(data.abNum, 0, nColumn); |
| 1958 |
for(i=0; i<nColumn; i++){ |
| 1959 |
const char *z = (const char*)sqlite3_column_name(p->pStmt,i); |
| 1960 |
int nNL = 0; |
| 1961 |
int n, w; |
| 1962 |
pStr = sqlite3_str_new(p->db); |
| 1963 |
qrfEncodeText(p, pStr, z ? z : ""); |
| 1964 |
n = sqlite3_str_length(pStr); |
| 1965 |
qrfStrErr(p, pStr); |
| 1966 |
z = data.az[data.n] = sqlite3_str_finish(pStr); |
| 1967 |
if( p->spec.nTitleLimit ){ |
| 1968 |
nNL = 0; |
| 1969 |
data.aiWth[data.n] = w = qrfTitleLimit(data.az[data.n], |
| 1970 |
p->spec.nTitleLimit ); |
| 1971 |
}else{ |
| 1972 |
data.aiWth[data.n] = w = qrfDisplayWidth(z, n, &nNL); |
| 1973 |
} |
| 1974 |
data.n++; |
| 1975 |
if( w>data.a[i].mxW ) data.a[i].mxW = w; |
| 1976 |
if( nNL ) data.bMultiRow = 1; |
| 1977 |
} |
| 1978 |
p->spec.eText = saved_eText; |
| 1979 |
p->nRow++; |
| 1980 |
} |
| 1981 |
do{ |
| 1982 |
if( data.n+nColumn > data.nAlloc ){ |
| 1983 |
if( qrfColDataEnlarge(&data) ) return; |
| 1984 |
} |
| 1985 |
for(i=0; i<nColumn; i++){ |
| 1986 |
char *z; |
| 1987 |
int nNL = 0; |
| 1988 |
int n, w; |
| 1989 |
int eType = sqlite3_column_type(p->pStmt,i); |
| 1990 |
pStr = sqlite3_str_new(p->db); |
| 1991 |
qrfRenderValue(p, pStr, i); |
| 1992 |
n = sqlite3_str_length(pStr); |
| 1993 |
qrfStrErr(p, pStr); |
| 1994 |
z = data.az[data.n] = sqlite3_str_finish(pStr); |
| 1995 |
data.abNum[data.n] = eType==SQLITE_INTEGER || eType==SQLITE_FLOAT; |
| 1996 |
data.aiWth[data.n] = w = qrfDisplayWidth(z, n, &nNL); |
| 1997 |
data.n++; |
| 1998 |
if( w>data.a[i].mxW ) data.a[i].mxW = w; |
| 1999 |
if( nNL ) data.bMultiRow = 1; |
| 2000 |
} |
| 2001 |
p->nRow++; |
| 2002 |
}while( sqlite3_step(p->pStmt)==SQLITE_ROW && p->iErr==SQLITE_OK ); |
| 2003 |
if( p->iErr ){ |
| 2004 |
qrfColDataFree(&data); |
| 2005 |
return; |
| 2006 |
} |
| 2007 |
|
| 2008 |
/* Compute the width and alignment of every column */ |
| 2009 |
if( p->spec.bTitles==QRF_No ){ |
| 2010 |
qrfLoadAlignment(&data, p); |
| 2011 |
}else{ |
| 2012 |
unsigned char e; |
| 2013 |
if( p->spec.eTitleAlign==QRF_Auto ){ |
| 2014 |
e = QRF_ALIGN_Center; |
| 2015 |
}else{ |
| 2016 |
e = p->spec.eTitleAlign; |
| 2017 |
} |
| 2018 |
for(i=0; i<nColumn; i++) data.a[i].e = e; |
| 2019 |
} |
| 2020 |
|
| 2021 |
for(i=0; i<nColumn; i++){ |
| 2022 |
int w = 0; |
| 2023 |
if( i<p->spec.nWidth ){ |
| 2024 |
w = p->spec.aWidth[i]; |
| 2025 |
if( w==(-32768) ){ |
| 2026 |
w = 0; |
| 2027 |
if( p->spec.nAlign>i && (p->spec.aAlign[i] & QRF_ALIGN_HMASK)==0 ){ |
| 2028 |
data.a[i].e |= QRF_ALIGN_Right; |
| 2029 |
} |
| 2030 |
}else if( w<0 ){ |
| 2031 |
w = -w; |
| 2032 |
if( p->spec.nAlign>i && (p->spec.aAlign[i] & QRF_ALIGN_HMASK)==0 ){ |
| 2033 |
data.a[i].e |= QRF_ALIGN_Right; |
| 2034 |
} |
| 2035 |
} |
| 2036 |
if( w ) data.a[i].fx = 1; |
| 2037 |
} |
| 2038 |
if( w==0 ){ |
| 2039 |
w = data.a[i].mxW; |
| 2040 |
if( p->spec.nWrap>0 && w>p->spec.nWrap ){ |
| 2041 |
w = p->spec.nWrap; |
| 2042 |
data.bMultiRow = 1; |
| 2043 |
} |
| 2044 |
}else if( (data.bMultiRow==0 || w==1) && data.a[i].mxW>w ){ |
| 2045 |
data.bMultiRow = 1; |
| 2046 |
if( w==1 ){ |
| 2047 |
/* If aiWth[j] is 2 or more, then there might be a double-wide |
| 2048 |
** character somewhere. So make the column width at least 2. */ |
| 2049 |
w = 2; |
| 2050 |
} |
| 2051 |
} |
| 2052 |
data.a[i].w = w; |
| 2053 |
} |
| 2054 |
|
| 2055 |
if( nColumn==1 |
| 2056 |
&& data.n>1 |
| 2057 |
&& p->spec.bSplitColumn==QRF_Yes |
| 2058 |
&& p->spec.eStyle==QRF_STYLE_Column |
| 2059 |
&& p->spec.bTitles==QRF_No |
| 2060 |
&& p->spec.nScreenWidth>data.a[0].w+3 |
| 2061 |
){ |
| 2062 |
/* Attempt to convert single-column tables into multi-column by |
| 2063 |
** verticle wrapping, if the screen is wide enough and if the |
| 2064 |
** bSplitColumn flag is set. */ |
| 2065 |
qrfSplitColumn(&data, p); |
| 2066 |
nColumn = data.nCol; |
| 2067 |
}else{ |
| 2068 |
/* Adjust the column widths due to screen width restrictions */ |
| 2069 |
qrfRestrictScreenWidth(&data, p); |
| 2070 |
} |
| 2071 |
|
| 2072 |
/* Draw the line across the top of the table. Also initialize |
| 2073 |
** the row boundary and column separator texts. */ |
| 2074 |
switch( p->spec.eStyle ){ |
| 2075 |
case QRF_STYLE_Box: |
| 2076 |
if( data.nMargin ){ |
| 2077 |
rowStart = BOX_13 " "; |
| 2078 |
colSep = " " BOX_13 " "; |
| 2079 |
rowSep = " " BOX_13 "\n"; |
| 2080 |
}else{ |
| 2081 |
rowStart = BOX_13; |
| 2082 |
colSep = BOX_13; |
| 2083 |
rowSep = BOX_13 "\n"; |
| 2084 |
} |
| 2085 |
if( p->spec.bBorder==QRF_No){ |
| 2086 |
rowStart += 3; |
| 2087 |
rowSep = "\n"; |
| 2088 |
}else{ |
| 2089 |
qrfBoxSeparator(p->pOut, &data, BOX_R23, BOX_234, BOX_R34, 0); |
| 2090 |
} |
| 2091 |
break; |
| 2092 |
case QRF_STYLE_Table: |
| 2093 |
if( data.nMargin ){ |
| 2094 |
rowStart = "| "; |
| 2095 |
colSep = " | "; |
| 2096 |
rowSep = " |\n"; |
| 2097 |
}else{ |
| 2098 |
rowStart = "|"; |
| 2099 |
colSep = "|"; |
| 2100 |
rowSep = "|\n"; |
| 2101 |
} |
| 2102 |
if( p->spec.bBorder==QRF_No ){ |
| 2103 |
rowStart += 1; |
| 2104 |
rowSep = "\n"; |
| 2105 |
}else{ |
| 2106 |
qrfRowSeparator(p->pOut, &data, '+'); |
| 2107 |
} |
| 2108 |
break; |
| 2109 |
case QRF_STYLE_Column: { |
| 2110 |
static const char zSpace[] = " "; |
| 2111 |
rowStart = ""; |
| 2112 |
if( data.nMargin<2 ){ |
| 2113 |
colSep = " "; |
| 2114 |
}else if( data.nMargin<=5 ){ |
| 2115 |
colSep = &zSpace[5-data.nMargin]; |
| 2116 |
}else{ |
| 2117 |
colSep = zSpace; |
| 2118 |
} |
| 2119 |
rowSep = "\n"; |
| 2120 |
break; |
| 2121 |
} |
| 2122 |
default: /*case QRF_STYLE_Markdown:*/ |
| 2123 |
if( data.nMargin ){ |
| 2124 |
rowStart = "| "; |
| 2125 |
colSep = " | "; |
| 2126 |
rowSep = " |\n"; |
| 2127 |
}else{ |
| 2128 |
rowStart = "|"; |
| 2129 |
colSep = "|"; |
| 2130 |
rowSep = "|\n"; |
| 2131 |
} |
| 2132 |
break; |
| 2133 |
} |
| 2134 |
szRowStart = (int)strlen(rowStart); |
| 2135 |
szRowSep = (int)strlen(rowSep); |
| 2136 |
szColSep = (int)strlen(colSep); |
| 2137 |
|
| 2138 |
bWW = (p->spec.bWordWrap==QRF_Yes && data.bMultiRow); |
| 2139 |
if( p->spec.eStyle==QRF_STYLE_Column |
| 2140 |
|| (p->spec.bBorder==QRF_No |
| 2141 |
&& (p->spec.eStyle==QRF_STYLE_Box || p->spec.eStyle==QRF_STYLE_Table) |
| 2142 |
) |
| 2143 |
){ |
| 2144 |
bRTrim = 1; |
| 2145 |
}else{ |
| 2146 |
bRTrim = 0; |
| 2147 |
} |
| 2148 |
for(i=0; i<data.n && sqlite3_str_errcode(p->pOut)==SQLITE_OK; i+=nColumn){ |
| 2149 |
int bMore; |
| 2150 |
int nRow = 0; |
| 2151 |
|
| 2152 |
/* Draw a single row of the table. This might be the title line |
| 2153 |
** (if there is a title line) or a row in the body of the table. |
| 2154 |
** The column number will be j. The row number is i/nColumn. |
| 2155 |
*/ |
| 2156 |
for(j=0; j<nColumn; j++){ |
| 2157 |
data.a[j].z = data.az[i+j]; |
| 2158 |
if( data.a[j].z==0 ) data.a[j].z = ""; |
| 2159 |
data.a[j].bNum = data.abNum[i+j]; |
| 2160 |
} |
| 2161 |
do{ |
| 2162 |
sqlite3_str_append(p->pOut, rowStart, szRowStart); |
| 2163 |
bMore = 0; |
| 2164 |
for(j=0; j<nColumn; j++){ |
| 2165 |
int nThis = 0; |
| 2166 |
int nWide = 0; |
| 2167 |
int iNext = 0; |
| 2168 |
int nWS; |
| 2169 |
qrfWrapLine(data.a[j].z, data.a[j].w, bWW, &nThis, &nWide, &iNext); |
| 2170 |
nWS = data.a[j].w - nWide; |
| 2171 |
qrfPrintAligned(p->pOut, &data.a[j], nThis, nWS); |
| 2172 |
data.a[j].z += iNext; |
| 2173 |
if( data.a[j].z[0]!=0 ){ |
| 2174 |
bMore = 1; |
| 2175 |
} |
| 2176 |
if( j<nColumn-1 ){ |
| 2177 |
sqlite3_str_append(p->pOut, colSep, szColSep); |
| 2178 |
}else{ |
| 2179 |
if( bRTrim ) qrfRTrim(p->pOut); |
| 2180 |
sqlite3_str_append(p->pOut, rowSep, szRowSep); |
| 2181 |
} |
| 2182 |
} |
| 2183 |
}while( bMore && ++nRow < p->mxHeight ); |
| 2184 |
if( bMore ){ |
| 2185 |
/* This row was terminated by nLineLimit. Show ellipsis. */ |
| 2186 |
sqlite3_str_append(p->pOut, rowStart, szRowStart); |
| 2187 |
for(j=0; j<nColumn; j++){ |
| 2188 |
if( data.a[j].z[0]==0 ){ |
| 2189 |
sqlite3_str_appendchar(p->pOut, data.a[j].w, ' '); |
| 2190 |
}else{ |
| 2191 |
int nE = 3; |
| 2192 |
if( nE>data.a[j].w ) nE = data.a[j].w; |
| 2193 |
data.a[j].z = "..."; |
| 2194 |
qrfPrintAligned(p->pOut, &data.a[j], nE, data.a[j].w-nE); |
| 2195 |
} |
| 2196 |
if( j<nColumn-1 ){ |
| 2197 |
sqlite3_str_append(p->pOut, colSep, szColSep); |
| 2198 |
}else{ |
| 2199 |
if( bRTrim ) qrfRTrim(p->pOut); |
| 2200 |
sqlite3_str_append(p->pOut, rowSep, szRowSep); |
| 2201 |
} |
| 2202 |
} |
| 2203 |
} |
| 2204 |
|
| 2205 |
/* Draw either (1) the separator between the title line and the body |
| 2206 |
** of the table, or (2) separators between individual rows of the table |
| 2207 |
** body. isTitleDataSeparator will be true if we are doing (1). |
| 2208 |
*/ |
| 2209 |
if( (i==0 || data.bMultiRow) && i+nColumn<data.n ){ |
| 2210 |
int isTitleDataSeparator = (i==0 && p->spec.bTitles==QRF_Yes); |
| 2211 |
if( isTitleDataSeparator ){ |
| 2212 |
qrfLoadAlignment(&data, p); |
| 2213 |
} |
| 2214 |
switch( p->spec.eStyle ){ |
| 2215 |
case QRF_STYLE_Table: { |
| 2216 |
if( isTitleDataSeparator || data.bMultiRow ){ |
| 2217 |
qrfRowSeparator(p->pOut, &data, '+'); |
| 2218 |
} |
| 2219 |
break; |
| 2220 |
} |
| 2221 |
case QRF_STYLE_Box: { |
| 2222 |
if( isTitleDataSeparator ){ |
| 2223 |
qrfBoxSeparator(p->pOut, &data, DBL_123, DBL_1234, DBL_134, 1); |
| 2224 |
}else if( data.bMultiRow ){ |
| 2225 |
qrfBoxSeparator(p->pOut, &data, BOX_123, BOX_1234, BOX_134, 0); |
| 2226 |
} |
| 2227 |
break; |
| 2228 |
} |
| 2229 |
case QRF_STYLE_Markdown: { |
| 2230 |
if( isTitleDataSeparator ){ |
| 2231 |
qrfRowSeparator(p->pOut, &data, '|'); |
| 2232 |
} |
| 2233 |
break; |
| 2234 |
} |
| 2235 |
case QRF_STYLE_Column: { |
| 2236 |
if( isTitleDataSeparator ){ |
| 2237 |
for(j=0; j<nColumn; j++){ |
| 2238 |
sqlite3_str_appendchar(p->pOut, data.a[j].w, '-'); |
| 2239 |
if( j<nColumn-1 ){ |
| 2240 |
sqlite3_str_append(p->pOut, colSep, szColSep); |
| 2241 |
}else{ |
| 2242 |
qrfRTrim(p->pOut); |
| 2243 |
sqlite3_str_append(p->pOut, rowSep, szRowSep); |
| 2244 |
} |
| 2245 |
} |
| 2246 |
}else if( data.bMultiRow ){ |
| 2247 |
qrfRTrim(p->pOut); |
| 2248 |
sqlite3_str_append(p->pOut, "\n", 1); |
| 2249 |
} |
| 2250 |
break; |
| 2251 |
} |
| 2252 |
} |
| 2253 |
} |
| 2254 |
} |
| 2255 |
|
| 2256 |
/* Draw the line across the bottom of the table */ |
| 2257 |
if( p->spec.bBorder!=QRF_No ){ |
| 2258 |
switch( p->spec.eStyle ){ |
| 2259 |
case QRF_STYLE_Box: |
| 2260 |
qrfBoxSeparator(p->pOut, &data, BOX_R12, BOX_124, BOX_R14, 0); |
| 2261 |
break; |
| 2262 |
case QRF_STYLE_Table: |
| 2263 |
qrfRowSeparator(p->pOut, &data, '+'); |
| 2264 |
break; |
| 2265 |
} |
| 2266 |
} |
| 2267 |
qrfWrite(p); |
| 2268 |
|
| 2269 |
qrfColDataFree(&data); |
| 2270 |
return; |
| 2271 |
} |
| 2272 |
|
| 2273 |
/* |
| 2274 |
** Parameter azArray points to a zero-terminated array of strings. zStr |
| 2275 |
** points to a single nul-terminated string. Return non-zero if zStr |
| 2276 |
** is equal, according to strcmp(), to any of the strings in the array. |
| 2277 |
** Otherwise, return zero. |
| 2278 |
*/ |
| 2279 |
static int qrfStringInArray(const char *zStr, const char **azArray){ |
| 2280 |
int i; |
| 2281 |
if( zStr==0 ) return 0; |
| 2282 |
for(i=0; azArray[i]; i++){ |
| 2283 |
if( 0==strcmp(zStr, azArray[i]) ) return 1; |
| 2284 |
} |
| 2285 |
return 0; |
| 2286 |
} |
| 2287 |
|
| 2288 |
/* |
| 2289 |
** Print out an EXPLAIN with indentation. This is a two-pass algorithm. |
| 2290 |
** |
| 2291 |
** On the first pass, we compute aiIndent[iOp] which is the amount of |
| 2292 |
** indentation to apply to the iOp-th opcode. The output actually occurs |
| 2293 |
** on the second pass. |
| 2294 |
** |
| 2295 |
** The indenting rules are: |
| 2296 |
** |
| 2297 |
** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent |
| 2298 |
** all opcodes that occur between the p2 jump destination and the opcode |
| 2299 |
** itself by 2 spaces. |
| 2300 |
** |
| 2301 |
** * Do the previous for "Return" instructions for when P2 is positive. |
| 2302 |
** See tag-20220407a in wherecode.c and vdbe.c. |
| 2303 |
** |
| 2304 |
** * For each "Goto", if the jump destination is earlier in the program |
| 2305 |
** and ends on one of: |
| 2306 |
** Yield SeekGt SeekLt RowSetRead Rewind |
| 2307 |
** or if the P1 parameter is one instead of zero, |
| 2308 |
** then indent all opcodes between the earlier instruction |
| 2309 |
** and "Goto" by 2 spaces. |
| 2310 |
*/ |
| 2311 |
static void qrfExplain(Qrf *p){ |
| 2312 |
int *abYield = 0; /* abYield[iOp] is rue if opcode iOp is an OP_Yield */ |
| 2313 |
int *aiIndent = 0; /* Indent the iOp-th opcode by aiIndent[iOp] */ |
| 2314 |
i64 nAlloc = 0; /* Allocated size of aiIndent[], abYield */ |
| 2315 |
int nIndent = 0; /* Number of entries in aiIndent[] */ |
| 2316 |
int iOp; /* Opcode number */ |
| 2317 |
int i; /* Column loop counter */ |
| 2318 |
|
| 2319 |
const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", |
| 2320 |
"Return", 0 }; |
| 2321 |
const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", |
| 2322 |
"Rewind", 0 }; |
| 2323 |
const char *azGoto[] = { "Goto", 0 }; |
| 2324 |
|
| 2325 |
/* The caller guarantees that the leftmost 4 columns of the statement |
| 2326 |
** passed to this function are equivalent to the leftmost 4 columns |
| 2327 |
** of EXPLAIN statement output. In practice the statement may be |
| 2328 |
** an EXPLAIN, or it may be a query on the bytecode() virtual table. */ |
| 2329 |
assert( sqlite3_column_count(p->pStmt)>=4 ); |
| 2330 |
assert( 0==sqlite3_stricmp( sqlite3_column_name(p->pStmt, 0), "addr" ) ); |
| 2331 |
assert( 0==sqlite3_stricmp( sqlite3_column_name(p->pStmt, 1), "opcode" ) ); |
| 2332 |
assert( 0==sqlite3_stricmp( sqlite3_column_name(p->pStmt, 2), "p1" ) ); |
| 2333 |
assert( 0==sqlite3_stricmp( sqlite3_column_name(p->pStmt, 3), "p2" ) ); |
| 2334 |
|
| 2335 |
for(iOp=0; SQLITE_ROW==sqlite3_step(p->pStmt) && !p->iErr; iOp++){ |
| 2336 |
int iAddr = sqlite3_column_int(p->pStmt, 0); |
| 2337 |
const char *zOp = (const char*)sqlite3_column_text(p->pStmt, 1); |
| 2338 |
int p1 = sqlite3_column_int(p->pStmt, 2); |
| 2339 |
int p2 = sqlite3_column_int(p->pStmt, 3); |
| 2340 |
|
| 2341 |
/* Assuming that p2 is an instruction address, set variable p2op to the |
| 2342 |
** index of that instruction in the aiIndent[] array. p2 and p2op may be |
| 2343 |
** different if the current instruction is part of a sub-program generated |
| 2344 |
** by an SQL trigger or foreign key. */ |
| 2345 |
int p2op = (p2 + (iOp-iAddr)); |
| 2346 |
|
| 2347 |
/* Grow the aiIndent array as required */ |
| 2348 |
if( iOp>=nAlloc ){ |
| 2349 |
nAlloc += 100; |
| 2350 |
aiIndent = (int*)sqlite3_realloc64(aiIndent, nAlloc*sizeof(int)); |
| 2351 |
abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); |
| 2352 |
if( aiIndent==0 || abYield==0 ){ |
| 2353 |
qrfOom(p); |
| 2354 |
sqlite3_free(aiIndent); |
| 2355 |
sqlite3_free(abYield); |
| 2356 |
return; |
| 2357 |
} |
| 2358 |
} |
| 2359 |
|
| 2360 |
abYield[iOp] = qrfStringInArray(zOp, azYield); |
| 2361 |
aiIndent[iOp] = 0; |
| 2362 |
nIndent = iOp+1; |
| 2363 |
if( qrfStringInArray(zOp, azNext) && p2op>0 ){ |
| 2364 |
for(i=p2op; i<iOp; i++) aiIndent[i] += 2; |
| 2365 |
} |
| 2366 |
if( qrfStringInArray(zOp, azGoto) && p2op<iOp && (abYield[p2op] || p1) ){ |
| 2367 |
for(i=p2op; i<iOp; i++) aiIndent[i] += 2; |
| 2368 |
} |
| 2369 |
} |
| 2370 |
sqlite3_free(abYield); |
| 2371 |
|
| 2372 |
/* Second pass. Actually generate output */ |
| 2373 |
sqlite3_reset(p->pStmt); |
| 2374 |
if( p->iErr==SQLITE_OK ){ |
| 2375 |
static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; |
| 2376 |
static const int aExplainMap[] = {0, 1, 2, 3, 4, 5, 6, 7 }; |
| 2377 |
static const int aScanExpWidth[] = {4,15, 6, 13, 4, 4, 4, 13, 2, 13}; |
| 2378 |
static const int aScanExpMap[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 }; |
| 2379 |
const int *aWidth = aExplainWidth; |
| 2380 |
const int *aMap = aExplainMap; |
| 2381 |
int nWidth = sizeof(aExplainWidth)/sizeof(int); |
| 2382 |
int iIndent = 1; |
| 2383 |
int nArg = p->nCol; |
| 2384 |
if( p->spec.eStyle==QRF_STYLE_StatsVm ){ |
| 2385 |
aWidth = aScanExpWidth; |
| 2386 |
aMap = aScanExpMap; |
| 2387 |
nWidth = sizeof(aScanExpWidth)/sizeof(int); |
| 2388 |
iIndent = 3; |
| 2389 |
} |
| 2390 |
if( nArg>nWidth ) nArg = nWidth; |
| 2391 |
|
| 2392 |
for(iOp=0; sqlite3_step(p->pStmt)==SQLITE_ROW && !p->iErr; iOp++){ |
| 2393 |
/* If this is the first row seen, print out the headers */ |
| 2394 |
if( iOp==0 ){ |
| 2395 |
for(i=0; i<nArg; i++){ |
| 2396 |
const char *zCol = sqlite3_column_name(p->pStmt, aMap[i]); |
| 2397 |
qrfWidthPrint(p,p->pOut, aWidth[i], zCol); |
| 2398 |
if( i==nArg-1 ){ |
| 2399 |
sqlite3_str_append(p->pOut, "\n", 1); |
| 2400 |
}else{ |
| 2401 |
sqlite3_str_append(p->pOut, " ", 2); |
| 2402 |
} |
| 2403 |
} |
| 2404 |
for(i=0; i<nArg; i++){ |
| 2405 |
sqlite3_str_appendf(p->pOut, "%.*c", aWidth[i], '-'); |
| 2406 |
if( i==nArg-1 ){ |
| 2407 |
sqlite3_str_append(p->pOut, "\n", 1); |
| 2408 |
}else{ |
| 2409 |
sqlite3_str_append(p->pOut, " ", 2); |
| 2410 |
} |
| 2411 |
} |
| 2412 |
} |
| 2413 |
|
| 2414 |
for(i=0; i<nArg; i++){ |
| 2415 |
const char *zSep = " "; |
| 2416 |
int w = aWidth[i]; |
| 2417 |
const char *zVal = (const char*)sqlite3_column_text(p->pStmt, aMap[i]); |
| 2418 |
int len; |
| 2419 |
if( i==nArg-1 ) w = 0; |
| 2420 |
if( zVal==0 ) zVal = ""; |
| 2421 |
len = (int)sqlite3_qrf_wcswidth(zVal); |
| 2422 |
if( len>w ){ |
| 2423 |
w = len; |
| 2424 |
zSep = " "; |
| 2425 |
} |
| 2426 |
if( i==iIndent && aiIndent && iOp<nIndent ){ |
| 2427 |
sqlite3_str_appendchar(p->pOut, aiIndent[iOp], ' '); |
| 2428 |
} |
| 2429 |
qrfWidthPrint(p, p->pOut, w, zVal); |
| 2430 |
if( i==nArg-1 ){ |
| 2431 |
sqlite3_str_append(p->pOut, "\n", 1); |
| 2432 |
}else{ |
| 2433 |
sqlite3_str_appendall(p->pOut, zSep); |
| 2434 |
} |
| 2435 |
} |
| 2436 |
p->nRow++; |
| 2437 |
} |
| 2438 |
qrfWrite(p); |
| 2439 |
} |
| 2440 |
sqlite3_free(aiIndent); |
| 2441 |
} |
| 2442 |
|
| 2443 |
/* |
| 2444 |
** Do a "scanstatus vm" style EXPLAIN listing on p->pStmt. |
| 2445 |
** |
| 2446 |
** p->pStmt is probably not an EXPLAIN query. Instead, construct a |
| 2447 |
** new query that is a bytecode() rendering of p->pStmt with extra |
| 2448 |
** columns for the "scanstatus vm" outputs, and run the results of |
| 2449 |
** that new query through the normal EXPLAIN formatting. |
| 2450 |
*/ |
| 2451 |
static void qrfScanStatusVm(Qrf *p){ |
| 2452 |
sqlite3_stmt *pOrigStmt = p->pStmt; |
| 2453 |
sqlite3_stmt *pExplain; |
| 2454 |
int rc; |
| 2455 |
static const char *zSql = |
| 2456 |
" SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec," |
| 2457 |
" format('% 6s (%.2f%%)'," |
| 2458 |
" CASE WHEN ncycle<100_000 THEN ncycle || ' '" |
| 2459 |
" WHEN ncycle<100_000_000 THEN (ncycle/1_000) || 'K'" |
| 2460 |
" WHEN ncycle<100_000_000_000 THEN (ncycle/1_000_000) || 'M'" |
| 2461 |
" ELSE (ncycle/1000_000_000) || 'G' END," |
| 2462 |
" ncycle*100.0/(sum(ncycle) OVER ())" |
| 2463 |
" ) AS cycles" |
| 2464 |
" FROM bytecode(?1)"; |
| 2465 |
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pExplain, 0); |
| 2466 |
if( rc ){ |
| 2467 |
qrfError(p, rc, "%s", sqlite3_errmsg(p->db)); |
| 2468 |
sqlite3_finalize(pExplain); |
| 2469 |
return; |
| 2470 |
} |
| 2471 |
sqlite3_bind_pointer(pExplain, 1, pOrigStmt, "stmt-pointer", 0); |
| 2472 |
p->pStmt = pExplain; |
| 2473 |
p->nCol = 10; |
| 2474 |
qrfExplain(p); |
| 2475 |
sqlite3_finalize(pExplain); |
| 2476 |
p->pStmt = pOrigStmt; |
| 2477 |
} |
| 2478 |
|
| 2479 |
/* |
| 2480 |
** Attempt to determine if identifier zName needs to be quoted, either |
| 2481 |
** because it contains non-alphanumeric characters, or because it is an |
| 2482 |
** SQLite keyword. Be conservative in this estimate: When in doubt assume |
| 2483 |
** that quoting is required. |
| 2484 |
** |
| 2485 |
** Return 1 if quoting is required. Return 0 if no quoting is required. |
| 2486 |
*/ |
| 2487 |
|
| 2488 |
static int qrf_need_quote(const char *zName){ |
| 2489 |
int i; |
| 2490 |
const unsigned char *z = (const unsigned char*)zName; |
| 2491 |
if( z==0 ) return 1; |
| 2492 |
if( !qrfAlpha(z[0]) ) return 1; |
| 2493 |
for(i=0; z[i]; i++){ |
| 2494 |
if( !qrfAlnum(z[i]) ) return 1; |
| 2495 |
} |
| 2496 |
return sqlite3_keyword_check(zName, i)!=0; |
| 2497 |
} |
| 2498 |
|
| 2499 |
/* |
| 2500 |
** Helper function for QRF_STYLE_Json and QRF_STYLE_JObject. |
| 2501 |
** The initial "{" for a JSON object that will contain row content |
| 2502 |
** has been output. Now output all the content. |
| 2503 |
*/ |
| 2504 |
static void qrfOneJsonRow(Qrf *p){ |
| 2505 |
int i, nItem; |
| 2506 |
for(nItem=i=0; i<p->nCol; i++){ |
| 2507 |
const char *zCName; |
| 2508 |
zCName = sqlite3_column_name(p->pStmt, i); |
| 2509 |
if( nItem>0 ) sqlite3_str_append(p->pOut, ",", 1); |
| 2510 |
nItem++; |
| 2511 |
qrfEncodeText(p, p->pOut, zCName); |
| 2512 |
sqlite3_str_append(p->pOut, ":", 1); |
| 2513 |
qrfRenderValue(p, p->pOut, i); |
| 2514 |
} |
| 2515 |
qrfWrite(p); |
| 2516 |
} |
| 2517 |
|
| 2518 |
/* |
| 2519 |
** Render a single row of output for non-columnar styles - any |
| 2520 |
** style that lets us render row by row as the content is received |
| 2521 |
** from the query. |
| 2522 |
*/ |
| 2523 |
static void qrfOneSimpleRow(Qrf *p){ |
| 2524 |
int i; |
| 2525 |
switch( p->spec.eStyle ){ |
| 2526 |
case QRF_STYLE_Off: |
| 2527 |
case QRF_STYLE_Count: { |
| 2528 |
/* No-op */ |
| 2529 |
break; |
| 2530 |
} |
| 2531 |
case QRF_STYLE_Json: { |
| 2532 |
if( p->nRow==0 ){ |
| 2533 |
sqlite3_str_append(p->pOut, "[{", 2); |
| 2534 |
}else{ |
| 2535 |
sqlite3_str_append(p->pOut, "},\n{", 4); |
| 2536 |
} |
| 2537 |
qrfOneJsonRow(p); |
| 2538 |
break; |
| 2539 |
} |
| 2540 |
case QRF_STYLE_JObject: { |
| 2541 |
if( p->nRow==0 ){ |
| 2542 |
sqlite3_str_append(p->pOut, "{", 1); |
| 2543 |
}else{ |
| 2544 |
sqlite3_str_append(p->pOut, "}\n{", 3); |
| 2545 |
} |
| 2546 |
qrfOneJsonRow(p); |
| 2547 |
break; |
| 2548 |
} |
| 2549 |
case QRF_STYLE_Html: { |
| 2550 |
if( p->nRow==0 && p->spec.bTitles==QRF_Yes ){ |
| 2551 |
sqlite3_str_append(p->pOut, "<TR>", 4); |
| 2552 |
for(i=0; i<p->nCol; i++){ |
| 2553 |
const char *zCName = sqlite3_column_name(p->pStmt, i); |
| 2554 |
sqlite3_str_append(p->pOut, "\n<TH>", 5); |
| 2555 |
qrfEncodeText(p, p->pOut, zCName); |
| 2556 |
} |
| 2557 |
sqlite3_str_append(p->pOut, "\n</TR>\n", 7); |
| 2558 |
} |
| 2559 |
sqlite3_str_append(p->pOut, "<TR>", 4); |
| 2560 |
for(i=0; i<p->nCol; i++){ |
| 2561 |
sqlite3_str_append(p->pOut, "\n<TD>", 5); |
| 2562 |
qrfRenderValue(p, p->pOut, i); |
| 2563 |
} |
| 2564 |
sqlite3_str_append(p->pOut, "\n</TR>\n", 7); |
| 2565 |
qrfWrite(p); |
| 2566 |
break; |
| 2567 |
} |
| 2568 |
case QRF_STYLE_Insert: { |
| 2569 |
unsigned int mxIns = p->spec.nMultiInsert; |
| 2570 |
int szStart = sqlite3_str_length(p->pOut); |
| 2571 |
if( p->u.nIns==0 || p->u.nIns>=mxIns ){ |
| 2572 |
if( p->u.nIns ){ |
| 2573 |
sqlite3_str_append(p->pOut, ";\n", 2); |
| 2574 |
p->u.nIns = 0; |
| 2575 |
} |
| 2576 |
if( qrf_need_quote(p->spec.zTableName) ){ |
| 2577 |
sqlite3_str_appendf(p->pOut,"INSERT INTO \"%w\"",p->spec.zTableName); |
| 2578 |
}else{ |
| 2579 |
sqlite3_str_appendf(p->pOut,"INSERT INTO %s",p->spec.zTableName); |
| 2580 |
} |
| 2581 |
if( p->spec.bTitles==QRF_Yes ){ |
| 2582 |
for(i=0; i<p->nCol; i++){ |
| 2583 |
const char *zCName = sqlite3_column_name(p->pStmt, i); |
| 2584 |
if( qrf_need_quote(zCName) ){ |
| 2585 |
sqlite3_str_appendf(p->pOut, "%c\"%w\"", |
| 2586 |
i==0 ? '(' : ',', zCName); |
| 2587 |
}else{ |
| 2588 |
sqlite3_str_appendf(p->pOut, "%c%s", |
| 2589 |
i==0 ? '(' : ',', zCName); |
| 2590 |
} |
| 2591 |
} |
| 2592 |
sqlite3_str_append(p->pOut, ")", 1); |
| 2593 |
} |
| 2594 |
sqlite3_str_append(p->pOut," VALUES(", 8); |
| 2595 |
}else{ |
| 2596 |
sqlite3_str_append(p->pOut,",\n (", 5); |
| 2597 |
} |
| 2598 |
for(i=0; i<p->nCol; i++){ |
| 2599 |
if( i>0 ) sqlite3_str_append(p->pOut, ",", 1); |
| 2600 |
qrfRenderValue(p, p->pOut, i); |
| 2601 |
} |
| 2602 |
p->u.nIns += sqlite3_str_length(p->pOut) + 2 - szStart; |
| 2603 |
if( p->u.nIns>=mxIns ){ |
| 2604 |
sqlite3_str_append(p->pOut, ");\n", 3); |
| 2605 |
p->u.nIns = 0; |
| 2606 |
}else{ |
| 2607 |
sqlite3_str_append(p->pOut, ")", 1); |
| 2608 |
} |
| 2609 |
qrfWrite(p); |
| 2610 |
break; |
| 2611 |
} |
| 2612 |
case QRF_STYLE_Line: { |
| 2613 |
sqlite3_str *pVal; |
| 2614 |
int mxW; |
| 2615 |
int bWW; |
| 2616 |
int nSep; |
| 2617 |
if( p->u.sLine.azCol==0 ){ |
| 2618 |
p->u.sLine.azCol = sqlite3_malloc64( p->nCol*sizeof(char*) ); |
| 2619 |
if( p->u.sLine.azCol==0 ){ |
| 2620 |
qrfOom(p); |
| 2621 |
break; |
| 2622 |
} |
| 2623 |
p->u.sLine.mxColWth = 0; |
| 2624 |
for(i=0; i<p->nCol; i++){ |
| 2625 |
int sz; |
| 2626 |
const char *zCName = sqlite3_column_name(p->pStmt, i); |
| 2627 |
if( zCName==0 ) zCName = "unknown"; |
| 2628 |
p->u.sLine.azCol[i] = sqlite3_mprintf("%s", zCName); |
| 2629 |
if( p->spec.nTitleLimit>0 ){ |
| 2630 |
(void)qrfTitleLimit(p->u.sLine.azCol[i], p->spec.nTitleLimit); |
| 2631 |
} |
| 2632 |
sz = (int)sqlite3_qrf_wcswidth(p->u.sLine.azCol[i]); |
| 2633 |
if( sz > p->u.sLine.mxColWth ) p->u.sLine.mxColWth = sz; |
| 2634 |
} |
| 2635 |
} |
| 2636 |
if( p->nRow ) sqlite3_str_append(p->pOut, "\n", 1); |
| 2637 |
pVal = sqlite3_str_new(p->db); |
| 2638 |
nSep = (int)strlen(p->spec.zColumnSep); |
| 2639 |
mxW = p->mxWidth - (nSep + p->u.sLine.mxColWth); |
| 2640 |
bWW = p->spec.bWordWrap==QRF_Yes; |
| 2641 |
for(i=0; i<p->nCol; i++){ |
| 2642 |
const char *zVal; |
| 2643 |
int cnt = 0; |
| 2644 |
qrfWidthPrint(p, p->pOut, -p->u.sLine.mxColWth, p->u.sLine.azCol[i]); |
| 2645 |
sqlite3_str_append(p->pOut, p->spec.zColumnSep, nSep); |
| 2646 |
qrfRenderValue(p, pVal, i); |
| 2647 |
zVal = sqlite3_str_value(pVal); |
| 2648 |
if( zVal==0 ) zVal = ""; |
| 2649 |
do{ |
| 2650 |
int nThis, nWide, iNext; |
| 2651 |
qrfWrapLine(zVal, mxW, bWW, &nThis, &nWide, &iNext); |
| 2652 |
if( cnt ){ |
| 2653 |
sqlite3_str_appendchar(p->pOut,p->u.sLine.mxColWth+nSep,' '); |
| 2654 |
} |
| 2655 |
cnt++; |
| 2656 |
if( cnt>p->mxHeight ){ |
| 2657 |
zVal = "..."; |
| 2658 |
nThis = iNext = 3; |
| 2659 |
} |
| 2660 |
sqlite3_str_append(p->pOut, zVal, nThis); |
| 2661 |
sqlite3_str_append(p->pOut, "\n", 1); |
| 2662 |
zVal += iNext; |
| 2663 |
}while( zVal[0] ); |
| 2664 |
sqlite3_str_reset(pVal); |
| 2665 |
} |
| 2666 |
qrfStrErr(p, pVal); |
| 2667 |
sqlite3_free(sqlite3_str_finish(pVal)); |
| 2668 |
qrfWrite(p); |
| 2669 |
break; |
| 2670 |
} |
| 2671 |
case QRF_STYLE_Eqp: { |
| 2672 |
const char *zEqpLine = (const char*)sqlite3_column_text(p->pStmt,3); |
| 2673 |
int iEqpId = sqlite3_column_int(p->pStmt, 0); |
| 2674 |
int iParentId = sqlite3_column_int(p->pStmt, 1); |
| 2675 |
if( zEqpLine==0 ) zEqpLine = ""; |
| 2676 |
if( zEqpLine[0]=='-' ) qrfEqpRender(p, 0); |
| 2677 |
qrfEqpAppend(p, iEqpId, iParentId, zEqpLine); |
| 2678 |
break; |
| 2679 |
} |
| 2680 |
default: { /* QRF_STYLE_List */ |
| 2681 |
if( p->nRow==0 && p->spec.bTitles==QRF_Yes ){ |
| 2682 |
int saved_eText = p->spec.eText; |
| 2683 |
p->spec.eText = p->spec.eTitle; |
| 2684 |
for(i=0; i<p->nCol; i++){ |
| 2685 |
const char *zCName = sqlite3_column_name(p->pStmt, i); |
| 2686 |
if( i>0 ) sqlite3_str_appendall(p->pOut, p->spec.zColumnSep); |
| 2687 |
qrfEncodeText(p, p->pOut, zCName); |
| 2688 |
} |
| 2689 |
sqlite3_str_appendall(p->pOut, p->spec.zRowSep); |
| 2690 |
qrfWrite(p); |
| 2691 |
p->spec.eText = saved_eText; |
| 2692 |
} |
| 2693 |
for(i=0; i<p->nCol; i++){ |
| 2694 |
if( i>0 ) sqlite3_str_appendall(p->pOut, p->spec.zColumnSep); |
| 2695 |
qrfRenderValue(p, p->pOut, i); |
| 2696 |
} |
| 2697 |
sqlite3_str_appendall(p->pOut, p->spec.zRowSep); |
| 2698 |
qrfWrite(p); |
| 2699 |
break; |
| 2700 |
} |
| 2701 |
} |
| 2702 |
p->nRow++; |
| 2703 |
} |
| 2704 |
|
| 2705 |
/* |
| 2706 |
** Initialize the internal Qrf object. |
| 2707 |
*/ |
| 2708 |
static void qrfInitialize( |
| 2709 |
Qrf *p, /* State object to be initialized */ |
| 2710 |
sqlite3_stmt *pStmt, /* Query whose output to be formatted */ |
| 2711 |
const sqlite3_qrf_spec *pSpec, /* Format specification */ |
| 2712 |
char **pzErr /* Write errors here */ |
| 2713 |
){ |
| 2714 |
size_t sz; /* Size of pSpec[], based on pSpec->iVersion */ |
| 2715 |
memset(p, 0, sizeof(*p)); |
| 2716 |
p->pzErr = pzErr; |
| 2717 |
if( pSpec->iVersion>1 ){ |
| 2718 |
qrfError(p, SQLITE_ERROR, |
| 2719 |
"unusable sqlite3_qrf_spec.iVersion (%d)", |
| 2720 |
pSpec->iVersion); |
| 2721 |
return; |
| 2722 |
} |
| 2723 |
p->pStmt = pStmt; |
| 2724 |
p->db = sqlite3_db_handle(pStmt); |
| 2725 |
p->pOut = sqlite3_str_new(p->db); |
| 2726 |
if( p->pOut==0 ){ |
| 2727 |
qrfOom(p); |
| 2728 |
return; |
| 2729 |
} |
| 2730 |
p->iErr = SQLITE_OK; |
| 2731 |
p->nCol = sqlite3_column_count(p->pStmt); |
| 2732 |
p->nRow = 0; |
| 2733 |
sz = sizeof(sqlite3_qrf_spec); |
| 2734 |
memcpy(&p->spec, pSpec, sz); |
| 2735 |
if( p->spec.zNull==0 ) p->spec.zNull = ""; |
| 2736 |
p->mxWidth = p->spec.nScreenWidth; |
| 2737 |
if( p->mxWidth<=0 ) p->mxWidth = QRF_MAX_WIDTH; |
| 2738 |
p->mxHeight = p->spec.nLineLimit; |
| 2739 |
if( p->mxHeight<=0 ) p->mxHeight = 2147483647; |
| 2740 |
if( p->spec.eStyle>QRF_STYLE_Table ) p->spec.eStyle = QRF_Auto; |
| 2741 |
if( p->spec.eEsc>QRF_ESC_Symbol ) p->spec.eEsc = QRF_Auto; |
| 2742 |
if( p->spec.eText>QRF_TEXT_Relaxed ) p->spec.eText = QRF_Auto; |
| 2743 |
if( p->spec.eTitle>QRF_TEXT_Relaxed ) p->spec.eTitle = QRF_Auto; |
| 2744 |
if( p->spec.eBlob>QRF_BLOB_Size ) p->spec.eBlob = QRF_Auto; |
| 2745 |
qrf_reinit: |
| 2746 |
switch( p->spec.eStyle ){ |
| 2747 |
case QRF_Auto: { |
| 2748 |
switch( sqlite3_stmt_isexplain(pStmt) ){ |
| 2749 |
case 0: p->spec.eStyle = QRF_STYLE_Box; break; |
| 2750 |
case 1: p->spec.eStyle = QRF_STYLE_Explain; break; |
| 2751 |
default: p->spec.eStyle = QRF_STYLE_Eqp; break; |
| 2752 |
} |
| 2753 |
goto qrf_reinit; |
| 2754 |
} |
| 2755 |
case QRF_STYLE_List: { |
| 2756 |
if( p->spec.zColumnSep==0 ) p->spec.zColumnSep = "|"; |
| 2757 |
if( p->spec.zRowSep==0 ) p->spec.zRowSep = "\n"; |
| 2758 |
break; |
| 2759 |
} |
| 2760 |
case QRF_STYLE_JObject: |
| 2761 |
case QRF_STYLE_Json: { |
| 2762 |
p->spec.eText = QRF_TEXT_Json; |
| 2763 |
p->spec.zNull = "null"; |
| 2764 |
break; |
| 2765 |
} |
| 2766 |
case QRF_STYLE_Html: { |
| 2767 |
p->spec.eText = QRF_TEXT_Html; |
| 2768 |
p->spec.zNull = "null"; |
| 2769 |
break; |
| 2770 |
} |
| 2771 |
case QRF_STYLE_Insert: { |
| 2772 |
p->spec.eText = QRF_TEXT_Sql; |
| 2773 |
p->spec.zNull = "NULL"; |
| 2774 |
if( p->spec.zTableName==0 || p->spec.zTableName[0]==0 ){ |
| 2775 |
p->spec.zTableName = "tab"; |
| 2776 |
} |
| 2777 |
p->u.nIns = 0; |
| 2778 |
break; |
| 2779 |
} |
| 2780 |
case QRF_STYLE_Line: { |
| 2781 |
if( p->spec.zColumnSep==0 ){ |
| 2782 |
p->spec.zColumnSep = ": "; |
| 2783 |
} |
| 2784 |
break; |
| 2785 |
} |
| 2786 |
case QRF_STYLE_Csv: { |
| 2787 |
p->spec.eStyle = QRF_STYLE_List; |
| 2788 |
p->spec.eText = QRF_TEXT_Csv; |
| 2789 |
p->spec.zColumnSep = ","; |
| 2790 |
p->spec.zRowSep = "\r\n"; |
| 2791 |
p->spec.zNull = ""; |
| 2792 |
break; |
| 2793 |
} |
| 2794 |
case QRF_STYLE_Quote: { |
| 2795 |
p->spec.eText = QRF_TEXT_Sql; |
| 2796 |
p->spec.zNull = "NULL"; |
| 2797 |
p->spec.zColumnSep = ","; |
| 2798 |
p->spec.zRowSep = "\n"; |
| 2799 |
break; |
| 2800 |
} |
| 2801 |
case QRF_STYLE_Eqp: { |
| 2802 |
int expMode = sqlite3_stmt_isexplain(p->pStmt); |
| 2803 |
if( expMode!=2 ){ |
| 2804 |
sqlite3_stmt_explain(p->pStmt, 2); |
| 2805 |
p->expMode = expMode+1; |
| 2806 |
} |
| 2807 |
break; |
| 2808 |
} |
| 2809 |
case QRF_STYLE_Explain: { |
| 2810 |
int expMode = sqlite3_stmt_isexplain(p->pStmt); |
| 2811 |
if( expMode!=1 ){ |
| 2812 |
sqlite3_stmt_explain(p->pStmt, 1); |
| 2813 |
p->expMode = expMode+1; |
| 2814 |
} |
| 2815 |
break; |
| 2816 |
} |
| 2817 |
} |
| 2818 |
if( p->spec.eEsc==QRF_Auto ){ |
| 2819 |
p->spec.eEsc = QRF_ESC_Ascii; |
| 2820 |
} |
| 2821 |
if( p->spec.eText==QRF_Auto ){ |
| 2822 |
p->spec.eText = QRF_TEXT_Plain; |
| 2823 |
} |
| 2824 |
if( p->spec.eTitle==QRF_Auto ){ |
| 2825 |
switch( p->spec.eStyle ){ |
| 2826 |
case QRF_STYLE_Box: |
| 2827 |
case QRF_STYLE_Column: |
| 2828 |
case QRF_STYLE_Table: |
| 2829 |
p->spec.eTitle = QRF_TEXT_Plain; |
| 2830 |
break; |
| 2831 |
default: |
| 2832 |
p->spec.eTitle = p->spec.eText; |
| 2833 |
break; |
| 2834 |
} |
| 2835 |
} |
| 2836 |
if( p->spec.eBlob==QRF_Auto ){ |
| 2837 |
switch( p->spec.eText ){ |
| 2838 |
case QRF_TEXT_Sql: p->spec.eBlob = QRF_BLOB_Sql; break; |
| 2839 |
case QRF_TEXT_Csv: p->spec.eBlob = QRF_BLOB_Tcl; break; |
| 2840 |
case QRF_TEXT_Tcl: p->spec.eBlob = QRF_BLOB_Tcl; break; |
| 2841 |
case QRF_TEXT_Json: p->spec.eBlob = QRF_BLOB_Json; break; |
| 2842 |
default: p->spec.eBlob = QRF_BLOB_Text; break; |
| 2843 |
} |
| 2844 |
} |
| 2845 |
if( p->spec.bTitles==QRF_Auto ){ |
| 2846 |
switch( p->spec.eStyle ){ |
| 2847 |
case QRF_STYLE_Box: |
| 2848 |
case QRF_STYLE_Csv: |
| 2849 |
case QRF_STYLE_Column: |
| 2850 |
case QRF_STYLE_Table: |
| 2851 |
case QRF_STYLE_Markdown: |
| 2852 |
p->spec.bTitles = QRF_Yes; |
| 2853 |
break; |
| 2854 |
default: |
| 2855 |
p->spec.bTitles = QRF_No; |
| 2856 |
break; |
| 2857 |
} |
| 2858 |
} |
| 2859 |
if( p->spec.bWordWrap==QRF_Auto ){ |
| 2860 |
p->spec.bWordWrap = QRF_Yes; |
| 2861 |
} |
| 2862 |
if( p->spec.bTextJsonb==QRF_Auto ){ |
| 2863 |
p->spec.bTextJsonb = QRF_No; |
| 2864 |
} |
| 2865 |
if( p->spec.zColumnSep==0 ) p->spec.zColumnSep = ","; |
| 2866 |
if( p->spec.zRowSep==0 ) p->spec.zRowSep = "\n"; |
| 2867 |
} |
| 2868 |
|
| 2869 |
/* |
| 2870 |
** Finish rendering the results |
| 2871 |
*/ |
| 2872 |
static void qrfFinalize(Qrf *p){ |
| 2873 |
switch( p->spec.eStyle ){ |
| 2874 |
case QRF_STYLE_Count: { |
| 2875 |
sqlite3_str_appendf(p->pOut, "%lld\n", p->nRow); |
| 2876 |
break; |
| 2877 |
} |
| 2878 |
case QRF_STYLE_Json: { |
| 2879 |
if( p->nRow>0 ){ |
| 2880 |
sqlite3_str_append(p->pOut, "}]\n", 3); |
| 2881 |
} |
| 2882 |
break; |
| 2883 |
} |
| 2884 |
case QRF_STYLE_JObject: { |
| 2885 |
if( p->nRow>0 ){ |
| 2886 |
sqlite3_str_append(p->pOut, "}\n", 2); |
| 2887 |
} |
| 2888 |
break; |
| 2889 |
} |
| 2890 |
case QRF_STYLE_Insert: { |
| 2891 |
if( p->u.nIns ){ |
| 2892 |
sqlite3_str_append(p->pOut, ";\n", 2); |
| 2893 |
} |
| 2894 |
break; |
| 2895 |
} |
| 2896 |
case QRF_STYLE_Line: { |
| 2897 |
if( p->u.sLine.azCol ){ |
| 2898 |
int i; |
| 2899 |
for(i=0; i<p->nCol; i++) sqlite3_free(p->u.sLine.azCol[i]); |
| 2900 |
sqlite3_free(p->u.sLine.azCol); |
| 2901 |
} |
| 2902 |
break; |
| 2903 |
} |
| 2904 |
case QRF_STYLE_Stats: |
| 2905 |
case QRF_STYLE_StatsEst: { |
| 2906 |
i64 nCycle = 0; |
| 2907 |
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS |
| 2908 |
sqlite3_stmt_scanstatus_v2(p->pStmt, -1, SQLITE_SCANSTAT_NCYCLE, |
| 2909 |
SQLITE_SCANSTAT_COMPLEX, (void*)&nCycle); |
| 2910 |
#endif |
| 2911 |
qrfEqpRender(p, nCycle); |
| 2912 |
break; |
| 2913 |
} |
| 2914 |
case QRF_STYLE_Eqp: { |
| 2915 |
qrfEqpRender(p, 0); |
| 2916 |
break; |
| 2917 |
} |
| 2918 |
} |
| 2919 |
qrfWrite(p); |
| 2920 |
qrfStrErr(p, p->pOut); |
| 2921 |
if( p->spec.pzOutput ){ |
| 2922 |
if( p->spec.pzOutput[0] ){ |
| 2923 |
sqlite3_int64 n, sz; |
| 2924 |
char *zCombined; |
| 2925 |
sz = strlen(p->spec.pzOutput[0]); |
| 2926 |
n = sqlite3_str_length(p->pOut); |
| 2927 |
zCombined = sqlite3_realloc64(p->spec.pzOutput[0], sz+n+1); |
| 2928 |
if( zCombined==0 ){ |
| 2929 |
sqlite3_free(p->spec.pzOutput[0]); |
| 2930 |
p->spec.pzOutput[0] = 0; |
| 2931 |
qrfOom(p); |
| 2932 |
}else{ |
| 2933 |
p->spec.pzOutput[0] = zCombined; |
| 2934 |
memcpy(zCombined+sz, sqlite3_str_value(p->pOut), n+1); |
| 2935 |
} |
| 2936 |
sqlite3_free(sqlite3_str_finish(p->pOut)); |
| 2937 |
}else{ |
| 2938 |
p->spec.pzOutput[0] = sqlite3_str_finish(p->pOut); |
| 2939 |
} |
| 2940 |
}else if( p->pOut ){ |
| 2941 |
sqlite3_free(sqlite3_str_finish(p->pOut)); |
| 2942 |
} |
| 2943 |
if( p->expMode>0 ){ |
| 2944 |
sqlite3_stmt_explain(p->pStmt, p->expMode-1); |
| 2945 |
} |
| 2946 |
if( p->actualWidth ){ |
| 2947 |
sqlite3_free(p->actualWidth); |
| 2948 |
} |
| 2949 |
if( p->pJTrans ){ |
| 2950 |
sqlite3 *db = sqlite3_db_handle(p->pJTrans); |
| 2951 |
sqlite3_finalize(p->pJTrans); |
| 2952 |
sqlite3_close(db); |
| 2953 |
} |
| 2954 |
} |
| 2955 |
|
| 2956 |
/* |
| 2957 |
** Run the prepared statement pStmt and format the results according |
| 2958 |
** to the specification provided in pSpec. Return an error code. |
| 2959 |
** If pzErr is not NULL and if an error occurs, write an error message |
| 2960 |
** into *pzErr. |
| 2961 |
*/ |
| 2962 |
int sqlite3_format_query_result( |
| 2963 |
sqlite3_stmt *pStmt, /* Statement to evaluate */ |
| 2964 |
const sqlite3_qrf_spec *pSpec, /* Format specification */ |
| 2965 |
char **pzErr /* Write error message here */ |
| 2966 |
){ |
| 2967 |
Qrf qrf; /* The new Qrf being created */ |
| 2968 |
|
| 2969 |
if( pStmt==0 ) return SQLITE_OK; /* No-op */ |
| 2970 |
if( pSpec==0 ) return SQLITE_MISUSE; |
| 2971 |
qrfInitialize(&qrf, pStmt, pSpec, pzErr); |
| 2972 |
switch( qrf.spec.eStyle ){ |
| 2973 |
case QRF_STYLE_Box: |
| 2974 |
case QRF_STYLE_Column: |
| 2975 |
case QRF_STYLE_Markdown: |
| 2976 |
case QRF_STYLE_Table: { |
| 2977 |
/* Columnar modes require that the entire query be evaluated and the |
| 2978 |
** results stored in memory, so that we can compute column widths */ |
| 2979 |
qrfColumnar(&qrf); |
| 2980 |
break; |
| 2981 |
} |
| 2982 |
case QRF_STYLE_Explain: { |
| 2983 |
qrfExplain(&qrf); |
| 2984 |
break; |
| 2985 |
} |
| 2986 |
case QRF_STYLE_StatsVm: { |
| 2987 |
qrfScanStatusVm(&qrf); |
| 2988 |
break; |
| 2989 |
} |
| 2990 |
case QRF_STYLE_Stats: |
| 2991 |
case QRF_STYLE_StatsEst: { |
| 2992 |
qrfEqpStats(&qrf); |
| 2993 |
break; |
| 2994 |
} |
| 2995 |
default: { |
| 2996 |
/* Non-columnar modes where the output can occur after each row |
| 2997 |
** of result is received */ |
| 2998 |
while( qrf.iErr==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 2999 |
qrfOneSimpleRow(&qrf); |
| 3000 |
} |
| 3001 |
break; |
| 3002 |
} |
| 3003 |
} |
| 3004 |
qrfResetStmt(&qrf); |
| 3005 |
qrfFinalize(&qrf); |
| 3006 |
return qrf.iErr; |
| 3007 |
} |