From effbc266f60f8ee82236151030b8eb69dc1cbd2c Mon Sep 17 00:00:00 2001 From: Haard K Shah Date: Thu, 23 Sep 2021 23:04:44 -0400 Subject: [PATCH 1/2] add headers --- mysqldump_to_csv.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/mysqldump_to_csv.py b/mysqldump_to_csv.py index b49cfe7..92d15c8 100644 --- a/mysqldump_to_csv.py +++ b/mysqldump_to_csv.py @@ -34,6 +34,30 @@ def values_sanity_check(values): # Assertions have not been raised return True +curr_is_header = False +next_is_header = False +def is_header(line): + global curr_is_header + global next_is_header + if line.startswith('CREATE TABLE'): + curr_is_header = False + next_is_header = True + elif 'ENGINE=' in line: + curr_is_header = False + next_is_header = False + elif next_is_header: + curr_is_header = True + next_is_header = True + + return curr_is_header + +def get_header(line): + return line.strip().split('`')[1] + +def parse_headers(headers: list, outfile): + + writer = csv.writer(outfile, quoting=csv.QUOTE_MINIMAL) + writer.writerow(headers) def parse_values(values, outfile): """ @@ -101,9 +125,18 @@ def main(): # listed in sys.argv[1:] # or stdin if no args given. try: + in_header = False + headers = [] for line in fileinput.input(): # Look for an INSERT statement and parse it. - if is_insert(line): + if is_header(line): + in_header = True + header = get_header(line) + headers.append(header) + elif in_header: # current isn't header but last was (so write to stdout now) + parse_headers(headers, sys.stdout) + in_header = False + elif is_insert(line): values = get_values(line) if values_sanity_check(values): parse_values(values, sys.stdout) From 760a07c586682a2a51c2301111382d0c8df3d7e2 Mon Sep 17 00:00:00 2001 From: Haard K Shah Date: Thu, 23 Sep 2021 23:47:31 -0400 Subject: [PATCH 2/2] ignores KEY keyword when adding column names --- mysqldump_to_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysqldump_to_csv.py b/mysqldump_to_csv.py index 92d15c8..79e4eef 100644 --- a/mysqldump_to_csv.py +++ b/mysqldump_to_csv.py @@ -42,7 +42,7 @@ def is_header(line): if line.startswith('CREATE TABLE'): curr_is_header = False next_is_header = True - elif 'ENGINE=' in line: + elif 'ENGINE=' in line or 'KEY' in line: curr_is_header = False next_is_header = False elif next_is_header: