/ Check-in [9d73a68c]
Login

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

Overview
Comment:Additional test cases for loadable extensions. (CVS 5247)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 9d73a68c305db673d393db118b6a41241683a670
User & Date: drh 2008-06-19 15:44:00
Context
2008-06-19
16:07
Fix some minor compile problems. (CVS 5248) check-in: 7d38da3e user: drh tags: trunk
15:44
Additional test cases for loadable extensions. (CVS 5247) check-in: 9d73a68c user: drh tags: trunk
15:06
Add recent API additions to the loadable extension interface. (CVS 5246) check-in: 12ba27d9 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/test_loadext.c.

     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** Test extension for testing the sqlite3_load_extension() function.
    13     13   **
    14         -** $Id: test_loadext.c,v 1.1 2006/06/14 10:38:03 danielk1977 Exp $
           14  +** $Id: test_loadext.c,v 1.2 2008/06/19 15:44:00 drh Exp $
    15     15   */
    16         -
           16  +#include <string.h>
    17     17   #include "sqlite3ext.h"
    18     18   SQLITE_EXTENSION_INIT1
    19     19   
    20     20   /*
    21     21   ** The half() SQL function returns half of its input value.
    22     22   */
    23     23   static void halfFunc(
    24     24     sqlite3_context *context,
    25     25     int argc,
    26     26     sqlite3_value **argv
    27     27   ){
    28     28     sqlite3_result_double(context, 0.5*sqlite3_value_double(argv[0]));
    29     29   }
           30  +
           31  +/*
           32  +** SQL functions to call the sqlite3_status function and return results.
           33  +*/
           34  +static void statusFunc(
           35  +  sqlite3_context *context,
           36  +  int argc,
           37  +  sqlite3_value **argv
           38  +){
           39  +  int op, mx, cur, resetFlag, rc;
           40  +  if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){
           41  +    op = sqlite3_value_int(argv[0]);
           42  +  }else if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
           43  +    int i;
           44  +    const char *zName;
           45  +    static const struct {
           46  +      const char *zName;
           47  +      int op;
           48  +    } aOp[] = {
           49  +      { "MEMORY_USED",         SQLITE_STATUS_MEMORY_USED         },
           50  +      { "PAGECACHE_USED",      SQLITE_STATUS_PAGECACHE_USED      },
           51  +      { "PAGECACHE_OVERFLOW",  SQLITE_STATUS_PAGECACHE_OVERFLOW  },
           52  +      { "SCRATCH_USED",        SQLITE_STATUS_SCRATCH_USED        },
           53  +      { "SCRATCH_OVERFLOW",    SQLITE_STATUS_SCRATCH_OVERFLOW    },
           54  +      { "MALLOC_SIZE",         SQLITE_STATUS_MALLOC_SIZE         },
           55  +    };
           56  +    int nOp = sizeof(aOp)/sizeof(aOp[0]);
           57  +    zName = (const char*)sqlite3_value_text(argv[0]);
           58  +    for(i=0; i<nOp; i++){
           59  +      if( strcmp(aOp[i].zName, zName)==0 ){
           60  +        op = aOp[i].op;
           61  +        break;
           62  +      }
           63  +    }
           64  +    if( i>=nOp ){
           65  +      char *zMsg = sqlite3_mprintf("unknown status property: %s", zName);
           66  +      sqlite3_result_error(context, zMsg, -1);
           67  +      sqlite3_free(zMsg);
           68  +      return;
           69  +    }
           70  +  }else{
           71  +    sqlite3_result_error(context, "unknown status type", -1);
           72  +    return;
           73  +  }
           74  +  if( argc==2 ){
           75  +    resetFlag = sqlite3_value_int(argv[1]);
           76  +  }else{
           77  +    resetFlag = 0;
           78  +  }
           79  +  rc = sqlite3_status(op, &cur, &mx, resetFlag);
           80  +  if( rc!=SQLITE_OK ){
           81  +    char *zMsg = sqlite3_mprintf("sqlite3_status(%d,...) returns %d", op, rc);
           82  +    sqlite3_result_error(context, zMsg, -1);
           83  +    sqlite3_free(zMsg);
           84  +    return;
           85  +  } 
           86  +  if( argc==2 ){
           87  +    sqlite3_result_int(context, mx);
           88  +  }else{
           89  +    sqlite3_result_int(context, cur);
           90  +  }
           91  +}
    30     92   
    31     93   /*
    32     94   ** Extension load function.
    33     95   */
    34     96   int testloadext_init(
    35     97     sqlite3 *db, 
    36     98     char **pzErrMsg, 
    37     99     const sqlite3_api_routines *pApi
    38    100   ){
    39    101     SQLITE_EXTENSION_INIT2(pApi);
    40    102     sqlite3_create_function(db, "half", 1, SQLITE_ANY, 0, halfFunc, 0, 0);
          103  +  sqlite3_create_function(db, "sqlite3_status", 1, SQLITE_ANY, 0,
          104  +                          statusFunc, 0, 0);
          105  +  sqlite3_create_function(db, "sqlite3_status", 2, SQLITE_ANY, 0,
          106  +                          statusFunc, 0, 0);
    41    107     return 0;
    42    108   }
    43    109   
    44    110   /*
    45    111   ** Another extension entry point. This one always fails.
    46    112   */
    47    113   int testbrokenext_init(

Changes to test/loadext.test.

     7      7   #    May you find forgiveness for yourself and forgive others.
     8      8   #    May you share freely, never taking more than you give.
     9      9   #
    10     10   #***********************************************************************
    11     11   # This file implements regression tests for SQLite library.  The
    12     12   # focus of this script is extension loading.
    13     13   #
    14         -# $Id: loadext.test,v 1.11 2007/09/01 06:19:06 danielk1977 Exp $
           14  +# $Id: loadext.test,v 1.12 2008/06/19 15:44:00 drh Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   ifcapable !load_ext {
    20     20     finish_test
    21     21     return
................................................................................
   183    183     }
   184    184   } {0 {{}}}
   185    185   do_test loadext-3.4 {
   186    186     catchsql {
   187    187       SELECT half(5);
   188    188     }
   189    189   } {0 2.5}
          190  +do_test loadext-3.5 {
          191  +  db eval {
          192  +    SELECT sqlite3_status('MEMORY_USED') AS mused
          193  +  } break
          194  +  puts -nonewline " (memory_used=$mused) "
          195  +  expr {$mused>0}
          196  +} {1}
          197  +do_test loadext-3.6 {
          198  +  catchsql {
          199  +    SELECT sqlite3_status('MEMORY_USED_X') AS mused
          200  +  }
          201  +} {1 {unknown status property: MEMORY_USED_X}}
          202  +do_test loadext-3.7 {
          203  +  catchsql {
          204  +    SELECT sqlite3_status(4.53) AS mused
          205  +  }
          206  +} {1 {unknown status type}}
          207  +do_test loadext-3.8 {
          208  +  catchsql {
          209  +    SELECT sqlite3_status(23) AS mused
          210  +  }
          211  +} {1 {sqlite3_status(23,...) returns 21}}
   190    212   
   191    213   # Ticket #1863
   192    214   # Make sure the extension loading mechanism will not work unless it
   193    215   # is explicitly enabled.
   194    216   #
   195    217   db close
   196    218   sqlite3 db test.db