mysql2sqlite
mysql2sqlite copied to clipboard
detect empty input file and issue a warning
You have the code comment
# FIXME detect empty input file and issue a warning
one approach is set a flag in the BEGIN reset it in any section before the END check that it was toggled in the END section and warn otherwise
example:
BEGIN{ empty_file = 1}
{ empty_file = 0 }
END{
if(empty_file)
print "EMPTY FILE"
else
print "GOT STUFF"
}
results in:
$ ./empty_file_report.awk empty_file_report.awk
GOT STUFF
$ ./empty_file_report.awk /dev/null
EMPTY FILE
$ touch empty
$ ./empty_file_report.awk empty
EMPTY FILE
A patch to consider
diff --git a/mysql2sqlite b/mysql2sqlite
index e739a28..66dfc49 100755
--- a/mysql2sqlite
+++ b/mysql2sqlite
@@ -50,6 +50,7 @@ BEGIN {
print "PRAGMA synchronous = OFF;"
print "PRAGMA journal_mode = MEMORY;"
print "BEGIN TRANSACTION;"
+ empty_file = 1
}
# historically 3 spaces separate non-argument local variables
@@ -98,7 +99,7 @@ inTrigger != 0 { print; next }
inView != 0 { next }
# skip comments
-/^\/\*/ { next }
+/^\/\*/ { empty_file = 0; next }
# skip PARTITION statements
/^ *[(]?(PARTITION|partition) +[^ ]+/ { next }
@@ -149,6 +150,7 @@ inView != 0 { next }
if( match( $0, /`[^`]+/ ) ){
tableName = substr( $0, RSTART+1, RLENGTH-1 )
}
+ empty_file = 0
aInc = 0
prev = ""
firstInTable = 1
@@ -263,6 +265,10 @@ aInc == 1 && /PRIMARY KEY|primary key/ { next }
END {
if( no_END ){ exit 1}
+ if( empty_file ){
+ printerr( "EMPTY FILE: " ARGV[1] )
+ exit 1
+ }
# print all KEY creation lines.
for( table in key ){ printf key[table] }
results in
$ ./mysql2sqlite /dev/null
PRAGMA synchronous = OFF;
PRAGMA journal_mode = MEMORY;
BEGIN TRANSACTION;
EMPTY FILE: /dev/null
Which may not as nice as detecting within BEGIN before printing your leading constants but it is simple and robust.
Thanks @TomConlin for bringing this up and for the proposal. I was rather thinking about writing my own print function which would support "deferred" printing of a specified string. I would use this function everywhere instead of the built in print directive and printf function. As the deferred string I would put the pragmas and transaction begin.
This way I would even not need to clutter the already overcluttered sections (yeah, one can notice awk is a predecessor of Perl :wink:).
What do you think?
Writing to a buffer and printing at the end is tried and true efficiency especially when you can append without copy and preallocate the space ... which I have never considered in awk.
Depending on the size of data and the machines resources in may be necessary to print as you go, but when I have memory to burn, I try to use it.
No, appending anything to any buffer is definitely not what I meant. I'll just postpone (defer) printing of the pragmas to the point when it'll be 100% sure we'll have also other content to print.
but still before the end, yes, where you choose to do that may also be the best place to flip the not empty file bit. which would be an improvement over repeatedly setting the flag to the same thing.