/ Check-in [6782c87b]
Login

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

Overview
Comment:Experimental implementation of the sqlite3_system_errno() interface.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sqlite_system_errno
Files: files | file ages | folders
SHA1:6782c87b3722fbd09684a5b1e5df05247956f1c6
User & Date: drh 2016-03-17 16:01:23
Context
2016-03-17
17:23
Simple test cases for sqlite3_system_errno() check-in: 185bc864 user: drh tags: sqlite_system_errno
16:01
Experimental implementation of the sqlite3_system_errno() interface. check-in: 6782c87b user: drh tags: sqlite_system_errno
2016-03-16
21:29
The prepared statements for some pragmas can now be reused without invoking an automatic reprepare. check-in: 97b0e88c user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/loadext.c.

   410    410     sqlite3_bind_zeroblob64,
   411    411     /* Version 3.9.0 and later */
   412    412     sqlite3_value_subtype,
   413    413     sqlite3_result_subtype,
   414    414     /* Version 3.10.0 and later */
   415    415     sqlite3_status64,
   416    416     sqlite3_strlike,
   417         -  sqlite3_db_cacheflush
          417  +  sqlite3_db_cacheflush,
          418  +  /* Version 3.12.0 and later */
          419  +  sqlite3_system_errno
   418    420   };
   419    421   
   420    422   /*
   421    423   ** Attempt to load an SQLite extension library contained in the file
   422    424   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   423    425   ** default entry point name (sqlite3_extension_init) is used.  Use
   424    426   ** of the default name is recommended.

Changes to src/main.c.

  2236   2236       return SQLITE_MISUSE_BKPT;
  2237   2237     }
  2238   2238     if( !db || db->mallocFailed ){
  2239   2239       return SQLITE_NOMEM_BKPT;
  2240   2240     }
  2241   2241     return db->errCode;
  2242   2242   }
         2243  +int sqlite3_system_errno(sqlite3 *db){
         2244  +  return db ? db->iSysErrno : 0;
         2245  +}  
  2243   2246   
  2244   2247   /*
  2245   2248   ** Return a string that describes the kind of error specified in the
  2246   2249   ** argument.  For now, this simply calls the internal sqlite3ErrStr()
  2247   2250   ** function.
  2248   2251   */
  2249   2252   const char *sqlite3_errstr(int rc){
................................................................................
  2861   2864     /* Open the backend database driver */
  2862   2865     rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
  2863   2866                           flags | SQLITE_OPEN_MAIN_DB);
  2864   2867     if( rc!=SQLITE_OK ){
  2865   2868       if( rc==SQLITE_IOERR_NOMEM ){
  2866   2869         rc = SQLITE_NOMEM_BKPT;
  2867   2870       }
         2871  +    sqlite3SystemError(db, rc);
  2868   2872       sqlite3Error(db, rc);
  2869   2873       goto opendb_out;
  2870   2874     }
  2871   2875     sqlite3BtreeEnter(db->aDb[0].pBt);
  2872   2876     db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
  2873   2877     if( !db->mallocFailed ) ENC(db) = SCHEMA_ENC(db);
  2874   2878     sqlite3BtreeLeave(db->aDb[0].pBt);

Changes to src/os.c.

   258    258   #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   259    259   int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
   260    260     return pVfs->xRandomness(pVfs, nByte, zBufOut);
   261    261   }
   262    262   int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
   263    263     return pVfs->xSleep(pVfs, nMicro);
   264    264   }
          265  +int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
          266  +  return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
          267  +}
   265    268   int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
   266    269     int rc;
   267    270     /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
   268    271     ** method to get the current date and time if that method is available
   269    272     ** (if iVersion is 2 or greater and the function pointer is not NULL) and
   270    273     ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
   271    274     ** unavailable.

