Diff
Not logged in

Differences From:

File src/db.c part of check-in [732d7c406e] - Update to the latest version of SQLite with improved sqlite3_trace() support. This makes the output of --sqltrace much more helpful. by drh on 2009-11-25 22:14:11. [view]

To:

File src/db.c part of check-in [f41358e7ca] - Ported user() and cgi() sql functions over from cvstrac by btheado on 2009-11-29 03:46:01. [view]

@@ -723,8 +723,43 @@
   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){
@@ -1160,8 +1195,9 @@
       sqlite3_trace(g.db, db_sql_trace, 0);
     }
     once = 0;
   }
+  db_add_functions();
 }
 
 /*
 ** Return true if the string zVal represents "true" (or "false").