Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
cicTeam
/
Clarity_Tools_Selah
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit b42dddc6
authored
Oct 24, 2021
by
Selah Lynch
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
tweaked to handle table names wihtout aliases
1 parent
adfc8de4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
10 deletions
sql_to_schema.py
sql_to_schema_test.py
sql_to_schema.py
View file @
b42dddc
...
@@ -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
...
...
sql_to_schema_test.py
View file @
b42dddc
...
@@ -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
):
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment