UnQL

Check-in [0a23ab0e48]
Login

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

Overview
Comment:Incremental check-in. Various bug fixes. Progress toward getting UPDATE running.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0a23ab0e48f6a914218f8602c81af66695bf09b2
User & Date: drh 2011-06-30 20:13:52
Context
2011-06-30
20:44
Basic UPDATE statements (and UPSERT) is now working. However, new fields are not added yet. check-in: e2bc14a5be user: drh tags: trunk
20:13
Incremental check-in. Various bug fixes. Progress toward getting UPDATE running. check-in: 0a23ab0e48 user: drh tags: trunk
15:51
Added preliminary UPSERT syntax. Added some simple UPDATE test cases, but since the UPDATE code is not in place, those test cases are currently failing. check-in: c0f72777e2 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/delete.c.

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    sqlite3_free(zSql);
    return XJD1_OK;
  }
  sqlite3_exec(db, "BEGIN; CREATE TEMP TABLE _t1(x INTEGER PRIMARY KEY)",
               0, 0, 0);
  zSql = sqlite3_mprintf("SELECT rowid, x FROM \"%w\"", pCmd->u.del.zName);
  sqlite3_prepare_v2(db, zSql, -1, &pQuery, 0);
  sqlite3_prepare_v2(db, "INSERT INTO _t1(x) VALUES(?)", -1, &pIns, 0);
  if( pQuery ){
    while( SQLITE_ROW==sqlite3_step(pQuery) ){
      const char *zJson = (const char*)sqlite3_column_text(pQuery, 1);
      pStmt->pDoc = xjd1JsonParse(zJson, -1);
      if( xjd1ExprTrue(pCmd->u.del.pWhere) ){
        sqlite3_bind_int64(pIns, 0, sqlite3_column_int64(pQuery, 0));
        sqlite3_step(pIns);
        sqlite3_reset(pIns);
      }
      xjd1JsonFree(pStmt->pDoc);
      pStmt->pDoc = 0;
    }
  }







|





|







40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    sqlite3_free(zSql);
    return XJD1_OK;
  }
  sqlite3_exec(db, "BEGIN; CREATE TEMP TABLE _t1(x INTEGER PRIMARY KEY)",
               0, 0, 0);
  zSql = sqlite3_mprintf("SELECT rowid, x FROM \"%w\"", pCmd->u.del.zName);
  sqlite3_prepare_v2(db, zSql, -1, &pQuery, 0);
  sqlite3_prepare_v2(db, "INSERT INTO _t1(x) VALUES(?1)", -1, &pIns, 0);
  if( pQuery ){
    while( SQLITE_ROW==sqlite3_step(pQuery) ){
      const char *zJson = (const char*)sqlite3_column_text(pQuery, 1);
      pStmt->pDoc = xjd1JsonParse(zJson, -1);
      if( xjd1ExprTrue(pCmd->u.del.pWhere) ){
        sqlite3_bind_int64(pIns, 1, sqlite3_column_int64(pQuery, 0));
        sqlite3_step(pIns);
        sqlite3_reset(pIns);
      }
      xjd1JsonFree(pStmt->pDoc);
      pStmt->pDoc = 0;
    }
  }

Changes to src/json.c.

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
...
120
121
122
123
124
125
126
127
128
129
130
131

132
133
134
135
136
137
138
...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
...
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
      pNew->u.ar.apElem = ap = malloc( sizeof(JsonNode*)*p->u.ar.nElem );
      if( ap==0 ){
        pNew->eJType = XJD1_NULL;
      }else{
        int i;
        pNew->u.ar.nElem = p->u.ar.nElem;
        for(i=0; i<p->u.ar.nElem; i++){
          ap[i] = xjd1JsonEdit(p->u.ar.apElem[i]);
        }
      }
      break;
    }
    case XJD1_STRUCT: {
      JsonStructElem *pSrc, *pDest, **ppPrev;
      ppPrev = &pNew->u.st.pFirst;
................................................................................
      for(pSrc=p->u.st.pFirst; pSrc; pSrc=pSrc->pNext){
        pNew->u.st.pLast = pDest = malloc( sizeof(*pDest) );
        if( pDest==0 ) break;
        memset(pDest, 0, sizeof(*pDest));
        *ppPrev = pDest;
        ppPrev = &pDest->pNext;
        pDest->zLabel = xjd1PoolDup(0, pSrc->zLabel, -1);
        pDest->pValue = xjd1JsonEdit(pSrc->pValue);
      }
      break;
    }
  }

  return pNew;
}


