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