/ Check-in [36e03162]
Login

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

Overview
Comment:Remove dataType and includeTypes flags from function definitions. Added new P3_FUNCDEF type for P3 arguments on opcodes. Fixes to several user functions. 28 tests fail now. (CVS 1464)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 36e031625995b2f7baf7654d771ca8fb764a0085
User & Date: drh 2004-05-26 16:54:42
Context
2004-05-26
23:25
Refactoring of the vdbe Mem functions and the APIs that deal with them. The code will not compile in its current state. (CVS 1465) check-in: bba6684d user: drh tags: trunk
16:54
Remove dataType and includeTypes flags from function definitions. Added new P3_FUNCDEF type for P3 arguments on opcodes. Fixes to several user functions. 28 tests fail now. (CVS 1464) check-in: 36e03162 user: drh tags: trunk
13:27
Ensure the type of an sqlite3_value* is not modified by calls to sqlite3_value_*() calls. (CVS 1463) check-in: ce8b1520 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/date.c.

    12     12   ** This file contains the C functions that implement date and time
    13     13   ** functions for SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: date.c,v 1.24 2004/05/26 06:18:37 danielk1977 Exp $
           19  +** $Id: date.c,v 1.25 2004/05/26 16:54:42 drh Exp $
    20     20   **
    21     21   ** NOTES:
    22     22   **
    23     23   ** SQLite processes all times and dates as Julian Day numbers.  The
    24     24   ** dates and times are stored as the number of days since noon
    25     25   ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
    26     26   ** calendar system.