/* Render a string as a string literal.
*/
void renderString(String *pOut, const char *z){
................................................................................
        if( i+1>=mx || !xjd1Isdigit(z[i+1]) ){
          p->n = 1;
          p->eType = JSON_ERROR;
          break;
        }
      }
      if( z[i+n]=='0' ){
        i++;
      }else{
        while( i+n<mx && xjd1Isdigit(z[i+n]) ) n++;
      }
      if( i+n+1<mx && z[i+n]=='.' && xjd1Isdigit(z[i+n+1]) ){
        n++;
        while( i+n<mx && xjd1Isdigit(z[i+n]) ) n++;
      }
................................................................................
      pNew = 0;
      break;
    }
  }
  return pNew;

json_error:
  free(pNew);
  return 0;
}

/*
** Parse up a JSON string
*/
JsonNode *xjd1JsonParse(const char *zIn, int mxIn){







|







 







|




>







 







|







 







|







105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
...
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
      pNew->u.ar.apElem = ap = malloc( sizeof(JsonNode*)*p->u.ar.nElem );
      if( ap==0 ){
        pNew->eJType = XJD1_NULL;
      }else{
        int i;
        pNew->u.ar.nElem = p->u.ar.nElem;
        for(i=0; i<p->u.ar.nElem; i++){
          ap[i] = xjd1JsonRef(p->u.ar.apElem[i]);
        }
      }
      break;
    }
    case XJD1_STRUCT: {
      JsonStructElem *pSrc, *pDest, **ppPrev;
      ppPrev = &pNew->u.st.pFirst;
................................................................................
      for(pSrc=p->u.st.pFirst; pSrc; pSrc=pSrc->pNext){
        pNew->u.st.pLast = pDest = malloc( sizeof(*pDest) );
        if( pDest==0 ) break;
        memset(pDest, 0, sizeof(*pDest));
        *ppPrev = pDest;
        ppPrev = &pDest->pNext;
        pDest->zLabel = xjd1PoolDup(0, pSrc->zLabel, -1);
        pDest->pValue = xjd1JsonRef(pSrc->pValue);
      }
      break;
    }
  }
  p->nRef--;
  return pNew;
}


/* Render a string as a string literal.
*/
void renderString(String *pOut, const char *z){
................................................................................
        if( i+1>=mx || !xjd1Isdigit(z[i+1]) ){
          p->n = 1;
          p->eType = JSON_ERROR;
          break;
        }
      }
      if( z[i+n]=='0' ){
        n++;
      }else{
        while( i+n<mx && xjd1Isdigit(z[i+n]) ) n++;
      }
      if( i+n+1<mx && z[i+n]=='.' && xjd1Isdigit(z[i+n+1]) ){
        n++;
        while( i+n<mx && xjd1Isdigit(z[i+n]) ) n++;
      }
................................................................................
      pNew = 0;
      break;
    }
  }
  return pNew;

json_error:
  xjd1JsonFree(pNew);
  return 0;
}

/*
** Parse up a JSON string
*/
JsonNode *xjd1JsonParse(const char *zIn, int mxIn){

Changes to src/shell.c.

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

164
165
166
167
168

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

189
190
191
192
193
194
195
...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
...
292
293
294
295
296
297
298

299
300
301
302
303

304
305
306
307

308
309
310
311

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

329
330
331
332
333
334
335
336
337
338
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
...
361
362
363
364
365
366
367

368
369
370
371
372
373
374
375

376
377
378
379
380
381
382

383
384
385
386


387
388
389
390
391
392
393

394
395
396
397
398
399
400
401
402
403
...
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447

448
449

450
451
452
453
454
455
456
...
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
static int shellIsSpace(char c){
  return c==' ' || c=='\t' || c=='\r' || c=='\n';
}

/*
** Set flags
*/
static void shellSet(Shell *p, int argc, char **argv){
  int i;
  if( argc>=2 ){
    p->shellFlags |= flagMask(argv[1]);
  }else{
    for(i=0; i<sizeof(FlagNames)/sizeof(FlagNames[0]); i++){
      printf("%-20s %s\n", 
        FlagNames[i].zName,
        (p->shellFlags & FlagNames[i].iValue)!=0 ? "on" : "off");
    }
  }

}
static void shellClear(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    p->shellFlags &= ~flagMask(argv[1]);
  }

}

