/ Check-in [f81b9c1c]
Login

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

Overview
Comment:Have "DEFAULT CURRENT_TIME" & co. work even if SQLITE_OMIT_DATETIME_FUNCS is defined. (CVS 2083)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f81b9c1c022772378aad32ec45d0027beeb36574
User & Date: danielk1977 2004-11-09 16:13:33
Context
2004-11-10
05:48
Add user documentation for the "pragma auto_vacuum" command. (CVS 2084) check-in: fe200eaf user: danielk1977 tags: trunk
2004-11-09
16:13
Have "DEFAULT CURRENT_TIME" & co. work even if SQLITE_OMIT_DATETIME_FUNCS is defined. (CVS 2083) check-in: f81b9c1c user: danielk1977 tags: trunk
12:44
Port the "DEFAULT CURRENT_TIME" etc. functionality from an earlier fork of sqlite. (CVS 2082) check-in: 0d27c8ff user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/date.c.

    12     12   ** This file contains the C functions that implement date and time
    13     13   ** functions for SQLite.  
    14     14   **
    15     15   ** There is only one exported symbol in this file - the function
    16     16   ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
    17     17   ** All other code has file scope.
    18     18   **
    19         -** $Id: date.c,v 1.38 2004/11/09 12:44:38 danielk1977 Exp $
           19  +** $Id: date.c,v 1.39 2004/11/09 16:13:33 danielk1977 Exp $
    20     20   **
    21     21   ** NOTES:
    22     22   **
    23     23   ** SQLite processes all times and dates as Julian Day numbers.  The
    24     24   ** dates and times are stored as the number of days since noon
    25     25   ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
    26     26   ** calendar system.
................................................................................
   913    913       sqlite3ValueSetStr(pVal, -1, "now", SQLITE_UTF8, SQLITE_STATIC);
   914    914       datetimeFunc(context, 1, &pVal);
   915    915       sqlite3ValueFree(pVal);
   916    916     }
   917    917   }
   918    918   #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
   919    919   
          920  +#ifdef SQLITE_OMIT_DATETIME_FUNCS
          921  +/*
          922  +** If the library is compiled to omit the full-scale date and time
          923  +** handling (to get a smaller binary), the following minimal version
          924  +** of the functions current_time(), current_date() and current_timestamp()
          925  +** are included instead. This is to support column declarations that
          926  +** include "DEFAULT CURRENT_TIME" etc.
          927  +**
          928  +** This function uses the C-library functions time(), localtime_r()
          929  +** and strftime(). The format string to pass to strftime() is supplied
          930  +** as the user-data for the function.
          931  +*/
          932  +
          933  +static void currentTimeFunc(
          934  +  sqlite3_context *context,
          935  +  int argc,
          936  +  sqlite3_value **argv
          937  +){
          938  +  time_t t;
          939  +  char *zFormat = (char *)sqlite3_user_data(context);
          940  +  char zBuf[20];
          941  +  struct tm now;
          942  +
          943  +#ifdef SQLITE_TEST
          944  +  /* This test variable is located in os_XXX.c */
          945  +extern int sqlite3_current_time;
          946  +#endif
          947  +  time(&t);
          948  +#ifdef SQLITE_TEST
          949  +  if( sqlite3_current_time ){
          950  +    t = sqlite3_current_time;
          951  +  }
          952  +#endif
          953  +  localtime_r(&t, &now);
          954  +  strftime(zBuf, 20, zFormat, &now);
          955  +  sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
          956  +}
          957  +#endif
          958  +
   920    959   /*
   921    960   ** This function registered all of the above C functions as SQL
   922    961   ** functions.  This should be the only routine in this file with
   923    962   ** external linkage.
   924    963   */
   925    964   void sqlite3RegisterDateTimeFunctions(sqlite3 *db){
   926    965   #ifndef SQLITE_OMIT_DATETIME_FUNCS
................................................................................
   940    979     };
   941    980     int i;
   942    981   
   943    982     for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
   944    983       sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
   945    984           SQLITE_UTF8, 0, aFuncs[i].xFunc, 0, 0);
   946    985     }
          986  +#else
          987  +  static const struct {
          988  +     char *zName;
          989  +     char *zFormat;
          990  +  } aFuncs[] = {
          991  +    { "current_time", "%H:%M:%S" },
          992  +    { "current_date", "%Y-%m-%d" },
          993  +    { "current_timestamp", "%Y-%m-%d %H:%M:%S" }
          994  +  };
          995  +  int i;
          996  +
          997  +  for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
          998  +    sqlite3_create_function(db, aFuncs[i].zName, 0, SQLITE_UTF8, 
          999  +        aFuncs[i].zFormat, currentTimeFunc, 0, 0);
         1000  +  }
   947   1001   #endif
   948   1002   }
   949   1003   