................................................................................
   660    660   */
   661    661   
   662    662   /*
   663    663   **    julianday( TIMESTRING, MOD, MOD, ...)
   664    664   **
   665    665   ** Return the julian day number of the date specified in the arguments
   666    666   */
   667         -static void juliandayFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          667  +static void juliandayFunc(
          668  +  sqlite3_context *context,
          669  +  int argc,
          670  +  sqlite3_value **argv
          671  +){
   668    672     DateTime x;
   669    673     if( isDate(argc, argv, &x)==0 ){
   670    674       computeJD(&x);
   671    675       sqlite3_result_double(context, x.rJD);
   672    676     }
   673    677   }
   674    678   
   675    679   /*
   676    680   **    datetime( TIMESTRING, MOD, MOD, ...)
   677    681   **
   678    682   ** Return YYYY-MM-DD HH:MM:SS
   679    683   */
   680         -static void datetimeFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          684  +static void datetimeFunc(
          685  +  sqlite3_context *context,
          686  +  int argc,
          687  +  sqlite3_value **argv
          688  +){
   681    689     DateTime x;
   682    690     if( isDate(argc, argv, &x)==0 ){
   683    691       char zBuf[100];
   684    692       computeYMD_HMS(&x);
   685    693       sprintf(zBuf, "%04d-%02d-%02d %02d:%02d:%02d",x.Y, x.M, x.D, x.h, x.m,
   686    694              (int)(x.s));
   687    695       sqlite3_result_text(context, zBuf, -1, 1);
................................................................................
   689    697   }
   690    698   
   691    699   /*
   692    700   **    time( TIMESTRING, MOD, MOD, ...)
   693    701   **
   694    702   ** Return HH:MM:SS
   695    703   */
   696         -static void timeFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          704  +static void timeFunc(
          705  +  sqlite3_context *context,
          706  +  int argc,
          707  +  sqlite3_value **argv
          708  +){
   697    709     DateTime x;
   698    710     if( isDate(argc, argv, &x)==0 ){
   699    711       char zBuf[100];
   700    712       computeHMS(&x);
   701    713       sprintf(zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
   702    714       sqlite3_result_text(context, zBuf, -1, 1);
   703    715     }
................................................................................
   704    716   }
   705    717   
   706    718   /*
   707    719   **    date( TIMESTRING, MOD, MOD, ...)
   708    720   **
   709    721   ** Return YYYY-MM-DD
   710    722   */
   711         -static void dateFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          723  +static void dateFunc(
          724  +  sqlite3_context *context,
          725  +  int argc,
          726  +  sqlite3_value **argv
          727  +){
   712    728     DateTime x;
   713    729     if( isDate(argc, argv, &x)==0 ){
   714    730       char zBuf[100];
   715    731       computeYMD(&x);
   716    732       sprintf(zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
   717    733       sqlite3_result_text(context, zBuf, -1, 1);
   718    734     }
................................................................................
   733    749   **   %s  seconds since 1970-01-01
   734    750   **   %S  seconds 00-59
   735    751   **   %w  day of week 0-6  sunday==0
   736    752   **   %W  week of year 00-53
   737    753   **   %Y  year 0000-9999
   738    754   **   %%  %
   739    755   */
   740         -static void strftimeFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          756  +static void strftimeFunc(
          757  +  sqlite3_context *context,
          758  +  int argc,
          759  +  sqlite3_value **argv
          760  +){
   741    761     DateTime x;
   742    762     int n, i, j;
   743    763     char *z;
   744    764     const char *zFmt = sqlite3_value_data(argv[0]);
   745    765     char zBuf[100];
   746    766     if( zFmt==0 || isDate(argc-1, argv+1, &x) ) return;
   747    767     for(i=0, n=1; zFmt[i]; i++, n++){
................................................................................
   848    868   ** functions.  This should be the only routine in this file with
   849    869   ** external linkage.
   850    870   */
   851    871   void sqlite3RegisterDateTimeFunctions(sqlite *db){
   852    872     static struct {
   853    873        char *zName;
   854    874        int nArg;
   855         -     int dataType;
   856    875        void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
   857    876     } aFuncs[] = {
   858    877   #ifndef SQLITE_OMIT_DATETIME_FUNCS
   859         -    { "julianday", -1, SQLITE_NUMERIC, juliandayFunc   },
   860         -    { "date",      -1, SQLITE_TEXT,    dateFunc        },
   861         -    { "time",      -1, SQLITE_TEXT,    timeFunc        },
   862         -    { "datetime",  -1, SQLITE_TEXT,    datetimeFunc    },
   863         -    { "strftime",  -1, SQLITE_TEXT,    strftimeFunc    },
          878  +    { "julianday", -1, juliandayFunc   },
          879  +    { "date",      -1, dateFunc        },
          880  +    { "time",      -1, timeFunc        },
          881  +    { "datetime",  -1, datetimeFunc    },
          882  +    { "strftime",  -1, strftimeFunc    },
   864    883   #endif
   865    884     };
   866    885     int i;
   867    886   
   868    887     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   869    888       sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 0, 0, 0,
   870    889           aFuncs[i].xFunc, 0, 0);
   871         -    if( aFuncs[i].xFunc ){
   872         -      sqlite3_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
   873         -    }
   874    890     }
   875    891   }
   876         -
   877         -
   878         -

Changes to src/expr.c.

     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   ** This file contains routines used for analyzing expressions and
    13     13   ** for generating VDBE code that evaluates expressions in SQLite.
    14     14   **
    15         -** $Id: expr.c,v 1.127 2004/05/21 13:39:51 drh Exp $
           15  +** $Id: expr.c,v 1.128 2004/05/26 16:54:43 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   #include <ctype.h>
    19     19   
    20     20   char const *sqlite3AffinityString(char affinity){
    21     21     switch( affinity ){
    22     22       case SQLITE_AFF_INTEGER: return "i";
................................................................................
   499    499   ** are made to pExpr:
   500    500   **
   501    501   **    pExpr->iDb           Set the index in db->aDb[] of the database holding
   502    502   **                         the table.
   503    503   **    pExpr->iTable        Set to the cursor number for the table obtained
   504    504   **                         from pSrcList.
   505    505   **    pExpr->iColumn       Set to the column number within the table.
   506         -**    pExpr->dataType      Set to the appropriate data type for the column.
   507    506   **    pExpr->op            Set to TK_COLUMN.
   508    507   **    pExpr->pLeft         Any expression this points to is deleted
   509    508   **    pExpr->pRight        Any expression this points to is deleted.
   510    509   **
   511    510   ** The pDbToken is the name of the database (the "X").  This value may be
   512    511   ** NULL meaning that name is of the form Y.Z or Z.  Any available database
   513    512   ** can be used.  The pTableToken is the name of the table (the "Y").  This
................................................................................
  1220   1219         int nExpr = pList ? pList->nExpr : 0;
  1221   1220         FuncDef *pDef;
  1222   1221         int nId;
  1223   1222         const char *zId;
  1224   1223         getFunctionName(pExpr, &zId, &nId);
  1225   1224         pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0);
  1226   1225         assert( pDef!=0 );
  1227         -      nExpr = sqlite3ExprCodeExprList(pParse, pList, pDef->includeTypes);
         1226  +      nExpr = sqlite3ExprCodeExprList(pParse, pList);
  1228   1227         /* FIX ME: The following is a temporary hack. */
  1229   1228         if( 0==sqlite3StrNICmp(zId, "classof", nId) ){
  1230   1229           assert( nExpr==1 );
  1231   1230           sqlite3VdbeAddOp(v, OP_Class, nExpr, 0);
  1232   1231         }else{
  1233         -        sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_POINTER);
         1232  +        sqlite3VdbeOp3(v, OP_Function, nExpr, 0, (char*)pDef, P3_FUNCDEF);
  1234   1233         }
  1235   1234         break;
  1236   1235       }
  1237   1236       case TK_SELECT: {
  1238   1237         sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
  1239   1238         break;
  1240   1239       }
................................................................................
  1342   1341       }
  1343   1342       break;
  1344   1343     }
  1345   1344   }
  1346   1345   
  1347   1346   /*
  1348   1347   ** Generate code that pushes the value of every element of the given
  1349         -** expression list onto the stack.  If the includeTypes flag is true,
  1350         -** then also push a string that is the datatype of each element onto
  1351         -** the stack after the value.
         1348  +** expression list onto the stack.
  1352   1349   **
  1353   1350   ** Return the number of elements pushed onto the stack.
  1354   1351   */
  1355   1352   int sqlite3ExprCodeExprList(
  1356   1353     Parse *pParse,     /* Parsing context */
  1357         -  ExprList *pList,   /* The expression list to be coded */
  1358         -  int includeTypes   /* TRUE to put datatypes on the stack too */
         1354  +  ExprList *pList    /* The expression list to be coded */
  1359   1355   ){
  1360   1356     struct ExprList_item *pItem;
  1361   1357     int i, n;
  1362   1358     Vdbe *v;
  1363   1359     if( pList==0 ) return 0;
  1364   1360     v = sqlite3GetVdbe(pParse);
  1365   1361     n = pList->nExpr;
  1366   1362     for(pItem=pList->a, i=0; i<n; i++, pItem++){
  1367   1363       sqlite3ExprCode(pParse, pItem->pExpr);
  1368         -    if( includeTypes ){
  1369         -      /** DEPRECATED.  This will go away with the new function interface **/
  1370         -      sqlite3VdbeOp3(v, OP_String, 0, 0, "numeric", P3_STATIC);
  1371         -    }
  1372   1364     }
  1373         -  return includeTypes ? n*2 : n;
         1365  +  return n;
  1374   1366   }
  1375   1367   
  1376   1368   /*
  1377   1369   ** Generate code for a boolean expression such that a jump is made
  1378   1370   ** to the label "dest" if the expression is true but execution
  1379   1371   ** continues straight thru if the expression is false.
  1380   1372   **
................................................................................
  1710   1702     if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
  1711   1703       return 0;
  1712   1704     }
  1713   1705     if( p==0 && pMaybe ){
  1714   1706       assert( createFlag==0 );
  1715   1707       return pMaybe;
  1716   1708     }
  1717         -  if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)))!=0 ){
         1709  +  if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)+nName+1))!=0 ){
  1718   1710       p->nArg = nArg;
  1719   1711       p->pNext = pFirst;
  1720         -    p->dataType = pFirst ? pFirst->dataType : SQLITE_NUMERIC;
  1721         -    sqlite3HashInsert(&db->aFunc, zName, nName, (void*)p);
         1712  +    p->zName = (char*)&p[1];
         1713  +    memcpy(p->zName, zName, nName);
         1714  +    p->zName[nName] = 0;
         1715  +    sqlite3HashInsert(&db->aFunc, p->zName, nName, (void*)p);
  1722   1716     }
  1723   1717     return p;
  1724   1718   }

Changes to src/func.c.

    12     12   ** This file contains the C functions that implement various SQL
    13     13   ** functions of SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: func.c,v 1.56 2004/05/26 06:18:37 danielk1977 Exp $
           19  +** $Id: func.c,v 1.57 2004/05/26 16:54:43 drh Exp $
    20     20   */
    21     21   #include <ctype.h>
    22     22   #include <math.h>
    23     23   #include <stdlib.h>
    24     24   #include <assert.h>
    25     25   #include "sqliteInt.h"
    26     26   #include "vdbeInt.h"
    27     27   #include "os.h"
    28     28   
    29     29   /*
    30     30   ** Implementation of the non-aggregate min() and max() functions
    31     31   */
    32         -static void minmaxFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
    33         -  const char *zBest; 
           32  +static void minmaxFunc(
           33  +  sqlite3_context *context,
           34  +  int argc,
           35  +  sqlite3_value **argv
           36  +){
    34     37     int i;
    35         -  int (*xCompare)(const char*, const char*);
    36     38     int mask;    /* 0 for min() or 0xffffffff for max() */
    37         -  const char *zArg;
           39  +  int iBest;
    38     40   
    39     41     if( argc==0 ) return;
    40     42     mask = (int)sqlite3_user_data(context);
    41         -  zBest = sqlite3_value_data(argv[0]);
    42         -  if( zBest==0 ) return;
    43         -  zArg = sqlite3_value_data(argv[1]);
    44         -  if( zArg[0]=='n' ){
    45         -    xCompare = sqlite3Compare;
    46         -  }else{
    47         -    xCompare = strcmp;
    48         -  }
    49         -  for(i=2; i<argc; i+=2){
    50         -    zArg = sqlite3_value_data(argv[i]);
    51         -    if( zArg==0 ) return;
    52         -    if( (xCompare(zArg, zBest)^mask)<0 ){
    53         -      zBest = zArg;
           43  +  iBest = 0;
           44  +  for(i=1; i<argc; i++){
           45  +    if( (sqlite3MemCompare(argv[iBest], argv[i], 0)^mask)<0 ){
           46  +      iBest = i;
    54     47       }
    55     48     }
    56         -  sqlite3_result_text(context, zBest, -1, 1);
           49  +  sqlite3_result(context, argv[iBest]);
    57     50   }
    58     51   
    59     52   /*
    60     53   ** Return the type of the argument.
    61     54   */
    62         -static void typeofFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
           55  +static void typeofFunc(
           56  +  sqlite3_context *context,
           57  +  int argc,
           58  +  sqlite3_value **argv
           59  +){
    63     60     const char *z = 0;
    64         -  assert( argc==2 );
    65     61     switch( sqlite3_value_type(argv[0]) ){
    66         -    case SQLITE3_NULL: z = "null" ; break;
    67         -    case SQLITE3_INTEGER: z = "integer" ; break;
    68         -    case SQLITE3_TEXT: z = "text" ; break;
    69         -    case SQLITE3_FLOAT: z = "real" ; break;
    70         -    case SQLITE3_BLOB: z = "blob" ; break;
           62  +    case SQLITE3_NULL:    z = "null";    break;
           63  +    case SQLITE3_INTEGER: z = "integer"; break;
           64  +    case SQLITE3_TEXT:    z = "text";    break;
           65  +    case SQLITE3_FLOAT:   z = "real";    break;
           66  +    case SQLITE3_BLOB:    z = "blob";    break;
    71     67     }
    72     68     sqlite3_result_text(context, z, -1, 0);
    73     69   }
    74     70   
    75     71   /*
    76     72   ** Implementation of the length() function
    77     73   */
    78         -static void lengthFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
           74  +static void lengthFunc(
           75  +  sqlite3_context *context,
           76  +  int argc,
           77  +  sqlite3_value **argv
           78  +){
    79     79     const char *z;
    80     80     int len;
    81     81   
    82     82     assert( argc==1 );
    83         -  z = sqlite3_value_data(argv[0]);
    84         -  if( z==0 ) return;
    85         -#ifdef SQLITE_UTF8
    86         -  for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
    87         -#else
    88         -  len = strlen(z);
    89         -#endif
    90         -  sqlite3_result_int32(context, len);
           83  +  switch( sqlite3_value_type(argv[0]) ){
           84  +    case SQLITE3_BLOB:
           85  +    case SQLITE3_INTEGER:
           86  +    case SQLITE3_FLOAT: {
           87  +      sqlite3_result_int32(context, sqlite3_value_bytes(argv[0]));
           88  +      break;
           89  +    }
           90  +    case SQLITE3_TEXT: {
           91  +      const char *z = sqlite3_value_data(argv[0]);
           92  +      for(len=0; *z; z++){ if( (0xc0&*z)!=0x80 ) len++; }
           93  +      sqlite3_result_int32(context, len);
           94  +      break;
           95  +    }
           96  +    default: {
           97  +      sqlite3_result_null(context);
           98  +      break;
           99  +    }
          100  +  }
    91    101   }
    92    102   
    93    103   /*
    94    104   ** Implementation of the abs() function
    95    105   */
    96    106   static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
    97    107     const char *z;
    98    108     assert( argc==1 );
    99         -  z = sqlite3_value_data(argv[0]);
   100         -  if( z==0 ) return;
   101         -  if( z[0]=='-' && isdigit(z[1]) ) z++;
   102         -  sqlite3_result_text(context, z, -1, 1);
          109  +  switch( sqlite3_value_type(argv[0]) ){
          110  +    case SQLITE3_INTEGER: {
          111  +      sqlite3_result_int64(context, -sqlite3_value_int(argv[0]));
          112  +      break;
          113  +    }
          114  +    case SQLITE3_NULL: {
          115  +      sqlite3_result_null(context);
          116  +      break;
          117  +    }
          118  +    default: {
          119  +      sqlite3_result_double(context, -sqlite3_value_float(argv[0]));
          120  +      break;
          121  +    }
          122  +  }
   103    123   }
   104    124   
   105    125   /*
   106    126   ** Implementation of the substr() function
   107    127   */
   108         -static void substrFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          128  +static void substrFunc(
          129  +  sqlite3_context *context,
          130  +  int argc,
          131  +  sqlite3_value **argv
          132  +){
   109    133     const char *z;
   110         -#ifdef SQLITE_UTF8
   111    134     const char *z2;
   112    135     int i;
   113         -#endif
   114    136     int p1, p2, len;
          137  +
   115    138     assert( argc==3 );
   116    139     z = sqlite3_value_data(argv[0]);
   117    140     if( z==0 ) return;
   118    141     p1 = sqlite3_value_int(argv[1]);
   119    142     p2 = sqlite3_value_int(argv[2]);
   120         -#ifdef SQLITE_UTF8
   121    143     for(len=0, z2=z; *z2; z2++){ if( (0xc0&*z2)!=0x80 ) len++; }
   122         -#else
   123         -  len = strlen(z);
   124         -#endif
   125    144     if( p1<0 ){
   126    145       p1 += len;
   127    146       if( p1<0 ){
   128    147         p2 += p1;
   129    148         p1 = 0;
   130    149       }
   131    150     }else if( p1>0 ){
   132    151       p1--;
   133    152     }
   134    153     if( p1+p2>len ){
   135    154       p2 = len-p1;
   136    155     }
   137         -#ifdef SQLITE_UTF8
   138    156     for(i=0; i<p1 && z[i]; i++){
   139    157       if( (z[i]&0xc0)==0x80 ) p1++;
   140    158     }
   141    159     while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p1++; }
   142    160     for(; i<p1+p2 && z[i]; i++){
   143    161       if( (z[i]&0xc0)==0x80 ) p2++;
   144    162     }
   145    163     while( z[i] && (z[i]&0xc0)==0x80 ){ i++; p2++; }
   146         -#endif
   147    164     if( p2<0 ) p2 = 0;
   148    165     sqlite3_result_text(context, &z[p1], p2, 1);
   149    166   }
   150    167   
   151    168   /*
   152    169   ** Implementation of the round() function
   153    170   */