/*
** Command:  .quit
*/
static void shellQuit(Shell *p, int argc, char **argv){
  exit(0);
}

/*
** Command:  .testcase NAME
*/
static void shellTestcase(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    int n = strlen(argv[1]);
    if( n>=sizeof(p->zTestCase) ) n = sizeof(p->zTestCase)-1;
    memcpy(p->zTestCase, argv[1], n);
    p->zTestCase[n] = 0;
    xjd1StringTruncate(&p->testOut);
  }

}

/*
** Return non-zero if string z matches glob pattern zGlob and zero if the
** pattern does not match.
**
** Globbing rules:
................................................................................
static int strglob(const char *zGlob, const char *z){
  return !strnotglob(zGlob,z);
}

/*
** Verify the output from a test.
*/
static void shellResultCheck(
  Shell *p,
  int argc,
  char **argv,
  int(*xCmp)(const char*,const char*)
){
  const char *zAns = argc>=2 ? argv[1] : "";
  const char *zRes = xjd1StringText(&p->testOut);
................................................................................
  if( zRes==0 ) zRes = "";
  p->nTest++;
  if( xCmp(zAns, zRes) ){
    p->nErr++;
    fprintf(stderr, " Test failed: %s.%s\n Expected: [%s]\n      Got: [%s]\n",
      p->zFile, p->zTestCase, argv[1], zRes);
  }

}

/* Command:  .result PATTERN */
static void shellResult(Shell *p, int argc, char **argv){
  shellResultCheck(p,argc,argv,strcmp);

}
/* Command:  .glob PATTERN */
static void shellGlob(Shell *p, int argc, char **argv){
  shellResultCheck(p,argc,argv,strglob);

}
/* Command:  .notglob PATTERN */
static void shellNotGlob(Shell *p, int argc, char **argv){
  shellResultCheck(p,argc,argv,strnotglob);

}

/*
** Command:  .open FILENAME
*/
static void shellOpenDB(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    int rc;
    if( p->pDb ) xjd1_close(p->pDb);
    rc = xjd1_open(0, argv[1], &p->pDb);
    if( rc!=XJD1_OK ){
      fprintf(stderr, "%s:%d: cannot open \"%s\"\n",
              p->zFile, p->nLine, argv[1]);
      p->nErr++;
      p->pDb = 0;
    }
  }    

}
/*
** Command:  .new FILENAME
*/
static void shellNewDB(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    if( p->pDb ) xjd1_close(p->pDb);
    p->pDb = 0;
    unlink(argv[1]);
    shellOpenDB(p, argc, argv);
  }

}

/* Forward declaration */
static void processOneFile(Shell*, const char*);

/*
** Command:  .read FILENAME
** Read and process text from a file.
*/
static void shellRead(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    int nErr = p->nErr;
    String newFile;
    char *zNew;
    xjd1StringInit(&newFile, 0, 0);
    if( argv[1][0]=='/' || strcmp(p->zFile,"-")==0 ){
      xjd1StringAppend(&newFile, argv[1], 0);
................................................................................
    }
    zNew = xjd1StringText(&newFile);
    printf("BEGIN %s\n", zNew);
    processOneFile(p, zNew);
    printf("END %s - %d new errors\n", zNew, p->nErr - nErr);
    xjd1StringClear(&newFile);
  }

}

/*
** Command:  .breakpoint
** A place to seet a breakpoint
*/
static void shellBreakpoint(Shell *p, int argc, char **argv){
  /* no-op */

}

/*
** Command:  .puts TEXT
*/
static void shellPuts(Shell *p, int argc, char **argv){
  if( argc>=2 ) printf("%s\n", argv[1]);

}

