/ Check-in [74c5ace4]
Login

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

Overview
Comment:Infrastructure for an extension C-library to implement sqlite3_db_dump() and a corresponding "dbdump" command-line utility - both of which do the same work as the ".dump" command of the CLI.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dbdump
Files: files | file ages | folders
SHA3-256: 74c5ace498f72d7f5495203678bedd0bc540211131a4e4db7b62115d5322a288
User & Date: drh 2017-03-13 19:26:34
Context
2017-03-13
21:26
First draft of the complete dbdump.c library. check-in: 84ea4fcc user: drh tags: dbdump
19:26
Infrastructure for an extension C-library to implement sqlite3_db_dump() and a corresponding "dbdump" command-line utility - both of which do the same work as the ".dump" command of the CLI. check-in: 74c5ace4 user: drh tags: dbdump
18:24
In the output of the ".dump" command in the CLI, quote newline and carriage-return characters using the char() function, so that they do not get eaten by end-of-line processing logic in the OS or in other command-line utilities and/or libraries. check-in: 68f6dc7a user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

  1150   1150   	echo "static const char *zMainloop = " >> $@
  1151   1151   	$(TCLSH_CMD) $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@
  1152   1152   	echo "; return zMainloop; }" >> $@
  1153   1153   
  1154   1154   sqlite3_analyzer$(TEXE): sqlite3_analyzer.c
  1155   1155   	$(LTLINK) sqlite3_analyzer.c -o $@ $(LIBTCL) $(TLIBS)
  1156   1156   
         1157  +dbdump$(TEXE): $(TOP)/ext/misc/dbdump.c sqlite3.lo
         1158  +	$(LTLINK) -DDBDUMP_STANDALONE -o $@ \
         1159  +           $(TOP)/ext/misc/dbdump.c sqlite3.lo $(TLIBS)
         1160  +
  1157   1161   showdb$(TEXE):	$(TOP)/tool/showdb.c sqlite3.lo
  1158   1162   	$(LTLINK) -o $@ $(TOP)/tool/showdb.c sqlite3.lo $(TLIBS)
  1159   1163   
  1160   1164   showstat4$(TEXE):	$(TOP)/tool/showstat4.c sqlite3.lo
  1161   1165   	$(LTLINK) -o $@ $(TOP)/tool/showstat4.c sqlite3.lo $(TLIBS)
  1162   1166   
  1163   1167   showjournal$(TEXE):	$(TOP)/tool/showjournal.c sqlite3.lo

