Skip to content

cavlabs/sqlm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sqlm

Go Reference Support Go 1.18+ Release Go Report Card Issues PRs License: MIT

A lightweight and extensible SQL templating and mapping tool for Go.

Key features

sqlm logo

  • Supports complex SQL template syntax (conditional statements, loops, variable substitution)
  • Flexible dynamic condition construction, similar to MyBatis Mapper style
  • Lightweight, zero-intrusive, easy to integrate
  • Compatible with various database drivers (MySQL, PostgreSQL, SQLite, etc.)
  • Provides simple API for quick start

Getting started

Prerequisites

sqlm requires Go version 1.18 or above.

Getting sqlm

With Go's module support, go [build|run|test] automatically fetches the necessary dependencies when you add the import in your code:

  import "github.com/cavlabs/sqlm"

Alternatively, use go get:

  go get -u github.com/cavlabs/sqlm

Coding with sqlm

A basic example:

package main

import (
	"fmt"

	"github.com/cavlabs/sqlm"
)

func main() {
	// Define a conditional SQL template
	tpl := `
SELECT id, name, age FROM users
WHERE 1=1
{{ if .Name }}
  AND name = '{{ .Name }}'
{{ end }}
{{ if .MinAge }}
  AND age >= {{ .MinAge }}
{{ end }}
    `

	params := map[string]any{
		"Name":   "Alice",
		"MinAge": 20,
	}

	sql, err := sqlm.Render(tpl, params)
	if err != nil {
		panic(err)
	}

	fmt.Println("Generated SQL:")
	fmt.Println(sql)
}

Roadmap

  • Add SQL syntax parsing optimization
  • Support more conditional expressions and functions
  • Add SQL pre-compilation and injection protection
  • Provide richer examples and plugin system
  • Etc.

Contributing

Contributions are welcome! Please follow the CONTRIBUTING.md guidelines.

License

MIT License © 2025 cavlabs/sqlm

About

A lightweight and extensible SQL templating and mapping tool for Go.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published