/*
** Process a command intended for this shell - not for the database.


*/
static void processMetaCommand(Shell *p){
  char *z;
  int n;
  char *azArg[2];
  int nArg;
  int i;

  static const struct shcmd {
    const char *zCmd;                   /* Name of the command */
    void (*xCmd)(Shell*,int,char**);    /* Procedure to run the command */
    const char *zHelp;                  /* Help string */
  } cmds[] = {
    { "quit",       shellQuit,        ".quit"               },
    { "testcase",   shellTestcase,    ".testcase NAME"      },
    { "result",     shellResult,      ".result TEXT"        },
    { "glob",       shellGlob,        ".glob PATTERN"       },
    { "notglob",    shellNotGlob,     ".notglob PATTERN"    },
................................................................................
    azArg[1] = 0;
    nArg = 1;
  }

  /* Find the command */
  for(i=0; i<sizeof(cmds)/sizeof(cmds[0]); i++){
    if( strcmp(azArg[0], cmds[i].zCmd)==0 ){
      cmds[i].xCmd(p, nArg, azArg);
      break;
    }
  }
  if( i>=sizeof(cmds)/sizeof(cmds[0]) ){
    p->nErr++;
    fprintf(stderr, "%s:%d: unknown command \".%s\"\n", 
            p->zFile, p->nLine, azArg[0]);

  }
  xjd1StringTruncate(&p->inBuf);

}

/*
** Append text to the end of the testOut string.  If there is already
** text in the testOut buffer, prepent a space first.
*/
static void appendTestOut(Shell *p, const char *z, int n){
................................................................................
    if( zLine[0]=='.' && xjd1StringLen(&p->inBuf) ){
      fprintf(stderr, "%s:%d: missing ';'\n", p->zFile, p->nLine);
      xjd1StringTruncate(&p->inBuf);
    }
    xjd1StringAppend(&p->inBuf, zLine, -1);
    zIn = xjd1StringText(&p->inBuf);
    if( zIn[0]=='.' ){
      processMetaCommand(p);
    }else{
      processScript(p);
    }
  }
  if( p->isTTY==0 ){
    fclose(p->pIn);
  }







|










>

|



>





|
|





|







>







 







|







 







>



|

>


|

>


|

>





|











>




|






>









|







 







>






|

>





|

>




>
>

|





>


|







 







|







>


>







 







|







146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
...
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
...
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
...
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
...
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
...
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
static int shellIsSpace(char c){
  return c==' ' || c=='\t' || c=='\r' || c=='\n';
}

/*
** Set flags
*/
static int shellSet(Shell *p, int argc, char **argv){
  int i;
  if( argc>=2 ){
    p->shellFlags |= flagMask(argv[1]);
  }else{
    for(i=0; i<sizeof(FlagNames)/sizeof(FlagNames[0]); i++){
      printf("%-20s %s\n", 
        FlagNames[i].zName,
        (p->shellFlags & FlagNames[i].iValue)!=0 ? "on" : "off");
    }
  }
  return 0;
}
static int shellClear(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    p->shellFlags &= ~flagMask(argv[1]);
  }
  return 0;
}

/*
** Command:  .quit
*/
static int shellQuit(Shell *p, int argc, char **argv){
  return 1;
}

/*
** Command:  .testcase NAME
*/
static int shellTestcase(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    int n = strlen(argv[1]);
    if( n>=sizeof(p->zTestCase) ) n = sizeof(p->zTestCase)-1;
    memcpy(p->zTestCase, argv[1], n);
    p->zTestCase[n] = 0;
    xjd1StringTruncate(&p->testOut);
  }
  return 0;
}

/*
** Return non-zero if string z matches glob pattern zGlob and zero if the
** pattern does not match.
**
** Globbing rules:
................................................................................
static int strglob(const char *zGlob, const char *z){
  return !strnotglob(zGlob,z);
}

/*
** Verify the output from a test.
*/
static int shellResultCheck(
  Shell *p,
  int argc,
  char **argv,
  int(*xCmp)(const char*,const char*)
){
  const char *zAns = argc>=2 ? argv[1] : "";
  const char *zRes = xjd1StringText(&p->testOut);
................................................................................
  if( zRes==0 ) zRes = "";
  p->nTest++;
  if( xCmp(zAns, zRes) ){
    p->nErr++;
    fprintf(stderr, " Test failed: %s.%s\n Expected: [%s]\n      Got: [%s]\n",
      p->zFile, p->zTestCase, argv[1], zRes);
  }
  return 0;
}

/* Command:  .result PATTERN */
static int shellResult(Shell *p, int argc, char **argv){
  shellResultCheck(p,argc,argv,strcmp);
  return 0;
}
/* Command:  .glob PATTERN */
static int shellGlob(Shell *p, int argc, char **argv){
  shellResultCheck(p,argc,argv,strglob);
  return 0;
}
/* Command:  .notglob PATTERN */
static int shellNotGlob(Shell *p, int argc, char **argv){
  shellResultCheck(p,argc,argv,strnotglob);
  return 0;
}