Changes to src/os.h.

   193    193   void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
   194    194   void sqlite3OsDlError(sqlite3_vfs *, int, char *);
   195    195   void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
   196    196   void sqlite3OsDlClose(sqlite3_vfs *, void *);
   197    197   #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   198    198   int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
   199    199   int sqlite3OsSleep(sqlite3_vfs *, int);
          200  +int sqlite3OsGetLastError(sqlite3_vfs*);
   200    201   int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
   201    202   
   202    203   /*
   203    204   ** Convenience functions for opening and closing files using 
   204    205   ** sqlite3_malloc() to obtain space for the file-handle structure.
   205    206   */
   206    207   int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
   207    208   int sqlite3OsCloseFree(sqlite3_file *);
   208    209   
   209    210   #endif /* _SQLITE_OS_H_ */

Changes to src/os_unix.c.

  6260   6260     *prNow = i/86400000.0;
  6261   6261     return rc;
  6262   6262   }
  6263   6263   #else
  6264   6264   # define unixCurrentTime 0
  6265   6265   #endif
  6266   6266   
  6267         -#ifndef SQLITE_OMIT_DEPRECATED
  6268   6267   /*
  6269         -** We added the xGetLastError() method with the intention of providing
  6270         -** better low-level error messages when operating-system problems come up
  6271         -** during SQLite operation.  But so far, none of that has been implemented
  6272         -** in the core.  So this routine is never called.  For now, it is merely
  6273         -** a place-holder.
         6268  +** The xGetLastError() method is designed to return a better
         6269  +** low-level error message when operating-system problems come up
         6270  +** during SQLite operation.  Only the integer return code is currently
         6271  +** used.
  6274   6272   */
  6275   6273   static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
  6276   6274     UNUSED_PARAMETER(NotUsed);
  6277   6275     UNUSED_PARAMETER(NotUsed2);
  6278   6276     UNUSED_PARAMETER(NotUsed3);
  6279         -  return 0;
         6277  +  return errno;
  6280   6278   }
  6281         -#else
  6282         -# define unixGetLastError 0
  6283         -#endif
  6284   6279   
  6285   6280   
  6286   6281   /*
  6287   6282   ************************ End of sqlite3_vfs methods ***************************
  6288   6283   ******************************************************************************/
  6289   6284   
  6290   6285   /******************************************************************************

Changes to src/sqlite.h.in.

  7911   7911   ** ^Otherwise, if no error occurs, [sqlite3_db_cacheflush()] returns SQLITE_OK.
  7912   7912   **
  7913   7913   ** ^This function does not set the database handle error code or message
  7914   7914   ** returned by the [sqlite3_errcode()] and [sqlite3_errmsg()] functions.
  7915   7915   */
  7916   7916   int sqlite3_db_cacheflush(sqlite3*);
  7917   7917   
         7918  +/*
         7919  +** CAPI3REF: Low-level system error code
         7920  +**
         7921  +** ^Attempt to return the underlying operating system error code or error
         7922  +** number that caused the most reason I/O error or failure to open a file.
         7923  +** The return value is OS-dependent.  For example, on unix systems, after
         7924  +** [sqlite3_open_v2()] returns [SQLITE_CANTOPEN], this interface could be
         7925  +** called to get back the underlying "errno" that caused the problem, such
         7926  +** as ENOSPC, EAUTH, EISDIR, and so forth.  
         7927  +*/
         7928  +int sqlite3_system_errno(sqlite3*);
         7929  +
  7918   7930   /*
  7919   7931   ** CAPI3REF: Database Snapshot
  7920   7932   ** KEYWORDS: {snapshot}
  7921   7933   ** EXPERIMENTAL
  7922   7934   **
  7923   7935   ** An instance of the snapshot object records the state of a [WAL mode]
  7924   7936   ** database for some specific point in history.

Changes to src/sqlite3ext.h.

   275    275     /* Version 3.9.0 and later */
   276    276     unsigned int (*value_subtype)(sqlite3_value*);
   277    277     void (*result_subtype)(sqlite3_context*,unsigned int);
   278    278     /* Version 3.10.0 and later */
   279    279     int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
   280    280     int (*strlike)(const char*,const char*,unsigned int);
   281    281     int (*db_cacheflush)(sqlite3*);
          282  +  /* Version 3.12.0 and later */
          283  +  int (*system_errno)(sqlite3*);
   282    284   };
   283    285   
   284    286   /*
   285    287   ** The following macros redefine the API routines so that they are
   286    288   ** redirected through the global sqlite3_api structure.
   287    289   **
   288    290   ** This header file is also used by the loadext.c source file
................................................................................
   518    520   /* Version 3.9.0 and later */
   519    521   #define sqlite3_value_subtype          sqlite3_api->value_subtype
   520    522   #define sqlite3_result_subtype         sqlite3_api->result_subtype
   521    523   /* Version 3.10.0 and later */
   522    524   #define sqlite3_status64               sqlite3_api->status64
   523    525   #define sqlite3_strlike                sqlite3_api->strlike
   524    526   #define sqlite3_db_cacheflush          sqlite3_api->db_cacheflush
          527  +/* Version 3.12.0 and later */
          528  +#define sqlite3_system_errno           sqlite3_api->system_errno
   525    529   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
   526    530   
   527    531   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
   528    532     /* This case when the file really is being compiled as a loadable 
   529    533     ** extension */
   530    534   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
   531    535   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;

