Fossil

Check-in [0dc3e7a0d5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:refactored db_generic_query_view() to use sqlite3 API directly so that it can treat SQL errors as non-fatal.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 0dc3e7a0d5a1d9886d4880e715916f38b6d6e70f
User & Date: stephan 2008-02-06 19:37:08.000
Context
2008-02-07
05:31
added missing #include check-in: 58ee4e6e16 user: stephan tags: trunk
2008-02-06
19:37
refactored db_generic_query_view() to use sqlite3 API directly so that it can treat SQL errors as non-fatal. check-in: 0dc3e7a0d5 user: stephan tags: trunk
19:02
added tokenize_path.c, containing a convenient path-like-string tokenizer and render_linked_path() to HTML-ize a path. check-in: 7f9226a858 user: stephan tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/db.c.
1137
1138
1139
1140
1141
1142
1143


1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167




1168
1169
1170
1171


1172
1173
1174

1175
1176
1177
1178
1179

1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192

1193
1194
1195

1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207


1208
* colnames does. Each index corresponds directly to an entry in
* colnames and the SQL results.  Any given entry may be 0. If it has
* fewer, undefined behaviour results.  If a column has an entry in
* xform, then the xform function will be called to transform the
* column data before rendering it. This function takes care of freeing
* the strings created by the xform functions.
*


* Example:
*
*  char const * const colnames[] = {
*   "Tag ID", "Tag Name", "Something Else", "UUID"
*  };
*  string_unary_xform_f xf[] = {
*    strxform_link_to_tagid,
*    strxform_link_to_tagname,
*    0,
*    strxform_link_to_uuid
*  };
*  db_generic_query_view( "select a,b,c,d from foo", colnames, xf );
*
*/
void db_generic_query_view(
  char const * sql,
  char const * const * coln,
  string_unary_xform_f const * xform )
{
  
  Stmt st;
  int i = 0;
  int rc = db_prepare( &st, sql );
  /**




    Achtung: makeheaders apparently can't pull the function
    name from this:
   if( SQLITE_OK != db_prepare( &st, sql ) )
  */


  if( SQLITE_OK != rc )
  {
    @ db_generic_query_view(): Error processing SQL: [%s(sql)]

    return;
  }
  int colc = db_column_count(&st);
  @ <table class='fossil_db_generic_query_view'><tbody>
  @ <tr class='header'>

  for( i = 0; i < colc; ++i ) {
    if( coln )
    {
      @ <th>%s(coln[i] ? coln[i] : db_column_name(&st,i))</th>
    }
    else
    {
      @ <td>%s(db_column_name(&st,i))</td>
    }
  }
  @ </tr>

  int row = 0;

  while( SQLITE_ROW == db_step(&st) ){
    @ <tr class='%s( (row++%2) ? "odd" : "even")'>
      for( i = 0; i < colc; ++i ) {

        char * xf = 0;
        char const * xcf = 0;
        xcf = (xform && xform[i])
          ? (xf=(xform[i])(db_column_text(&st,i)))
          : db_column_text(&st,i);
        @ <td>%s(xcf)</td>
        if( xf ) free( xf );
      }
    @ </tr>
  }
  db_finalize( &st );
  @ </tbody></table>


}







>
>














|




<
<
<
<

>
>
>
>
|
<
|
|
>
>


|
>
|

|


>



|



|





>
|


>



|
|





<

>
>

1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164




1165
1166
1167
1168
1169
1170

1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212

1213
1214
1215
1216
* colnames does. Each index corresponds directly to an entry in
* colnames and the SQL results.  Any given entry may be 0. If it has
* fewer, undefined behaviour results.  If a column has an entry in
* xform, then the xform function will be called to transform the
* column data before rendering it. This function takes care of freeing
* the strings created by the xform functions.
*
* Returns SQLITE_OK on success and any other value on error.
*
* Example:
*
*  char const * const colnames[] = {
*   "Tag ID", "Tag Name", "Something Else", "UUID"
*  };
*  string_unary_xform_f xf[] = {
*    strxform_link_to_tagid,
*    strxform_link_to_tagname,
*    0,
*    strxform_link_to_uuid
*  };
*  db_generic_query_view( "select a,b,c,d from foo", colnames, xf );
*
*/
int db_generic_query_view(
  char const * sql,
  char const * const * coln,
  string_unary_xform_f const * xform )
{




  /**
     Reminder: we use sqlite3_stmt directly, instead
     of using db_prepare(), so that we can treat SQL
     errors as non-fatal. We are executing arbitrary
     SQL here, some of it entered via the browser,
     and we don't want to kill the app when some of

     the SQL isn't quite right.
  */  
  sqlite3_stmt * st;
  int rc = sqlite3_prepare(g.db, sql, -1, &st, 0);
  if( SQLITE_OK != rc )
  {
    @ <span style='color:red'>db_generic_query_view() SQL error:
    @ %h(sqlite3_errmsg(g.db))</span>
    return rc;
  }
  int colc = sqlite3_column_count(st);
  @ <table class='fossil_db_generic_query_view'><tbody>
  @ <tr class='header'>
  int i = 0;
  for( i = 0; i < colc; ++i ) {
    if( coln )
    {
      @ <th>%s(coln[i] ? coln[i] : sqlite3_column_name(st,i))</th>
    }
    else
    {
      @ <td>%s(sqlite3_column_name(st,i))</td>
    }
  }
  @ </tr>

  int row = 0;
  char const * text = 0;
  while( SQLITE_ROW == sqlite3_step(st) ){
    @ <tr class='%s( (row++%2) ? "odd" : "even")'>
      for( i = 0; i < colc; ++i ) {
        text = (char const *) sqlite3_column_text(st,i);
        char * xf = 0;
        char const * xcf = 0;
        xcf = (xform && xform[i])
          ? (xf=(xform[i])(text))
          : text;
        @ <td>%s(xcf)</td>
        if( xf ) free( xf );
      }
    @ </tr>
  }

  @ </tbody></table>
  sqlite3_finalize(st);
  return SQLITE_OK;
}