Commit b42dddc6 by Selah Lynch

tweaked to handle table names wihtout aliases

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