/*
** Command:  .open FILENAME
*/
static int shellOpenDB(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    int rc;
    if( p->pDb ) xjd1_close(p->pDb);
    rc = xjd1_open(0, argv[1], &p->pDb);
    if( rc!=XJD1_OK ){
      fprintf(stderr, "%s:%d: cannot open \"%s\"\n",
              p->zFile, p->nLine, argv[1]);
      p->nErr++;
      p->pDb = 0;
    }
  }    
  return 0;
}
/*
** Command:  .new FILENAME
*/
static int shellNewDB(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    if( p->pDb ) xjd1_close(p->pDb);
    p->pDb = 0;
    unlink(argv[1]);
    shellOpenDB(p, argc, argv);
  }
  return 0;
}

/* Forward declaration */
static void processOneFile(Shell*, const char*);

/*
** Command:  .read FILENAME
** Read and process text from a file.
*/
static int shellRead(Shell *p, int argc, char **argv){
  if( argc>=2 ){
    int nErr = p->nErr;
    String newFile;
    char *zNew;
    xjd1StringInit(&newFile, 0, 0);
    if( argv[1][0]=='/' || strcmp(p->zFile,"-")==0 ){
      xjd1StringAppend(&newFile, argv[1], 0);
................................................................................
    }
    zNew = xjd1StringText(&newFile);
    printf("BEGIN %s\n", zNew);
    processOneFile(p, zNew);
    printf("END %s - %d new errors\n", zNew, p->nErr - nErr);
    xjd1StringClear(&newFile);
  }
  return 0;
}

/*
** Command:  .breakpoint
** A place to seet a breakpoint
*/
static int shellBreakpoint(Shell *p, int argc, char **argv){
  /* no-op */
  return 0;
}

/*
** Command:  .puts TEXT
*/
static int shellPuts(Shell *p, int argc, char **argv){
  if( argc>=2 ) printf("%s\n", argv[1]);
  return 0;
}

/*
** Process a command intended for this shell - not for the database.
**
** Return 1 to abort or 0 to continue.
*/
static int processMetaCommand(Shell *p){
  char *z;
  int n;
  char *azArg[2];
  int nArg;
  int i;
  int rc;
  static const struct shcmd {
    const char *zCmd;                   /* Name of the command */
    int (*xCmd)(Shell*,int,char**);     /* Procedure to run the command */
    const char *zHelp;                  /* Help string */
  } cmds[] = {
    { "quit",       shellQuit,        ".quit"               },
    { "testcase",   shellTestcase,    ".testcase NAME"      },
    { "result",     shellResult,      ".result TEXT"        },
    { "glob",       shellGlob,        ".glob PATTERN"       },
    { "notglob",    shellNotGlob,     ".notglob PATTERN"    },
................................................................................
    azArg[1] = 0;
    nArg = 1;
  }

  /* Find the command */
  for(i=0; i<sizeof(cmds)/sizeof(cmds[0]); i++){
    if( strcmp(azArg[0], cmds[i].zCmd)==0 ){
      rc = cmds[i].xCmd(p, nArg, azArg);
      break;
    }
  }
  if( i>=sizeof(cmds)/sizeof(cmds[0]) ){
    p->nErr++;
    fprintf(stderr, "%s:%d: unknown command \".%s\"\n", 
            p->zFile, p->nLine, azArg[0]);
    return 1;
  }
  xjd1StringTruncate(&p->inBuf);
  return rc;
}

/*
** Append text to the end of the testOut string.  If there is already
** text in the testOut buffer, prepent a space first.
*/
static void appendTestOut(Shell *p, const char *z, int n){
................................................................................
    if( zLine[0]=='.' && xjd1StringLen(&p->inBuf) ){
      fprintf(stderr, "%s:%d: missing ';'\n", p->zFile, p->nLine);
      xjd1StringTruncate(&p->inBuf);
    }
    xjd1StringAppend(&p->inBuf, zLine, -1);
    zIn = xjd1StringText(&p->inBuf);
    if( zIn[0]=='.' ){
      if( processMetaCommand(p) ) break;
    }else{
      processScript(p);
    }
  }
  if( p->isTTY==0 ){
    fclose(p->pIn);
  }

