showsql

package module
v0.0.0-...-4c615e9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Show Sql

Description

showsql is a utility that analyzes Go code, extracts SQL queries built with github.com/Masterminds/squirrel, and prints them to stdout.

Installation

go install github.com/mrsubudei/showsql@latest

Usage

The utility must be run from the project's root directory.

showsql [OPTION]...

After launch, a navigation interface opens:

  1. Use the ↑ / ↓ keys to move through directories.
  2. Press Enter to select the file containing the queries.
  3. Select the method name.

Alternatively, run it with the -p={path to file} and -m={method name} flags:

showsql -p=internal/repo/users/store.go -m=GetByID [OPTION]...

How it works

The utility copies the files containing the queries into a temporary directory, cmd/showsql_tmp, generates a main.go entry point, runs it, and then deletes the temporary directory. To keep the temporary directory, run with the -k flag.


Only methods are supported; plain functions are not processed.


The fields of the repository struct are not populated, so if the code calls a method on a struct field (for example, s.someField.SomeMethod) before the database call, the utility will panic due to a nil dereference.


By default, the utility looks for the following database calls: Get, Getx, Select, Selectx, Exec, Execx, Query, Queryx, QueryRow, QueryxRow, and inserts early return statements at those points, since no database connection is initialized. If your code uses other method names, you can list them in the showsql.yaml config file. By default, the file is expected at scripts/showsql.yaml. You can specify a different path with the -f={path to file} flag.

Example config file:

exec:
- ExecContext
- QueryContext
- QueryRowContext
get:
- GetContext
- SelectContext
execx:
- ExecxContext
- QueryxContext
- QueryxRowContext
getx:
- GetxContext
- SelectxContext

The exec group is for methods with the arguments (ctx context.Context, query string, args ...interface{}) The get group is for methods with the arguments (ctx context.Context, dest interface{}, query string, args ...interface{}) The execx group is for methods with the arguments (ctx context.Context, sq Sqlizer) The getx group is for methods with the arguments (ctx context.Context, dest interface{}, sqlizer Sqlizer)


If a method contains two or more database calls, only the first query will be printed.


Before the method containing the query is called, all input arguments are filled with values. Composite types, including structs, are also filled recursively down to primitive types. Strings are filled with str1, str2, str3, and so on; numbers with prime numbers starting from 7, 11, 13, and so on; booleans are always true; time.Time values start at 2025-01-01 and increase by 1 day each. To leave default (zero) values instead, use these flags: -i sets all numbers to 0, -s sets all strings to empty, -b sets all booleans to false, -t sets all time.Time values to 0001-01-01.

By default, slices, arrays, and maps are filled with only one element. To increase the number of elements, use the -n={number} flag (maximum 10).


If an interface other than context.Context is passed as a method argument, that argument is not filled and nil is passed instead.

Options

-v, version -h, help -p=string, path to the file -m=string, method name -k, do not delete the temporary directory -i, fill all numeric arguments with the default value -s, fill all string arguments with the default value -b, fill all boolean arguments with the default value -t, fill all time.Time arguments with the default value -n=int, number of elements in filled slices, arrays, and maps (maximum 10) -f=string, path to the config file listing the database call methods

Documentation

Index

Constants

This section is empty.

Variables

View Source
var QueriesMap = map[string]map[string]struct{}{
	"Exec": {
		"Exec":     {},
		"Query":    {},
		"QueryRow": {},
	},
	"Get": {
		"Get":    {},
		"Select": {},
	},
	"Execx": {
		"Execx":     {},
		"Queryx":    {},
		"QueryxRow": {},
	},
	"Getx": {
		"Getx":    {},
		"Selectx": {},
	},
}

Functions

func CheckGoModExists

func CheckGoModExists() bool

func CopyAndModifyFiles

func CopyAndModifyFiles(
	srcDir, destDir, fileName string,
	callDataMap map[int]CallData,
	signatureMap map[string]SignatureData,
) error

func ExtractSignature

func ExtractSignature(
	file *ast.File,
) map[string]SignatureData

func GenerateMainFile

func GenerateMainFile(data GenerateData, destDir string) error

func GeneratePopulatingFile

func GeneratePopulatingFile(flags Flags, destDir string) error

func GeneratePrintingFile

func GeneratePrintingFile(destDir string) error

func GetCallData

func GetCallData(
	node *ast.File,
	fset *token.FileSet,
	squirrelImport string,
) map[int]CallData

func Handle

func Handle(
	fileName, callMethodName string,
	flags Flags,
) error
func Navigate(flags Flags) error

func ParseMutationQueries

func ParseMutationQueries(queriesFile string)

Types

type CallData

type CallData struct {
	ParentMethodName string
	IsSqlizer        bool
	SqlizerVarName   string
	SqlVarName       string
	SqlArgsVarName   string
	Pos              int
}

type Flags

type Flags struct {
	KeepGenerated bool
	AllIntsZero   bool
	AllStrsEmpty  bool
	AllboolsFalse bool
	AllTimeEmpty  bool
	ElemsCount    int
	QueriesFile   string
}

type GenerateData

type GenerateData struct {
	StructName     string
	CallMethodName string
	Arguments      []string
	Imports        []string
}

type MutationQueries

type MutationQueries struct {
	Exec  []string `yaml:"exec"`
	Get   []string `yaml:"get"`
	Execx []string `yaml:"execx"`
	Getx  []string `yaml:"getx"`
}

type SignatureData

type SignatureData struct {
	StructName string
	MethodName string
	Arguments  []string
	Returns    []string
}

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

func (*Stack) Add

func (s *Stack) Add(n int)

func (*Stack) Delete

func (s *Stack) Delete()

func (*Stack) Get

func (s *Stack) Get() int

Directories

Path Synopsis
cmd
showsql command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址