Skip to content

Instantly share code, notes, and snippets.

@ivanjr0
Created October 13, 2017 01:15
Show Gist options
  • Save ivanjr0/02892cf61efd532597f9d93382f72804 to your computer and use it in GitHub Desktop.
Save ivanjr0/02892cf61efd532597f9d93382f72804 to your computer and use it in GitHub Desktop.
import re
import shlex
from glob import glob
from subprocess import Popen, PIPE
r = []
for f in glob('questao*.sql'):
n = int(f[len('questao'):-len('.sql')])
r.append((n, f))
r = sorted(r)
for n, f in r:
p = Popen(shlex.split('mysql -u root --database=IMDB'), stdin=PIPE, stdout=PIPE, stderr=PIPE)
sql = open(f).readlines()
sql = ' '.join([l.strip() for l in sql if l.strip() and not l.strip().startswith('--')])
sql = re.sub('\s+', ' ', sql)
sql = sql.replace('( ', '(')
sql = sql.replace(' )', ')')
out, _ = p.communicate(sql)
header, body = out.split('\n', 1)
columns = header.count('\t') + 1
rows = body.count('\n')
print('#%d | %d | %d | %s' % (n, rows, columns, sql))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment