/ Check-in [9ff6ccde]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Fixes for compiler warnings and errors in the makefiles. Rename the one test script to json101.test.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | json
Files: files | file ages | folders
SHA1: 9ff6ccde5f26f18073587c320290570854ffc833
User & Date: drh 2015-08-21 20:37:12
Context
2015-08-21
20:43
Add extension functions for processing JSON. check-in: 178f9a35 user: drh tags: trunk
20:37
Fixes for compiler warnings and errors in the makefiles. Rename the one test script to json101.test. Closed-Leaf check-in: 9ff6ccde user: drh tags: json
20:12
Change the name of the json loadable extension to "json1.c", in anticipation of having future major changes to the interface. check-in: d0d4bec9 user: drh tags: json
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   413    413     $(TOP)/ext/misc/closure.c \
   414    414     $(TOP)/ext/misc/eval.c \
   415    415     $(TOP)/ext/misc/fileio.c \
   416    416     $(TOP)/ext/misc/fuzzer.c \
   417    417     $(TOP)/ext/fts5/fts5_tcl.c \
   418    418     $(TOP)/ext/fts5/fts5_test_mi.c \
   419    419     $(TOP)/ext/misc/ieee754.c \
   420         -  $(TOP)/ext/misc/json.c \
          420  +  $(TOP)/ext/misc/json1.c \
   421    421     $(TOP)/ext/misc/nextchar.c \
   422    422     $(TOP)/ext/misc/percentile.c \
   423    423     $(TOP)/ext/misc/regexp.c \
   424    424     $(TOP)/ext/misc/series.c \
   425    425     $(TOP)/ext/misc/spellfix.c \
   426    426     $(TOP)/ext/misc/totype.c \
   427    427     $(TOP)/ext/misc/wholenumber.c

Changes to Makefile.msc.

  1079   1079     $(TOP)\ext\misc\eval.c \
  1080   1080     $(TOP)\ext\misc\fileio.c \
  1081   1081     $(TOP)\ext\misc\fuzzer.c \
  1082   1082     fts5.c \
  1083   1083     $(TOP)\ext\fts5\fts5_tcl.c \
  1084   1084     $(TOP)\ext\fts5\fts5_test_mi.c \
  1085   1085     $(TOP)\ext\misc\ieee754.c \
  1086         -  $(TOP)\ext\misc\json.c \
         1086  +  $(TOP)\ext\misc\json1.c \
  1087   1087     $(TOP)\ext\misc\nextchar.c \
  1088   1088     $(TOP)\ext\misc\percentile.c \
  1089   1089     $(TOP)\ext\misc\regexp.c \
  1090   1090     $(TOP)\ext\misc\series.c \
  1091   1091     $(TOP)\ext\misc\spellfix.c \
  1092   1092     $(TOP)\ext\misc\totype.c \
  1093   1093     $(TOP)\ext\misc\wholenumber.c

Changes to ext/misc/json1.c.

    25     25   SQLITE_EXTENSION_INIT1
    26     26   #include <assert.h>
    27     27   #include <string.h>
    28     28   #include <ctype.h>
    29     29   #include <stdlib.h>
    30     30   #include <stdarg.h>
    31     31   
           32  +#define UNUSED_PARAM(X)  (void)(X)
           33  +
    32     34   /* Unsigned integer types */
    33     35   typedef sqlite3_uint64 u64;
    34     36   typedef unsigned int u32;
    35     37   typedef unsigned char u8;
    36     38   
    37     39   /* Objects */
    38     40   typedef struct JsonString JsonString;
