Fossil SCM

Refactored prepareBranchStatement() to simplify its usage, get rid of ambiguous arg handling, and allow the caller to specify the priority of the all-vs-closed-vs-opened decision. Made it non-static and renamed to branch_prepare_statement() for re-use in /json/branch/list.

stephan 2011-09-21 18:20 json
Commit 5a81a5ead600ac8a3ae7e1e1ca696e889bba909d
2 files changed +10 -6 +7 -3
+10 -6
--- src/branch.c
+++ src/branch.c
@@ -178,14 +178,18 @@
178178
/* Do an autosync push, if requested */
179179
if( !isPrivate ) autosync(AUTOSYNC_PUSH);
180180
}
181181
182182
/*
183
-** Prepare a query that will list all branches.
183
+** Prepare a query that will list branches.
184
+**
185
+** If (which<0) then the query pulls only closed branches. If
186
+** (which>0) then the query pulls all (closed and opened)
187
+** branches. Else the query pulls currently-opened branches.
184188
*/
185
-void prepareBranchQuery(Stmt *pQuery, int showAll, int showClosed){
186
- if( showClosed ){
189
+void branch_prepare_query(Stmt *pQuery, int which ){
190
+ if( which < 0 ){
187191
db_prepare(pQuery,
188192
"SELECT value FROM tagxref"
189193
" WHERE tagid=%d AND value NOT NULL "
190194
"EXCEPT "
191195
"SELECT value FROM tagxref"
@@ -193,11 +197,11 @@
193197
" AND rid IN leaf"
194198
" AND NOT %z"
195199
" ORDER BY value COLLATE nocase /*sort*/",
196200
TAG_BRANCH, TAG_BRANCH, leaf_is_closed_sql("tagxref.rid")
197201
);
198
- }else if( showAll ){
202
+ }else if( which>0 ){
199203
db_prepare(pQuery,
200204
"SELECT DISTINCT value FROM tagxref"
201205
" WHERE tagid=%d AND value NOT NULL"
202206
" AND rid IN leaf"
203207
" ORDER BY value COLLATE nocase /*sort*/",
@@ -260,11 +264,11 @@
260264
if( g.localOpen ){
261265
vid = db_lget_int("checkout", 0);
262266
zCurrent = db_text(0, "SELECT value FROM tagxref"
263267
" WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
264268
}
265
- prepareBranchQuery(&q, showAll, showClosed);
269
+ branch_prepare_query(&q, showAll?1:(showClosed?-1:0));
266270
while( db_step(&q)==SQLITE_ROW ){
267271
const char *zBr = db_column_text(&q, 0);
268272
int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
269273
fossil_print("%s%s\n", (isCur ? "* " : " "), zBr);
270274
}
@@ -327,11 +331,11 @@
327331
@ Closed branches are fixed and do not change (unless they are first
328332
@ reopened)</li>
329333
@ </ol>
330334
style_sidebox_end();
331335
332
- prepareBranchQuery(&q, showAll, showClosed);
336
+ branch_prepare_query(&q, showAll?1:(showClosed?-1:0));
333337
cnt = 0;
334338
while( db_step(&q)==SQLITE_ROW ){
335339
const char *zBr = db_column_text(&q, 0);
336340
if( cnt==0 ){
337341
if( colorTest ){
338342
--- src/branch.c
+++ src/branch.c
@@ -178,14 +178,18 @@
178 /* Do an autosync push, if requested */
179 if( !isPrivate ) autosync(AUTOSYNC_PUSH);
180 }
181
182 /*
183 ** Prepare a query that will list all branches.
 
 
 
 
184 */
185 void prepareBranchQuery(Stmt *pQuery, int showAll, int showClosed){
186 if( showClosed ){
187 db_prepare(pQuery,
188 "SELECT value FROM tagxref"
189 " WHERE tagid=%d AND value NOT NULL "
190 "EXCEPT "
191 "SELECT value FROM tagxref"
@@ -193,11 +197,11 @@
193 " AND rid IN leaf"
194 " AND NOT %z"
195 " ORDER BY value COLLATE nocase /*sort*/",
196 TAG_BRANCH, TAG_BRANCH, leaf_is_closed_sql("tagxref.rid")
197 );
198 }else if( showAll ){
199 db_prepare(pQuery,
200 "SELECT DISTINCT value FROM tagxref"
201 " WHERE tagid=%d AND value NOT NULL"
202 " AND rid IN leaf"
203 " ORDER BY value COLLATE nocase /*sort*/",
@@ -260,11 +264,11 @@
260 if( g.localOpen ){
261 vid = db_lget_int("checkout", 0);
262 zCurrent = db_text(0, "SELECT value FROM tagxref"
263 " WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
264 }
265 prepareBranchQuery(&q, showAll, showClosed);
266 while( db_step(&q)==SQLITE_ROW ){
267 const char *zBr = db_column_text(&q, 0);
268 int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
269 fossil_print("%s%s\n", (isCur ? "* " : " "), zBr);
270 }
@@ -327,11 +331,11 @@
327 @ Closed branches are fixed and do not change (unless they are first
328 @ reopened)</li>
329 @ </ol>
330 style_sidebox_end();
331
332 prepareBranchQuery(&q, showAll, showClosed);
333 cnt = 0;
334 while( db_step(&q)==SQLITE_ROW ){
335 const char *zBr = db_column_text(&q, 0);
336 if( cnt==0 ){
337 if( colorTest ){
338
--- src/branch.c
+++ src/branch.c
@@ -178,14 +178,18 @@
178 /* Do an autosync push, if requested */
179 if( !isPrivate ) autosync(AUTOSYNC_PUSH);
180 }
181
182 /*
183 ** Prepare a query that will list branches.
184 **
185 ** If (which<0) then the query pulls only closed branches. If
186 ** (which>0) then the query pulls all (closed and opened)
187 ** branches. Else the query pulls currently-opened branches.
188 */
189 void branch_prepare_query(Stmt *pQuery, int which ){
190 if( which < 0 ){
191 db_prepare(pQuery,
192 "SELECT value FROM tagxref"
193 " WHERE tagid=%d AND value NOT NULL "
194 "EXCEPT "
195 "SELECT value FROM tagxref"
@@ -193,11 +197,11 @@
197 " AND rid IN leaf"
198 " AND NOT %z"
199 " ORDER BY value COLLATE nocase /*sort*/",
200 TAG_BRANCH, TAG_BRANCH, leaf_is_closed_sql("tagxref.rid")
201 );
202 }else if( which>0 ){
203 db_prepare(pQuery,
204 "SELECT DISTINCT value FROM tagxref"
205 " WHERE tagid=%d AND value NOT NULL"
206 " AND rid IN leaf"
207 " ORDER BY value COLLATE nocase /*sort*/",
@@ -260,11 +264,11 @@
264 if( g.localOpen ){
265 vid = db_lget_int("checkout", 0);
266 zCurrent = db_text(0, "SELECT value FROM tagxref"
267 " WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH);
268 }
269 branch_prepare_query(&q, showAll?1:(showClosed?-1:0));
270 while( db_step(&q)==SQLITE_ROW ){
271 const char *zBr = db_column_text(&q, 0);
272 int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
273 fossil_print("%s%s\n", (isCur ? "* " : " "), zBr);
274 }
@@ -327,11 +331,11 @@
331 @ Closed branches are fixed and do not change (unless they are first
332 @ reopened)</li>
333 @ </ol>
334 style_sidebox_end();
335
336 branch_prepare_query(&q, showAll?1:(showClosed?-1:0));
337 cnt = 0;
338 while( db_step(&q)==SQLITE_ROW ){
339 const char *zBr = db_column_text(&q, 0);
340 if( cnt==0 ){
341 if( colorTest ){
342
+7 -3
--- src/json.c
+++ src/json.c
@@ -1559,24 +1559,28 @@
15591559
return json_page_dispatch_helper(depth,&JsonPageDefs_Branch[0]);
15601560
}
15611561
15621562
/*
15631563
** Impl for /json/branch/list
1564
+**
1565
+** TODO: change how the "range" of branches is specified.
1566
+** Take a string arg in the form ("open","all","closed")
1567
+** and decide based off of that.
15641568
*/
15651569
static cson_value * json_branch_list(unsigned int depth){
15661570
cson_value * payV = cson_value_new_object();
15671571
cson_object * pay = cson_value_get_object(payV);
15681572
cson_value * listV = cson_value_new_array();
15691573
cson_array * list = cson_value_get_array(listV);
1570
- int showAll = json_getenv_int("showAll",0);
1571
- int showClosed = showAll ? 0 : json_getenv_int("showClosed",0);
1574
+ int showAll = json_getenv_int("all",0);
1575
+ int showClosed = showAll ? 0 : json_getenv_int("closed",0);
15721576
Stmt q;
15731577
char const * range = showAll
15741578
? "all"
15751579
: (showClosed?"closed":"open");
15761580
cson_object_set(pay,"range",cson_value_new_string(range,strlen(range)));
1577
- prepareBranchQuery(&q, showAll, showClosed);
1581
+ branch_prepare_query(&q, showAll?1:(showClosed?-1:0));
15781582
cson_object_set(pay,"branches",listV);
15791583
while((SQLITE_ROW==db_step(&q))){
15801584
cson_value * v = cson_sqlite3_column_to_value(q.pStmt,0);
15811585
if(v){
15821586
cson_array_append(list,v);
15831587
--- src/json.c
+++ src/json.c
@@ -1559,24 +1559,28 @@
1559 return json_page_dispatch_helper(depth,&JsonPageDefs_Branch[0]);
1560 }
1561
1562 /*
1563 ** Impl for /json/branch/list
 
 
 
 
1564 */
1565 static cson_value * json_branch_list(unsigned int depth){
1566 cson_value * payV = cson_value_new_object();
1567 cson_object * pay = cson_value_get_object(payV);
1568 cson_value * listV = cson_value_new_array();
1569 cson_array * list = cson_value_get_array(listV);
1570 int showAll = json_getenv_int("showAll",0);
1571 int showClosed = showAll ? 0 : json_getenv_int("showClosed",0);
1572 Stmt q;
1573 char const * range = showAll
1574 ? "all"
1575 : (showClosed?"closed":"open");
1576 cson_object_set(pay,"range",cson_value_new_string(range,strlen(range)));
1577 prepareBranchQuery(&q, showAll, showClosed);
1578 cson_object_set(pay,"branches",listV);
1579 while((SQLITE_ROW==db_step(&q))){
1580 cson_value * v = cson_sqlite3_column_to_value(q.pStmt,0);
1581 if(v){
1582 cson_array_append(list,v);
1583
--- src/json.c
+++ src/json.c
@@ -1559,24 +1559,28 @@
1559 return json_page_dispatch_helper(depth,&JsonPageDefs_Branch[0]);
1560 }
1561
1562 /*
1563 ** Impl for /json/branch/list
1564 **
1565 ** TODO: change how the "range" of branches is specified.
1566 ** Take a string arg in the form ("open","all","closed")
1567 ** and decide based off of that.
1568 */
1569 static cson_value * json_branch_list(unsigned int depth){
1570 cson_value * payV = cson_value_new_object();
1571 cson_object * pay = cson_value_get_object(payV);
1572 cson_value * listV = cson_value_new_array();
1573 cson_array * list = cson_value_get_array(listV);
1574 int showAll = json_getenv_int("all",0);
1575 int showClosed = showAll ? 0 : json_getenv_int("closed",0);
1576 Stmt q;
1577 char const * range = showAll
1578 ? "all"
1579 : (showClosed?"closed":"open");
1580 cson_object_set(pay,"range",cson_value_new_string(range,strlen(range)));
1581 branch_prepare_query(&q, showAll?1:(showClosed?-1:0));
1582 cson_object_set(pay,"branches",listV);
1583 while((SQLITE_ROW==db_step(&q))){
1584 cson_value * v = cson_sqlite3_column_to_value(q.pStmt,0);
1585 if(v){
1586 cson_array_append(list,v);
1587

Keyboard Shortcuts

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