Added ext/misc/dbdump.c.

            1  +/*
            2  +** 2016-03-13
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file implements a C-language subroutine that converts the content
           14  +** of an SQLite database into UTF-8 text SQL statements that can be used
           15  +** to exactly recreate the original database.
           16  +**
           17  +** A prototype of the implemented subroutine is this:
           18  +**
           19  +**   int sqlite3_db_dump(
           20  +**          sqlite3 *db,
           21  +**          const char *zSchema,
           22  +**          const char *zTable,
           23  +**          void (*xCallback)(void*, const char*),
           24  +**          void *pArg
           25  +**   );
           26  +**
           27  +** The db parameter is the database connection.  zSchema is the schema within
           28  +** that database which is to be dumped.  Usually the zSchema is "main" but
           29  +** can also be "temp" or any ATTACH-ed database.  If zTable is not NULL, then
           30  +** only the content of that one table is dumped.  If zTable is NULL, then all
           31  +** tables are dumped.
           32  +**
           33  +** The generate text is passed to xCallback() in multiple calls.  The second
           34  +** argument to xCallback() is a copy of the pArg parameter.  The first
           35  +** argument is some of the output text that this routine generates.  The
           36  +** signature to xCallback() is designed to make it compatible with fputs().
           37  +**
           38  +** The sqlite3_db_dump() subroutine returns SQLITE_OK on success or some error
           39  +** code if it encounters a problem.
           40  +**
           41  +** If this file is compiled with -DDBDUMP_STANDALONE then a "main()" routine
           42  +** is included so that this routine becomes a command-line utility.  The
           43  +** command-line utility takes two or three arguments which are the name
           44  +** of the database file, the schema, and optionally the table, forming the
           45  +** first three arguments of a single call to the library routine.
           46  +*/
           47  +#include "sqlite3.h"
           48  +
           49  +/*
           50  +** Convert an SQLite database into SQL statements that will recreate that
           51  +** database.
           52  +*/
           53  +int sqlite3_db_dump(
           54  +  sqlite3 *db,               /* The database connection */
           55  +  const char *zSchema,       /* Which schema to dump.  Usually "main". */
           56  +  const char *zTable,        /* Which table to dump.  NULL means everything. */
           57  +  int (*xCallback)(const char*,void*),   /* Output sent to this callback */
           58  +  void *pArg                             /* Second argument of the callback */
           59  +){
           60  +  return SQLITE_OK;
           61  +}
           62  +
           63  +
           64  +
           65  +/* The generic subroutine is above.  The code the follows implements
           66  +** the command-line interface.
           67  +*/
           68  +#ifdef DBDUMP_STANDALONE
           69  +#include <stdio.h>
           70  +
           71  +/*
           72  +** Command-line interface
           73  +*/
           74  +int main(int argc, char **argv){
           75  +  sqlite3 *db;
           76  +  const char *zDb;
           77  +  const char *zSchema;
           78  +  const char *zTable = 0;
           79  +  int rc;
           80  +
           81  +  if( argc<2 || argc>4 ){
           82  +    fprintf(stderr, "Usage: %s DATABASE ?SCHEMA? ?TABLE?\n", argv[0]);
           83  +    return 1;
           84  +  }
           85  +  zDb = argv[1];
           86  +  zSchema = argc>=3 ? argv[2] : "main";
           87  +  zTable = argc==4 ? argv[3] : 0;
           88  +
           89  +  rc = sqlite3_open(zDb, &db);
           90  +  if( rc ){
           91  +    fprintf(stderr, "Cannot open \"%s\": %s\n", zDb, sqlite3_errmsg(db));
           92  +    sqlite3_close(db);
           93  +    return 1;
           94  +  }
           95  +  rc = sqlite3_db_dump(db, zSchema, zTable, 
           96  +          (int(*)(const char*,void*))fputs, (void*)stdout);
           97  +  if( rc ){
           98  +    fprintf(stderr, "Error: sqlite3_db_dump() returns %d\n", rc);
           99  +  }
          100  +  sqlite3_close(db);
          101  +  return rc!=SQLITE_OK;  
          102  +}
          103  +#endif /* DBDUMP_STANDALONE */

Changes to main.mk.

   757    757   	echo "static const char *zMainloop = " >> $@
   758    758   	tclsh $(TOP)/tool/tostr.tcl $(TOP)/tool/spaceanal.tcl >> $@
   759    759   	echo "; return zMainloop; }" >> $@
   760    760   
   761    761   sqlite3_analyzer$(EXE): sqlite3_analyzer.c
   762    762   	$(TCCX) $(TCL_FLAGS) sqlite3_analyzer.c -o $@ $(LIBTCL) $(THREADLIB) 
   763    763   
          764  +dbdump$(EXE):	$(TOP)/ext/misc/dbdump.c sqlite3.o
          765  +	$(TCCX) -DDBDUMP_STANDALONE -o dbdump$(EXE) \
          766  +            $(TOP)/ext/misc/dbdump.c sqlite3.o $(THREADLIB)
          767  +
   764    768   # Rules to build the 'testfixture' application.
   765    769   #
   766    770   TESTFIXTURE_FLAGS  = -DSQLITE_TEST=1 -DSQLITE_CRASH_TEST=1
   767    771   TESTFIXTURE_FLAGS += -DSQLITE_SERVER=1 -DSQLITE_PRIVATE="" -DSQLITE_CORE
   768    772   TESTFIXTURE_FLAGS += -DSQLITE_SERIES_CONSTRAINT_VERIFY=1
   769    773   TESTFIXTURE_FLAGS += -DSQLITE_DEFAULT_PAGE_SIZE=1024
   770    774