]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
The new --script option to dbtotxt.c looks for an SQL script at the head of
authordrh <>
Mon, 15 Aug 2022 18:36:08 +0000 (18:36 +0000)
committerdrh <>
Mon, 15 Aug 2022 18:36:08 +0000 (18:36 +0000)
the file and generates a CLI script that will construct the database then run
the SQL.

FossilOrigin-Name: 6170e638ebeb12cc40c3247324237978401f701afc270de88ad03e183e82cefc

manifest
manifest.uuid
tool/dbtotxt.c

index e0b62ac483756f081b24d0f9cb581715ef1c41f2..ff9202b070f4da60f72281d631337a54c734918b 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C In\sthe\sdocumentation\semphasize\sthat\sthe\suse\sof\sshared\scache\sis\sdiscouraged.\nFix\stest\scases\sso\sthat\sthey\sall\swork\swith\sshared\scache\sdisabled.
-D 2022-08-15T12:26:26.113
+C The\snew\s--script\soption\sto\sdbtotxt.c\slooks\sfor\san\sSQL\sscript\sat\sthe\shead\sof\nthe\sfile\sand\sgenerates\sa\sCLI\sscript\sthat\swill\sconstruct\sthe\sdatabase\sthen\srun\nthe\sSQL.
+D 2022-08-15T18:36:08.339
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -1904,7 +1904,7 @@ F tool/build-shell.sh 950f47c6174f1eea171319438b93ba67ff5bf367
 F tool/cg_anno.tcl c1f875f5a4c9caca3d59937b16aff716f8b1883935f1b4c9ae23124705bc8099 x
 F tool/checkSpacing.c 810e51703529a204fc4e1eb060e9ab663e3c06d2
 F tool/dbhash.c 5da0c61032d23d74f2ab84ffc5740f0e8abec94f2c45c0b4306be7eb3ae96df0
-F tool/dbtotxt.c 2620a354d109b70d71bf758d2a64e042d74e0aaa5bd6e447900ed3542410d7f9
+F tool/dbtotxt.c ca48d34eaca6d6b6e4bd6a7be2b72caf34475869054240244c60fa7e69a518d6
 F tool/dbtotxt.md c9a57af8739957ef36d2cfad5c4b1443ff3688ed33e4901ee200c8b651f43f3c
 F tool/enlargedb.c 3e8b2612b985cfa7e3e8800031ee191b43ae80de96abb5abbd5eada62651ee21
 F tool/extract-sqlite3h.tcl 069ceab0cee26cba99952bfa08c0b23e35941c837acabe143f0c355d96c9e2eb x
@@ -1999,8 +1999,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 49828bdec5f926cd18a069d39a5db0b1e1f3528a2affcfbaa1cf7b98aca51b3b
-R 138b56807af43017f46d2fa433baa3f1
+P 52c3ae063e133c5f88cad2c3084cf46218f865ac817ab937e3529d2c6a7af300
+R e96f02106a09b713c72886888af2923e
 U drh
-Z 713a84bc1904cad175917d5abd266782
+Z 30df68295258a2292c6be3adf651edab
 # Remove this line to create a well-formed Fossil manifest.
index fc125e1c0d245fd7dff3ba7e425aeb718afebf2e..3f4443f6bfbbaa2c9dba34b6428cf6c963c60a51 100644 (file)
@@ -1 +1 @@
-52c3ae063e133c5f88cad2c3084cf46218f865ac817ab937e3529d2c6a7af300
\ No newline at end of file
+6170e638ebeb12cc40c3247324237978401f701afc270de88ad03e183e82cefc
\ No newline at end of file
index a56416dcc429b9906469af6512e0246d9a78a46f..fbd6e3d5195171a510ce4cb187b6f924b7334235 100644 (file)
 **         dbtotxt [OPTIONS] FILENAME
 **
 ** where OPTIONS are zero or more of:
-**    --pagesize N  => setting the database page size for later readin
-**    --for-cli     => prepending '.open --hexdb' to the output
+**
+**    --for-cli          prepending '.open --hexdb' to the output
+**
+**    --script           The input file is expected to start with a
+**                       zero-terminated SQL string.  Output the
+**                       ".open --hexdb" header, then the database
+**                       then the SQL.
+**
+**    --pagesize N       set the database page size for later reading
 **
 ** The translation of the database appears on standard output.  If the
 ** --pagesize command-line option is omitted, then the page size is taken
