UnQL

Check-in [05d60ec33a]
Login

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

Overview
Comment:Update adds new fields as appropriate.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 05d60ec33afc7c08138e97e6dfc743e487d93339
User & Date: drh 2011-06-30 22:24:28
Context
2011-06-30
23:22
Added demonstration script #1. check-in: 4893830330 user: drh tags: trunk
22:24
Update adds new fields as appropriate. check-in: 05d60ec33a user: drh tags: trunk
20:44
Basic UPDATE statements (and UPSERT) is now working. However, new fields are not added yet. check-in: e2bc14a5be user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

101
102
103
104
105
106
107

108
109
110
111
112
113
114
JsonNode *xjd1JsonEdit(JsonNode *p){
  JsonNode *pNew;
  if( p==0 ) return 0;
  if( p->nRef==1 ) return p;
  pNew = xjd1JsonNew(0);
  if( pNew==0 ) return 0;
  pNew->eJType = p->eJType;

  switch( pNew->eJType ){
    case XJD1_STRING: {
      pNew->u.z = xjd1PoolDup(0, p->u.z, -1);
      break;
    }
    case XJD1_ARRAY: {
      JsonNode **ap;







>







101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
JsonNode *xjd1JsonEdit(JsonNode *p){
  JsonNode *pNew;
  if( p==0 ) return 0;
  if( p->nRef==1 ) return p;
  pNew = xjd1JsonNew(0);
  if( pNew==0 ) return 0;
  pNew->eJType = p->eJType;
  pNew->u = p->u;
  switch( pNew->eJType ){
    case XJD1_STRING: {
      pNew->u.z = xjd1PoolDup(0, p->u.z, -1);
      break;
    }
    case XJD1_ARRAY: {
      JsonNode **ap;

Changes to src/update.c.

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
**   http://www.hwaci.com/drh/
**
*************************************************************************
** Code to evaluate an UPDATE statement
*/
#include "xjd1Int.h"


































/*
** The expression p is an L-value.  Find the corresponding JsonNode.
** Create it if necessary.
*/
static JsonNode *findOrCreateJsonNode(JsonNode **ppRoot, Expr *p){
  if( p==0 ) return 0;
  switch( p->eType ){
    case TK_DOT: {
      JsonNode *pBase = findOrCreateJsonNode(ppRoot, p->u.lvalue.pLeft);
      JsonStructElem *pElem;
      JsonNode *pRes = 0;
      if( pBase && pBase->eJType==XJD1_STRUCT ){
        for(pElem=pBase->u.st.pFirst; pElem; pElem=pElem->pNext){
          if( strcmp(pElem->zLabel, p->u.lvalue.zId)==0 ){
            pRes = pElem->pValue;
            break;
          }
        }
      }
      return pRes;
    }
    case TK_LB: {
      return 0;   /* TBD */
    }
    case TK_ID: {
      return *ppRoot;
    }







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









<
<
<
<
|
<
<
<
<
<
<







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
**   http://www.hwaci.com/drh/
**
*************************************************************************
** Code to evaluate an UPDATE statement
*/
#include "xjd1Int.h"


/*
** pBase is a JSON structure object.  Or if it is not, it should be
** converted into one.  Then lookup or insert the zField element.
*/
static JsonNode *findStructElement(JsonNode *pBase, const char *zField){
  JsonStructElem *pElem;
  if( pBase==0 ) return 0;
  if( pBase->eJType!=XJD1_STRUCT ){
    xjd1JsonToNull(pBase);
    pBase->eJType = XJD1_STRUCT;
    pBase->u.st.pFirst = 0;
    pBase->u.st.pLast = 0;
  }
  for(pElem=pBase->u.st.pFirst; pElem; pElem=pElem->pNext){
    if( strcmp(pElem->zLabel, zField)==0 ){
      return pElem->pValue;
    }
  }
  pElem = malloc( sizeof(*pElem) );
  if( pElem==0 ) return 0;
  pElem->pNext = 0;
  if( pBase->u.st.pLast==0 ){
    pBase->u.st.pFirst = pElem;
  }else{
    pBase->u.st.pLast->pNext = pElem;
  }
  pBase->u.st.pLast = pElem;
  pElem->zLabel = xjd1PoolDup(0, zField, -1);
  pElem->pValue = xjd1JsonNew(0);
  return pElem->pValue;  
}

/*
** The expression p is an L-value.  Find the corresponding JsonNode.
** Create it if necessary.
*/
static JsonNode *findOrCreateJsonNode(JsonNode **ppRoot, Expr *p){
  if( p==0 ) return 0;
  switch( p->eType ){
    case TK_DOT: {
      JsonNode *pBase = findOrCreateJsonNode(ppRoot, p->u.lvalue.pLeft);




      return findStructElement(pBase, p->u.lvalue.zId);






    }
    case TK_LB: {
      return 0;   /* TBD */
    }
    case TK_ID: {
      return *ppRoot;
    }

Changes to test/base01.test.

74
75
76
77
78
79
80









.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":3} {"name":"pqr","n":0}
















>
>
>
>
>
>
>
>
>
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
.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":3} {"name":"pqr","n":0}

.testcase 180
UPDATE counter SET counter.ctime=1234 WHERE counter.name=="xyz";
SELECT FROM counter WHERE counter.name=="xyz";
.result {"name":"xyz","n":3,"ctime":1234}
.testcase 181
UPDATE counter SET counter.ctime.x.y="This is a test" WHERE counter.name=="xyz";
SELECT FROM counter WHERE counter.name=="xyz";
.result {"name":"xyz","n":3,"ctime":{"x":{"y":"This is a test"}}}