aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2007-01-29 15:50:05 +0000
committerdrh <drh@noemail.net>2007-01-29 15:50:05 +0000
commit63cf66f02ea088c7284c3fbfab879f48ceadc20a (patch)
tree21306843a35e4c59c3983208976508ad3d068b0c /src
parent5fecee1557ea4b41214df5bce3bc0e1436ad2de0 (diff)
downloadsqlite-63cf66f02ea088c7284c3fbfab879f48ceadc20a.tar.gz
sqlite-63cf66f02ea088c7284c3fbfab879f48ceadc20a.zip
Add the randomhex() function as a built-in. (CVS 3619)
FossilOrigin-Name: a6001589ab1349f7a6b4af941e9e0fd73d13c1c0
Diffstat (limited to 'src')
-rw-r--r--src/func.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/func.c b/src/func.c
index 11a6e1ad1..2a803b38f 100644
--- a/src/func.c
+++ b/src/func.c
@@ -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>
@@ -273,6 +273,33 @@ static void randomFunc(
}
/*
+** 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 },