Skip to content

a toolkit to get relation between tables from a statement for a schema

Notifications You must be signed in to change notification settings

shyandsy/SqlRelationParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SqlRelationParser

License: MIT Go codecov

SqlRelationParser is a open source component to parse relations from sql statement

Usage

get relations for a single sql statement

package main

import (
	"fmt"

	SqlRelationParser "github.com/shyandsy/SqlRelationParser"
)

func main() {
	query := "select person_id from address adr " +
		"inner join option_address_type opt_adt on opt_adt.option_id = ? and opt_adt.type_id = adr.type_id " +
		"and opt_adt.sequence_id = adr.sequence_id and opt_adt.sequence_id=1"

	parser := SqlRelationParser.NewSqlRelationParser()
	result, err := parser.ParseRelation(query)
	if err != nil {
		fmt.Println("failed to parse")
		return
	}
	fmt.Println("\n===============================")
	relations := result.GetRelations()
	fmt.Println("parse the relations success")
	for _, relation := range relations {
		fmt.Println(&relation)
	}
}

result

parse the relations success
option_address_type:type_id => address:type_id
option_address_type:sequence_id => address:sequence_id

get relations for a batch of sql statements

package main

import (
	"fmt"

	SqlRelationParser "github.com/shyandsy/SqlRelationParser"
)

func main() {
	query := []string{
		"SELECT b.id as bid, b.title, b.type, a.last_name AS author, t.last_name AS translator FROM books b " +
			"LEFT JOIN authors a ON b.author_id = a.id " +
			"LEFT JOIN translators t ON b.translator_id = t.id " +
			"ORDER BY b.id;",
		"select person_id from address adr " +
			"inner join option_address_type opt_adt on opt_adt.option_id = ? and opt_adt.type_id = adr.type_id " +
			"and opt_adt.sequence_id = adr.sequence_id and opt_adt.sequence_id=1",
	}

	parser := SqlRelationParser.NewSqlRelationParser()
	result, err := parser.ParseRelationFromBatchSql(query)
	if err != nil {
		fmt.Println("failed to parse")
		return
	}
	fmt.Println("\n===============================")
	relations := result.GetRelations()
	fmt.Println("parse the relations success")
	for _, relation := range relations {
		fmt.Println(&relation)
	}
}

result

Tables:
books:b
	books:id
	books:title
	books:type
	books:author_id
	books:translator_id
authors:a
	authors:last_name
	authors:id
translators:t
	translators:last_name
	translators:id
address:adr
	address:type_id
	address:sequence_id
option_address_type:opt_adt
	option_address_type:option_id
	option_address_type:type_id
	option_address_type:sequence_id
Relations:
books:author_id => authors:id
books:translator_id => translators:id
option_address_type:type_id => address:type_id
option_address_type:sequence_id => address:sequence_id

Installation

go get github.com/shyandsy/SqlRelationParser@latest

Examples

example can be found in the example folder for this package.

Support

Welcome for your Issue and PR

About

a toolkit to get relation between tables from a statement for a schema

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages