Fossil SCM
added db_generic_query_view() to simplify /tabview and /my implementations
Commit
74ba41a5104a814128597cf8facbbb250a0c360b
Parent
2cb3290e6767e51…
1 file changed
+92
M
src/db.c
+92
| --- src/db.c | ||
| +++ src/db.c | ||
| @@ -1111,5 +1111,97 @@ | ||
| 1111 | 1111 | } |
| 1112 | 1112 | }else{ |
| 1113 | 1113 | usage("?PROPERTY? ?VALUE?"); |
| 1114 | 1114 | } |
| 1115 | 1115 | } |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | +/** | |
| 1120 | +* db_generic_query_view(): | |
| 1121 | +* | |
| 1122 | +* A very primitive helper to run an SQL query and table-ize the | |
| 1123 | +* results. | |
| 1124 | +* | |
| 1125 | +* The sql parameter should be a single, complete SQL statement. | |
| 1126 | +* | |
| 1127 | +* The coln parameter is optional (it may be 0). If it is 0 then the | |
| 1128 | +* column names used in the output will be taken directly from the | |
| 1129 | +* SQL. If it is not null then it must have as many entries as the SQL | |
| 1130 | +* result has columns. Each entry is a column name for the SQL result | |
| 1131 | +* column of the same index. Any given entry may be 0, in which case | |
| 1132 | +* the column name from the SQL is used. | |
| 1133 | +* | |
| 1134 | +* The xform argument is an array of transformation functions (type | |
| 1135 | +* string_unary_xform_f). The array, or any single entry, may be 0, but | |
| 1136 | +* if the array is non-0 then it must have at least as many entries as | |
| 1137 | +* colnames does. Each index corresponds directly to an entry in | |
| 1138 | +* colnames and the SQL results. Any given entry may be 0. If it has | |
| 1139 | +* fewer, undefined behaviour results. If a column has an entry in | |
| 1140 | +* xform, then the xform function will be called to transform the | |
| 1141 | +* column data before rendering it. This function takes care of freeing | |
| 1142 | +* the strings created by the xform functions. | |
| 1143 | +* | |
| 1144 | +* Example: | |
| 1145 | +* | |
| 1146 | +* char const * const colnames[] = { | |
| 1147 | +* "Tag ID", "Tag Name", "Something Else", "UUID" | |
| 1148 | +* }; | |
| 1149 | +* string_unary_xform_f xf[] = { | |
| 1150 | +* strxform_link_to_tagid, | |
| 1151 | +* strxform_link_to_tagname, | |
| 1152 | +* 0, | |
| 1153 | +* strxform_link_to_uuid | |
| 1154 | +* }; | |
| 1155 | +* db_generic_query_view( "select a,b,c,d from foo", colnames, xf ); | |
| 1156 | +* | |
| 1157 | +*/ | |
| 1158 | +void db_generic_query_view( | |
| 1159 | + char const * sql, | |
| 1160 | + char const * const * coln, | |
| 1161 | + string_unary_xform_f * xform ) | |
| 1162 | +{ | |
| 1163 | + | |
| 1164 | + Stmt st; | |
| 1165 | + int i = 0; | |
| 1166 | + int rc = db_prepare( &st, sql ); | |
| 1167 | + /** | |
| 1168 | + Achtung: makeheaders apparently can't pull the function | |
| 1169 | + name from this: | |
| 1170 | + if( SQLITE_OK != db_prepare( &st, sql ) ) | |
| 1171 | + */ | |
| 1172 | + if( SQLITE_OK != rc ) | |
| 1173 | + { | |
| 1174 | + @ db_generic_query_view(): Error processing SQL: [%s(sql)] | |
| 1175 | + return; | |
| 1176 | + } | |
| 1177 | + int colc = db_column_count(&st); | |
| 1178 | + @ <table cellpadding='4px' border='1'><tbody> | |
| 1179 | + @ <tr> | |
| 1180 | + for( i = 0; i < colc; ++i ) { | |
| 1181 | + if( coln ) | |
| 1182 | + { | |
| 1183 | + @ <th>%s(coln[i] ? coln[i] : db_column_name(&st,i))</th> | |
| 1184 | + } | |
| 1185 | + else | |
| 1186 | + { | |
| 1187 | + @ <td>%s(db_column_name(&st,i))</td> | |
| 1188 | + } | |
| 1189 | + } | |
| 1190 | + @ </tr> | |
| 1191 | + | |
| 1192 | + while( SQLITE_ROW == db_step(&st) ){ | |
| 1193 | + @ <tr> | |
| 1194 | + for( i = 0; i < colc; ++i ) { | |
| 1195 | + char * xf = 0; | |
| 1196 | + char const * xcf = 0; | |
| 1197 | + xcf = (xform && xform[i]) | |
| 1198 | + ? (xf=(xform[i])(db_column_text(&st,i))) | |
| 1199 | + : db_column_text(&st,i); | |
| 1200 | + @ <td>%s(xcf)</td> | |
| 1201 | + if( xf ) free( xf ); | |
| 1202 | + } | |
| 1203 | + @ </tr> | |
| 1204 | + } | |
| 1205 | + db_finalize( &st ); | |
| 1206 | + @ </tbody></table> | |
| 1207 | +} | |
| 1116 | 1208 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -1111,5 +1111,97 @@ | |
| 1111 | } |
| 1112 | }else{ |
| 1113 | usage("?PROPERTY? ?VALUE?"); |
| 1114 | } |
| 1115 | } |
| 1116 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -1111,5 +1111,97 @@ | |
| 1111 | } |
| 1112 | }else{ |
| 1113 | usage("?PROPERTY? ?VALUE?"); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | |
| 1118 | |
| 1119 | /** |
| 1120 | * db_generic_query_view(): |
| 1121 | * |
| 1122 | * A very primitive helper to run an SQL query and table-ize the |
| 1123 | * results. |
| 1124 | * |
| 1125 | * The sql parameter should be a single, complete SQL statement. |
| 1126 | * |
| 1127 | * The coln parameter is optional (it may be 0). If it is 0 then the |
| 1128 | * column names used in the output will be taken directly from the |
| 1129 | * SQL. If it is not null then it must have as many entries as the SQL |
| 1130 | * result has columns. Each entry is a column name for the SQL result |
| 1131 | * column of the same index. Any given entry may be 0, in which case |
| 1132 | * the column name from the SQL is used. |
| 1133 | * |
| 1134 | * The xform argument is an array of transformation functions (type |
| 1135 | * string_unary_xform_f). The array, or any single entry, may be 0, but |
| 1136 | * if the array is non-0 then it must have at least as many entries as |
| 1137 | * colnames does. Each index corresponds directly to an entry in |
| 1138 | * colnames and the SQL results. Any given entry may be 0. If it has |
| 1139 | * fewer, undefined behaviour results. If a column has an entry in |
| 1140 | * xform, then the xform function will be called to transform the |
| 1141 | * column data before rendering it. This function takes care of freeing |
| 1142 | * the strings created by the xform functions. |
| 1143 | * |
| 1144 | * Example: |
| 1145 | * |
| 1146 | * char const * const colnames[] = { |
| 1147 | * "Tag ID", "Tag Name", "Something Else", "UUID" |
| 1148 | * }; |
| 1149 | * string_unary_xform_f xf[] = { |
| 1150 | * strxform_link_to_tagid, |
| 1151 | * strxform_link_to_tagname, |
| 1152 | * 0, |
| 1153 | * strxform_link_to_uuid |
| 1154 | * }; |
| 1155 | * db_generic_query_view( "select a,b,c,d from foo", colnames, xf ); |
| 1156 | * |
| 1157 | */ |
| 1158 | void db_generic_query_view( |
| 1159 | char const * sql, |
| 1160 | char const * const * coln, |
| 1161 | string_unary_xform_f * xform ) |
| 1162 | { |
| 1163 | |
| 1164 | Stmt st; |
| 1165 | int i = 0; |
| 1166 | int rc = db_prepare( &st, sql ); |
| 1167 | /** |
| 1168 | Achtung: makeheaders apparently can't pull the function |
| 1169 | name from this: |
| 1170 | if( SQLITE_OK != db_prepare( &st, sql ) ) |
| 1171 | */ |
| 1172 | if( SQLITE_OK != rc ) |
| 1173 | { |
| 1174 | @ db_generic_query_view(): Error processing SQL: [%s(sql)] |
| 1175 | return; |
| 1176 | } |
| 1177 | int colc = db_column_count(&st); |
| 1178 | @ <table cellpadding='4px' border='1'><tbody> |
| 1179 | @ <tr> |
| 1180 | for( i = 0; i < colc; ++i ) { |
| 1181 | if( coln ) |
| 1182 | { |
| 1183 | @ <th>%s(coln[i] ? coln[i] : db_column_name(&st,i))</th> |
| 1184 | } |
| 1185 | else |
| 1186 | { |
| 1187 | @ <td>%s(db_column_name(&st,i))</td> |
| 1188 | } |
| 1189 | } |
| 1190 | @ </tr> |
| 1191 | |
| 1192 | while( SQLITE_ROW == db_step(&st) ){ |
| 1193 | @ <tr> |
| 1194 | for( i = 0; i < colc; ++i ) { |
| 1195 | char * xf = 0; |
| 1196 | char const * xcf = 0; |
| 1197 | xcf = (xform && xform[i]) |
| 1198 | ? (xf=(xform[i])(db_column_text(&st,i))) |
| 1199 | : db_column_text(&st,i); |
| 1200 | @ <td>%s(xcf)</td> |
| 1201 | if( xf ) free( xf ); |
| 1202 | } |
| 1203 | @ </tr> |
| 1204 | } |
| 1205 | db_finalize( &st ); |
| 1206 | @ </tbody></table> |
| 1207 | } |
| 1208 |