Fossil

Check-in [23a35ba1]
Login

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

Overview
Comment:Refactored json_stmt_to_array_of_xxx() to actually take a cson_array argument (not generic cson_value arg), now that the cson API allows that simplification.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | json-multitag-test | json
Files: files | file ages | folders
SHA1:23a35ba1cccad37844f23867319668dafe6430b4
User & Date: stephan 2011-10-20 01:20:07
Context
2011-10-20
07:00
Moved the user-related JSON handlers into their own file. check-in: 6e18fc2e user: stephan tags: json-multitag-test, json
01:20
Refactored json_stmt_to_array_of_xxx() to actually take a cson_array argument (not generic cson_value arg), now that the cson API allows that simplification. check-in: 23a35ba1 user: stephan tags: json-multitag-test, json
01:04
added a TODO. check-in: 23d69df6 user: stephan tags: json-multitag-test, json
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/json.c.

1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
....
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
    g.zErrMsg = msg;
  }
  return code;
}

/*
** Iterates through a prepared SELECT statement and converts each row
** to a JSON object. If pTgt is not NULL then it must be-a Array
** object and this function will return pTgt. If pTgt is NULL then a
** new Array object is created and returned (owned by the
** caller). Each row of pStmt is converted to an Object and appended
** to the array. If the result set has no rows AND pTgt is NULL then
** NULL is returned.
*/
cson_value * json_stmt_to_array_of_obj(Stmt *pStmt,
                                       cson_value * pTgt){
  cson_value * v = pTgt;
  cson_array * a = NULL;
  char const * warnMsg = NULL;
  cson_value * colNamesV = NULL;
  cson_array * colNames = NULL;
  if(v && !cson_value_is_array(v)){
    return NULL;
  }
  while( (SQLITE_ROW==db_step(pStmt)) ){
    cson_value * row = NULL;
    if(!a){
      if(!v){
        v = cson_value_new_array();
      }
      a = cson_value_get_array(v);
      assert(NULL!=a);
    }
    if(!colNames){
      colNamesV = cson_sqlite3_column_names(pStmt->pStmt);
      assert(NULL != colNamesV);
      cson_value_add_reference(colNamesV);
      colNames = cson_value_get_array(colNamesV);
................................................................................
    if(!row && !warnMsg){
      warnMsg = "Could not convert at least one result row to JSON.";
      continue;
    }
    if( 0 != cson_array_append(a, row) ){
      cson_value_free(row);
      assert( 0 && "Alloc error.");
      if(pTgt != v) {
        cson_value_free(v);
      }
      return NULL;
    }
  }
  cson_value_free(colNamesV);
  if(warnMsg){
    json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, warnMsg );
  }
  return v;  
}

/*
** Works just like json_stmt_to_array_of_obj(), but each row in the
** result set is represented as an Array of values instead of an
** Object (key/value pairs). If pTgt is NULL and the statement
** has no results then NULL is returned, not an empty array.
*/
cson_value * json_stmt_to_array_of_array(Stmt *pStmt,
                                         cson_value * pTgt){
  cson_value * v = pTgt;
  cson_array * a = NULL;
  if(v && !cson_value_is_array(v)){
    return NULL;
  }
  while( (SQLITE_ROW==db_step(pStmt)) ){
    cson_value * row = NULL;
    if(!a){
      if(!v){
        v = cson_value_new_array();
      }
      a = cson_value_get_array(v);
      assert(NULL!=a);
    }
    row = cson_sqlite3_row_to_array(pStmt->pStmt);
    cson_array_append(a, row);
  }
  return v;  
}


/*
** Executes the given SQL and runs it through
** json_stmt_to_array_of_obj(), returning the result of that
** function. If resetBlob is true then blob_reset(pSql) is called
** after preparing the query.
**
** pTgt has the same semantics as described for
** json_stmt_to_array_of_obj().
*/
cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_value * pTgt,
                                      char resetBlob){
  Stmt q = empty_Stmt;
  cson_value * pay = NULL;
  assert( blob_size(pSql) > 0 );
  db_prepare(&q, "%s", blob_str(pSql));
  if(resetBlob){
    blob_reset(pSql);







|
|
|
|
|
|


|
<
|



<
<
<



<
|
<
<







 







|
|








|









|
<
|
<
<
<



<
|
<
<





|












|







1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692

1693
1694
1695
1696



1697
1698
1699

1700


1701
1702
1703
1704
1705
1706
1707
....
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738

1739



1740
1741
1742

1743


1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
    g.zErrMsg = msg;
  }
  return code;
}

