Check-in [f41358e7ca]
Not logged in
Overview

SHA1 Hash:f41358e7ca0edf6d2b6c4688e6ccf064cc278fa4
Date: 2009-11-29 03:46:01
User: btheado
Comment:Ported user() and cgi() sql functions over from cvstrac
Timelines: ancestors | descendants | both | sql-func
Other Links: files | ZIP archive | manifest

Tags And Properties
Changes
[hide diffs]

Modified src/db.c from [295793e5a5] to [afc733b37d].

@@ -722,10 +722,45 @@
   }
   g.configOpen = 1;
 }
 
 /*
+** Implement the user() SQL function.  user() takes no arguments and
+** returns the user ID of the current user.
+*/
+static void f_user(sqlite3_context *context, int argc, sqlite3_value **argv){
+  if( g.zLogin!=0 ) sqlite3_result_text(context, g.zLogin, -1, SQLITE_STATIC);
+}
+
+/*
+** Implement the cgi() SQL function.  cgi() takes a an argument which is
+** a name of CGI query parameter. The value of that parameter is returned,
+** if available. optional second argument will be returned if the first
+** doesn't exist as a CGI parameter.
+*/
+static void f_cgi(sqlite3_context *context, int argc, sqlite3_value **argv){
+  const char* zP;
+  if( argc!=1 && argc!=2 ) return;
+  zP = P((const char*)sqlite3_value_text(argv[0]));
+  if( zP ){
+    sqlite3_result_text(context, zP, -1, SQLITE_STATIC);
+  }else if( argc==2 ){
+    zP = (const char*)sqlite3_value_text(argv[1]);
+    if( zP ) sqlite3_result_text(context, zP, -1, SQLITE_TRANSIENT);
+  }
+}
+
+/*
+** This routine adds the extra SQL functions to the SQL engine.
+*/
+void db_add_functions(void){
+  sqlite3_create_function(g.db, "user", 0, SQLITE_ANY, 0, &f_user, 0, 0);
+  sqlite3_create_function(g.db, "cgi", 1, SQLITE_ANY, 0, &f_cgi, 0, 0);
+  sqlite3_create_function(g.db, "cgi", 2, SQLITE_ANY, 0, &f_cgi, 0, 0);
+}
+
+/*
 ** If zDbName is a valid local database file, open it and return
 ** true.  If it is not a valid local database file, return 0.
 */
 static int isValidLocalDb(const char *zDbName){
   i64 lsize;
@@ -1159,10 +1194,11 @@
     if( g.fSqlTrace ){
       sqlite3_trace(g.db, db_sql_trace, 0);
     }
     once = 0;
   }
+  db_add_functions();
 }
 
 /*
 ** Return true if the string zVal represents "true" (or "false").
 */