| | @@ -34,11 +34,11 @@ |
| 34 | 34 | */ |
| 35 | 35 | static const JsonPageDef JsonPageDefs_Report[] = { |
| 36 | 36 | {"create", json_report_create, 0}, |
| 37 | 37 | {"get", json_report_get, 0}, |
| 38 | 38 | {"list", json_report_list, 0}, |
| 39 | | -{"run", json_report_run, 1}, |
| 39 | +{"run", json_report_run, 0}, |
| 40 | 40 | {"save", json_report_save, 0}, |
| 41 | 41 | /* Last entry MUST have a NULL name. */ |
| 42 | 42 | {NULL,NULL,0} |
| 43 | 43 | }; |
| 44 | 44 | /* |
| | @@ -66,29 +66,141 @@ |
| 66 | 66 | |
| 67 | 67 | static cson_value * json_report_list(){ |
| 68 | 68 | Blob sql = empty_blob; |
| 69 | 69 | cson_value * pay = NULL; |
| 70 | 70 | blob_append(&sql, "SELECT" |
| 71 | | - " rn number," |
| 71 | + " rn AS report," |
| 72 | 72 | " title as title," |
| 73 | 73 | " owner as owner" |
| 74 | | - " FROM reportfmt", -1); |
| 74 | + " FROM reportfmt" |
| 75 | + " WHERE 1", -1); |
| 76 | + /* This logic was adapted from the HTML mode report list |
| 77 | + page. However, for reasons i cannot explain, enbling this block |
| 78 | + causes a crash in CGI mode when request is made via a user |
| 79 | + without TktFmt. |
| 80 | + */ |
| 75 | 81 | if(!g.perm.TktFmt){ |
| 76 | 82 | blob_append(&sql, |
| 77 | 83 | " AND title NOT LIKE '_%'", -1); |
| 78 | 84 | } |
| 79 | 85 | blob_append(&sql," ORDER BY title",-1); |
| 80 | | - pay = json_sql_to_array_of_obj(&sql, 1); |
| 86 | + pay = json_sql_to_array_of_obj(&sql, NULL, 1); |
| 81 | 87 | if(!pay){ |
| 82 | 88 | json_set_err(FSL_JSON_E_UNKNOWN, |
| 83 | 89 | "Quite unexpected: no ticket reports found."); |
| 84 | 90 | } |
| 85 | 91 | return pay; |
| 86 | 92 | } |
| 87 | 93 | |
| 94 | +/* |
| 95 | +** Impl for /json/report/run |
| 96 | +** |
| 97 | +** Options/arguments: |
| 98 | +** |
| 99 | +** report=int (CLI: -report # or -r #) is the report number to run. |
| 100 | +** |
| 101 | +** limit=int (CLI: -limit # or -n #) -n is for compat. with other commands. |
| 102 | +** |
| 103 | +** format=a|o Specifies result format: a=each row is an arry, o=each |
| 104 | +** row is an object. Default=o. |
| 105 | +*/ |
| 88 | 106 | static cson_value * json_report_run(){ |
| 89 | | - return NULL; |
| 107 | + int nReport; |
| 108 | + Stmt q = empty_Stmt; |
| 109 | + cson_object * pay = NULL; |
| 110 | + cson_array * tktList = NULL; |
| 111 | + char const * zFmt; |
| 112 | + char * zTitle = NULL; |
| 113 | + Blob sql = empty_blob; |
| 114 | + int limit = 0; |
| 115 | + cson_value * colNames = NULL; |
| 116 | + int i; |
| 117 | + |
| 118 | + nReport = json_find_option_int("report",NULL,"r",-1); |
| 119 | + if( nReport <= 0 ){ |
| 120 | + char const * arg = json_command_arg(3); |
| 121 | + if(arg && fossil_isdigit(*arg)) { |
| 122 | + nReport = atoi(arg); |
| 123 | + } |
| 124 | + } |
| 125 | + if(nReport <=0){ |
| 126 | + json_set_err(FSL_JSON_E_MISSING_ARGS, |
| 127 | + "Missing or invalid 'number' (-n) parameter."); |
| 128 | + goto error; |
| 129 | + } |
| 130 | + zFmt = json_find_option_cstr2("format",NULL,"f",3); |
| 131 | + if(!zFmt) zFmt = "o"; |
| 132 | + db_prepare(&q, |
| 133 | + "SELECT sqlcode, " |
| 134 | + " title" |
| 135 | + " FROM reportfmt" |
| 136 | + " WHERE rn=%d", |
| 137 | + nReport); |
| 138 | + if(SQLITE_ROW != db_step(&q)){ |
| 139 | + json_set_err(FSL_JSON_E_INVALID_ARGS, |
| 140 | + "Report number %d not found.", |
| 141 | + nReport); |
| 142 | + db_finalize(&q); |
| 143 | + goto error; |
| 144 | + } |
| 145 | + |
| 146 | + limit = json_find_option_int("limit",NULL,"n",-1); |
| 147 | + |
| 148 | + |
| 149 | + /* Copy over report's SQL...*/ |
| 150 | + blob_append(&sql, db_column_text(&q,0), -1); |
| 151 | + zTitle = mprintf("%s", db_column_text(&q,1)); |
| 152 | + db_finalize(&q); |
| 153 | + db_prepare(&q, "%s", blob_str(&sql)); |
| 154 | + |
| 155 | + /** Build the response... */ |
| 156 | + pay = cson_new_object(); |
| 157 | + |
| 158 | + cson_object_set(pay, "report", json_new_int(nReport)); |
| 159 | + cson_object_set(pay, "title", json_new_string(zTitle)); |
| 160 | + if(limit>0){ |
| 161 | + cson_object_set(pay, "limit", json_new_int((limit<0) ? 0 : limit)); |
| 162 | + } |
| 163 | + free(zTitle); |
| 164 | + zTitle = NULL; |
| 165 | + |
| 166 | + if(g.perm.WrTkt){ |
| 167 | + cson_object_set(pay, "sqlcode", |
| 168 | + cson_value_new_string(blob_str(&sql), |
| 169 | + (unsigned int)blob_size(&sql))); |
| 170 | + } |
| 171 | + blob_reset(&sql); |
| 172 | + |
| 173 | + colNames = cson_sqlite3_column_names(q.pStmt); |
| 174 | + cson_object_set( pay, "columnNames", colNames); |
| 175 | + for( i = 0 ; ((limit>0) ?(i < limit) : 1) |
| 176 | + && (SQLITE_ROW == db_step(&q)); |
| 177 | + ++i){ |
| 178 | + cson_value * row = ('a'==*zFmt) |
| 179 | + ? cson_sqlite3_row_to_array(q.pStmt) |
| 180 | + : cson_sqlite3_row_to_object2(q.pStmt, |
| 181 | + cson_value_get_array(colNames)); |
| 182 | + ; |
| 183 | + if(row && !tktList){ |
| 184 | + tktList = cson_new_array(); |
| 185 | + } |
| 186 | + cson_array_append(tktList, row); |
| 187 | + } |
| 188 | + db_finalize(&q); |
| 189 | + cson_object_set(pay, "tickets", |
| 190 | + tktList ? cson_array_value(tktList) : cson_value_null()); |
| 191 | + |
| 192 | + goto end; |
| 193 | + |
| 194 | + error: |
| 195 | + assert(0 != g.json.resultCode); |
| 196 | + cson_value_free( cson_object_value(pay) ); |
| 197 | + pay = NULL; |
| 198 | + end: |
| 199 | + |
| 200 | + return pay ? cson_object_value(pay) : NULL; |
| 201 | + |
| 90 | 202 | } |
| 91 | 203 | |
| 92 | 204 | static cson_value * json_report_save(){ |
| 93 | 205 | return NULL; |
| 94 | 206 | } |
| 95 | 207 | |