@@ -43,17 +50,20 @@ static int allZero(unsigned char *aLine){
 int main(int argc, char **argv){
   int pgsz = 0;               /* page size */
   int forCli = 0;             /* whether to prepend with .open */
+  int bSQL = 0;               /* Expect and SQL prefix */
   long szFile;                /* Size of the input file in bytes */
   FILE *in;                   /* Input file */
+  int nSQL;                   /* Number of bytes of script */
   int i, j;                   /* Loop counters */
   int nErr = 0;               /* Number of errors */
   const char *zInputFile = 0; /* Name of the input file */
   const char *zBaseName = 0;  /* Base name of the file */
   int lastPage = 0;           /* Last page number shown */
   int iPage;                  /* Current page number */
-  unsigned char aLine[16];    /* A single line of the file */
-  unsigned char aHdr[100];    /* File header */
-  unsigned char bShow[256];      /* Characters ok to display */
+  unsigned char *aData = 0;   /* All data */
+  unsigned char *aLine;       /* A single line of the file */
+  unsigned char *aHdr;        /* File header */
+  unsigned char bShow[256];   /* Characters ok to display */
   memset(bShow, '.', sizeof(bShow));
   for(i=' '; i<='~'; i++){
     if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i;
@@ -75,6 +85,10 @@ int main(int argc, char **argv){
       }else if( strcmp(z,"for-cli")==0 ){
         forCli = 1;
         continue;
+      }else if( strcmp(z,"script")==0 ){
+        forCli = 1;
+        bSQL = 1;
+        continue;
       }
       fprintf(stderr, "Unknown option: %s\n", argv[i]);
       nErr++;
@@ -90,7 +104,8 @@ int main(int argc, char **argv){
     nErr++;
   }
   if( nErr ){
-    fprintf(stderr, "Usage: %s [--pagesize N] FILENAME\n", argv[0]);
+    fprintf(stderr, 
+       "Usage: %s [--pagesize N] [--script] [--for-cli] FILENAME\n", argv[0]);
     exit(1);
   }
   in = fopen(zInputFile, "rb");
@@ -105,11 +120,32 @@ int main(int argc, char **argv){
     fprintf(stderr, "File too short. Minimum size is 100 bytes.\n");
     exit(1);
   }
-  if( fread(aHdr, 100, 1, in)!=1 ){
-    fprintf(stderr, "Cannot read file header\n");
+  aData = malloc( szFile+16 );
+  if( aData==0 ){
+    fprintf(stderr, "Failed to allocate %ld bytes\n", szFile);
     exit(1);
   }
-  rewind(in);
+  if( fread(aData, szFile, 1, in)!=1 ){
+    fprintf(stderr, "Cannot read file info memory\n");
+    exit(1);
+  }
+  memset(aData+szFile, 0, 16);
+  fclose(in);
+  if( bSQL ){
+    for(i=0; i<szFile && aData[i]!=0; i++){}
+    if( i==szFile ){
+      fprintf(stderr, "No zero terminator on SQL script\n");
+      exit(1);
+    }
+    nSQL = i+1;
+    if( szFile - nSQL<100 ){
+      fprintf(stderr, "Less than 100 bytes in the database\n");
+      exit(1);
+    }
+  }else{
+    nSQL = 0;
+  }
+  aHdr = aData + nSQL;
   if( pgsz==0 ){
     pgsz = (aHdr[16]<<8) | aHdr[17];
     if( pgsz==1 ) pgsz = 65536;
@@ -126,16 +162,8 @@ int main(int argc, char **argv){
     printf(".open --hexdb\n");
   }
   printf("| size %d pagesize %d filename %s\n",(int)szFile,pgsz,zBaseName);
-  for(i=0; i<szFile; i+=16){
-    int got = (int)fread(aLine, 1, 16, in);
-    if( got!=16 ){
-      static int once = 1;
-      if( once ){
-        fprintf(stderr, "Could not read input file starting at byte %d\n",
-                         i+got);
-      }
-      memset(aLine+got, 0, 16-got);
-    }
+  for(i=nSQL; i<szFile; i+=16){
+    aLine = aData+i;
     if( allZero(aLine) ) continue;
     iPage = i/pgsz + 1;
     if( lastPage!=iPage ){
@@ -151,7 +179,10 @@ int main(int argc, char **argv){
     }
     fputc('\n', stdout);
   }
-  fclose(in);
   printf("| end %s\n", zBaseName);
+  if( nSQL>0 ){
+    printf("%s\n", aData);
+  }
+  free( aData );
   return 0;
 }