................................................................................
   199    216   }
   200    217   
   201    218   /*
   202    219   ** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
   203    220   ** All three do the same thing.  They return the first non-NULL
   204    221   ** argument.
   205    222   */
   206         -static void ifnullFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          223  +static void ifnullFunc(
          224  +  sqlite3_context *context,
          225  +  int argc,
          226  +  sqlite3_value **argv
          227  +){
   207    228     int i;
   208    229     for(i=0; i<argc; i++){
   209    230       if( SQLITE3_NULL!=sqlite3_value_type(argv[i]) ){
   210         -      sqlite3_result_text(context, sqlite3_value_data(argv[i]), -1, 1);
          231  +      sqlite3_result(context, argv[i]);
   211    232         break;
   212    233       }
   213    234     }
   214    235   }
   215    236   
   216    237   /*
   217    238   ** Implementation of random().  Return a random integer.  
   218    239   */
   219         -static void randomFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          240  +static void randomFunc(
          241  +  sqlite3_context *context,
          242  +  int argc,
          243  +  sqlite3_value **argv
          244  +){
   220    245     int r;
   221    246     sqlite3Randomness(sizeof(r), &r);
   222    247     sqlite3_result_int32(context, r);
   223    248   }
   224    249   
   225    250   /*
   226    251   ** Implementation of the last_insert_rowid() SQL function.  The return
................................................................................
   228    253   */
   229    254   static void last_insert_rowid(
   230    255     sqlite3_context *context, 
   231    256     int arg, 
   232    257     sqlite3_value **argv
   233    258   ){
   234    259     sqlite *db = sqlite3_user_data(context);
   235         -  sqlite3_result_int32(context, sqlite3_last_insert_rowid(db));
          260  +  sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
   236    261   }
   237    262   
   238    263   /*
   239    264   ** Implementation of the change_count() SQL function.  The return
   240    265   ** value is the same as the sqlite3_changes() API function.
   241    266   */
   242         -static void change_count(sqlite3_context *context, int arg, sqlite3_value **argv){
          267  +static void change_count(
          268  +  sqlite3_context *context,
          269  +  int arg,
          270  +  sqlite3_value **argv
          271  +){
   243    272     sqlite *db = sqlite3_user_data(context);
   244    273     sqlite3_result_int32(context, sqlite3_changes(db));
   245    274   }
   246    275   
   247    276   /*
   248    277   ** Implementation of the last_statement_change_count() SQL function.  The
   249    278   ** return value is the same as the sqlite3_last_statement_changes() API
................................................................................
   297    326   }
   298    327   
   299    328   /*
   300    329   ** Implementation of the NULLIF(x,y) function.  The result is the first
   301    330   ** argument if the arguments are different.  The result is NULL if the
   302    331   ** arguments are equal to each other.
   303    332   */
   304         -static void nullifFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   305         -  const unsigned char *zX = sqlite3_value_data(argv[0]);
   306         -  const unsigned char *zY = sqlite3_value_data(argv[1]);
   307         -  if( zX!=0 && sqlite3Compare(zX, zY)!=0 ){
   308         -    sqlite3_result_text(context, zX, -1, 1);
          333  +static void nullifFunc(
          334  +  sqlite3_context *context,
          335  +  int argc,
          336  +  sqlite3_value **argv
          337  +){
          338  +  if( sqlite3MemCompare(argv[0], argv[1], 0)!=0 ){
          339  +    sqlite3_result(context, argv[0]);
   309    340     }
   310    341   }
   311    342   
   312    343   /*
   313    344   ** Implementation of the VERSION(*) function.  The result is the version
   314    345   ** of the SQLite library that is running.
   315    346   */
   316         -static void versionFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
          347  +static void versionFunc(
          348  +  sqlite3_context *context,
          349  +  int argc,
          350  +  sqlite3_value **argv
          351  +){
   317    352     sqlite3_result_text(context, sqlite3_version, -1, 0);
   318    353   }
   319    354   
   320    355   /*
   321    356   ** EXPERIMENTAL - This is not an official function.  The interface may
   322    357   ** change.  This function may disappear.  Do not write code that depends
   323    358   ** on this function.
................................................................................
   327    362   ** the argument.  If the argument is NULL, the return value is the string
   328    363   ** "NULL".  Otherwise, the argument is enclosed in single quotes with
   329    364   ** single-quote escapes.
   330    365   */
   331    366   static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
   332    367     const char *zArg = sqlite3_value_data(argv[0]);
   333    368     if( argc<1 ) return;
   334         -  if( zArg==0 ){
   335         -    sqlite3_result_text(context, "NULL", 4, 0);
   336         -  }else if( sqlite3IsNumber(zArg, 0, TEXT_Utf8) ){
   337         -    sqlite3_result_text(context, zArg, -1, 1);
   338         -  }else{
   339         -    int i,j,n;
   340         -    char *z;
   341         -    for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
   342         -    z = sqliteMalloc( i+n+3 );
   343         -    if( z==0 ) return;
   344         -    z[0] = '\'';
   345         -    for(i=0, j=1; zArg[i]; i++){
   346         -      z[j++] = zArg[i];
   347         -      if( zArg[i]=='\'' ){
   348         -        z[j++] = '\'';
   349         -      }
          369  +  switch( sqlite3_value_type(argv[0]) ){
          370  +    case SQLITE3_NULL: {
          371  +      sqlite3_result_text(context, "NULL", 4, 0);
          372  +      break;
          373  +    }
          374  +    case SQLITE3_INTEGER:
          375  +    case SQLITE3_FLOAT: {
          376  +      sqlite3_result(context, argv[0]);
          377  +      break;
   350    378       }
   351         -    z[j++] = '\'';
   352         -    z[j] = 0;
   353         -    sqlite3_result_text(context, z, j, 1);
   354         -    sqliteFree(z);
          379  +    case SQLITE3_BLOB:  /*** FIX ME.  Use a BLOB encoding ***/
          380  +    case SQLITE3_TEXT: {
          381  +      int i,j,n;
          382  +      const char *zArg = sqlite3_value_data(argv[0]);
          383  +      char *z;
          384  +
          385  +      for(i=n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
          386  +      z = sqliteMalloc( i+n+3 );
          387  +      if( z==0 ) return;
          388  +      z[0] = '\'';
          389  +      for(i=0, j=1; zArg[i]; i++){
          390  +        z[j++] = zArg[i];
          391  +        if( zArg[i]=='\'' ){
          392  +          z[j++] = '\'';
          393  +        }
          394  +      }
          395  +      z[j++] = '\'';
          396  +      z[j] = 0;
          397  +      sqlite3_result_text(context, z, j, 1);
          398  +      sqliteFree(z);
          399  +    }
   355    400     }
   356    401   }
   357    402   
   358    403   #ifdef SQLITE_SOUNDEX
   359    404   /*
   360    405   ** Compute the soundex encoding of a word.
   361    406   */
................................................................................
   405    450        "abcdefghijklmnopqrstuvwxyz"
   406    451        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   407    452        "0123456789"
   408    453        ".-!,:*^+=_|?/<> ";
   409    454     int iMin, iMax, n, r, i;
   410    455     unsigned char zBuf[1000];
   411    456     if( argc>=1 ){
   412         -    iMin = atoi(sqlite3_value_data(argv[0]));
          457  +    iMin = sqlite3_value_int(argv[0]);
   413    458       if( iMin<0 ) iMin = 0;
   414    459       if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
   415    460     }else{
   416    461       iMin = 1;
   417    462     }
   418    463     if( argc>=2 ){
   419         -    iMax = atoi(sqlite3_value_data(argv[1]));
          464  +    iMax = sqlite3_value_int(argv[1]);
   420    465       if( iMax<iMin ) iMax = iMin;
   421    466       if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
   422    467     }else{
   423    468       iMax = 50;
   424    469     }
   425    470     n = iMin;
   426    471     if( iMax>iMin ){
................................................................................
   574    619     }else{
   575    620       sqlite3VdbeMemCopy(pBest, pArg);
   576    621     }
   577    622   }
   578    623   static void minMaxFinalize(sqlite3_context *context){
   579    624     sqlite3_value *pRes;
   580    625     pRes = (sqlite3_value *)sqlite3_get_context(context, sizeof(Mem));
   581         -  
   582    626     if( pRes->flags ){
   583         -    switch( sqlite3_value_type(pRes) ){
   584         -      case SQLITE3_INTEGER: 
   585         -        sqlite3_result_int32(context, sqlite3_value_int(pRes));
   586         -        break;
   587         -      case SQLITE3_FLOAT: 
   588         -        sqlite3_result_double(context, sqlite3_value_float(pRes));
   589         -      case SQLITE3_TEXT: 
   590         -      case SQLITE3_BLOB: 
   591         -        sqlite3_result_text(context,
   592         -            sqlite3_value_data(pRes), sqlite3_value_bytes(pRes), 1);
   593         -        break;
   594         -      case SQLITE3_NULL: 
   595         -      default:
   596         -        assert(0);
   597         -    }
          627  +    sqlite3_result(context, pRes);
   598    628     }
   599    629   }
   600    630   
   601    631   /*
   602    632   ** This function registered all of the above C functions as SQL
   603    633   ** functions.  This should be the only routine in this file with
   604    634   ** external linkage.
   605    635   */
   606    636   void sqlite3RegisterBuiltinFunctions(sqlite *db){
   607    637     static struct {
   608    638        char *zName;
   609    639        signed char nArg;
   610         -     signed char dataType;
   611    640        u8 argType;               /* 0: none.  1: db  2: (-1) */
   612    641        void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
   613    642     } aFuncs[] = {
   614         -    { "min",       -1, SQLITE_ARGS,    0, minmaxFunc },
   615         -    { "min",        0, 0,              0, 0          },
   616         -    { "max",       -1, SQLITE_ARGS,    2, minmaxFunc },
   617         -    { "max",        0, 0,              2, 0          },
   618         -    { "typeof",     1, SQLITE_TEXT,    0, typeofFunc },
   619         -    { "classof",    1, SQLITE_TEXT,    0, typeofFunc }, /* FIX ME: hack */
   620         -    { "length",     1, SQLITE_NUMERIC, 0, lengthFunc },
   621         -    { "substr",     3, SQLITE_TEXT,    0, substrFunc },
   622         -    { "abs",        1, SQLITE_NUMERIC, 0, absFunc    },
   623         -    { "round",      1, SQLITE_NUMERIC, 0, roundFunc  },
   624         -    { "round",      2, SQLITE_NUMERIC, 0, roundFunc  },
   625         -    { "upper",      1, SQLITE_TEXT,    0, upperFunc  },
   626         -    { "lower",      1, SQLITE_TEXT,    0, lowerFunc  },
   627         -    { "coalesce",  -1, SQLITE_ARGS,    0, ifnullFunc },
   628         -    { "coalesce",   0, 0,              0, 0          },
   629         -    { "coalesce",   1, 0,              0, 0          },
   630         -    { "ifnull",     2, SQLITE_ARGS,    0, ifnullFunc },
   631         -    { "random",    -1, SQLITE_NUMERIC, 0, randomFunc },
   632         -    { "like",       2, SQLITE_NUMERIC, 0, likeFunc   },
   633         -    { "glob",       2, SQLITE_NUMERIC, 0, globFunc   },
   634         -    { "nullif",     2, SQLITE_ARGS,    0, nullifFunc },
   635         -    { "sqlite_version",0,SQLITE_TEXT,  0, versionFunc},
   636         -    { "quote",      1, SQLITE_ARGS,    0, quoteFunc  },
   637         -    { "last_insert_rowid", 0, SQLITE_NUMERIC, 1, last_insert_rowid },
   638         -    { "change_count",      0, SQLITE_NUMERIC, 1, change_count      },
   639         -    { "last_statement_change_count",
   640         -                           0, SQLITE_NUMERIC, 1, last_statement_change_count },
          643  +    { "min",                        -1, 0, minmaxFunc },
          644  +    { "min",                         0, 0, 0          },
          645  +    { "max",                        -1, 2, minmaxFunc },
          646  +    { "max",                         0, 2, 0          },
          647  +    { "typeof",                      1, 0, typeofFunc },
          648  +    { "classof",                     1, 0, typeofFunc }, /* FIX ME: hack */
          649  +    { "length",                      1, 0, lengthFunc },
          650  +    { "substr",                      3, 0, substrFunc },
          651  +    { "abs",                         1, 0, absFunc    },
          652  +    { "round",                       1, 0, roundFunc  },
          653  +    { "round",                       2, 0, roundFunc  },
          654  +    { "upper",                       1, 0, upperFunc  },
          655  +    { "lower",                       1, 0, lowerFunc  },
          656  +    { "coalesce",                   -1, 0, ifnullFunc },
          657  +    { "coalesce",                    0, 0, 0          },
          658  +    { "coalesce",                    1, 0, 0          },
          659  +    { "ifnull",                      2, 0, ifnullFunc },
          660  +    { "random",                     -1, 0, randomFunc },
          661  +    { "like",                        2, 0, likeFunc   },
          662  +    { "glob",                        2, 0, globFunc   },
          663  +    { "nullif",                      2, 0, nullifFunc },
          664  +    { "sqlite_version",              0, 0, versionFunc},
          665  +    { "quote",                       1, 0, quoteFunc  },
          666  +    { "last_insert_rowid",           0, 1, last_insert_rowid },
          667  +    { "change_count",                0, 1, change_count      },
          668  +    { "last_statement_change_count", 0, 1, last_statement_change_count },
   641    669   #ifdef SQLITE_SOUNDEX
   642         -    { "soundex",    1, SQLITE_TEXT,    0, soundexFunc},
          670  +    { "soundex",                     1, 0, soundexFunc},
   643    671   #endif
   644    672   #ifdef SQLITE_TEST
   645         -    { "randstr",    2, SQLITE_TEXT,    0, randStr    },
          673  +    { "randstr",                     2, 0, randStr    },
   646    674   #endif
   647    675     };
   648    676     static struct {
   649    677       char *zName;
   650    678       signed char nArg;
   651         -    signed char dataType;
   652    679       u8 argType;
   653    680       void (*xStep)(sqlite3_context*,int,sqlite3_value**);
   654    681       void (*xFinalize)(sqlite3_context*);
   655    682     } aAggs[] = {
   656         -    { "min",    1, 0,              0, minmaxStep,   minMaxFinalize },
   657         -    { "max",    1, 0,              2, minmaxStep,   minMaxFinalize },
   658         -    { "sum",    1, SQLITE_NUMERIC, 0, sumStep,      sumFinalize    },
   659         -    { "avg",    1, SQLITE_NUMERIC, 0, sumStep,      avgFinalize    },
   660         -    { "count",  0, SQLITE_NUMERIC, 0, countStep,    countFinalize  },
   661         -    { "count",  1, SQLITE_NUMERIC, 0, countStep,    countFinalize  },
          683  +    { "min",    1, 0, minmaxStep,   minMaxFinalize },
          684  +    { "max",    1, 2, minmaxStep,   minMaxFinalize },
          685  +    { "sum",    1, 0, sumStep,      sumFinalize    },
          686  +    { "avg",    1, 0, sumStep,      avgFinalize    },
          687  +    { "count",  0, 0, countStep,    countFinalize  },
          688  +    { "count",  1, 0, countStep,    countFinalize  },
   662    689   #if 0
   663         -    { "stddev", 1, SQLITE_NUMERIC, 0, stdDevStep,   stdDevFinalize },
          690  +    { "stddev", 1, 0, stdDevStep,   stdDevFinalize },
   664    691   #endif
   665    692     };
   666         -  static const char *azTypeFuncs[] = { "min", "max", "typeof" };
   667    693     int i;
   668    694   
   669    695     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   670    696       void *pArg = aFuncs[i].argType==2 ? (void*)(-1) : db;
   671    697       sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, 0, 0,
   672    698           pArg, aFuncs[i].xFunc, 0, 0);
   673         -    if( aFuncs[i].xFunc ){
   674         -      sqlite3_function_type(db, aFuncs[i].zName, aFuncs[i].dataType);
   675         -    }
   676    699     }
   677    700     for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
   678    701       void *pArg = aAggs[i].argType==2 ? (void*)(-1) : db;
   679    702       sqlite3_create_function(db, aAggs[i].zName, aAggs[i].nArg, 0, 0, pArg,
   680    703           0, aAggs[i].xStep, aAggs[i].xFinalize);
   681         -    sqlite3_function_type(db, aAggs[i].zName, aAggs[i].dataType);
   682         -  }
   683         -
   684         -  for(i=0; i<sizeof(azTypeFuncs)/sizeof(azTypeFuncs[0]); i++){
   685         -    int n = strlen(azTypeFuncs[i]);
   686         -    FuncDef *p = sqlite3HashFind(&db->aFunc, azTypeFuncs[i], n);
   687         -    while( p ){
   688         -      p->includeTypes = 1;
   689         -      p = p->pNext;
   690         -    }
   691    704     }
   692    705     sqlite3RegisterDateTimeFunctions(db);
   693    706   }
   694         -
   695         -
   696         -