................................................................................
   155    157     if( p->bStatic ){
   156    158       if( p->bErr ) return 1;
   157    159       zNew = sqlite3_malloc64(nTotal);
   158    160       if( zNew==0 ){
   159    161         jsonOom(p);
   160    162         return SQLITE_NOMEM;
   161    163       }
   162         -    memcpy(zNew, p->zBuf, p->nUsed);
          164  +    memcpy(zNew, p->zBuf, (size_t)p->nUsed);
   163    165       p->zBuf = zNew;
   164    166       p->bStatic = 0;
   165    167     }else{
   166    168       zNew = sqlite3_realloc64(p->zBuf, nTotal);
   167    169       if( zNew==0 ){
   168    170         jsonOom(p);
   169    171         return SQLITE_NOMEM;
................................................................................
   574    576     u32 iThis;
   575    577     int x;
   576    578     while( isspace(pParse->zJson[i]) ){ i++; }
   577    579     if( (c = pParse->zJson[i])==0 ) return 0;
   578    580     if( c=='{' ){
   579    581       /* Parse object */
   580    582       iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
   581         -    if( iThis<0 ) return -1;
   582    583       for(j=i+1;;j++){
   583    584         while( isspace(pParse->zJson[j]) ){ j++; }
   584    585         x = jsonParseValue(pParse, j);
   585    586         if( x<0 ){
   586    587           if( x==(-2) && pParse->nNode==iThis+1 ) return j+1;
   587    588           return -1;
   588    589         }
................................................................................
   601    602         break;
   602    603       }
   603    604       pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
   604    605       return j+1;
   605    606     }else if( c=='[' ){
   606    607       /* Parse array */
   607    608       iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
   608         -    if( iThis<0 ) return -1;
   609    609       for(j=i+1;;j++){
   610    610         while( isspace(pParse->zJson[j]) ){ j++; }
   611    611         x = jsonParseValue(pParse, j);
   612    612         if( x<0 ){
   613    613           if( x==(-3) && pParse->nNode==iThis+1 ) return j+1;
   614    614           return -1;
   615    615         }
................................................................................
   934    934   static void jsonNodeCountFunc(
   935    935     sqlite3_context *context,
   936    936     int argc,
   937    937     sqlite3_value **argv
   938    938   ){
   939    939     JsonParse x;  /* The parse */
   940    940     if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
   941         -  sqlite3_result_int64(context, x.nNode);
          941  +  sqlite3_result_int64(context, (sqlite3_int64)x.nNode);
   942    942     jsonParseReset(&x);
   943    943   }
   944    944   #endif /* SQLITE_DEBUG */
   945    945   
   946    946   /****************************************************************************
   947    947   ** SQL function implementations
   948    948   ****************************************************************************/
................................................................................
  1095   1095     JsonNode *pNode;
  1096   1096     const char *zPath;
  1097   1097     u32 i;
  1098   1098   
  1099   1099     if( argc<1 ) return;
  1100   1100     if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
  1101   1101     if( x.nNode ){
  1102         -    for(i=1; i<argc; i++){
         1102  +    for(i=1; i<(u32)argc; i++){
  1103   1103         zPath = (const char*)sqlite3_value_text(argv[i]);
  1104   1104         if( zPath==0 ) continue;
  1105   1105         if( zPath[0]!='$' ) continue;
  1106   1106         pNode = jsonLookup(&x, 0, &zPath[1], 0);
  1107   1107         if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
  1108   1108       }
  1109   1109       if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
................................................................................
  1133   1133     if( (argc&1)==0 ) {
  1134   1134       sqlite3_result_error(context,
  1135   1135                            "json_replace() needs an odd number of arguments", -1);
  1136   1136       return;
  1137   1137     }
  1138   1138     if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
  1139   1139     if( x.nNode ){
  1140         -    for(i=1; i<argc; i+=2){
         1140  +    for(i=1; i<(u32)argc; i+=2){
  1141   1141         zPath = (const char*)sqlite3_value_text(argv[i]);
  1142   1142         if( zPath==0 ) continue;
  1143   1143         if( zPath[0]!='$' ) continue;
  1144   1144         pNode = jsonLookup(&x, 0, &zPath[1], 0);
  1145   1145         if( pNode ){
  1146   1146           pNode->jnFlags |= JNODE_REPLACE;
  1147   1147           pNode->iVal = i+1;
................................................................................
  1184   1184     if( (argc&1)==0 ) {
  1185   1185       sqlite3_result_error(context,
  1186   1186                            "json_set() needs an odd number of arguments", -1);
  1187   1187       return;
  1188   1188     }
  1189   1189     if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
  1190   1190     if( x.nNode ){
  1191         -    for(i=1; i<argc; i+=2){
         1191  +    for(i=1; i<(u32)argc; i+=2){
  1192   1192         zPath = (const char*)sqlite3_value_text(argv[i]);
  1193   1193         if( zPath==0 ) continue;
  1194   1194         if( zPath[0]!='$' ) continue;
  1195   1195         bApnd = 0;
  1196   1196         pNode = jsonLookup(&x, 0, &zPath[1], &bApnd);
  1197   1197         if( pNode && (bApnd || bIsSet) ){
  1198   1198           pNode->jnFlags |= JNODE_REPLACE;
................................................................................
  1274   1274   #define JEACH_ATOM    3
  1275   1275   #define JEACH_ID      4
  1276   1276   #define JEACH_PARENT  5
  1277   1277   #define JEACH_FULLKEY 6
  1278   1278   #define JEACH_JSON    7
  1279   1279   #define JEACH_PATH    8
  1280   1280   
         1281  +  UNUSED_PARAM(pzErr);
         1282  +  UNUSED_PARAM(argv);
         1283  +  UNUSED_PARAM(argc);
         1284  +  UNUSED_PARAM(pAux);
  1281   1285     rc = sqlite3_declare_vtab(db, 
  1282   1286        "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,"
  1283   1287                       "json HIDDEN,path HIDDEN)");
  1284   1288     if( rc==SQLITE_OK ){
  1285   1289       pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
  1286   1290       if( pNew==0 ) return SQLITE_NOMEM;
  1287   1291       memset(pNew, 0, sizeof(*pNew));
................................................................................
  1294   1298     sqlite3_free(pVtab);
  1295   1299     return SQLITE_OK;
  1296   1300   }
  1297   1301   
  1298   1302   /* constructor for a JsonEachCursor object for json_each(). */
  1299   1303   static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
  1300   1304     JsonEachCursor *pCur;
         1305  +
         1306  +  UNUSED_PARAM(p);
  1301   1307     pCur = sqlite3_malloc( sizeof(*pCur) );
  1302   1308     if( pCur==0 ) return SQLITE_NOMEM;
  1303   1309     memset(pCur, 0, sizeof(*pCur));
  1304   1310     *ppCursor = &pCur->base;
  1305   1311     return SQLITE_OK;
  1306   1312   }
  1307   1313   
................................................................................
  1428   1434           u32 iKey;
  1429   1435           if( p->bRecursive ){
  1430   1436             if( p->iRowid==0 ) break;
  1431   1437             iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey - 1;
  1432   1438           }else{
  1433   1439             iKey = p->iRowid;
  1434   1440           }
  1435         -        sqlite3_result_int64(ctx, iKey);
         1441  +        sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
  1436   1442         }
  1437   1443         break;
  1438   1444       }
  1439   1445       case JEACH_VALUE: {
  1440   1446         if( p->eType==JSON_OBJECT ) pThis++;
  1441   1447         jsonReturn(pThis, ctx, 0);
  1442   1448         break;
................................................................................
  1449   1455       case JEACH_ATOM: {
  1450   1456         if( p->eType==JSON_OBJECT ) pThis++;
  1451   1457         if( pThis->eType>=JSON_ARRAY ) break;
  1452   1458         jsonReturn(pThis, ctx, 0);
  1453   1459         break;
  1454   1460       }
  1455   1461       case JEACH_ID: {
  1456         -      sqlite3_result_int64(ctx, p->i + (p->eType==JSON_OBJECT));
         1462  +      sqlite3_result_int64(ctx, (sqlite3_int64)p->i + (p->eType==JSON_OBJECT));
  1457   1463         break;
  1458   1464       }
  1459   1465       case JEACH_PARENT: {
  1460   1466         if( p->i>0 && p->bRecursive ){
  1461         -        sqlite3_result_int64(ctx, p->sParse.aUp[p->i]);
         1467  +        sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
  1462   1468         }
  1463   1469         break;
  1464   1470       }
  1465   1471       case JEACH_FULLKEY: {
  1466   1472         JsonString x;
  1467   1473         jsonInit(&x, ctx);
  1468   1474         if( p->bRecursive ){
................................................................................
  1522   1528     sqlite3_vtab *tab,
  1523   1529     sqlite3_index_info *pIdxInfo
  1524   1530   ){
  1525   1531     int i;
  1526   1532     int jsonIdx = -1;
  1527   1533     int pathIdx = -1;
  1528   1534     const struct sqlite3_index_constraint *pConstraint;
         1535  +
         1536  +  UNUSED_PARAM(tab);
  1529   1537     pConstraint = pIdxInfo->aConstraint;
  1530   1538     for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
  1531   1539       if( pConstraint->usable==0 ) continue;
  1532   1540       if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
  1533   1541       switch( pConstraint->iColumn ){
  1534   1542         case JEACH_JSON:   jsonIdx = i;    break;
  1535   1543         case JEACH_PATH:   pathIdx = i;    break;
................................................................................
  1561   1569     int argc, sqlite3_value **argv
  1562   1570   ){
  1563   1571     JsonEachCursor *p = (JsonEachCursor*)cur;
  1564   1572     const char *z;
  1565   1573     const char *zPath;
  1566   1574     sqlite3_int64 n;
  1567   1575   
         1576  +  UNUSED_PARAM(idxStr);
         1577  +  UNUSED_PARAM(argc);
  1568   1578     jsonEachCursorReset(p);
  1569   1579     if( idxNum==0 ) return SQLITE_OK;
  1570   1580     z = (const char*)sqlite3_value_text(argv[0]);
  1571   1581     if( z==0 ) return SQLITE_OK;
  1572   1582     if( idxNum&2 ){
  1573   1583       zPath = (const char*)sqlite3_value_text(argv[1]);
  1574   1584       if( zPath==0 || zPath[0]!='$' ) return SQLITE_OK;
  1575   1585     }
  1576   1586     n = sqlite3_value_bytes(argv[0]);
  1577         -  p->zJson = sqlite3_malloc( n+1 );
         1587  +  p->zJson = sqlite3_malloc64( n+1 );
  1578   1588     if( p->zJson==0 ) return SQLITE_NOMEM;
  1579         -  memcpy(p->zJson, z, n+1);
         1589  +  memcpy(p->zJson, z, (size_t)n+1);
  1580   1590     if( jsonParse(&p->sParse, p->zJson) 
  1581   1591      || (p->bRecursive && jsonParseFindParents(&p->sParse))
  1582   1592     ){
  1583   1593       jsonEachCursorReset(p);
  1584   1594     }else{
  1585   1595       JsonNode *pNode;
  1586   1596       if( idxNum==3 ){
  1587   1597         p->bRecursive = 0;
  1588   1598         n = sqlite3_value_bytes(argv[1]);
  1589         -      p->zPath = sqlite3_malloc( n+1 );
         1599  +      p->zPath = sqlite3_malloc64( n+1 );
  1590   1600         if( p->zPath==0 ) return SQLITE_NOMEM;
  1591         -      memcpy(p->zPath, zPath, n+1);
         1601  +      memcpy(p->zPath, zPath, (size_t)n+1);
  1592   1602         pNode = jsonLookup(&p->sParse, 0, p->zPath+1, 0);
  1593   1603         if( pNode==0 ){
  1594   1604           jsonEachCursorReset(p);
  1595   1605           return SQLITE_OK;
  1596   1606         }
  1597   1607       }else{
  1598   1608         pNode = p->sParse.aNode;
................................................................................
  1627   1637     0,                         /* xUpdate */
  1628   1638     0,                         /* xBegin */
  1629   1639     0,                         /* xSync */
  1630   1640     0,                         /* xCommit */
  1631   1641     0,                         /* xRollback */
  1632   1642     0,                         /* xFindMethod */
  1633   1643     0,                         /* xRename */
         1644  +  0,                         /* xSavepoint */
         1645  +  0,                         /* xRelease */
         1646  +  0                          /* xRollbackTo */
  1634   1647   };
  1635   1648   
  1636   1649   /* The methods of the json_tree virtual table. */
  1637   1650   static sqlite3_module jsonTreeModule = {
  1638   1651     0,                         /* iVersion */
  1639   1652     0,                         /* xCreate */
  1640   1653     jsonEachConnect,           /* xConnect */
................................................................................
  1651   1664     0,                         /* xUpdate */
  1652   1665     0,                         /* xBegin */
  1653   1666     0,                         /* xSync */
  1654   1667     0,                         /* xCommit */
  1655   1668     0,                         /* xRollback */
  1656   1669     0,                         /* xFindMethod */
  1657   1670     0,                         /* xRename */
         1671  +  0,                         /* xSavepoint */
         1672  +  0,                         /* xRelease */
         1673  +  0                          /* xRollbackTo */
  1658   1674   };
  1659   1675   
  1660   1676   /****************************************************************************
  1661   1677   ** The following routine is the only publically visible identifier in this
  1662   1678   ** file.  Call the following routine in order to register the various SQL
  1663   1679   ** functions and the virtual table implemented by this file.
  1664   1680   ****************************************************************************/
................................................................................
  1668   1684   #endif
  1669   1685   int sqlite3_json_init(
  1670   1686     sqlite3 *db, 
  1671   1687     char **pzErrMsg, 
  1672   1688     const sqlite3_api_routines *pApi
  1673   1689   ){
  1674   1690     int rc = SQLITE_OK;
  1675         -  int i;
         1691  +  unsigned int i;
  1676   1692     static const struct {
  1677   1693        const char *zName;
  1678   1694        int nArg;
  1679   1695        int flag;
  1680   1696        void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  1681   1697     } aFunc[] = {
  1682   1698       { "json_array",          -1, 0,   jsonArrayFunc         },

Changes to main.mk.

   293    293   TESTSRC += \
   294    294     $(TOP)/ext/misc/amatch.c \
   295    295     $(TOP)/ext/misc/closure.c \
   296    296     $(TOP)/ext/misc/eval.c \
   297    297     $(TOP)/ext/misc/fileio.c \
   298    298     $(TOP)/ext/misc/fuzzer.c \
   299    299     $(TOP)/ext/misc/ieee754.c \
   300         -  $(TOP)/ext/misc/json.c \
          300  +  $(TOP)/ext/misc/json1.c \
   301    301     $(TOP)/ext/misc/nextchar.c \
   302    302     $(TOP)/ext/misc/percentile.c \
   303    303     $(TOP)/ext/misc/regexp.c \
   304    304     $(TOP)/ext/misc/series.c \
   305    305     $(TOP)/ext/misc/spellfix.c \
   306    306     $(TOP)/ext/misc/totype.c \
   307    307     $(TOP)/ext/misc/wholenumber.c \

Name change from test/json1.test to test/json101.test.