Skip to content

TableTrace is a lightweight HTTP API that extracts the table names involved in a given SQL query. It supports parsing queries and returning a JSON list of table names.

License

Notifications You must be signed in to change notification settings

rafafrdz/table-trace

Repository files navigation

Table Trace

TableTrace is a lightweight HTTP API that extracts the table names involved in a given SQL query. It supports parsing queries and returning a JSON list of table names. This is a Scala version of the Table Trace project written in Rust.

Getting Started

Follow these steps to set up and run your Table Trace api:

Cloning the Repository

First, clone this repository to your local machine:

git clone git@github.com:rafafrdz/table-trace.git
cd table-trace

Prerequisites

  • JDK 11+
  • SBT 1.8+
  • Scala 2.13

Running the Table Trace api locally

You can run the Table Trace api directly from SBT. Open a terminal in the project directory and execute:

sbt run-api

By default, the service runs on http://localhost:9876

API Usage

Endpoint

POST /analyze

Body: JSON with the field query containing the SQL string.

Response: JSON array with the extracted tables.

Examples

Succeeded Request

curl -X POST http://localhost:9876/analyze -H "Content-Type: application/json" -d '{
"query": "UPDATE wine w SET stock = stock - (SELECT SUM(quantity) FROM order WHERE date = CURRENT_DATE AND order.wine_name = w.name) WHERE w.name IN (SELECT order.wine_name FROM order WHERE date = CURRENT_DATE)"
}'

Succeeded Response

{
  "tables": [
    "wine",
    "order"
  ]
} 

Failed Request

curl -X POST http://localhost:9876/analyze -H "Content-Type: application/json" -d '{
"query": "UPDATE wine w WHERE w.name IN (SELECT order.wine_name FROM order WHERE date = CURRENT_DATE)"
}'

Failed Response

Error processing the query `UPDATE wine w WHERE w.name IN (SELECT order.wine_name FROM order WHERE date = CURRENT_DATE)`

Building the project

Prepare your project for distribution or deployment by building it with SBT:

  1. Navigate to the project directory:
cd table-trace
  1. Run the assembly:
sbt clean api/assembly

This command creates a JAR file in the api/target/scala-2.13/ directory. For more details on configuring * sbt-assembly*, refer to the sbt-assembly documentation.

Running the Uber JAR

Once the JAR is built, you can run the API directly with:

java -jar api/target/scala-2.13/table-trace-api-0.0.1.jar

By default, the service runs on http://localhost:9876

License

This project is available under your choice of the Apache 2.0 or CC0 1.0 license. Choose the one that best suits your needs:

This template is provided "as-is" without any warranties. Modify and distribute as needed to fit your project requirements.

About

TableTrace is a lightweight HTTP API that extracts the table names involved in a given SQL query. It supports parsing queries and returning a JSON list of table names.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages