SQLite

Check-in [473a72d700]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Ensure the correct collation sequences are used when sorting data in sqlite3ota.c.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | ota-update
Files: files | file ages | folders
SHA1: 473a72d7009a22ea514a98ee8869e7e7bca14cf5
User & Date: dan 2014-09-15 14:54:07.328
Context
2014-09-15
15:22
Have sqlite3ota.c use grave accents instead of double-quotes to enclose identifiers in generated SQL. To avoid having the SQL engine substitute a literal string if a column reference cannot be resolved. (check-in: 79f2418429 user: dan tags: ota-update)
14:54
Ensure the correct collation sequences are used when sorting data in sqlite3ota.c. (check-in: 473a72d700 user: dan tags: ota-update)
12:18
Have the sqlite3_index_writer() VMs check that the final values of records inserted into indexes on rowid tables are integers. (check-in: cca376bff3 user: dan tags: ota-update)
Changes
Unified Diff Ignore Whitespace Patch
Changes to ext/ota/sqlite3ota.c.
397
398
399
400
401
402
403


































404
405
406
407
408

409
410
411
412
413
414
415
416



417
418
419
420
421
422
423
      p->rc = SQLITE_ERROR;
    }
  }

  return p->rc;
}



































static char *otaObjIterGetCollist(
  sqlite3ota *p, 
  OtaObjIter *pIter, 
  int nCol, 
  int *aiCol

){
  char *zList = 0;
  if( p->rc==SQLITE_OK ){
    const char *zSep = "";
    int i;
    for(i=0; i<nCol; i++){
      int iCol = aiCol ? aiCol[i] : i;
      zList = sqlite3_mprintf("%z%s%s", zList, zSep, pIter->azTblCol[iCol]);



      zSep = ", ";
      if( zList==0 ){
        p->rc = SQLITE_NOMEM;
        break;
      }
    }
  }







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
|
|
|
>








>
>
>







397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
      p->rc = SQLITE_ERROR;
    }
  }

  return p->rc;
}

