Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

vench/sqlcmp

Open more actions menu

Repository files navigation

SQL compare

sqlcmp is a Go package designed to facilitate the comparison of SQL queries by parsing them into tokens and allowing for the comparison of these tokens in parts. This functionality is particularly useful for applications that need to determine if two SQL queries are semantically equivalent, ignoring differences in formatting, whitespace, or the order of clauses.

The package provides a function SemiHash that takes a SQL query and a mask specifying which parts of the query to consider for comparison. The mask can include segments such as WHERE, FROM, and SkipValues, which allows for a flexible comparison of queries. For example, two queries with the same WHERE and FROM clauses but in different orders, or with different values in the WHERE clause, can be considered equivalent if the mask is set to compare these segments.

Examples

Comparing two SQL queries by FROM and WHERE sections:

import ( 
"fmt"
"github.com/vench/sqlcmp" 
)

var (
    query1 = "SELECT * FROM phones WHERE  date > '2023-09-27' AND user_id = 1004;"
    query2 = "SELECT * FROM phones WHERE  user_id = 1004 AND date > '2023-09-27';"
    mask = sqlcmp.SegmentWhere|sqlcmp.SegmentFrom|sqlcmp.SegmentSkipValues
)

hash1, _ := sqlcmp.SemiHash(query1, mask)
hash2, _ := sqlcmp.SemiHash(query2, mask)

fmt.Println(hash1 == hash2) // print true

You can find more examples in the directory examples.

TODO list

About

This package can parse SQL query and compare

Resources

Stars

Watchers

Forks

Packages

No packages published
Morty Proxy This is a proxified and sanitized view of the page, visit original site.