Add the randomhex() function as a built-in. (CVS 3619)

FossilOrigin-Name: a6001589ab1349f7a6b4af941e9e0fd73d13c1c0
This commit is contained in:
drh 2007-01-29 15:50:05 +00:00
parent 5fecee1557
commit 63cf66f02e
5 changed files with 72 additions and 13 deletions

View File

@ -1,5 +1,5 @@
C Implement\sthe\splatform\sspecific\spart\sof\sthe\sshared\slibrary\sinterface\son\sOS/2\s(CVS\s3618)
D 2007-01-28T21:42:08
C Add\sthe\srandomhex()\sfunction\sas\sa\sbuilt-in.\s(CVS\s3619)
D 2007-01-29T15:50:06
F Makefile.in 7fa74bf4359aa899da5586e394d17735f221315f
F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -66,7 +66,7 @@ F src/date.c 393c73fc027597e008dcd81454544659e978b05c
F src/delete.c 804384761144fe1a5035b99f4bd7d706976831bd
F src/experimental.c 1b2d1a6cd62ecc39610e97670332ca073c50792b
F src/expr.c 3d2cf15fd9aa6b60b67f45504782cce72cd07963
F src/func.c af537156dfeb91dbac8ffed4db6c4fa1a3164cc9
F src/func.c 5df5260055fc8225df7daa4a6776763c78839a6a
F src/hash.c 449f3d6620193aa557f5d86cbc5cc6b87702b185
F src/hash.h 1b3f7e2609141fd571f62199fc38687d262e9564
F src/insert.c e9526ced19978a55687b55faea969b6ff2a53fb4
@ -217,7 +217,7 @@ F test/fts2f.test b5f2dde48199d79e859f59d3d857c17dd62a0129
F test/fts2g.test c69a8ab43ec77d123976ba6cf9422d647ae63032
F test/fts2h.test 223af921323b409d4b5b18ff4e51619541b174bb
F test/fts2i.test 1b22451d1f13f7c509baec620dc3a4a754885dd6
F test/func.test 0ed54b5aeaad319f68016c033acfebef56f5874a
F test/func.test a2a780de5f32fcdda3db9421281f87c7cf5f1077
F test/hook.test 7e7645fd9a033f79cce8fdff151e32715e7ec50a
F test/in.test 369cb2aa1eab02296b4ec470732fe8c131260b1d
F test/index.test e65df12bed94b2903ee89987115e1578687e9266
@ -406,7 +406,7 @@ F www/fullscanb.gif f7c94cb227f060511f8909e10f570157263e9a25
F www/index-ex1-x-b.gif f9b1d85c3fa2435cf38b15970c7e3aa1edae23a3
F www/index.tcl 7f67d421e4c1f48083c178bee460a27220f4fcc1
F www/indirect1b1.gif adfca361d2df59e34f9c5cac52a670c2bfc303a1
F www/lang.tcl fb1070256c211a3dec735e1273c506c1f8db7556
F www/lang.tcl ccbd38d9597117841b130287a19534b14dbf8990
F www/lockingv3.tcl f59b19d6c8920a931f096699d6faaf61c05db55f
F www/mingw.tcl d96b451568c5d28545fefe0c80bee3431c73f69c
F www/nulls.tcl ec35193f92485b87b90a994a01d0171b58823fcf
@ -428,7 +428,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P ba76107cd1fc1898f5357b20b339727e2e034e23
R 79a8f84a406ccc28c1097bb566a6ba96
U pweilbacher
Z 7318492f59e9e286472c87b62bde92c0
P 027251a6fc9971b337172436137fabdafec1d264
R 26303d3b8e5ddcde80949e4541bf7afa
U drh
Z 353a878b84641f62bd1524ee3fc8f147

View File

@ -1 +1 @@
027251a6fc9971b337172436137fabdafec1d264
a6001589ab1349f7a6b4af941e9e0fd73d13c1c0

View File

@ -16,7 +16,7 @@
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.134 2006/09/16 21:45:14 drh Exp $
** $Id: func.c,v 1.135 2007/01/29 15:50:06 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@ -272,6 +272,33 @@ static void randomFunc(
sqlite3_result_int64(context, r);
}
/*
** Implementation of randomhex(N). Return a random hexadecimal string
** that is N characters long.
*/
static void randomHex(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
int n, i, j;
unsigned char c, zBuf[1001];
assert( argc==1 );
n = sqlite3_value_int(argv[0]);
if( n&1 ) n++;
if( n<2 ) n = 2;
if( n>sizeof(zBuf)-1 ) n = sizeof(zBuf)-1;
sqlite3Randomness(n/2, zBuf);
for(i=n-1, j=n/2-1; i>=1; i-=2, j--){
static const char zDigits[] = "0123456789ABCDEF";
c = zBuf[j];
zBuf[i] = zDigits[c&0xf];
zBuf[i-1] = zDigits[c>>4];
}
zBuf[n] = 0;
sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
}
/*
** Implementation of the last_insert_rowid() SQL function. The return
** value is the same as the sqlite3_last_insert_rowid() API function.
@ -1024,6 +1051,7 @@ void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
{ "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
{ "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
{ "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
{ "randomhex", 1, 0, SQLITE_UTF8, 0, randomHex },
{ "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
{ "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
{ "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },

View File

@ -11,7 +11,7 @@
# This file implements regression tests for SQLite library. The
# focus of this file is testing built-in functions.
#
# $Id: func.test,v 1.55 2006/09/16 21:45:14 drh Exp $
# $Id: func.test,v 1.56 2007/01/29 15:50:06 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -296,6 +296,28 @@ do_test func-9.1 {
SELECT random() is not null;
}
} {1}
do_test func-9.2 {
execsql {
SELECT typeof(random());
}
} {integer}
do_test func-9.3 {
execsql {
SELECT randomhex(32) is not null;
}
} {1}
do_test func-9.4 {
execsql {
SELECT typeof(randomhex(32));
}
} {text}
do_test func-9.5 {
execsql {
SELECT length(randomhex(32)), length(randomhex(-5)),
length(randomhex(2000)), length(randomhex(31));
}
} {32 2 1000 32}
# Use the "sqlite_register_test_function" TCL command which is part of
# the text fixture in order to verify correct operation of some of

View File

@ -1,7 +1,7 @@
#
# Run this Tcl script to generate the lang-*.html files.
#
set rcsid {$Id: lang.tcl,v 1.118 2006/09/23 20:46:23 drh Exp $}
set rcsid {$Id: lang.tcl,v 1.119 2007/01/29 15:50:06 drh Exp $}
source common.tcl
if {[llength $argv]>0} {
@ -1373,6 +1373,15 @@ is also useful when writing triggers to implement undo/redo functionality.
between -9223372036854775808 and +9223372036854775807.</td>
</tr>
<tr>
<td valign="top" align="right">randomhex(<i>N</i>)</td>
<td valign="top">Return a pseudo-random hexadecimal string that is
<i>N</i> characters in length. <i>N</i> should be an even integer between
2 and 1000. The intended use of this function is to generate
universally unique identifiers (UUID). For that purpose, it is recommended
that <i>N</i> be at least 32.</td>
</tr>
<tr>
<td valign="top" align="right">round(<i>X</i>)<br>round(<i>X</i>,<i>Y</i>)</td>
<td valign="top">Round off the number <i>X</i> to <i>Y</i> digits to the