Changes to src/main.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** Main file for the SQLite library.  The routines in this file
    13     13   ** implement the programmer interface to the library.  Routines in
    14     14   ** other files are for internal use by SQLite and should not be
    15     15   ** accessed by users of the library.
    16     16   **
    17         -** $Id: main.c,v 1.193 2004/05/26 10:11:06 danielk1977 Exp $
           17  +** $Id: main.c,v 1.194 2004/05/26 16:54:43 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "os.h"
    21     21   #include <ctype.h>
    22     22   
    23     23   /*
    24     24   ** A pointer to this structure is used to communicate information
................................................................................
   408    408     }
   409    409     return rc;
   410    410   }
   411    411   
   412    412   /*
   413    413   ** Return the ROWID of the most recent insert
   414    414   */
   415         -int sqlite3_last_insert_rowid(sqlite *db){
          415  +long long int sqlite3_last_insert_rowid(sqlite *db){
   416    416     return db->lastRowid;
   417    417   }
   418    418   
   419    419   /*
   420    420   ** Return the number of changes in the most recent call to sqlite3_exec().
   421    421   */
   422    422   int sqlite3_changes(sqlite *db){
................................................................................
   691    691     }
   692    692     rc = sqlite3_create_function(db, zFunctionName8, nArg, eTextRep, 
   693    693         iCollateArg, pUserData, xFunc, xStep, xFinal);
   694    694     sqliteFree(zFunctionName8);
   695    695     return rc;
   696    696   }
   697    697   
   698         -/*
   699         -** Change the datatype for all functions with a given name.  See the
   700         -** header comment for the prototype of this function in sqlite.h for
   701         -** additional information.
   702         -*/
   703         -int sqlite3_function_type(sqlite *db, const char *zName, int dataType){
   704         -  FuncDef *p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, strlen(zName));
   705         -  while( p ){
   706         -    p->dataType = dataType; 
   707         -    p = p->pNext;
   708         -  }
   709         -  return SQLITE_OK;
   710         -}
   711         -
   712    698   /*
   713    699   ** Register a trace function.  The pArg from the previously registered trace
   714    700   ** is returned.  
   715    701   **
   716    702   ** A NULL trace function means that no tracing is executes.  A non-NULL
   717    703   ** trace is a pointer to a function that is invoked at the start of each
   718    704   ** sqlite3_exec().
................................................................................
  1024   1010     db->onError = OE_Default;
  1025   1011     db->priorNewRowid = 0;
  1026   1012     db->magic = SQLITE_MAGIC_BUSY;
  1027   1013     db->nDb = 2;
  1028   1014     db->aDb = db->aDbStatic;
  1029   1015     db->enc = def_enc;
  1030   1016     /* db->flags |= SQLITE_ShortColNames; */
  1031         -  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
         1017  +  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 0);
  1032   1018     sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
  1033   1019     for(i=0; i<db->nDb; i++){
  1034   1020       sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
  1035   1021       sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
  1036   1022       sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
  1037   1023       sqlite3HashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
  1038   1024     }