Changes to src/stmt.c.

282
283
284
285
286
287
288

289
290
291
292
293
294
295
  pCmd = pStmt->pCmd;
  if( pCmd==0 ) return 0;
  switch( pCmd->eCmdType ){
    case TK_SELECT: {
      pRes = xjd1QueryDoc(pCmd->u.q.pQuery, zDocName);
      break;
    }

    case TK_DELETE: {
      pRes = xjd1JsonRef(pStmt->pDoc);
      break;
    }
  }
  return pRes;
}







>







282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  pCmd = pStmt->pCmd;
  if( pCmd==0 ) return 0;
  switch( pCmd->eCmdType ){
    case TK_SELECT: {
      pRes = xjd1QueryDoc(pCmd->u.q.pQuery, zDocName);
      break;
    }
    case TK_UPDATE:
    case TK_DELETE: {
      pRes = xjd1JsonRef(pStmt->pDoc);
      break;
    }
  }
  return pRes;
}

Changes to src/update.c.

13
14
15
16
17
18
19













20
21
22
23
24
25
26





27
28































































29
30
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** Code to evaluate an UPDATE statement
*/
#include "xjd1Int.h"














/*
** Evaluate an UPDATE.
*/
int xjd1UpdateStep(xjd1_stmt *pStmt){
  Command *pCmd = pStmt->pCmd;
  int rc = XJD1_OK;





  assert( pCmd!=0 );
  assert( pCmd->eCmdType==TK_UPDATE );































































  return rc;
}







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







>
>
>
>
>


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


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** Code to evaluate an UPDATE statement
*/
#include "xjd1Int.h"


/*
** Perform an edit on a JSON value.  Return the document after the change.
*/
static JsonNode *reviseOneField(
  JsonNode *pDoc,        /* The document to be edited */
  Expr *pLvalue,         /* Definition of field in document to be changed */
  Expr *pValue           /* New value for the field */
){
  return pDoc;
}


/*
** Evaluate an UPDATE.
*/
int xjd1UpdateStep(xjd1_stmt *pStmt){
  Command *pCmd = pStmt->pCmd;
  int rc = XJD1_OK;
  int nUpdate = 0;
  sqlite3 *db = pStmt->pConn->db;
  sqlite3_stmt *pQuery, *pReplace;
  char *zSql;

  assert( pCmd!=0 );
  assert( pCmd->eCmdType==TK_UPDATE );
  if( pCmd->u.update.pUpsert ){
    sqlite3_exec(db, "BEGIN", 0, 0, 0);
  }
  zSql = sqlite3_mprintf("SELECT rowid, x FROM \"%w\"", pCmd->u.update.zName);
  sqlite3_prepare_v2(db, zSql, -1, &pQuery, 0);
  sqlite3_free(zSql);
  zSql = sqlite3_mprintf("UPDATE \"%w\" SET x=?1 WHERE rowid=?2",
                         pCmd->u.update.zName);
  sqlite3_prepare_v2(db, zSql, -1, &pReplace, 0);
  sqlite3_free(zSql);
  if( pQuery && pReplace ){
    while( SQLITE_ROW==sqlite3_step(pQuery) ){
      const char *zJson = (const char*)sqlite3_column_text(pQuery, 1);
      pStmt->pDoc = xjd1JsonParse(zJson, -1);
      if( xjd1ExprTrue(pCmd->u.del.pWhere) ){
        JsonNode *pNewDoc;  /* Revised document content */
        ExprList *pChng;    /* List of changes */
        String jsonNewDoc;  /* Text rendering of revised document */
        int i, n;

        pNewDoc  = xjd1JsonRef(pStmt->pDoc);
        pChng = pCmd->u.update.pChng;
        n = pChng->nEItem;
        for(i=0; i<n-1; i += 2){
          Expr *pLvalue = pChng->apEItem[i].pExpr;
          Expr *pExpr = pChng->apEItem[i+1].pExpr;
          pNewDoc = reviseOneField(pNewDoc, pLvalue, pExpr);
        }
        xjd1StringInit(&jsonNewDoc, 0, 0);
        xjd1JsonRender(&jsonNewDoc, pNewDoc);
        sqlite3_bind_int64(pReplace, 1, sqlite3_column_int64(pQuery, 0));
        sqlite3_bind_text(pReplace, 2, xjd1StringText(&jsonNewDoc), -1,
                          SQLITE_STATIC);
        sqlite3_step(pReplace);
        sqlite3_reset(pReplace);
        xjd1StringClear(&jsonNewDoc);
        xjd1JsonFree(pNewDoc);
        nUpdate++;
      }
      xjd1JsonFree(pStmt->pDoc);
      pStmt->pDoc = 0;
    }  
  }
  sqlite3_finalize(pQuery);
  sqlite3_finalize(pReplace);

  if( pCmd->u.update.pUpsert ){
    if( nUpdate==0 ){
      JsonNode *pToIns;
      String jsonToIns;
      pToIns = xjd1ExprEval(pCmd->u.update.pUpsert);
      xjd1StringInit(&jsonToIns, 0, 0); 
      xjd1JsonRender(&jsonToIns, pToIns);
      xjd1JsonFree(pToIns);
      zSql = sqlite3_mprintf(
           "INSERT INTO \"%w\" VALUES('%q')",
           pCmd->u.update.zName, xjd1StringText(&jsonToIns));
      xjd1StringClear(&jsonToIns);
      sqlite3_exec(db, zSql, 0, 0, 0);
      sqlite3_free(zSql);
    }
    sqlite3_exec(db, "COMMIT", 0, 0, 0);
  }
  return rc;
}

