xref: /sqlite-3.40.0/ext/misc/remember.c (revision aabebc27)
16bada272Sdrh /*
26bada272Sdrh ** 2016-08-09
36bada272Sdrh **
46bada272Sdrh ** The author disclaims copyright to this source code.  In place of
56bada272Sdrh ** a legal notice, here is a blessing:
66bada272Sdrh **
76bada272Sdrh **    May you do good and not evil.
86bada272Sdrh **    May you find forgiveness for yourself and forgive others.
96bada272Sdrh **    May you share freely, never taking more than you give.
106bada272Sdrh **
116bada272Sdrh *************************************************************************
126bada272Sdrh **
136bada272Sdrh ** This file demonstrates how to create an SQL function that is a pass-through
146bada272Sdrh ** for integer values (it returns a copy of its argument) but also saves the
156bada272Sdrh ** value that is passed through into a C-language variable.  The address of
166bada272Sdrh ** the C-language variable is supplied as the second argument.
176bada272Sdrh **
186bada272Sdrh ** This allows, for example, a counter to incremented and the original
196bada272Sdrh ** value retrieved, atomically, using a single statement:
206bada272Sdrh **
216bada272Sdrh **    UPDATE counterTab SET cnt=remember(cnt,$PTR)+1 WHERE id=$ID
226bada272Sdrh **
236bada272Sdrh ** Prepare the above statement once.  Then to use it, bind the address
24ae3ec3f9Sdrh ** of the output variable to $PTR using sqlite3_bind_pointer() with a
25ae3ec3f9Sdrh ** pointer type of "carray" and bind the id of the counter to $ID and
26ae3ec3f9Sdrh ** run the prepared statement.
276bada272Sdrh **
28*aabebc27Sdrh ** This implementation of the remember() function uses a "carray"
29*aabebc27Sdrh ** pointer so that it can share pointers with the carray() extension.
30*aabebc27Sdrh **
316bada272Sdrh ** One can imagine doing similar things with floating-point values and
326bada272Sdrh ** strings, but this demonstration extension will stick to using just
336bada272Sdrh ** integers.
346bada272Sdrh */
356bada272Sdrh #include "sqlite3ext.h"
366bada272Sdrh SQLITE_EXTENSION_INIT1
376bada272Sdrh #include <assert.h>
386bada272Sdrh 
396bada272Sdrh /*
406bada272Sdrh **      remember(V,PTR)
416bada272Sdrh **
426bada272Sdrh ** Return the integer value V.  Also save the value of V in a
436bada272Sdrh ** C-language variable whose address is PTR.
446bada272Sdrh */
rememberFunc(sqlite3_context * pCtx,int argc,sqlite3_value ** argv)456bada272Sdrh static void rememberFunc(
466bada272Sdrh   sqlite3_context *pCtx,
476bada272Sdrh   int argc,
486bada272Sdrh   sqlite3_value **argv
496bada272Sdrh ){
506bada272Sdrh   sqlite3_int64 v;
513561dd4aSdrh   sqlite3_int64 *ptr;
526bada272Sdrh   assert( argc==2 );
536bada272Sdrh   v = sqlite3_value_int64(argv[0]);
54ae3ec3f9Sdrh   ptr = sqlite3_value_pointer(argv[1], "carray");
553561dd4aSdrh   if( ptr ) *ptr = v;
566bada272Sdrh   sqlite3_result_int64(pCtx, v);
576bada272Sdrh }
586bada272Sdrh 
596bada272Sdrh #ifdef _WIN32
606bada272Sdrh __declspec(dllexport)
616bada272Sdrh #endif
sqlite3_remember_init(sqlite3 * db,char ** pzErrMsg,const sqlite3_api_routines * pApi)626bada272Sdrh int sqlite3_remember_init(
636bada272Sdrh   sqlite3 *db,
646bada272Sdrh   char **pzErrMsg,
656bada272Sdrh   const sqlite3_api_routines *pApi
666bada272Sdrh ){
676bada272Sdrh   int rc = SQLITE_OK;
686bada272Sdrh   SQLITE_EXTENSION_INIT2(pApi);
696bada272Sdrh   rc = sqlite3_create_function(db, "remember", 2, SQLITE_UTF8, 0,
706bada272Sdrh                                rememberFunc, 0, 0);
716bada272Sdrh   return rc;
726bada272Sdrh }
73