From 458c4bfa4f2a0e17206e0f8daaffef43d19a7f88 Mon Sep 17 00:00:00 2001 From: drh Date: Mon, 16 Sep 2013 11:16:48 +0000 Subject: [PATCH] Multiple calls to julianday('now') and similar date functions within the same statement return exactly the same time. FossilOrigin-Name: 54ea93575db0c0af02b8a075d09a8a42f7416735 --- manifest | 26 +++++++++++++++----------- manifest.uuid | 2 +- src/date.c | 8 ++++---- src/sqliteInt.h | 1 + src/vdbeInt.h | 1 + src/vdbeapi.c | 17 +++++++++++++++++ test/date.test | 19 +++++++++++++++++++ 7 files changed, 58 insertions(+), 16 deletions(-) diff --git a/manifest b/manifest index ff008ce7d2..6afb99ee8d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sWindows\sSDK\scompiler\swarning. -D 2013-09-13T23:27:39.040 +C Multiple\scalls\sto\sjulianday('now')\sand\ssimilar\sdate\sfunctions\swithin\sthe\nsame\sstatement\sreturn\sexactly\sthe\ssame\stime. +D 2013-09-16T11:16:48.723 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -170,7 +170,7 @@ F src/build.c f63e8929c7f89c0074fbc74929bc946ea117b2f8 F src/callback.c f99a8957ba2adf369645fac0db09ad8adcf1caa2 F src/complete.c dc1d136c0feee03c2f7550bafc0d29075e36deac F src/ctime.c ea4b7f3623a0fcb1146e7f245d7410033e86859c -F src/date.c 067a81c9942c497aafd2c260e13add8a7d0c7dd4 +F src/date.c 65196e95e69f36993659bd7781abe7c2f1994739 F src/delete.c 2dc64ca360b7d7da481183ea920a813a0c203c97 F src/expr.c 1017f482217e093ecdaca91e8666681e83733252 F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb @@ -221,7 +221,7 @@ F src/shell.c d920a891ca09b8bd262cced7fb0ab9d723f7a747 F src/sqlite.h.in ec40aa958a270416fb04b4f72210357bf163d2c5 F src/sqlite3.rc fea433eb0a59f4c9393c8e6d76a6e2596b1fe0c0 F src/sqlite3ext.h 886f5a34de171002ad46fae8c36a7d8051c190fc -F src/sqliteInt.h 896034293a7e52e2d4a6edff61a797f36d43c0a9 +F src/sqliteInt.h 18c7f80e7e23098942436f7286e9c93adc6908be F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d F src/status.c 7ac05a5c7017d0b9f0b4bcd701228b784f987158 F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e @@ -279,8 +279,8 @@ F src/util.c 7f3e35432d6888d1e770c488c35bd98970c44eec F src/vacuum.c d9c5759f4c5a438bb43c2086f72c5d2edabc36c8 F src/vdbe.c a393a94c0d77b86c7c4ad2cfb43ec4ba278d9596 F src/vdbe.h 4f554b5627f26710c4c36d919110a3fc611ca5c4 -F src/vdbeInt.h cbe71b8b36d8b3bba5709cc3f436c7e3b47b7b08 -F src/vdbeapi.c 96b24b946cf21894f63d9393e821baa2f0a80979 +F src/vdbeInt.h ff57f67aee1ba26a3a47e786533dab155ab6dad6 +F src/vdbeapi.c f807df9023116a6778d7ed37664bda7aa7cad47f F src/vdbeaux.c 88beca92f2ed0bbe2c6f87946d0e999a6807ea1b F src/vdbeblob.c 5dc79627775bd9a9b494dd956e26297946417d69 F src/vdbemem.c 817ce21ab4ca57f902619bb8fef3f8a51bbd0ed8 @@ -413,7 +413,7 @@ F test/crashtest1.c 09c1c7d728ccf4feb9e481671e29dda5669bbcc2 F test/createtab.test b5de160630b209c4b8925bdcbbaf48cc90b67fe8 F test/cse.test 277350a26264495e86b1785f34d2d0c8600e021c F test/ctime.test 7bd009071e242aac4f18521581536b652b789a47 -F test/date.test f3228180c87bbe5d39c9397bf001c0095c3821b9 +F test/date.test 502ddcbaaac9ce103bcd76d8e9d9bc8aa04e61b0 F test/dbstatus.test aee30c3f337e6c217ff06df59fb8fe6e6448dce2 F test/dbstatus2.test 10418e62b3db5dca070f0c3eef3ea13946f339c2 F test/default.test 6faf23ccb300114924353007795aa9a8ec0aa9dc @@ -1114,7 +1114,11 @@ F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381 F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff -P 650b32825a85baff11b8e568649fd797066c8ab1 -R 86c4f81df3e0f8f912478820f6baf7a0 -U mistachkin -Z 2b97a046c7bb8da4da56f5c1f9954d1c +P d5fc3f1dabc7227230dcabdd808357f63432357b +R b9d415e4a1daf98d36c31faffc151456 +T *bgcolor * #fec084 +T *branch * private +T *sym-private * +T -sym-trunk * +U drh +Z 2caef263f0942731f88232278f291420 diff --git a/manifest.uuid b/manifest.uuid index ca910973a5..47774df710 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d5fc3f1dabc7227230dcabdd808357f63432357b \ No newline at end of file +54ea93575db0c0af02b8a075d09a8a42f7416735 \ No newline at end of file diff --git a/src/date.c b/src/date.c index 758dd7c89b..5fe3f67867 100644 --- a/src/date.c +++ b/src/date.c @@ -294,8 +294,8 @@ static int parseYyyyMmDd(const char *zDate, DateTime *p){ ** Return the number of errors. */ static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){ - sqlite3 *db = sqlite3_context_db_handle(context); - if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){ + p->iJD = sqlite3StmtCurrentTime(context); + if( p->iJD>0 ){ p->validJD = 1; return 0; }else{ @@ -1078,8 +1078,8 @@ static void currentTimeFunc( UNUSED_PARAMETER(argc); UNUSED_PARAMETER(argv); - db = sqlite3_context_db_handle(context); - if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return; + iT = sqlite3StmtCurrentTime(context); + if( iT<=0 ) return; t = iT/1000 - 10000*(sqlite3_int64)21086676; #ifdef HAVE_GMTIME_R pTm = gmtime_r(&t, &sNow); diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 8d01b902f1..4a52b64f62 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -3184,6 +3184,7 @@ int sqlite3VtabCallDestroy(sqlite3*, int, const char *); int sqlite3VtabBegin(sqlite3 *, VTable *); FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*); void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**); +sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*); int sqlite3VdbeParameterIndex(Vdbe*, const char*, int); int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *); int sqlite3Reprepare(Vdbe*); diff --git a/src/vdbeInt.h b/src/vdbeInt.h index a699c414b0..3cac04016c 100644 --- a/src/vdbeInt.h +++ b/src/vdbeInt.h @@ -350,6 +350,7 @@ struct Vdbe { #ifndef SQLITE_OMIT_TRACE i64 startTime; /* Time when query started - used for profiling */ #endif + i64 iCurrentTime; /* Value of julianday('now') for this statement */ i64 nFkConstraint; /* Number of imm. FK constraints this VM */ i64 nStmtDefCons; /* Number of def. constraints when stmt started */ i64 nStmtDefImmCons; /* Number of def. imm constraints when stmt started */ diff --git a/src/vdbeapi.c b/src/vdbeapi.c index 52c6b2a797..9895d28399 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -510,6 +510,7 @@ int sqlite3_step(sqlite3_stmt *pStmt){ return rc; } + /* ** Extract the user data from a sqlite3_context structure and return a ** pointer to it. @@ -534,6 +535,22 @@ sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){ return p->s.db; } +/* +** Return the current time for a statement +*/ +sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){ + Vdbe *v = p->pVdbe; + int rc; + if( v->iCurrentTime==0 ){ + rc = sqlite3OsCurrentTimeInt64(p->s.db->pVfs, &v->iCurrentTime); + if( rc ){ + v->iCurrentTime = 0; + return rc; + } + } + return v->iCurrentTime; +} + /* ** The following is the implementation of an SQL function that always ** fails with an error message stating that the function is used in the diff --git a/test/date.test b/test/date.test index a30402c0d6..895b415436 100644 --- a/test/date.test +++ b/test/date.test @@ -528,4 +528,23 @@ if {0==[sqlite3 -has-codec]} { } {1} } } + +# Verify that multiple calls to date functions with 'now' return the +# same answer. +# +proc sleeper {} {after 100} +do_test date-15.1 { + db func sleeper sleeper + db eval { + SELECT c - a FROM (SELECT julianday('now') AS a, + sleeper(), julianday('now') AS c); + } +} {0.0} +do_test date-15.2 { + db eval { + SELECT a==b FROM (SELECT current_timestamp AS a, + sleeper(), current_timestamp AS b); + } +} {1} + finish_test -- 2.39.5