/ Check-in [fb1048cb]
Login

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

Overview
Comment:Add the readfile(FILENAME) and writefile(FILENAME,CONTENT) SQL functions to the command-line shell.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: fb1048cb2b613a0dbfe625a5df05e9dcd736a433
User & Date: drh 2014-07-24 12:39:59
Context
2014-07-24
22:41
Fix a bug in the whereRangeSkipScanEst() procedure (added by check-in [d09ca6d5efad3e4cfa]) where it fails to consider the possibility of a ROWID column when computing the affinity of a table column. check-in: 6aea2258 user: drh tags: trunk
22:13
Merge updates from trunk. check-in: 8dc0cdf6 user: mistachkin tags: asciiMode
16:54
Merge all recent trunk changes into the threads branch. check-in: 77068589 user: drh tags: threads
16:43
Merge recent trunk changes into the apple-osx branch. check-in: 3810dc62 user: drh tags: apple-osx
16:23
Merge recent trunk changes into the sessions branch. check-in: a9db017e user: drh tags: sessions
12:39
Add the readfile(FILENAME) and writefile(FILENAME,CONTENT) SQL functions to the command-line shell. check-in: fb1048cb user: drh tags: trunk
12:19
Add support for hexadecimal integer literals in the parser. check-in: f8f79f28 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.

  1646   1646     ".vfsname ?AUX?         Print the name of the VFS stack\n"
  1647   1647     ".width NUM1 NUM2 ...   Set column widths for \"column\" mode\n"
  1648   1648     "                         Negative values right-justify\n"
  1649   1649   ;
  1650   1650   
  1651   1651   /* Forward reference */
  1652   1652   static int process_input(struct callback_data *p, FILE *in);
         1653  +/*
         1654  +** Implementation of the "readfile(X)" SQL function.  The entire content
         1655  +** of the file named X is read and returned as a BLOB.  NULL is returned
         1656  +** if the file does not exist or is unreadable.
         1657  +*/
         1658  +static void readfileFunc(
         1659  +  sqlite3_context *context,
         1660  +  int argc,
         1661  +  sqlite3_value **argv
         1662  +){
         1663  +  const char *zName;
         1664  +  FILE *in;
         1665  +  long nIn;
         1666  +  void *pBuf;
         1667  +
         1668  +  zName = (const char*)sqlite3_value_text(argv[0]);
         1669  +  if( zName==0 ) return;
         1670  +  in = fopen(zName, "rb");
         1671  +  if( in==0 ) return;
         1672  +  fseek(in, 0, SEEK_END);
         1673  +  nIn = ftell(in);
         1674  +  rewind(in);
         1675  +  pBuf = sqlite3_malloc( nIn );
         1676  +  if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
         1677  +    sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
         1678  +  }else{
         1679  +    sqlite3_free(pBuf);
         1680  +  }
         1681  +  fclose(in);
         1682  +}
         1683  +
         1684  +/*
         1685  +** Implementation of the "writefile(X,Y)" SQL function.  The argument Y
         1686  +** is written into file X.  The number of bytes written is returned.  Or
         1687  +** NULL is returned if something goes wrong, such as being unable to open
         1688  +** file X for writing.
         1689  +*/
         1690  +static void writefileFunc(
         1691  +  sqlite3_context *context,
         1692  +  int argc,
         1693  +  sqlite3_value **argv
         1694  +){
         1695  +  FILE *out;
         1696  +  const char *z;
         1697  +  int n;
         1698  +  sqlite3_int64 rc;
         1699  +  const char *zFile;
         1700  +
         1701  +  zFile = (const char*)sqlite3_value_text(argv[0]);
         1702  +  if( zFile==0 ) return;
         1703  +  out = fopen(zFile, "wb");
         1704  +  if( out==0 ) return;
         1705  +  z = (const char*)sqlite3_value_blob(argv[1]);
         1706  +  if( z==0 ){
         1707  +    n = 0;
         1708  +    rc = 0;
         1709  +  }else{
         1710  +    n = sqlite3_value_bytes(argv[1]);
         1711  +    rc = fwrite(z, 1, n, out);
         1712  +  }
         1713  +  fclose(out);
         1714  +  sqlite3_result_int64(context, rc);
         1715  +}
  1653   1716   
  1654   1717   /*
  1655   1718   ** Make sure the database is open.  If it is not, then open it.  If
  1656   1719   ** the database fails to open, print an error message and exit.
  1657   1720   */
  1658   1721   static void open_db(struct callback_data *p, int keepAlive){
  1659   1722     if( p->db==0 ){
................................................................................
  1669   1732             p->zDbFilename, sqlite3_errmsg(db));
  1670   1733         if( keepAlive ) return;
  1671   1734         exit(1);
  1672   1735       }
  1673   1736   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  1674   1737       sqlite3_enable_load_extension(p->db, 1);
  1675   1738   #endif
         1739  +    sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
         1740  +                            readfileFunc, 0, 0);
         1741  +    sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
         1742  +                            writefileFunc, 0, 0);
  1676   1743     }
  1677   1744   }
  1678   1745   
  1679   1746   /*
  1680   1747   ** Do C-language style dequoting.
  1681   1748   **
  1682   1749   **    \t    -> tab