Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some minor tweaks to mysqldump_to_csv.py #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions mysqldump_to_csv.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
#!/usr/bin/env python
import fileinput

import csv
import fileinput
import sys

# This prevents prematurely closed pipes from raising
# an exception in Python
from signal import signal, SIGPIPE, SIG_DFL

signal(SIGPIPE, SIG_DFL)

# allow large content in the dump
csv.field_size_limit(sys.maxsize)


def is_insert(line):
"""
Returns true if the line begins a SQL insert statement.
"""
return line.startswith('INSERT INTO') or False
return line.startswith("INSERT INTO") or False


def get_values(line):
"""
Returns the portion of an INSERT statement containing values
"""
return line.partition('` VALUES ')[2]
return line.partition("` VALUES ")[2]


def values_sanity_check(values):
"""
Ensures that values from the INSERT statement meet basic checks.
"""
assert values
assert values[0] == '('
assert values[0].startswith("(")
# Assertions have not been raised
return True

Expand All @@ -42,27 +45,29 @@ def parse_values(values, outfile):
"""
latest_row = []

reader = csv.reader([values], delimiter=',',
doublequote=False,
escapechar='\\',
quotechar="'",
strict=True
reader = csv.reader(
[values],
delimiter=",",
doublequote=False,
escapechar="\\",
quotechar="'",
strict=True,
)

writer = csv.writer(outfile, quoting=csv.QUOTE_MINIMAL)
for reader_row in reader:
for column in reader_row:
# If our current string is empty...
if len(column) == 0 or column == 'NULL':
if not column or column == "NULL":
latest_row.append(chr(0))
continue
# If our string starts with an open paren
if column[0] == "(":
if column.startswith("("):
# Assume that this column does not begin
# a new row.
new_row = False
# If we've been filling out a row
if len(latest_row) > 0:
if latest_row:
# Check if the previous entry ended in
# a close paren. If so, the row we've
# been filling out has been COMPLETED
Expand All @@ -80,15 +85,15 @@ def parse_values(values, outfile):
latest_row = []
# If we're beginning a new row, eliminate the
# opening parentheses.
if len(latest_row) == 0:
if not latest_row:
column = column[1:]
# Add our column to the row we're working on.
latest_row.append(column)
# At the end of an INSERT statement, we'll
# have the semicolon.
# Make sure to remove the semicolon and
# the close paren.
if latest_row[-1][-2:] == ");":
if latest_row[-1].endswith(");"):
latest_row[-1] = latest_row[-1][:-2]
writer.writerow(latest_row)

Expand All @@ -110,5 +115,6 @@ def main():
except KeyboardInterrupt:
sys.exit(0)


if __name__ == "__main__":
main()