Changes to src/select.c.

     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   ** This file contains C code routines that are called by the parser
    13     13   ** to handle SELECT statements in SQLite.
    14     14   **
    15         -** $Id: select.c,v 1.177 2004/05/26 10:11:06 danielk1977 Exp $
           15  +** $Id: select.c,v 1.178 2004/05/26 16:54:44 drh Exp $
    16     16   */
    17     17   #include "sqliteInt.h"
    18     18   
    19     19   
    20     20   /*
    21     21   ** Allocate a new Select structure and return a pointer to that
    22     22   ** structure.
................................................................................
  2334   2334     /* Reset the aggregator
  2335   2335     */
  2336   2336     if( isAgg ){
  2337   2337       sqlite3VdbeAddOp(v, OP_AggReset, 0, pParse->nAgg);
  2338   2338       for(i=0; i<pParse->nAgg; i++){
  2339   2339         FuncDef *pFunc;
  2340   2340         if( (pFunc = pParse->aAgg[i].pFunc)!=0 && pFunc->xFinalize!=0 ){
  2341         -        sqlite3VdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_POINTER);
         2341  +        sqlite3VdbeOp3(v, OP_AggInit, 0, i, (char*)pFunc, P3_FUNCDEF);
  2342   2342         }
  2343   2343       }
  2344   2344       if( pGroupBy==0 ){
  2345   2345         sqlite3VdbeAddOp(v, OP_String, 0, 0);
  2346   2346         sqlite3VdbeAddOp(v, OP_AggFocus, 0, 0);
  2347   2347       }
  2348   2348     }
................................................................................
  2408   2408         if( !pAgg->isAgg ) continue;
  2409   2409         assert( pAgg->pFunc!=0 );
  2410   2410         assert( pAgg->pFunc->xStep!=0 );
  2411   2411         pDef = pAgg->pFunc;
  2412   2412         pE = pAgg->pExpr;
  2413   2413         assert( pE!=0 );
  2414   2414         assert( pE->op==TK_AGG_FUNCTION );
  2415         -      nExpr = sqlite3ExprCodeExprList(pParse, pE->pList, pDef->includeTypes);
         2415  +      nExpr = sqlite3ExprCodeExprList(pParse, pE->pList);
  2416   2416         sqlite3VdbeAddOp(v, OP_Integer, i, 0);
  2417   2417         sqlite3VdbeOp3(v, OP_AggFunc, 0, nExpr, (char*)pDef, P3_POINTER);
  2418   2418       }
  2419   2419     }
  2420   2420   
  2421   2421     /* End the database scan loop.
  2422   2422     */

Changes to src/sqlite.h.in.

     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   ** This header file defines the interface that the SQLite library
    13     13   ** presents to client programs.
    14     14   **
    15         -** @(#) $Id: sqlite.h.in,v 1.80 2004/05/26 06:18:38 danielk1977 Exp $
           15  +** @(#) $Id: sqlite.h.in,v 1.81 2004/05/26 16:54:44 drh Exp $
    16     16   */
    17     17   #ifndef _SQLITE_H_
    18     18   #define _SQLITE_H_
    19     19   #include <stdarg.h>     /* Needed for the definition of va_list */
    20     20   
    21     21   /*
    22     22   ** Make sure we can call this stuff from C++.
................................................................................
   159    159   ** the value of the INTEGER PRIMARY KEY column if there is such a column,
   160    160   ** otherwise the key is generated at random.  The unique key is always
   161    161   ** available as the ROWID, OID, or _ROWID_ column.)  The following routine
   162    162   ** returns the integer key of the most recent insert in the database.
   163    163   **
   164    164   ** This function is similar to the mysql_insert_id() function from MySQL.
   165    165   */
   166         -int sqlite3_last_insert_rowid(sqlite*);
          166  +long long int sqlite3_last_insert_rowid(sqlite*);
   167    167   
   168    168   /*
   169    169   ** This function returns the number of database rows that were changed
   170    170   ** (or inserted or deleted) by the most recent called sqlite3_exec().
   171    171   **
   172    172   ** All changes are counted, even if they were later undone by a
   173    173   ** ROLLBACK or ABORT.  Except, changes associated with creating and
................................................................................
  1120   1120     int iCollateArg,
  1121   1121     void*,
  1122   1122     void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  1123   1123     void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  1124   1124     void (*xFinal)(sqlite3_context*)
  1125   1125   );
  1126   1126   
  1127         -/*
  1128         -** Use the following routine to define the datatype returned by a
  1129         -** user-defined function.  The second argument can be one of the
  1130         -** constants SQLITE_NUMERIC, SQLITE_TEXT, or SQLITE_ARGS or it
  1131         -** can be an integer greater than or equal to zero.  When the datatype
  1132         -** parameter is non-negative, the type of the result will be the
  1133         -** same as the datatype-th argument.  If datatype==SQLITE_NUMERIC
  1134         -** then the result is always numeric.  If datatype==SQLITE_TEXT then
  1135         -** the result is always text.  If datatype==SQLITE_ARGS then the result
  1136         -** is numeric if any argument is numeric and is text otherwise.
  1137         -*/
  1138         -int sqlite3_function_type(
  1139         -  sqlite *db,               /* The database there the function is registered */
  1140         -  const char *zName,        /* Name of the function */
  1141         -  int datatype              /* The datatype for this function */
  1142         -);
  1143         -#define SQLITE_NUMERIC     (-1)
  1144         -#define SQLITE_TEXT        (-2)
  1145         -#define SQLITE_ARGS        (-3)
  1146         -
  1147   1127   /*
  1148   1128   ** The next routine returns the number of calls to xStep for a particular
  1149   1129   ** aggregate function instance.  The current call to xStep counts so this
  1150   1130   ** routine always returns at least 1.
  1151   1131   */
  1152   1132   int sqlite3_aggregate_count(sqlite3_context*);
  1153   1133   
................................................................................
  1319   1299   ** characters) in the string passed as the second argument. If the third
  1320   1300   ** parameter is negative, then the string is read up to the first nul
  1321   1301   ** terminator character.
  1322   1302   */
  1323   1303   void sqlite3_result_error(sqlite3_context*, const char*, int);
  1324   1304   void sqlite3_result_error16(sqlite3_context*, const void*, int);
  1325   1305   
         1306  +/*
         1307  +** Copy a function parameter into the result of the function.
         1308  +*/
         1309  +void sqlite3_result(sqlite3_context*, sqlite3_value*);
         1310  +
  1326   1311   #ifdef __cplusplus
  1327   1312   }  /* End of the 'extern "C"' block */
  1328   1313   #endif
  1329   1314   #endif

Changes to src/sqliteInt.h.

     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   ** Internal interface definitions for SQLite.
    13     13   **
    14         -** @(#) $Id: sqliteInt.h,v 1.252 2004/05/26 06:58:44 danielk1977 Exp $
           14  +** @(#) $Id: sqliteInt.h,v 1.253 2004/05/26 16:54:45 drh Exp $
    15     15   */
    16     16   #include "config.h"
    17     17   #include "sqlite.h"
    18     18   #include "hash.h"
    19     19   #include "parse.h"
    20     20   #include <stdio.h>
    21     21   #include <stdlib.h>
................................................................................
   156    156   ** This macro casts a pointer to an integer.  Useful for doing
   157    157   ** pointer arithmetic.
   158    158   */
   159    159   #define Addr(X)  ((uptr)X)
   160    160   
   161    161   /*
   162    162   ** The maximum number of bytes of data that can be put into a single
   163         -** row of a single table.  The upper bound on this limit is 16777215
   164         -** bytes (or 16MB-1).  We have arbitrarily set the limit to just 1MB
   165         -** here because the overflow page chain is inefficient for really big
   166         -** records and we want to discourage people from thinking that 
          163  +** row of a single table.  The upper bound on this limit is
          164  +** 9223372036854775808 bytes (or 2**63).  We have arbitrarily set the
          165  +** limit to just 1MB here because the overflow page chain is inefficient
          166  +** for really big records and we want to discourage people from thinking that 
   167    167   ** multi-megabyte records are OK.  If your needs are different, you can
   168    168   ** change this define and recompile to increase or decrease the record
   169    169   ** size.
   170         -**
   171         -** The 16777198 is computed as follows:  238 bytes of payload on the
   172         -** original pages plus 16448 overflow pages each holding 1020 bytes of
   173         -** data.
   174    170   */
   175    171   #define MAX_BYTES_PER_ROW  1048576
   176         -/* #define MAX_BYTES_PER_ROW 16777198 */
   177    172   
   178    173   /*
   179    174   ** If memory allocation problems are found, recompile with
   180    175   **
   181    176   **      -DMEMORY_DEBUG=1
   182    177   **
   183    178   ** to enable some sanity checking on malloc() and free().  To
................................................................................
   330    325   #define TEXT_Utf16le       2
   331    326   #define TEXT_Utf16be       3
   332    327   #define TEXT_Utf16         (SQLITE3_BIGENDIAN?TEXT_Utf16be:TEXT_Utf16le)
   333    328   
   334    329   /*
   335    330   ** Each database is an instance of the following structure.
   336    331   **
   337         -** The sqlite.file_format is initialized by the database file
   338         -** and helps determines how the data in the database file is
   339         -** represented.  This field allows newer versions of the library
   340         -** to read and write older databases.  The various file formats
   341         -** are as follows:
   342         -**
   343         -**     file_format==1    Version 2.1.0.
   344         -**     file_format==2    Version 2.2.0. Add support for INTEGER PRIMARY KEY.
   345         -**     file_format==3    Version 2.6.0. Fix empty-string index bug.
   346         -**     file_format==4    Version 2.7.0. Add support for separate numeric and
   347         -**                       text datatypes.
   348         -**
   349    332   ** The sqlite.temp_store determines where temporary database files
   350    333   ** are stored.  If 1, then a file is created to hold those tables.  If
   351    334   ** 2, then they are held in memory.  0 means use the default value in
   352    335   ** the TEMP_STORE macro.
   353    336   **
   354    337   ** The sqlite.lastRowid records the last insert rowid generated by an
   355    338   ** insert statement.  Inserts on views do not affect its value.  Each
................................................................................
   458    441   /*
   459    442   ** Each SQL function is defined by an instance of the following
   460    443   ** structure.  A pointer to this structure is stored in the sqlite.aFunc
   461    444   ** hash table.  When multiple functions have the same name, the hash table
   462    445   ** points to a linked list of these structures.
   463    446   */
   464    447   struct FuncDef {
   465         -  void (*xFunc)(sqlite3_context*,int,sqlite3_value**);  /* Regular function */
   466         -  void (*xStep)(sqlite3_context*,int,sqlite3_value**);  /* Aggregate function step */
   467         -  void (*xFinalize)(sqlite3_context*);           /* Aggregate function finializer */
   468         -  signed char nArg;         /* Number of arguments.  -1 means unlimited */
   469         -  signed char dataType;     /* Arg that determines datatype.  -1=NUMERIC, */
   470         -                            /* -2=TEXT. -3=SQLITE_ARGS */
   471         -  u8 includeTypes;          /* Add datatypes to args of xFunc and xStep */
   472         -  void *pUserData;          /* User data parameter */
   473         -  FuncDef *pNext;           /* Next function with same name */
          448  +  char *zName;         /* SQL name of the function */
          449  +  int nArg;            /* Number of arguments.  -1 means unlimited */
          450  +  void *pUserData;     /* User data parameter */
          451  +  FuncDef *pNext;      /* Next function with same name */
          452  +  void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
          453  +  void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
          454  +  void (*xFinalize)(sqlite3_context*);                /* Aggregate finializer */
   474    455   };
   475    456   
   476    457   /*
   477    458   ** information about each column of an SQL table is held in an instance
   478    459   ** of this structure.
   479    460   */
   480    461   struct Column {
................................................................................
  1252   1233   Table *sqlite3SrcListLookup(Parse*, SrcList*);
  1253   1234   int sqlite3IsReadOnly(Parse*, Table*, int);
  1254   1235   void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
  1255   1236   void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  1256   1237   WhereInfo *sqlite3WhereBegin(Parse*, SrcList*, Expr*, int, ExprList**);
  1257   1238   void sqlite3WhereEnd(WhereInfo*);
  1258   1239   void sqlite3ExprCode(Parse*, Expr*);
  1259         -int sqlite3ExprCodeExprList(Parse*, ExprList*, int);
         1240  +int sqlite3ExprCodeExprList(Parse*, ExprList*);
  1260   1241   void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
  1261   1242   void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
  1262   1243   Table *sqlite3FindTable(sqlite*,const char*, const char*);
  1263   1244   Table *sqlite3LocateTable(Parse*,const char*, const char*);
  1264   1245   Index *sqlite3FindIndex(sqlite*,const char*, const char*);
  1265   1246   void sqlite3UnlinkAndDeleteIndex(sqlite*,Index*);
  1266   1247   void sqlite3Copy(Parse*, SrcList*, Token*, Token*, int);

Changes to src/tclsqlite.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   ** A TCL Interface to SQLite
    13     13   **
    14         -** $Id: tclsqlite.c,v 1.71 2004/05/26 06:18:38 danielk1977 Exp $
           14  +** $Id: tclsqlite.c,v 1.72 2004/05/26 16:54:46 drh Exp $
    15     15   */
    16     16   #ifndef NO_TCL     /* Omit this whole file if TCL is unavailable */
    17     17   
    18     18   #include "sqliteInt.h"
    19     19   #include "tcl.h"
    20     20   #include <stdlib.h>
    21     21   #include <string.h>
................................................................................
   860    860       pFunc = (SqlFunc*)Tcl_Alloc( sizeof(*pFunc) + nScript + 1 );
   861    861       if( pFunc==0 ) return TCL_ERROR;
   862    862       pFunc->interp = interp;
   863    863       pFunc->pNext = pDb->pFunc;
   864    864       pFunc->zScript = (char*)&pFunc[1];
   865    865       strcpy(pFunc->zScript, zScript);
   866    866       sqlite3_create_function(pDb->db, zName, -1, 0, 0, pFunc, tclSqlFunc, 0, 0);
   867         -    sqlite3_function_type(pDb->db, zName, SQLITE_NUMERIC);
   868    867       break;
   869    868     }
   870    869   
   871    870     /*
   872    871     **     $db last_insert_rowid 
   873    872     **
   874    873     ** Return an integer which is the ROWID for the most recent insert.
................................................................................
  1241   1240       Tcl_GlobalEval(interp, zMainloop);
  1242   1241     }
  1243   1242     return 0;
  1244   1243   }
  1245   1244   #endif /* TCLSH */
  1246   1245   
  1247   1246   #endif /* !defined(NO_TCL) */
  1248         -
  1249         -
  1250         -

Changes to src/vdbe.c.

    39     39   **
    40     40   ** Various scripts scan this source file in order to generate HTML
    41     41   ** documentation, headers files, or other derived files.  The formatting
    42     42   ** of the code in this file is, therefore, important.  See other comments
    43     43   ** in this file for details.  If in doubt, do not deviate from existing
    44     44   ** commenting and indentation practices when changing or adding code.
    45     45   **
    46         -** $Id: vdbe.c,v 1.334 2004/05/26 13:27:00 danielk1977 Exp $
           46  +** $Id: vdbe.c,v 1.335 2004/05/26 16:54:47 drh Exp $
    47     47   */
    48     48   #include "sqliteInt.h"
    49     49   #include "os.h"
    50     50   #include <ctype.h>
    51     51   #include "vdbeInt.h"
    52     52   
    53     53   /*
................................................................................
  5860   5860           assert( (pTos->flags & MEM_Short)==0 || pTos->z==pTos->zShort );
  5861   5861           assert( (pTos->flags & MEM_Short)!=0 || pTos->z!=pTos->zShort );
  5862   5862         }else{
  5863   5863           /* Cannot define a string subtype for non-string objects */
  5864   5864           assert( (pTos->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
  5865   5865         }
  5866   5866         /* MEM_Null excludes all other types */
  5867         -      assert( pTos->flags==MEM_Null || (pTos->flags&MEM_Null)==0 );
         5867  +      assert( (pTos->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
         5868  +              || (pTos->flags&MEM_Null)==0 );
  5868   5869       }
  5869   5870       if( pc<-1 || pc>=p->nOp ){
  5870   5871         sqlite3SetString(&p->zErrMsg, "jump destination out of range", (char*)0);
  5871   5872         rc = SQLITE_INTERNAL;
  5872   5873       }
  5873   5874       if( p->trace && pTos>=p->aStack ){
  5874   5875         int i;

Changes to src/vdbe.h.

    11     11   *************************************************************************
    12     12   ** Header file for the Virtual DataBase Engine (VDBE)
    13     13   **
    14     14   ** This header defines the interface to the virtual database engine
    15     15   ** or VDBE.  The VDBE implements an abstract machine that runs a
    16     16   ** simple program to access and modify the underlying database.
    17     17   **
    18         -** $Id: vdbe.h,v 1.83 2004/05/26 10:11:07 danielk1977 Exp $
           18  +** $Id: vdbe.h,v 1.84 2004/05/26 16:54:48 drh Exp $
    19     19   */
    20     20   #ifndef _SQLITE_VDBE_H_
    21     21   #define _SQLITE_VDBE_H_
    22     22   #include <stdio.h>
    23     23   
    24     24   /*
    25     25   ** A single VDBE is an opaque structure named "Vdbe".  Only routines
................................................................................
    65     65   ** Allowed values of VdbeOp.p3type
    66     66   */
    67     67   #define P3_NOTUSED    0   /* The P3 parameter is not used */
    68     68   #define P3_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
    69     69   #define P3_STATIC   (-2)  /* Pointer to a static string */
    70     70   #define P3_POINTER  (-3)  /* P3 is a pointer to some structure or object */
    71     71   #define P3_COLLSEQ  (-4)  /* P3 is a pointer to a CollSeq structure */
    72         -#define P3_KEYINFO  (-5)  /* P3 is a pointer to a KeyInfo structure */
           72  +#define P3_FUNCDEF  (-5)  /* P3 is a pointer to a FuncDef structure */
           73  +#define P3_KEYINFO  (-6)  /* P3 is a pointer to a KeyInfo structure */
    73     74   
    74     75   /* When adding a P3 argument using P3_KEYINFO, a copy of the KeyInfo structure
    75     76   ** is made.  That copy is freed when the Vdbe is finalized.  But if the
    76     77   ** argument is P3_KEYINFO_HANDOFF, the passed in pointer is used.  It still
    77     78   ** gets freed when the Vdbe is finalized so it still should be obtained
    78     79   ** from a single sqliteMalloc().  But no copy is made and the calling
    79     80   ** function should *not* try to free the KeyInfo.
    80     81   */
    81         -#define P3_KEYINFO_HANDOFF (-6)
           82  +#define P3_KEYINFO_HANDOFF (-7)
    82     83   
    83     84   /*
    84     85   ** The following macro converts a relative address in the p2 field
    85     86   ** of a VdbeOp structure into a negative number so that 
    86     87   ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
    87     88   ** the macro again restores the address.
    88     89   */

Changes to src/vdbeaux.c.

   512    512         break;
   513    513       }
   514    514       case P3_COLLSEQ: {
   515    515         CollSeq *pColl = (CollSeq*)pOp->p3;
   516    516         sprintf(zTemp, "collseq(%.20s)", pColl->zName);
   517    517         zP3 = zTemp;
   518    518         break;
          519  +    }
          520  +    case P3_FUNCDEF: {
          521  +      FuncDef *pDef = (FuncDef*)pOp->p3;
          522  +      char zNum[30];
          523  +      sprintf(zTemp, "%.*s", nTemp, pDef->zName);
          524  +      sprintf(zNum,"(%d)", pDef->nArg);
          525  +      if( strlen(zTemp)+strlen(zNum)+1<=nTemp ){
          526  +        strcat(zTemp, zNum);
          527  +      }
          528  +      zP3 = zTemp;
          529  +      break;
   519    530       }
   520    531       default: {
   521    532         zP3 = pOp->p3;
   522    533         if( zP3==0 ){
   523    534           zP3 = "";
   524    535         }
   525    536       }
................................................................................
  1865   1876     */
  1866   1877     memcpy(&pMem->z[pMem->n], "\0\0", nulTermLen);
  1867   1878     pMem->n += nulTermLen;
  1868   1879     pMem->flags |= MEM_Term;
  1869   1880   }
  1870   1881   
  1871   1882   /*
  1872         -** The following nine routines, named sqlite3_result_*(), are used to
         1883  +** The following ten routines, named sqlite3_result_*(), are used to
  1873   1884   ** return values or errors from user-defined functions and aggregate
  1874   1885   ** operations. They are commented in the header file sqlite.h (sqlite.h.in)
  1875   1886   */
         1887  +void sqlite3_result(sqlite3_context *pCtx, sqlite3_value *pValue){
         1888  +  sqlite3VdbeMemCopy(&pCtx->s, pValue);
         1889  +}
  1876   1890   void sqlite3_result_int32(sqlite3_context *pCtx, int iVal){
  1877   1891     MemSetInt(&pCtx->s, iVal);
  1878   1892   }
  1879   1893   void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
  1880   1894     MemSetInt(&pCtx->s, iVal);
  1881   1895   }
  1882   1896   void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
................................................................................
  1914   1928     pCtx->isError = 1;
  1915   1929     MemSetStr(&pCtx->s, z, n, TEXT_Utf8, 1);
  1916   1930   }
  1917   1931   void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
  1918   1932     pCtx->isError = 1;
  1919   1933     MemSetStr(&pCtx->s, z, n, TEXT_Utf16, 1);
  1920   1934   }
  1921         -