/*
** Iterates through a prepared SELECT statement and converts each row
** to a JSON object. If pTgt is not NULL then this function will
** append the results to pTgt and return cson_array_value(pTgt). If
** pTgt is NULL then a new Array object is created and returned (owned
** by the caller). Each row of pStmt is converted to an Object and
** appended to the array. If the result set has no rows AND pTgt is
** NULL then NULL (not an empty array) is returned.
*/
cson_value * json_stmt_to_array_of_obj(Stmt *pStmt,
                                       cson_array * pTgt){

  cson_array * a = pTgt;
  char const * warnMsg = NULL;
  cson_value * colNamesV = NULL;
  cson_array * colNames = NULL;



  while( (SQLITE_ROW==db_step(pStmt)) ){
    cson_value * row = NULL;
    if(!a){

      a = cson_new_array();


      assert(NULL!=a);
    }
    if(!colNames){
      colNamesV = cson_sqlite3_column_names(pStmt->pStmt);
      assert(NULL != colNamesV);
      cson_value_add_reference(colNamesV);
      colNames = cson_value_get_array(colNamesV);
................................................................................
    if(!row && !warnMsg){
      warnMsg = "Could not convert at least one result row to JSON.";
      continue;
    }
    if( 0 != cson_array_append(a, row) ){
      cson_value_free(row);
      assert( 0 && "Alloc error.");
      if(pTgt != a) {
        cson_free_array(a);
      }
      return NULL;
    }
  }
  cson_value_free(colNamesV);
  if(warnMsg){
    json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, warnMsg );
  }
  return cson_array_value(a);  
}

/*
** Works just like json_stmt_to_array_of_obj(), but each row in the
** result set is represented as an Array of values instead of an
** Object (key/value pairs). If pTgt is NULL and the statement
** has no results then NULL is returned, not an empty array.
*/
cson_value * json_stmt_to_array_of_array(Stmt *pStmt,
                                         cson_array * pTgt){

  cson_array * a = pTgt;



  while( (SQLITE_ROW==db_step(pStmt)) ){
    cson_value * row = NULL;
    if(!a){

      a = cson_new_array();


      assert(NULL!=a);
    }
    row = cson_sqlite3_row_to_array(pStmt->pStmt);
    cson_array_append(a, row);
  }
  return cson_array_value(a);
}


/*
** Executes the given SQL and runs it through
** json_stmt_to_array_of_obj(), returning the result of that
** function. If resetBlob is true then blob_reset(pSql) is called
** after preparing the query.
**
** pTgt has the same semantics as described for
** json_stmt_to_array_of_obj().
*/
cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_array * pTgt,
                                      char resetBlob){
  Stmt q = empty_Stmt;
  cson_value * pay = NULL;
  assert( blob_size(pSql) > 0 );
  db_prepare(&q, "%s", blob_str(pSql));
  if(resetBlob){
    blob_reset(pSql);

Changes to src/json_artifact.c.

234
235
236
237
238
239
240






241
242
243
244
245
246
247
...
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
cson_value * json_artifact_file(int rid){
  cson_value * payV = NULL;
  cson_object * pay = NULL;
  const char *zMime;
  Blob content = empty_blob;
  Stmt q = empty_Stmt;
  cson_array * checkin_arr = NULL;







  if( ! g.perm.Read ){
    json_set_err(FSL_JSON_E_DENIED,
                 "Requires 'o' privileges.");
    return NULL;
  }
  
................................................................................
                  json_new_int( blob_size(&content) )
                  /* achtung: overflow potential on 32-bit builds! */);
  zMime = mimetype_from_content(&content);

  cson_object_set(pay, "contentType",
                  json_new_string(zMime ? zMime : "text/plain"));
  if( json_artifact_include_content_flag() && !zMime ){
#if 0
    /*see next #if block below*/
      cson_string * tagKey = NULL;
      cson_value * checkinV = NULL;
      cson_object * checkin = NULL;
#endif
      cson_object_set(pay, "content",
                      cson_value_new_string(blob_str(&content),
                                            (unsigned int)blob_size(&content)));
  }
  blob_reset(&content);

  db_prepare(&q,







>
>
>
>
>
>







 







<
<
<
<
<
<







234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
...
259
260
261
262
263
264
265






266
267
268
269
270
271
272
cson_value * json_artifact_file(int rid){
  cson_value * payV = NULL;
  cson_object * pay = NULL;
  const char *zMime;
  Blob content = empty_blob;
  Stmt q = empty_Stmt;
  cson_array * checkin_arr = NULL;
#if 0
    /*see next #if block below*/
      cson_string * tagKey = NULL;
      cson_value * checkinV = NULL;
      cson_object * checkin = NULL;
#endif

  if( ! g.perm.Read ){
    json_set_err(FSL_JSON_E_DENIED,
                 "Requires 'o' privileges.");
    return NULL;
  }
  
................................................................................
                  json_new_int( blob_size(&content) )
                  /* achtung: overflow potential on 32-bit builds! */);
  zMime = mimetype_from_content(&content);

  cson_object_set(pay, "contentType",
                  json_new_string(zMime ? zMime : "text/plain"));
  if( json_artifact_include_content_flag() && !zMime ){






      cson_object_set(pay, "content",
                      cson_value_new_string(blob_str(&content),
                                            (unsigned int)blob_size(&content)));
  }
  blob_reset(&content);

  db_prepare(&q,

Changes to src/json_timeline.c.

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
...
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
**
*/
cson_value * json_timeline_wiki(){
  /* This code is 95% the same as json_timeline_ci(), by the way. */
  cson_value * payV = NULL;
  cson_object * pay = NULL;
  cson_value * tmp = NULL;
  cson_value * listV = NULL;
  cson_array * list = NULL;
  int check = 0;
  Stmt q = empty_Stmt;
  Blob sql = empty_blob;
  if( !g.perm.RdWiki && !g.perm.Read ){
    json_set_err( FSL_JSON_E_DENIED, "Wiki timeline requires 'o' or 'j' access.");
    return NULL;
................................................................................
             " tags AS tags," /*FIXME: split this into
                                a JSON array*/
             " tagId AS tagId,"
#endif
             " FROM json_timeline"
             " ORDER BY rowid",
             -1);
  listV = cson_value_new_array();
  list = cson_value_get_array(listV);
  tmp = listV;
  SET("timeline");
  json_stmt_to_array_of_obj(&q, listV);
#undef SET
  goto ok;
  error:
  assert( 0 != g.json.resultCode );
  cson_value_free(payV);
  payV = NULL;
  ok:







<







 







|
|
<

|







517
518
519
520
521
522
523

524
525
526
527
528
529
530
...
564
565
566
567
568
569
570
571
572

573
574
575
576
577
578
579
580
581
**
*/
cson_value * json_timeline_wiki(){
  /* This code is 95% the same as json_timeline_ci(), by the way. */
  cson_value * payV = NULL;
  cson_object * pay = NULL;
  cson_value * tmp = NULL;

  cson_array * list = NULL;
  int check = 0;
  Stmt q = empty_Stmt;
  Blob sql = empty_blob;
  if( !g.perm.RdWiki && !g.perm.Read ){
    json_set_err( FSL_JSON_E_DENIED, "Wiki timeline requires 'o' or 'j' access.");
    return NULL;
................................................................................
             " tags AS tags," /*FIXME: split this into
                                a JSON array*/
             " tagId AS tagId,"
#endif
             " FROM json_timeline"
             " ORDER BY rowid",
             -1);
  list = cson_new_array();
  tmp = cson_array_value(list);

  SET("timeline");
  json_stmt_to_array_of_obj(&q, list);
#undef SET
  goto ok;
  error:
  assert( 0 != g.json.resultCode );
  cson_value_free(payV);
  payV = NULL;
  ok: