/ Check-in [a6001589]
Login

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

Overview
Comment:Add the randomhex() function as a built-in. (CVS 3619)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a6001589ab1349f7a6b4af941e9e0fd73d13c1c0
User & Date: drh 2007-01-29 15:50:06
Context
2007-01-29
17:58
Replace the randomHex() function with separate functions randomBlob() and hex(). (CVS 3620) check-in: f5ad74a9 user: drh tags: trunk
15:50
Add the randomhex() function as a built-in. (CVS 3619) check-in: a6001589 user: drh tags: trunk
2007-01-28
21:42
Implement the platform specific part of the shared library interface on OS/2 (CVS 3618) check-in: 027251a6 user: pweilbacher 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.134 2006/09/16 21:45:14 drh Exp $
           19  +** $Id: func.c,v 1.135 2007/01/29 15:50:06 drh Exp $
    20     20   */
    21     21   #include "sqliteInt.h"
    22     22   #include <ctype.h>
    23     23   /* #include <math.h> */
    24     24   #include <stdlib.h>
    25     25   #include <assert.h>
    26     26   #include "vdbeInt.h"
................................................................................
   267    267   ){
   268    268     sqlite_int64 r;
   269    269     sqlite3Randomness(sizeof(r), &r);
   270    270     if( (r<<1)==0 ) r = 0;  /* Prevent 0x8000.... as the result so that we */
   271    271                             /* can always do abs() of the result */
   272    272     sqlite3_result_int64(context, r);
   273    273   }
          274  +
          275  +/*
          276  +** Implementation of randomhex(N).  Return a random hexadecimal string
          277  +** that is N characters long.
          278  +*/
          279  +static void randomHex(
          280  +  sqlite3_context *context,
          281  +  int argc,
          282  +  sqlite3_value **argv
          283  +){
          284  +  int n, i, j;
          285  +  unsigned char c, zBuf[1001];
          286  +  assert( argc==1 );
          287  +  n = sqlite3_value_int(argv[0]);
          288  +  if( n&1 ) n++;
          289  +  if( n<2 ) n = 2;
          290  +  if( n>sizeof(zBuf)-1 )  n = sizeof(zBuf)-1;
          291  +  sqlite3Randomness(n/2, zBuf);
          292  +  for(i=n-1, j=n/2-1; i>=1; i-=2, j--){
          293  +    static const char zDigits[] = "0123456789ABCDEF";
          294  +    c = zBuf[j];
          295  +    zBuf[i] = zDigits[c&0xf];
          296  +    zBuf[i-1] = zDigits[c>>4];
          297  +  }
          298  +  zBuf[n] = 0;
          299  +  sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
          300  +}
   274    301   
   275    302   /*
   276    303   ** Implementation of the last_insert_rowid() SQL function.  The return
   277    304   ** value is the same as the sqlite3_last_insert_rowid() API function.
   278    305   */
   279    306   static void last_insert_rowid(
   280    307     sqlite3_context *context, 
................................................................................
  1020   1047       { "upper",              1, 0, SQLITE_UTF8,    0, upperFunc  },
  1021   1048       { "lower",              1, 0, SQLITE_UTF8,    0, lowerFunc  },
  1022   1049       { "coalesce",          -1, 0, SQLITE_UTF8,    0, ifnullFunc },
  1023   1050       { "coalesce",           0, 0, SQLITE_UTF8,    0, 0          },
  1024   1051       { "coalesce",           1, 0, SQLITE_UTF8,    0, 0          },
  1025   1052       { "ifnull",             2, 0, SQLITE_UTF8,    1, ifnullFunc },
  1026   1053       { "random",            -1, 0, SQLITE_UTF8,    0, randomFunc },
         1054  +    { "randomhex",          1, 0, SQLITE_UTF8,    0, randomHex  },
  1027   1055       { "nullif",             2, 0, SQLITE_UTF8,    1, nullifFunc },
  1028   1056       { "sqlite_version",     0, 0, SQLITE_UTF8,    0, versionFunc},
  1029   1057       { "quote",              1, 0, SQLITE_UTF8,    0, quoteFunc  },
  1030   1058       { "last_insert_rowid",  0, 1, SQLITE_UTF8,    0, last_insert_rowid },
  1031   1059       { "changes",            0, 1, SQLITE_UTF8,    0, changes    },
  1032   1060       { "total_changes",      0, 1, SQLITE_UTF8,    0, total_changes },
  1033   1061   #ifdef SQLITE_SOUNDEX

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.55 2006/09/16 21:45:14 drh Exp $
           14  +# $Id: func.test,v 1.56 2007/01/29 15:50:06 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 {
................................................................................
   292    292   # How do you test the random() function in a meaningful, deterministic way?
   293    293   #
   294    294   do_test func-9.1 {
   295    295     execsql {
   296    296       SELECT random() is not null;
   297    297     }
   298    298   } {1}
          299  +do_test func-9.2 {
          300  +  execsql {
          301  +    SELECT typeof(random());
          302  +  }
          303  +} {integer}
          304  +do_test func-9.3 {
          305  +  execsql {
          306  +    SELECT randomhex(32) is not null;
          307  +  }
          308  +} {1}
          309  +do_test func-9.4 {
          310  +  execsql {
          311  +    SELECT typeof(randomhex(32));
          312  +  }
          313  +} {text}
          314  +do_test func-9.5 {
          315  +  execsql {
          316  +    SELECT length(randomhex(32)), length(randomhex(-5)),
          317  +           length(randomhex(2000)), length(randomhex(31));
          318  +  }
          319  +} {32 2 1000 32}
          320  +
   299    321   
   300    322   # Use the "sqlite_register_test_function" TCL command which is part of
   301    323   # the text fixture in order to verify correct operation of some of
   302    324   # the user-defined SQL function APIs that are not used by the built-in
   303    325   # functions.
   304    326   #
   305    327   set ::DB [sqlite3_connection_pointer db]

Changes to www/lang.tcl.

     1      1   #
     2      2   # Run this Tcl script to generate the lang-*.html files.
     3      3   #
     4         -set rcsid {$Id: lang.tcl,v 1.118 2006/09/23 20:46:23 drh Exp $}
            4  +set rcsid {$Id: lang.tcl,v 1.119 2007/01/29 15:50:06 drh Exp $}
     5      5   source common.tcl
     6      6   
     7      7   if {[llength $argv]>0} {
     8      8     set outputdir [lindex $argv 0]
     9      9   } else {
    10     10     set outputdir ""
    11     11   }
................................................................................
  1369   1369   
  1370   1370   <tr>
  1371   1371   <td valign="top" align="right">random(*)</td>
  1372   1372   <td valign="top">Return a pseudo-random integer
  1373   1373   between -9223372036854775808 and +9223372036854775807.</td>
  1374   1374   </tr>
  1375   1375   
         1376  +<tr>
         1377  +<td valign="top" align="right">randomhex(<i>N</i>)</td>
         1378  +<td valign="top">Return a pseudo-random hexadecimal string that is
         1379  +<i>N</i> characters in length.  <i>N</i> should be an even integer between
         1380  +2 and 1000.  The intended use of this function is to generate
         1381  +universally unique identifiers (UUID).  For that purpose, it is recommended
         1382  +that <i>N</i> be at least 32.</td>
         1383  +</tr>
         1384  +
  1376   1385   <tr>
  1377   1386   <td valign="top" align="right">round(<i>X</i>)<br>round(<i>X</i>,<i>Y</i>)</td>
  1378   1387   <td valign="top">Round off the number <i>X</i> to <i>Y</i> digits to the
  1379   1388   right of the decimal point.  If the <i>Y</i> argument is omitted, 0 is 
  1380   1389   assumed.</td>
  1381   1390   </tr>
  1382   1391