Fossil SCM

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.

stephan 2011-10-20 01:20 UTC json-multitag-test
Commit 23a35ba1cccad37844f23867319668dafe6430b4
+17 -31
--- src/json.c
+++ src/json.c
@@ -1679,34 +1679,27 @@
16791679
return code;
16801680
}
16811681
16821682
/*
16831683
** Iterates through a prepared SELECT statement and converts each row
1684
-** to a JSON object. If pTgt is not NULL then it must be-a Array
1685
-** object and this function will return pTgt. If pTgt is NULL then a
1686
-** new Array object is created and returned (owned by the
1687
-** caller). Each row of pStmt is converted to an Object and appended
1688
-** to the array. If the result set has no rows AND pTgt is NULL then
1689
-** NULL is returned.
1684
+** to a JSON object. If pTgt is not NULL then this function will
1685
+** append the results to pTgt and return cson_array_value(pTgt). If
1686
+** pTgt is NULL then a new Array object is created and returned (owned
1687
+** by the caller). Each row of pStmt is converted to an Object and
1688
+** appended to the array. If the result set has no rows AND pTgt is
1689
+** NULL then NULL (not an empty array) is returned.
16901690
*/
16911691
cson_value * json_stmt_to_array_of_obj(Stmt *pStmt,
1692
- cson_value * pTgt){
1693
- cson_value * v = pTgt;
1694
- cson_array * a = NULL;
1692
+ cson_array * pTgt){
1693
+ cson_array * a = pTgt;
16951694
char const * warnMsg = NULL;
16961695
cson_value * colNamesV = NULL;
16971696
cson_array * colNames = NULL;
1698
- if(v && !cson_value_is_array(v)){
1699
- return NULL;
1700
- }
17011697
while( (SQLITE_ROW==db_step(pStmt)) ){
17021698
cson_value * row = NULL;
17031699
if(!a){
1704
- if(!v){
1705
- v = cson_value_new_array();
1706
- }
1707
- a = cson_value_get_array(v);
1700
+ a = cson_new_array();
17081701
assert(NULL!=a);
17091702
}
17101703
if(!colNames){
17111704
colNamesV = cson_sqlite3_column_names(pStmt->pStmt);
17121705
assert(NULL != colNamesV);
@@ -1720,49 +1713,42 @@
17201713
continue;
17211714
}
17221715
if( 0 != cson_array_append(a, row) ){
17231716
cson_value_free(row);
17241717
assert( 0 && "Alloc error.");
1725
- if(pTgt != v) {
1726
- cson_value_free(v);
1718
+ if(pTgt != a) {
1719
+ cson_free_array(a);
17271720
}
17281721
return NULL;
17291722
}
17301723
}
17311724
cson_value_free(colNamesV);
17321725
if(warnMsg){
17331726
json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, warnMsg );
17341727
}
1735
- return v;
1728
+ return cson_array_value(a);
17361729
}
17371730
17381731
/*
17391732
** Works just like json_stmt_to_array_of_obj(), but each row in the
17401733
** result set is represented as an Array of values instead of an
17411734
** Object (key/value pairs). If pTgt is NULL and the statement
17421735
** has no results then NULL is returned, not an empty array.
17431736
*/
17441737
cson_value * json_stmt_to_array_of_array(Stmt *pStmt,
1745
- cson_value * pTgt){
1746
- cson_value * v = pTgt;
1747
- cson_array * a = NULL;
1748
- if(v && !cson_value_is_array(v)){
1749
- return NULL;
1750
- }
1738
+ cson_array * pTgt){
1739
+ cson_array * a = pTgt;
17511740
while( (SQLITE_ROW==db_step(pStmt)) ){
17521741
cson_value * row = NULL;
17531742
if(!a){
1754
- if(!v){
1755
- v = cson_value_new_array();
1756
- }
1757
- a = cson_value_get_array(v);
1743
+ a = cson_new_array();
17581744
assert(NULL!=a);
17591745
}
17601746
row = cson_sqlite3_row_to_array(pStmt->pStmt);
17611747
cson_array_append(a, row);
17621748
}
1763
- return v;
1749
+ return cson_array_value(a);
17641750
}
17651751
17661752
17671753
/*
17681754
** Executes the given SQL and runs it through
@@ -1771,11 +1757,11 @@
17711757
** after preparing the query.
17721758
**
17731759
** pTgt has the same semantics as described for
17741760
** json_stmt_to_array_of_obj().
17751761
*/
1776
-cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_value * pTgt,
1762
+cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_array * pTgt,
17771763
char resetBlob){
17781764
Stmt q = empty_Stmt;
17791765
cson_value * pay = NULL;
17801766
assert( blob_size(pSql) > 0 );
17811767
db_prepare(&q, "%s", blob_str(pSql));
17821768
--- src/json.c
+++ src/json.c
@@ -1679,34 +1679,27 @@
1679 return code;
1680 }
1681
1682 /*
1683 ** Iterates through a prepared SELECT statement and converts each row
1684 ** to a JSON object. If pTgt is not NULL then it must be-a Array
1685 ** object and this function will return pTgt. If pTgt is NULL then a
1686 ** new Array object is created and returned (owned by the
1687 ** caller). Each row of pStmt is converted to an Object and appended
1688 ** to the array. If the result set has no rows AND pTgt is NULL then
1689 ** NULL is returned.
1690 */
1691 cson_value * json_stmt_to_array_of_obj(Stmt *pStmt,
1692 cson_value * pTgt){
1693 cson_value * v = pTgt;
1694 cson_array * a = NULL;
1695 char const * warnMsg = NULL;
1696 cson_value * colNamesV = NULL;
1697 cson_array * colNames = NULL;
1698 if(v && !cson_value_is_array(v)){
1699 return NULL;
1700 }
1701 while( (SQLITE_ROW==db_step(pStmt)) ){
1702 cson_value * row = NULL;
1703 if(!a){
1704 if(!v){
1705 v = cson_value_new_array();
1706 }
1707 a = cson_value_get_array(v);
1708 assert(NULL!=a);
1709 }
1710 if(!colNames){
1711 colNamesV = cson_sqlite3_column_names(pStmt->pStmt);
1712 assert(NULL != colNamesV);
@@ -1720,49 +1713,42 @@
1720 continue;
1721 }
1722 if( 0 != cson_array_append(a, row) ){
1723 cson_value_free(row);
1724 assert( 0 && "Alloc error.");
1725 if(pTgt != v) {
1726 cson_value_free(v);
1727 }
1728 return NULL;
1729 }
1730 }
1731 cson_value_free(colNamesV);
1732 if(warnMsg){
1733 json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, warnMsg );
1734 }
1735 return v;
1736 }
1737
1738 /*
1739 ** Works just like json_stmt_to_array_of_obj(), but each row in the
1740 ** result set is represented as an Array of values instead of an
1741 ** Object (key/value pairs). If pTgt is NULL and the statement
1742 ** has no results then NULL is returned, not an empty array.
1743 */
1744 cson_value * json_stmt_to_array_of_array(Stmt *pStmt,
1745 cson_value * pTgt){
1746 cson_value * v = pTgt;
1747 cson_array * a = NULL;
1748 if(v && !cson_value_is_array(v)){
1749 return NULL;
1750 }
1751 while( (SQLITE_ROW==db_step(pStmt)) ){
1752 cson_value * row = NULL;
1753 if(!a){
1754 if(!v){
1755 v = cson_value_new_array();
1756 }
1757 a = cson_value_get_array(v);
1758 assert(NULL!=a);
1759 }
1760 row = cson_sqlite3_row_to_array(pStmt->pStmt);
1761 cson_array_append(a, row);
1762 }
1763 return v;
1764 }
1765
1766
1767 /*
1768 ** Executes the given SQL and runs it through
@@ -1771,11 +1757,11 @@
1771 ** after preparing the query.
1772 **
1773 ** pTgt has the same semantics as described for
1774 ** json_stmt_to_array_of_obj().
1775 */
1776 cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_value * pTgt,
1777 char resetBlob){
1778 Stmt q = empty_Stmt;
1779 cson_value * pay = NULL;
1780 assert( blob_size(pSql) > 0 );
1781 db_prepare(&q, "%s", blob_str(pSql));
1782
--- src/json.c
+++ src/json.c
@@ -1679,34 +1679,27 @@
1679 return code;
1680 }
1681
1682 /*
1683 ** Iterates through a prepared SELECT statement and converts each row
1684 ** to a JSON object. If pTgt is not NULL then this function will
1685 ** append the results to pTgt and return cson_array_value(pTgt). If
1686 ** pTgt is NULL then a new Array object is created and returned (owned
1687 ** by the caller). Each row of pStmt is converted to an Object and
1688 ** appended to the array. If the result set has no rows AND pTgt is
1689 ** NULL then NULL (not an empty array) is returned.
1690 */
1691 cson_value * json_stmt_to_array_of_obj(Stmt *pStmt,
1692 cson_array * pTgt){
1693 cson_array * a = pTgt;
 
1694 char const * warnMsg = NULL;
1695 cson_value * colNamesV = NULL;
1696 cson_array * colNames = NULL;
 
 
 
1697 while( (SQLITE_ROW==db_step(pStmt)) ){
1698 cson_value * row = NULL;
1699 if(!a){
1700 a = cson_new_array();
 
 
 
1701 assert(NULL!=a);
1702 }
1703 if(!colNames){
1704 colNamesV = cson_sqlite3_column_names(pStmt->pStmt);
1705 assert(NULL != colNamesV);
@@ -1720,49 +1713,42 @@
1713 continue;
1714 }
1715 if( 0 != cson_array_append(a, row) ){
1716 cson_value_free(row);
1717 assert( 0 && "Alloc error.");
1718 if(pTgt != a) {
1719 cson_free_array(a);
1720 }
1721 return NULL;
1722 }
1723 }
1724 cson_value_free(colNamesV);
1725 if(warnMsg){
1726 json_warn( FSL_JSON_W_ROW_TO_JSON_FAILED, warnMsg );
1727 }
1728 return cson_array_value(a);
1729 }
1730
1731 /*
1732 ** Works just like json_stmt_to_array_of_obj(), but each row in the
1733 ** result set is represented as an Array of values instead of an
1734 ** Object (key/value pairs). If pTgt is NULL and the statement
1735 ** has no results then NULL is returned, not an empty array.
1736 */
1737 cson_value * json_stmt_to_array_of_array(Stmt *pStmt,
1738 cson_array * pTgt){
1739 cson_array * a = pTgt;
 
 
 
 
1740 while( (SQLITE_ROW==db_step(pStmt)) ){
1741 cson_value * row = NULL;
1742 if(!a){
1743 a = cson_new_array();
 
 
 
1744 assert(NULL!=a);
1745 }
1746 row = cson_sqlite3_row_to_array(pStmt->pStmt);
1747 cson_array_append(a, row);
1748 }
1749 return cson_array_value(a);
1750 }
1751
1752
1753 /*
1754 ** Executes the given SQL and runs it through
@@ -1771,11 +1757,11 @@
1757 ** after preparing the query.
1758 **
1759 ** pTgt has the same semantics as described for
1760 ** json_stmt_to_array_of_obj().
1761 */
1762 cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_array * pTgt,
1763 char resetBlob){
1764 Stmt q = empty_Stmt;
1765 cson_value * pay = NULL;
1766 assert( blob_size(pSql) > 0 );
1767 db_prepare(&q, "%s", blob_str(pSql));
1768
--- src/json_artifact.c
+++ src/json_artifact.c
@@ -236,10 +236,16 @@
236236
cson_object * pay = NULL;
237237
const char *zMime;
238238
Blob content = empty_blob;
239239
Stmt q = empty_Stmt;
240240
cson_array * checkin_arr = NULL;
241
+#if 0
242
+ /*see next #if block below*/
243
+ cson_string * tagKey = NULL;
244
+ cson_value * checkinV = NULL;
245
+ cson_object * checkin = NULL;
246
+#endif
241247
242248
if( ! g.perm.Read ){
243249
json_set_err(FSL_JSON_E_DENIED,
244250
"Requires 'o' privileges.");
245251
return NULL;
@@ -255,16 +261,10 @@
255261
zMime = mimetype_from_content(&content);
256262
257263
cson_object_set(pay, "contentType",
258264
json_new_string(zMime ? zMime : "text/plain"));
259265
if( json_artifact_include_content_flag() && !zMime ){
260
-#if 0
261
- /*see next #if block below*/
262
- cson_string * tagKey = NULL;
263
- cson_value * checkinV = NULL;
264
- cson_object * checkin = NULL;
265
-#endif
266266
cson_object_set(pay, "content",
267267
cson_value_new_string(blob_str(&content),
268268
(unsigned int)blob_size(&content)));
269269
}
270270
blob_reset(&content);
271271
--- src/json_artifact.c
+++ src/json_artifact.c
@@ -236,10 +236,16 @@
236 cson_object * pay = NULL;
237 const char *zMime;
238 Blob content = empty_blob;
239 Stmt q = empty_Stmt;
240 cson_array * checkin_arr = NULL;
 
 
 
 
 
 
241
242 if( ! g.perm.Read ){
243 json_set_err(FSL_JSON_E_DENIED,
244 "Requires 'o' privileges.");
245 return NULL;
@@ -255,16 +261,10 @@
255 zMime = mimetype_from_content(&content);
256
257 cson_object_set(pay, "contentType",
258 json_new_string(zMime ? zMime : "text/plain"));
259 if( json_artifact_include_content_flag() && !zMime ){
260 #if 0
261 /*see next #if block below*/
262 cson_string * tagKey = NULL;
263 cson_value * checkinV = NULL;
264 cson_object * checkin = NULL;
265 #endif
266 cson_object_set(pay, "content",
267 cson_value_new_string(blob_str(&content),
268 (unsigned int)blob_size(&content)));
269 }
270 blob_reset(&content);
271
--- src/json_artifact.c
+++ src/json_artifact.c
@@ -236,10 +236,16 @@
236 cson_object * pay = NULL;
237 const char *zMime;
238 Blob content = empty_blob;
239 Stmt q = empty_Stmt;
240 cson_array * checkin_arr = NULL;
241 #if 0
242 /*see next #if block below*/
243 cson_string * tagKey = NULL;
244 cson_value * checkinV = NULL;
245 cson_object * checkin = NULL;
246 #endif
247
248 if( ! g.perm.Read ){
249 json_set_err(FSL_JSON_E_DENIED,
250 "Requires 'o' privileges.");
251 return NULL;
@@ -255,16 +261,10 @@
261 zMime = mimetype_from_content(&content);
262
263 cson_object_set(pay, "contentType",
264 json_new_string(zMime ? zMime : "text/plain"));
265 if( json_artifact_include_content_flag() && !zMime ){
 
 
 
 
 
 
266 cson_object_set(pay, "content",
267 cson_value_new_string(blob_str(&content),
268 (unsigned int)blob_size(&content)));
269 }
270 blob_reset(&content);
271
--- src/json_timeline.c
+++ src/json_timeline.c
@@ -519,11 +519,10 @@
519519
cson_value * json_timeline_wiki(){
520520
/* This code is 95% the same as json_timeline_ci(), by the way. */
521521
cson_value * payV = NULL;
522522
cson_object * pay = NULL;
523523
cson_value * tmp = NULL;
524
- cson_value * listV = NULL;
525524
cson_array * list = NULL;
526525
int check = 0;
527526
Stmt q = empty_Stmt;
528527
Blob sql = empty_blob;
529528
if( !g.perm.RdWiki && !g.perm.Read ){
@@ -567,15 +566,14 @@
567566
" tagId AS tagId,"
568567
#endif
569568
" FROM json_timeline"
570569
" ORDER BY rowid",
571570
-1);
572
- listV = cson_value_new_array();
573
- list = cson_value_get_array(listV);
574
- tmp = listV;
571
+ list = cson_new_array();
572
+ tmp = cson_array_value(list);
575573
SET("timeline");
576
- json_stmt_to_array_of_obj(&q, listV);
574
+ json_stmt_to_array_of_obj(&q, list);
577575
#undef SET
578576
goto ok;
579577
error:
580578
assert( 0 != g.json.resultCode );
581579
cson_value_free(payV);
582580
--- src/json_timeline.c
+++ src/json_timeline.c
@@ -519,11 +519,10 @@
519 cson_value * json_timeline_wiki(){
520 /* This code is 95% the same as json_timeline_ci(), by the way. */
521 cson_value * payV = NULL;
522 cson_object * pay = NULL;
523 cson_value * tmp = NULL;
524 cson_value * listV = NULL;
525 cson_array * list = NULL;
526 int check = 0;
527 Stmt q = empty_Stmt;
528 Blob sql = empty_blob;
529 if( !g.perm.RdWiki && !g.perm.Read ){
@@ -567,15 +566,14 @@
567 " tagId AS tagId,"
568 #endif
569 " FROM json_timeline"
570 " ORDER BY rowid",
571 -1);
572 listV = cson_value_new_array();
573 list = cson_value_get_array(listV);
574 tmp = listV;
575 SET("timeline");
576 json_stmt_to_array_of_obj(&q, listV);
577 #undef SET
578 goto ok;
579 error:
580 assert( 0 != g.json.resultCode );
581 cson_value_free(payV);
582
--- src/json_timeline.c
+++ src/json_timeline.c
@@ -519,11 +519,10 @@
519 cson_value * json_timeline_wiki(){
520 /* This code is 95% the same as json_timeline_ci(), by the way. */
521 cson_value * payV = NULL;
522 cson_object * pay = NULL;
523 cson_value * tmp = NULL;
 
524 cson_array * list = NULL;
525 int check = 0;
526 Stmt q = empty_Stmt;
527 Blob sql = empty_blob;
528 if( !g.perm.RdWiki && !g.perm.Read ){
@@ -567,15 +566,14 @@
566 " tagId AS tagId,"
567 #endif
568 " FROM json_timeline"
569 " ORDER BY rowid",
570 -1);
571 list = cson_new_array();
572 tmp = cson_array_value(list);
 
573 SET("timeline");
574 json_stmt_to_array_of_obj(&q, list);
575 #undef SET
576 goto ok;
577 error:
578 assert( 0 != g.json.resultCode );
579 cson_value_free(payV);
580

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button