Commit b42dddc6 by Selah Lynch

tweaked to handle table names wihtout aliases

1 parent adfc8de4
......@@ -34,7 +34,7 @@ def extract_tables(sql):
if state == "afterFROM":
assert type(chunk) == sqlparse.sql.Identifier
tables.append(chunk.value)
tables.append(chunk)
cursor += 1;
if cursor >= len(tksf):
state = "done"
......@@ -47,7 +47,7 @@ def extract_tables(sql):
assert chunk.value in ["INNER JOIN", "LEFT OUTER JOIN"]
cursor += 1; chunk = tksf[cursor]
assert type(chunk) == sqlparse.sql.Identifier
tables.append(chunk.value)
tables.append(chunk)
cursor += 1; chunk = tksf[cursor]
assert chunk.value == 'ON'
cursor += 1; chunk = tksf[cursor]
......@@ -59,8 +59,11 @@ def extract_tables(sql):
state = "afterFROM"
tables2 = []
for table in tables:
(tname, talias) = table.split(' ')
tables2.append({'name':tname,'alias':talias})
tsplit = table.value.split(' ')
if len(tsplit) == 2:
tables2.append({'name':tsplit[0],'alias':tsplit[1]})
else:
tables2.append({'name':tsplit[0]})
return tables2
......
......@@ -18,12 +18,11 @@ class TestStuff(unittest.TestCase):
self.assertEqual(tksf[2].value, 'FROM')
#TODO - fix this to handle tables with or without aliases
# def test_extract_table(self):
# sql = "SELECT rc.dateCooked FROM recipeCooked"
# tables = sts.extract_tables(sql)
# self.assertEqual(len(tables), 1)
# self.assertEqual(tables[0]['name'], 'recipeCooked')
def test_extract_table(self):
sql = "SELECT rc.dateCooked FROM recipeCooked"
tables = sts.extract_tables(sql)
self.assertEqual(len(tables), 1)
self.assertEqual(tables[0]['name'], 'recipeCooked')
def test_extract_tables(self):
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!