DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Is Podman a Drop-In Replacement for Docker?
  • Effective Java Collection Framework: Best Practices and Tips
  • Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
  • Writing a Vector Database in a Week in Rust

Trending

  • Is Podman a Drop-In Replacement for Docker?
  • Effective Java Collection Framework: Best Practices and Tips
  • Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
  • Writing a Vector Database in a Week in Rust
  1. DZone
  2. Data Engineering
  3. Databases
  4. Using the Mozilla SQL Parser — Part 2

Using the Mozilla SQL Parser — Part 2

Bipin Patwardhan user avatar by
Bipin Patwardhan
·
Oct. 13, 20 · Tutorial
Like (2)
Save
Tweet
Share
5.92K Views

Join the DZone community and get the full member experience.

Join For Free

In the Using the Mozilla SQL Parser — Part 1 article on Mozilla SQL Parser, we covered how the moz-sql-parser package can be used to parse an SQL Select statement into a JSONified structure. In this part, we will cover how to extract names of the tables mentioned in the 'from' clause of the query.

The SQL we are using is

SQL
 




xxxxxxxxxx
1


 
1
SELECT s_name from student where s_id in
2
(select s_id from student_course where c_id in
3
(select c_id from course where c_name = 'DSA'
4
and c_name = 'dbms' or c_name = 'algorithm'));



To parse this query, we use

parsed_query = parse(query)

where, parse is a method provided by the moz_sql_parser.

After parsing, the module generates a JSONified structure, as given below.

JSON
 




xxxxxxxxxx
1
25


 
1
{
2
  "select": { "value": "s_name" },
3
  "from": "student",
4
  "where": {
5
    "in": [ "s_id", {
6
      "select": { "value": "s_id" },
7
      "from": "student_course",
8
      "where": {
9
        "in": [ "c_id", {
10
          "select": { "value": "c_id" },
11
          "from": "course",
12
          "where": {
13
            "or": [ {
14
              "and": [
15
                { "eq": [ "c_name", { "literal": "DSA" } ] },
16
                { "eq": [ "c_name", { "literal": "dbms" } ] }
17
              ] },
18
              { "eq": [ "c_name", { "literal": "algorithm" } ] }
19
            ]
20
          }
21
        } ]
22
      }
23
    } ]
24
  }
25
}



After parsing, the JSONified structure contains three main keys, namely 'select', 'from' and 'where'.

To extract names of tables, we will extract them from the 'from' clause. For this, we define a method named 'get_from_clause_tables'. To hold the table names and the table name aliases, we define two dictionaries, named table_map and table_alias_map.

Python
 




xxxxxxxxxx
1


 
1
table_map = {}
2
table_alias_map = {}
3

          
4
if "from" in parsed_query:
5
    from_clause = parsed_query["from"]
6
    table_map, table_alias_map = get_from_clause_tables(from_clause, table_map, table_alias_map)



The 'get_from_clause_tables' method is

Python
 




xxxxxxxxxx
1
17


 
1
def get_from_clause_tables(from_clause, table_usage_map, table_alias_usage_map, table_alias_map):
2
  if isinstance(from_clause, list):
3
    print("from_clause is type list")
4
    for t in from_clause:
5
      if isinstance(t, str):
6
        value = t
7
        table_usage_map[value] = table_usage_map.get(value, 0) + 1
8
      else:
9
        value = t["value"]
10
        alias = t["name"]
11
        table_usage_map[value] = table_usage_map.get(value, 0) + 1
12
        table_alias_usage_map[alias] = table_alias_usage_map.get(alias, 0) + 1
13
        table_alias_map[alias] = value
14
  elif isinstance(from_clause, str):
15
    print("from_clause is type string")
16
    table_usage_map[from_clause] = table_usage_map.get(from_clause, 0) + 1
17
  return table_usage_map, table_alias_usage_map, table_alias_map



In the method, we are checking the data type of the from clause. We need to check the instance type of the from_clause variable, due to the way the data table names are stored in the JSONified structure.

  • Type string -- If the from_clause has only one table, its data type is string and the 'from_clause' is nothing but the name of the table mentioned in the from clause of the query.
  • Type list -- If the from_clause has multiple tables, its data type is list (of type string) or list (of type dictionary), that contains names of the tables mentioned in the from clause. If the table has an alias, the entry is of type dictionary. If the table does not have an alias, the entry is of type string.

In the 'get_from_clause_tables' method, we are collecting the names of the tables used and their usage frequency. We are also collecting the alias names of the table and their usage frequency.

Upon execution, we get the output as

Python
 




xxxxxxxxxx
1


 
1
table_usage_map - {'student': 1, 'course': 1}
2
table_alias_usage_map - {'c': 1}
3
table_alias_map - {'c': 'course'}



In this part, we covered how to extract names of tables given in the from clause. In the next part (part three), we will cover how to extract tables mentioned in the where clause.

sql Database Parser (programming language) Data Types

Opinions expressed by DZone contributors are their own.

Trending

  • Is Podman a Drop-In Replacement for Docker?
  • Effective Java Collection Framework: Best Practices and Tips
  • Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
  • Writing a Vector Database in a Week in Rust

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: