/ Check-in [6aca3f86]
Login

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

Overview
Comment:Added the last_insert_rowid() SQL function. (CVS 521)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6aca3f86bc08849e9d806fdd490f98e4daf71025
User & Date: drh 2002-04-06 14:10:47
Context
2002-04-08
02:42
Add a -column option to the sqlite command-line utility. Patch from Matthew O. Persico. (CVS 522) check-in: 760bf568 user: drh tags: trunk
2002-04-06
14:10
Added the last_insert_rowid() SQL function. (CVS 521) check-in: 6aca3f86 user: drh tags: trunk
13:57
Make the FROM clause on a SELECT optional. If omitted, the result of the SELECT is a single row consisting of the values in the expression list. (CVS 520) check-in: 28ce42f7 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

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.14 2002/03/04 02:26:16 drh Exp $
           19  +** $Id: func.c,v 1.15 2002/04/06 14:10:47 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   
................................................................................
   191    191   
   192    192   /*
   193    193   ** Implementation of random().  Return a random integer.  
   194    194   */
   195    195   static void randomFunc(sqlite_func *context, int argc, const char **argv){
   196    196     sqlite_set_result_int(context, sqliteRandomInteger());
   197    197   }
          198  +
          199  +/*
          200  +** Implementation of the last_insert_rowid() SQL function.  The return
          201  +** value is the same as the sqlite_last_insert_rowid() API function.
          202  +*/
          203  +static void last_insert_rowid(sqlite_func *context, int arg, char **argv){
          204  +  sqlite *db = sqlite_user_data(context);
          205  +  sqlite_set_result_int(context, sqlite_last_insert_rowid(db));
          206  +}
   198    207   
   199    208   /*
   200    209   ** An instance of the following structure holds the context of a
   201    210   ** sum() or avg() aggregate computation.
   202    211   */
   203    212   typedef struct SumCtx SumCtx;
   204    213   struct SumCtx {
................................................................................
   404    413     };
   405    414     int i;
   406    415   
   407    416     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   408    417       sqlite_create_function(db, aFuncs[i].zName,
   409    418              aFuncs[i].nArg, aFuncs[i].xFunc, 0);
   410    419     }
          420  +  sqlite_create_function(db, "last_insert_rowid", 0, 
          421  +           last_insert_rowid, db);
   411    422     for(i=0; i<sizeof(aAggs)/sizeof(aAggs[0]); i++){
   412    423       sqlite_create_aggregate(db, aAggs[i].zName,
   413    424              aAggs[i].nArg, aAggs[i].xStep, aAggs[i].xFinalize, 0);
   414    425     }
   415    426   }

Changes to test/func.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 file is testing built-in functions.
    13     13   #
    14         -# $Id: func.test,v 1.9 2002/03/02 17:04:09 drh Exp $
           14  +# $Id: func.test,v 1.10 2002/04/06 14:10:47 drh Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   # Create a table to work with.
    20     20   #
    21     21   do_test func-0.0 {
................................................................................
   225    225   #
   226    226   do_test func-6.1 {
   227    227     execsql {SELECT coalesce(a,'xyz') FROM t2}
   228    228   } {1 xyz 345 xyz 67890}
   229    229   do_test func-6.2 {
   230    230     execsql {SELECT coalesce(upper(a),'nil') FROM t2}
   231    231   } {1 nil 345 nil 67890}
          232  +
          233  +# Test the last_insert_rowid() function
          234  +#
          235  +do_test func-7.1 {
          236  +  execsql {SELECT last_insert_rowid()}
          237  +} [db last_insert_rowid]
   232    238   
   233    239   
   234    240   finish_test

Changes to www/changes.tcl.

    21     21   
    22     22   
    23     23   proc chng {date desc} {
    24     24     puts "<DT><B>$date</B></DT>"
    25     25     puts "<DD><P><UL>$desc</UL></P></DD>"
    26     26   }
    27     27   
    28         -chng {2002 Apr 03 (2.4.7)} {
           28  +chng {2002 Apr 06 (2.4.7)} {
    29     29   <li>Add the ability to put TABLE.* in the column list of a
    30     30       SELECT statement.</li>
    31     31   <li>Permit SELECT statements without a FROM clause.</li>
           32  +<li>Added the <b>last_insert_rowid()</b> SQL function.</li>
    32     33   }
    33     34   
    34     35   chng {2002 Apr 02 (2.4.6)} {
    35     36   <li>Bug fix: Correctly handle terms in the WHERE clause of a join that
    36     37       do not contain a comparison operator.</li>
    37     38   }
    38     39   

Changes to www/lang.tcl.

     1      1   #
     2      2   # Run this Tcl script to generate the sqlite.html file.
     3      3   #
     4         -set rcsid {$Id: lang.tcl,v 1.30 2002/04/06 13:57:44 drh Exp $}
            4  +set rcsid {$Id: lang.tcl,v 1.31 2002/04/06 14:10:48 drh Exp $}
     5      5   
     6      6   puts {<html>
     7      7   <head>
     8      8     <title>Query Language Understood By SQLite</title>
     9      9   </head>
    10     10   <body bgcolor=white>
    11     11   <h1 align=center>
................................................................................
   562    562   
   563    563   <tr>
   564    564   <td valign="top" align="right">coalesce(<i>X</i>,<i>Y</i>,...)</td>
   565    565   <td valign="top">Return a copy of the first non-NULL argument.  If
   566    566   all arguments are NULL then NULL is returned.</td>
   567    567   </tr>
   568    568   
          569  +<tr>
          570  +<td valign="top" align="right">last_insert_rowid()</td>
          571  +<td valign="top">Return the ROWID of the last row insert from this
          572  +connection to the database.  This is the same value that would be returned
          573  +from the <b>sqlite_last_insert_rowid()</b> API function.</td>
          574  +</tr>
          575  +
   569    576   <tr>
   570    577   <td valign="top" align="right">length(<i>X</i>)</td>
   571    578   <td valign="top">Return the string length of <i>X</i> in characters.
   572    579   If SQLite is configured to support UTF-8, then the number of UTF-8
   573    580   characters is returned, not the number of bytes.</td>
   574    581   </tr>
   575    582