Changes to test/base01.test.

44
45
46
47
48
49
50







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
DELETE FROM abc WHERE abc.name=="xyz";
SELECT FROM abc;
.result {"name":"abc"}
.testcase 161
DELETE FROM abc WHERE abc.name=="abc";
SELECT FROM abc;
.result









.testcase 170
CREATE COLLECTION counter;
UPDATE counter SET counter.n=counter.n+1 WHERE counter.name=="xyz";
SELECT FROM counter;
.result
.testcase 171
UPDATE counter SET counter.n=counter.n+1 WHERE counter.name=="xyz"
  ELSE INSERT {name:"xyz", n:1};
SELECT FROM counter;
.result {"name":"xyz",n:1}
.testcase 172
UPDATE counter SET counter.n=counter.n+1 WHERE counter.name=="xyz"
  ELSE INSERT {name:"xyz", n:1};
SELECT FROM counter;
.result {"name":"xyz",n:2}
.testcase 173
INSERT INTO counter VALUE {name:"pqr", n:0};
UPDATE counter SET counter.n=counter.n+1 WHERE counter.name=="xyz"
  ELSE INSERT {name:"xyz", n:1};
SELECT FROM counter;
.result {"name":"xyz",n:2} {"name":"pqr",n:0}







>
>
>
>
>
>
>











|




|





|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
DELETE FROM abc WHERE abc.name=="xyz";
SELECT FROM abc;
.result {"name":"abc"}
.testcase 161
DELETE FROM abc WHERE abc.name=="abc";
SELECT FROM abc;
.result
.testcase 162
INSERT INTO abc VALUE {a:1, b:2, c:3};
INSERT INTO abc VALUE {a:4, b:5, c:6};
INSERT INTO abc VALUE {a:7, x:99, z:100};
DELETE FROM abc WHERE abc.b==5;
SELECT FROM abc;
.result {"a":1,"b":2,"c":3} {"a":7,"x":99,"z":100}


.testcase 170
CREATE COLLECTION counter;
UPDATE counter SET counter.n=counter.n+1 WHERE counter.name=="xyz";
SELECT FROM counter;
.result
.testcase 171
UPDATE counter SET counter.n=counter.n+1 WHERE counter.name=="xyz"
  ELSE INSERT {name:"xyz", n:1};
SELECT FROM counter;
.result {"name":"xyz","n":1}
.testcase 172
UPDATE counter SET counter.n=counter.n+1 WHERE counter.name=="xyz"
  ELSE INSERT {name:"xyz", n:1};
SELECT FROM counter;
.result {"name":"xyz","n":2}
.testcase 173
INSERT INTO counter VALUE {name:"pqr", n:0};
UPDATE counter SET counter.n=counter.n+1 WHERE counter.name=="xyz"
  ELSE INSERT {name:"xyz", n:1};
SELECT FROM counter;
.result {"name":"xyz","n":2} {"name":"pqr","n":0}