Changes to src/parse.y.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains SQLite's grammar for SQL.  Process this file
    13     13   ** using the lemon parser generator to generate C code that runs
    14     14   ** the parser.  Lemon will also generate a header file containing
    15     15   ** numeric codes for all of the tokens.
    16     16   **
    17         -** @(#) $Id: parse.y,v 1.151 2004/11/09 12:44:38 danielk1977 Exp $
           17  +** @(#) $Id: parse.y,v 1.152 2004/11/09 16:13:33 danielk1977 Exp $
    18     18   */
    19     19   %token_prefix TK_
    20     20   %token_type {Token}
    21     21   %default_type {Token}
    22     22   %extra_argument {Parse *pParse}
    23     23   %syntax_error {
    24     24     if( pParse->zErrMsg==0 ){
................................................................................
   573    573   
   574    574   %type expr {Expr*}
   575    575   %destructor expr {sqlite3ExprDelete($$);}
   576    576   %type term {Expr*}
   577    577   %destructor term {sqlite3ExprDelete($$);}
   578    578   
   579    579   expr(A) ::= term(X).             {A = X;}
   580         -term(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
          580  +expr(A) ::= LP(B) expr(X) RP(E). {A = X; sqlite3ExprSpan(A,&B,&E); }
   581    581   term(A) ::= NULL(X).             {A = sqlite3Expr(@X, 0, 0, &X);}
   582    582   expr(A) ::= ID(X).               {A = sqlite3Expr(TK_ID, 0, 0, &X);}
   583    583   expr(A) ::= JOIN_KW(X).          {A = sqlite3Expr(TK_ID, 0, 0, &X);}
   584    584   expr(A) ::= nm(X) DOT nm(Y). {
   585    585     Expr *temp1 = sqlite3Expr(TK_ID, 0, 0, &X);
   586    586     Expr *temp2 = sqlite3Expr(TK_ID, 0, 0, &Y);
   587    587     A = sqlite3Expr(TK_DOT, temp1, temp2, 0);

Changes to test/table.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 the CREATE TABLE statement.
    13     13   #
    14         -# $Id: table.test,v 1.31 2004/11/09 12:44:39 danielk1977 Exp $
           14  +# $Id: table.test,v 1.32 2004/11/09 16:13:33 danielk1977 Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   
    19     19   # Create a basic table and verify it is added to sqlite_master
    20     20   #
    21     21   do_test table-1.1 {
................................................................................
   542    542   foreach {date time} {
   543    543     1976-07-04 12:00:00
   544    544     1994-04-16 14:00:00
   545    545     2000-01-01 00:00:00
   546    546     2003-12-31 12:34:56
   547    547   } {
   548    548     incr i
   549         -  set sqlite_current_time [execsql "SELECT strftime('%s','$date $time')"]
          549  +#  set sqlite_current_time [execsql "SELECT strftime('%s','$date $time')"]
          550  +  set sqlite_current_time [clock scan "$date $time"]
   550    551     do_test table-13.2.$i {
   551    552       execsql "
   552    553         INSERT INTO tablet8(a) VALUES($i);
   553    554         SELECT tm, dt, dttm FROM tablet8 WHERE a=$i;
   554    555       "
   555    556     } [list $time $date [list $date $time]]
   556    557   }
   557    558   set sqlite_current_time 0
   558    559   
   559    560   finish_test