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.
Commit
5a81a5ead600ac8a3ae7e1e1ca696e889bba909d
Parent
f266ebdd3f942b0…
2 files changed
+10
-6
+7
-3
+10
-6
| --- src/branch.c | ||
| +++ src/branch.c | ||
| @@ -178,14 +178,18 @@ | ||
| 178 | 178 | /* Do an autosync push, if requested */ |
| 179 | 179 | if( !isPrivate ) autosync(AUTOSYNC_PUSH); |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | /* |
| 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. | |
| 184 | 188 | */ |
| 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 ){ | |
| 187 | 191 | db_prepare(pQuery, |
| 188 | 192 | "SELECT value FROM tagxref" |
| 189 | 193 | " WHERE tagid=%d AND value NOT NULL " |
| 190 | 194 | "EXCEPT " |
| 191 | 195 | "SELECT value FROM tagxref" |
| @@ -193,11 +197,11 @@ | ||
| 193 | 197 | " AND rid IN leaf" |
| 194 | 198 | " AND NOT %z" |
| 195 | 199 | " ORDER BY value COLLATE nocase /*sort*/", |
| 196 | 200 | TAG_BRANCH, TAG_BRANCH, leaf_is_closed_sql("tagxref.rid") |
| 197 | 201 | ); |
| 198 | - }else if( showAll ){ | |
| 202 | + }else if( which>0 ){ | |
| 199 | 203 | db_prepare(pQuery, |
| 200 | 204 | "SELECT DISTINCT value FROM tagxref" |
| 201 | 205 | " WHERE tagid=%d AND value NOT NULL" |
| 202 | 206 | " AND rid IN leaf" |
| 203 | 207 | " ORDER BY value COLLATE nocase /*sort*/", |
| @@ -260,11 +264,11 @@ | ||
| 260 | 264 | if( g.localOpen ){ |
| 261 | 265 | vid = db_lget_int("checkout", 0); |
| 262 | 266 | zCurrent = db_text(0, "SELECT value FROM tagxref" |
| 263 | 267 | " WHERE rid=%d AND tagid=%d", vid, TAG_BRANCH); |
| 264 | 268 | } |
| 265 | - prepareBranchQuery(&q, showAll, showClosed); | |
| 269 | + branch_prepare_query(&q, showAll?1:(showClosed?-1:0)); | |
| 266 | 270 | while( db_step(&q)==SQLITE_ROW ){ |
| 267 | 271 | const char *zBr = db_column_text(&q, 0); |
| 268 | 272 | int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0; |
| 269 | 273 | fossil_print("%s%s\n", (isCur ? "* " : " "), zBr); |
| 270 | 274 | } |
| @@ -327,11 +331,11 @@ | ||
| 327 | 331 | @ Closed branches are fixed and do not change (unless they are first |
| 328 | 332 | @ reopened)</li> |
| 329 | 333 | @ </ol> |
| 330 | 334 | style_sidebox_end(); |
| 331 | 335 | |
| 332 | - prepareBranchQuery(&q, showAll, showClosed); | |
| 336 | + branch_prepare_query(&q, showAll?1:(showClosed?-1:0)); | |
| 333 | 337 | cnt = 0; |
| 334 | 338 | while( db_step(&q)==SQLITE_ROW ){ |
| 335 | 339 | const char *zBr = db_column_text(&q, 0); |
| 336 | 340 | if( cnt==0 ){ |
| 337 | 341 | if( colorTest ){ |
| 338 | 342 |
| --- 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 @@ | ||
| 1559 | 1559 | return json_page_dispatch_helper(depth,&JsonPageDefs_Branch[0]); |
| 1560 | 1560 | } |
| 1561 | 1561 | |
| 1562 | 1562 | /* |
| 1563 | 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. | |
| 1564 | 1568 | */ |
| 1565 | 1569 | static cson_value * json_branch_list(unsigned int depth){ |
| 1566 | 1570 | cson_value * payV = cson_value_new_object(); |
| 1567 | 1571 | cson_object * pay = cson_value_get_object(payV); |
| 1568 | 1572 | cson_value * listV = cson_value_new_array(); |
| 1569 | 1573 | 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); | |
| 1572 | 1576 | Stmt q; |
| 1573 | 1577 | char const * range = showAll |
| 1574 | 1578 | ? "all" |
| 1575 | 1579 | : (showClosed?"closed":"open"); |
| 1576 | 1580 | 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)); | |
| 1578 | 1582 | cson_object_set(pay,"branches",listV); |
| 1579 | 1583 | while((SQLITE_ROW==db_step(&q))){ |
| 1580 | 1584 | cson_value * v = cson_sqlite3_column_to_value(q.pStmt,0); |
| 1581 | 1585 | if(v){ |
| 1582 | 1586 | cson_array_append(list,v); |
| 1583 | 1587 |
| --- 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 |