/*
** This function constructs and returns a pointer to a nul-terminated 
** string containing some SQL clause or list based on one or more of the 
** column names currently stored in the pIter->azTblCol[] array.
**
** If an OOM error is encountered, NULL is returned and an error code
** left in the OTA handle passed as the first argument. Otherwise, a pointer
** to the allocated string buffer is returned. It is the responsibility
** of the caller to eventually free this buffer using sqlite3_free().
**
** The number of column names to include in the returned string is passed
** as the third argument.
**
** If arguments aiCol and azCollate are both NULL, then the returned string
** contains the first nCol column names as a comma-separated list. For 
** example:
**
**     "a", "b", "c"
**
** If argument aiCol is not NULL, it must point to an array containing nCol
** entries - the index of each column name to include in the comma-separated
** list. For example, if aiCol[] contains {2, 0, 1), then the returned 
** string is changed to:
**
**     "c", "a", "b"
**
** If azCollate is not NULL, it must also point to an array containing nCol
** entries - collation sequence names to associated with each element of
** the comma separated list. For example, ef azCollate[] contains 
** {"BINARY", "NOCASE", "REVERSE"}, then the retuned string is:
**
**     "c" COLLATE "BINARY", "a" COLLATE "NOCASE", "b" COLLATE "REVERSE"
**
*/
static char *otaObjIterGetCollist(
  sqlite3ota *p,                  /* OTA object */
  OtaObjIter *pIter,              /* Object iterator for column names */
  int nCol,                       /* Number of column names */
  int *aiCol,                     /* Array of nCol column indexes */
  const char **azCollate          /* Array of nCol collation sequence names */
){
  char *zList = 0;
  if( p->rc==SQLITE_OK ){
    const char *zSep = "";
    int i;
    for(i=0; i<nCol; i++){
      int iCol = aiCol ? aiCol[i] : i;
      zList = sqlite3_mprintf("%z%s%s", zList, zSep, pIter->azTblCol[iCol]);
      if( zList && azCollate ){
        zList = sqlite3_mprintf("%z COLLATE %Q", zList, azCollate[i]);
      }
      zSep = ", ";
      if( zList==0 ){
        p->rc = SQLITE_NOMEM;
        break;
      }
    }
  }
544
545
546
547
548
549
550

551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
    if( nOffset ){
      zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
      if( !zLimit ) p->rc = SQLITE_NOMEM;
    }

    if( zIdx ){
      int *aiCol;                 /* Column map */


      /* Create the index writers */
      if( p->rc==SQLITE_OK ){
        p->rc = sqlite3_index_writer(
            p->db, 0, zIdx, &pIter->pInsert, &aiCol, &pIter->nCol
        );
      }
      if( p->rc==SQLITE_OK ){
        p->rc = sqlite3_index_writer(
            p->db, 1, zIdx, &pIter->pDelete, &aiCol, &pIter->nCol
        );
      }

      /* Create the SELECT statement to read keys in sorted order */
      zCollist = otaObjIterGetCollist(p, pIter, pIter->nCol, aiCol);
      if( p->rc==SQLITE_OK ){
        p->rc = prepareFreeAndCollectError(p->db, &pIter->pSelect, pz,
            sqlite3_mprintf(
              "SELECT %s, ota_control FROM ota.'data_%q' "
              "WHERE typeof(ota_control)='integer' AND ota_control!=1 "
                "UNION ALL "
              "SELECT %s, ota_control FROM ota.'ota_tmp_%q' "
              "ORDER BY %s%s",
              zCollist, pIter->zTbl, 
              zCollist, pIter->zTbl, 
              zCollist, zLimit
            )
        );
      }
    }else{
      char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol);
      char *zWhere = otaObjIterGetWhere(p, pIter);
      char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
      char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
      zCollist = otaObjIterGetCollist(p, pIter, pIter->nTblCol, 0);
      pIter->nCol = pIter->nTblCol;

      /* Create the SELECT statement to read keys from data_xxx */
      if( p->rc==SQLITE_OK ){
        p->rc = prepareFreeAndCollectError(p->db, &pIter->pSelect, pz,
            sqlite3_mprintf(
              "SELECT %s, ota_control FROM ota.'data_%q'%s", 







>




|




|




|



















|







582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
    if( nOffset ){
      zLimit = sqlite3_mprintf(" LIMIT -1 OFFSET %d", nOffset);
      if( !zLimit ) p->rc = SQLITE_NOMEM;
    }

    if( zIdx ){
      int *aiCol;                 /* Column map */
      const char **azColl;        /* Collation sequences */

      /* Create the index writers */
      if( p->rc==SQLITE_OK ){
        p->rc = sqlite3_index_writer(
            p->db, 0, zIdx, &pIter->pInsert, &azColl, &aiCol, &pIter->nCol
        );
      }
      if( p->rc==SQLITE_OK ){
        p->rc = sqlite3_index_writer(
            p->db, 1, zIdx, &pIter->pDelete, &azColl, &aiCol, &pIter->nCol
        );
      }

      /* Create the SELECT statement to read keys in sorted order */
      zCollist = otaObjIterGetCollist(p, pIter, pIter->nCol, aiCol, azColl);
      if( p->rc==SQLITE_OK ){
        p->rc = prepareFreeAndCollectError(p->db, &pIter->pSelect, pz,
            sqlite3_mprintf(
              "SELECT %s, ota_control FROM ota.'data_%q' "
              "WHERE typeof(ota_control)='integer' AND ota_control!=1 "
                "UNION ALL "
              "SELECT %s, ota_control FROM ota.'ota_tmp_%q' "
              "ORDER BY %s%s",
              zCollist, pIter->zTbl, 
              zCollist, pIter->zTbl, 
              zCollist, zLimit
            )
        );
      }
    }else{
      char *zBindings = otaObjIterGetBindlist(p, pIter->nTblCol);
      char *zWhere = otaObjIterGetWhere(p, pIter);
      char *zOldlist = otaObjIterGetOldlist(p, pIter, "old");
      char *zNewlist = otaObjIterGetOldlist(p, pIter, "new");
      zCollist = otaObjIterGetCollist(p, pIter, pIter->nTblCol, 0, 0);
      pIter->nCol = pIter->nTblCol;

      /* Create the SELECT statement to read keys from data_xxx */
      if( p->rc==SQLITE_OK ){
        p->rc = prepareFreeAndCollectError(p->db, &pIter->pSelect, pz,
            sqlite3_mprintf(
              "SELECT %s, ota_control FROM ota.'data_%q'%s", 
Changes to src/sqlite.h.in.
7411
7412
7413
7414
7415
7416
7417

7418
7419
7420
7421
7422
7423
7424
** to insert operations.
*/
int sqlite3_index_writer(
  sqlite3 *db, 
  int bDelete,                    /* Zero for insert, non-zero for delete */
  const char *zIndex,             /* Index to write to */
  sqlite3_stmt**,                 /* OUT: New statement handle */

  int **paiCol, int *pnCol        /* OUT: See above */
);

/*
** This function is used to save the state of an ongoing WAL mode write 
** transaction on the "main" database of the supplied database handle.
**







>







7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
** to insert operations.
*/
int sqlite3_index_writer(
  sqlite3 *db, 
  int bDelete,                    /* Zero for insert, non-zero for delete */
  const char *zIndex,             /* Index to write to */
  sqlite3_stmt**,                 /* OUT: New statement handle */
  const char ***pazColl,          /* OUT: Collation sequences for each column */
  int **paiCol, int *pnCol        /* OUT: See above */
);

/*
** This function is used to save the state of an ongoing WAL mode write 
** transaction on the "main" database of the supplied database handle.
**
Changes to src/vdbeblob.c.
458
459
460
461
462
463
464
465































































466
467
468
469
470

471

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
  }

  rc = sqlite3ApiExit(db, rc);
  assert( rc==SQLITE_OK || p->pStmt==0 );
  sqlite3_mutex_leave(db->mutex);
  return rc;
}
































































int sqlite3_index_writer(
  sqlite3 *db, 
  int bDelete,
  const char *zIndex, 
  sqlite3_stmt **ppStmt,

  int **paiCol, int *pnCol

){
  int rc = SQLITE_OK;
  Parse *pParse = 0;
  Index *pIdx = 0;                /* The index to write to */
  Table *pTab;
  int i;                          /* Used to iterate through index columns */
  Vdbe *v = 0;
  int regRec;                     /* Register to assemble record in */
  int *aiCol = 0;
  const char *zAffinity = 0;      /* Affinity string for the current index */

  sqlite3_mutex_enter(db->mutex);
  sqlite3BtreeEnterAll(db);

  /* Allocate the parse context */
  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





>
|
>








<







458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544

545
546
547
548
549
550
551
  }

  rc = sqlite3ApiExit(db, rc);
  assert( rc==SQLITE_OK || p->pStmt==0 );
  sqlite3_mutex_leave(db->mutex);
  return rc;
}

static int indexWriterOutputVars(
  sqlite3 *db,
  Index *pIdx,
  const char ***pazColl,          /* OUT: Array of collation sequences */
  int **paiCol,                   /* OUT: Array of column indexes */
  int *pnCol                      /* OUT: Total columns in index keys */
){
  Table *pTbl = pIdx->pTable;     /* Table index is attached to */
  Index *pPk = 0;
  int nByte = 0;                  /* Total bytes of space to allocate */
  int i;                          /* Iterator variable */

  int *aiCol;
  const char **azColl;
  char *pCsr;

  if( !HasRowid(pTbl) ){
    pPk = sqlite3PrimaryKeyIndex(pTbl);
  }

  for(i=0; i<pIdx->nColumn; i++){
    const char *zColl = 0;
    if( i<pIdx->nKeyCol ){
      zColl = pIdx->azColl[i];
    }else if( pPk ){
      zColl = pPk->azColl[i-pIdx->nKeyCol];
    }
    if( zColl==0 ) zColl = "BINARY";
    nByte += sqlite3Strlen30(zColl) + 1;
  }
  nByte += (pIdx->nColumn) * (sizeof(const char*) + sizeof(int));

  /* Populate the output variables */
  *pazColl = azColl = (const char**)sqlite3DbMallocZero(db, nByte);
  if( azColl==0 ) return SQLITE_NOMEM;
  *paiCol = aiCol = (int*)&azColl[pIdx->nColumn];
  *pnCol = pIdx->nColumn;
  pCsr = (char*)&aiCol[pIdx->nColumn];

  for(i=0; i<pIdx->nColumn; i++){
    const char *zColl = 0;
    int nColl;
    int iCol = pTbl->iPKey;
    if( i<pIdx->nKeyCol ){
      zColl = pIdx->azColl[i];
      iCol = pIdx->aiColumn[i];
    }else if( pPk ){
      zColl = pPk->azColl[i-pIdx->nKeyCol];
      iCol = pPk->aiColumn[i-pIdx->nKeyCol];
    }
    if( zColl==0 ) zColl = "BINARY";

    aiCol[i] = iCol;
    azColl[i] = pCsr;
    nColl = 1 + sqlite3Strlen30(zColl);
    memcpy(pCsr, zColl, nColl);
    pCsr += nColl;
  }

  return SQLITE_OK;
}


int sqlite3_index_writer(
  sqlite3 *db, 
  int bDelete,
  const char *zIndex, 
  sqlite3_stmt **ppStmt,
  const char ***pazColl,          /* OUT: Array of collation sequences */
  int **paiCol,                   /* OUT: Array of column indexes */
  int *pnCol                      /* OUT: Total columns in index keys */
){
  int rc = SQLITE_OK;
  Parse *pParse = 0;
  Index *pIdx = 0;                /* The index to write to */
  Table *pTab;
  int i;                          /* Used to iterate through index columns */
  Vdbe *v = 0;
  int regRec;                     /* Register to assemble record in */

  const char *zAffinity = 0;      /* Affinity string for the current index */

  sqlite3_mutex_enter(db->mutex);
  sqlite3BtreeEnterAll(db);

  /* Allocate the parse context */
  pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
  if( pIdx==0 ){
    sqlite3ErrorMsg(pParse, "no such index: %s", zIndex);
    goto index_writer_out;
  }
  pTab = pIdx->pTable;
  zAffinity = sqlite3IndexAffinityStr(v, pIdx);

  /* Populate the two output variables, *pnCol and *pnAiCol. */
  *pnCol = pIdx->nColumn;
  *paiCol = aiCol = sqlite3DbMallocZero(db, sizeof(int) * pIdx->nColumn);
  if( aiCol==0 ){
    rc = SQLITE_NOMEM;
    goto index_writer_out;
  }
  for(i=0; i<pIdx->nKeyCol; i++){
    aiCol[i] = pIdx->aiColumn[i];
  }
  if( !HasRowid(pTab) ){
    Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
    assert( pIdx->nColumn==pIdx->nKeyCol+pPk->nKeyCol );
    if( pPk==pIdx ){
      rc = SQLITE_ERROR;
      goto index_writer_out;
    }
    for(i=0; i<pPk->nKeyCol; i++){
      aiCol[pIdx->nKeyCol+i] = pPk->aiColumn[i];
    }
  }else{
    assert( pIdx->nColumn==pIdx->nKeyCol+1 );
    aiCol[i] = pTab->iPKey;
  }

  /* Add an OP_Noop to the VDBE program. Then store a pointer to the 
  ** output array *paiCol as its P4 value. This is so that the array
  ** is automatically deleted when the user finalizes the statement. The
  ** OP_Noop serves no other purpose. */
  sqlite3VdbeAddOp0(v, OP_Noop);
  sqlite3VdbeChangeP4(v, -1, (const char*)aiCol, P4_INTARRAY);

  sqlite3BeginWriteOperation(pParse, 0, 0);

  /* Open a write cursor on the index */
  pParse->nTab = 1;
  sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, pIdx->tnum, 0);
  sqlite3VdbeSetP4KeyInfo(pParse, pIdx);







<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<






|







562
563
564
565
566
567
568





569









570








571
572
573
574
575
576
577
578
579
580
581
582
583
584
  if( pIdx==0 ){
    sqlite3ErrorMsg(pParse, "no such index: %s", zIndex);
    goto index_writer_out;
  }
  pTab = pIdx->pTable;
  zAffinity = sqlite3IndexAffinityStr(v, pIdx);






  rc = indexWriterOutputVars(db, pIdx, pazColl, paiCol, pnCol);









  if( rc!=SQLITE_OK ) goto index_writer_out;









  /* Add an OP_Noop to the VDBE program. Then store a pointer to the 
  ** output array *paiCol as its P4 value. This is so that the array
  ** is automatically deleted when the user finalizes the statement. The
  ** OP_Noop serves no other purpose. */
  sqlite3VdbeAddOp0(v, OP_Noop);
  sqlite3VdbeChangeP4(v, -1, (const char*)(*pazColl), P4_INTARRAY);

  sqlite3BeginWriteOperation(pParse, 0, 0);

  /* Open a write cursor on the index */
  pParse->nTab = 1;
  sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, pIdx->tnum, 0);
  sqlite3VdbeSetP4KeyInfo(pParse, pIdx);