UnQL

Check-in [5b31aa5264]
Login

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

Overview
Comment:Fix problems with arrays and objects with zero members.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5b31aa52645c7d82dcd120dd2e94a8d41e2d652b
User & Date: dan 2011-07-16 16:12:12
Context
2011-07-16
19:42
Fix compiler warnings. Remove an unused declaration. check-in: dba083d964 user: drh tags: trunk
16:12
Fix problems with arrays and objects with zero members. check-in: 5b31aa5264 user: dan tags: trunk
15:47
Add support for the [] operator on arrays and strings. check-in: 0d9044c381 user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

196
197
198
199
200
201
202

203
204
205
206
207
208
209
210
211
212
213

214
215
216
217
218
219
220
      case XJD1_STRING: {
        renderString(pOut, p->u.z);
        break;
      }
      case XJD1_ARRAY: {
        char cSep = '[';
        int i;

        for(i=0; i<p->u.ar.nElem; i++){
          xjd1StringAppend(pOut, &cSep, 1);
          cSep = ',';
          xjd1JsonRender(pOut, p->u.ar.apElem[i]);
        }
        xjd1StringAppend(pOut, "]", 1);
        break;
      }
      case XJD1_STRUCT: {
        char cSep = '{';
        JsonStructElem *pElem;

        for(pElem=p->u.st.pFirst; pElem; pElem=pElem->pNext){
          xjd1StringAppend(pOut, &cSep, 1);
          cSep = ',';
          renderString(pOut, pElem->zLabel);
          xjd1StringAppend(pOut, ":", 1);
          xjd1JsonRender(pOut, pElem->pValue);
        }







>











>







196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
      case XJD1_STRING: {
        renderString(pOut, p->u.z);
        break;
      }
      case XJD1_ARRAY: {
        char cSep = '[';
        int i;
        if( p->u.ar.nElem==0 ) xjd1StringAppend(pOut, &cSep, 1);
        for(i=0; i<p->u.ar.nElem; i++){
          xjd1StringAppend(pOut, &cSep, 1);
          cSep = ',';
          xjd1JsonRender(pOut, p->u.ar.apElem[i]);
        }
        xjd1StringAppend(pOut, "]", 1);
        break;
      }
      case XJD1_STRUCT: {
        char cSep = '{';
        JsonStructElem *pElem;
        if( p->u.st.pFirst==0 ) xjd1StringAppend(pOut, &cSep, 1);
        for(pElem=p->u.st.pFirst; pElem; pElem=pElem->pNext){
          xjd1StringAppend(pOut, &cSep, 1);
          cSep = ',';
          renderString(pOut, pElem->zLabel);
          xjd1StringAppend(pOut, ":", 1);
          xjd1JsonRender(pOut, pElem->pValue);
        }

Changes to src/parse.y.

206
207
208
209
210
211
212

213



214
215
216
217
218
219
220
221
222
223

224



225
226
227
228
229
230
231

  /* Generate an Expr object that is a structure */
  static Expr *stExpr(Parse *p, ExprList *pList){
    Expr *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew));
    if( pNew ){
      pNew->eType = TK_STRUCT;
      pNew->eClass = XJD1_EXPR_STRUCT;

      pNew->u.st = pList;



    }
    return pNew;
  }

  /* Generate an Expr object that is an array7 */
  static Expr *arExpr(Parse *p, ExprList *pList){
    Expr *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew));
    if( pNew ){
      pNew->eType = TK_ARRAY;
      pNew->eClass = XJD1_EXPR_ARRAY;

      pNew->u.ar = pList;



    }
    return pNew;
  }

  /* Generate an Expr object that is JSON value literal */
  static Expr *jsonExpr(Parse *p, JsonNode *pNode){
    Expr *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew));







>
|
>
>
>










>
|
>
>
>







206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

  /* Generate an Expr object that is a structure */
  static Expr *stExpr(Parse *p, ExprList *pList){
    Expr *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew));
    if( pNew ){
      pNew->eType = TK_STRUCT;
      pNew->eClass = XJD1_EXPR_STRUCT;
      if( pList ){
        pNew->u.st = pList;
      }else{
        pNew->u.st = (ExprList *)xjd1PoolMallocZero(p->pPool, sizeof(ExprList));
      }
    }
    return pNew;
  }

  /* Generate an Expr object that is an array7 */
  static Expr *arExpr(Parse *p, ExprList *pList){
    Expr *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew));
    if( pNew ){
      pNew->eType = TK_ARRAY;
      pNew->eClass = XJD1_EXPR_ARRAY;
      if( pList ){
        pNew->u.ar = pList;
      }else{
        pNew->u.ar = (ExprList *)xjd1PoolMallocZero(p->pPool, sizeof(ExprList));
      }
    }
    return pNew;
  }

  /* Generate an Expr object that is JSON value literal */
  static Expr *jsonExpr(Parse *p, JsonNode *pNode){
    Expr *pNew = xjd1PoolMallocZero(p->pPool, sizeof(*pNew));

Changes to test/base02.test.

144
145
146
147
148
149
150




151
SELECT c2.x[2.01]    FROM c2;
SELECT c2.x[1+1+1]   FROM c2;
SELECT c2.x[-1]      FROM c2;
SELECT c2.x[7]       FROM c2;
SELECT c2.x[0][0][0] FROM c2;
.result "a" "b" null "d" null null "a"













>
>
>
>

144
145
146
147
148
149
150
151
152
153
154
155
SELECT c2.x[2.01]    FROM c2;
SELECT c2.x[1+1+1]   FROM c2;
SELECT c2.x[-1]      FROM c2;
SELECT c2.x[7]       FROM c2;
SELECT c2.x[0][0][0] FROM c2;
.result "a" "b" null "d" null null "a"

.testcase 20
SELECT [];
SELECT {};
.result [] {}