Changes to src/sqliteInt.h.

  1216   1216     int nDb;                      /* Number of backends currently in use */
  1217   1217     int flags;                    /* Miscellaneous flags. See below */
  1218   1218     i64 lastRowid;                /* ROWID of most recent insert (see above) */
  1219   1219     i64 szMmap;                   /* Default mmap_size setting */
  1220   1220     unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
  1221   1221     int errCode;                  /* Most recent error code (SQLITE_*) */
  1222   1222     int errMask;                  /* & result codes with this before returning */
         1223  +  int iSysErrno;                /* Errno value from last system error */
  1223   1224     u16 dbOptFlags;               /* Flags to enable/disable optimizations */
  1224   1225     u8 enc;                       /* Text encoding */
  1225   1226     u8 autoCommit;                /* The auto-commit flag. */
  1226   1227     u8 temp_store;                /* 1: file 2: memory 0: default */
  1227   1228     u8 mallocFailed;              /* True if we have seen a malloc failure */
  1228   1229     u8 bBenignMalloc;             /* Do not require OOMs if true */
  1229   1230     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
................................................................................
  3759   3760   char sqlite3CompareAffinity(Expr *pExpr, char aff2);
  3760   3761   int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
  3761   3762   char sqlite3ExprAffinity(Expr *pExpr);
  3762   3763   int sqlite3Atoi64(const char*, i64*, int, u8);
  3763   3764   int sqlite3DecOrHexToI64(const char*, i64*);
  3764   3765   void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
  3765   3766   void sqlite3Error(sqlite3*,int);
         3767  +void sqlite3SystemError(sqlite3*,int);
  3766   3768   void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
  3767   3769   u8 sqlite3HexToInt(int h);
  3768   3770   int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
  3769   3771   
  3770   3772   #if defined(SQLITE_NEED_ERR_NAME)
  3771   3773   const char *sqlite3ErrName(int);
  3772   3774   #endif

Changes to src/util.c.

   121    121   ** Set the current error code to err_code and clear any prior error message.
   122    122   */
   123    123   void sqlite3Error(sqlite3 *db, int err_code){
   124    124     assert( db!=0 );
   125    125     db->errCode = err_code;
   126    126     if( db->pErr ) sqlite3ValueSetNull(db->pErr);
   127    127   }
          128  +
          129  +/*
          130  +** Load the sqlite3.iSysErrno field if that is an appropriate thing
          131  +** to do based on the SQLite error code in rc.
          132  +*/
          133  +void sqlite3SystemError(sqlite3 *db, int rc){
          134  +  if( rc==SQLITE_IOERR_NOMEM ) return;
          135  +  rc &= 0xff;
          136  +  if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
          137  +    db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
          138  +  }
          139  +}
   128    140   
   129    141   /*
   130    142   ** Set the most recent error code and error string for the sqlite
   131    143   ** handle "db". The error code is set to "err_code".
   132    144   **
   133    145   ** If it is not NULL, string zFormat specifies the format of the
   134    146   ** error string in the style of the printf functions: The following