Coder Social home page Coder Social logo

xgfone / go-sqlx Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 1.0 224 KB

A set of the simple, flexible and powerful SQL builders with zero-config.

License: Apache License 2.0

Go 100.00%
sql builder sql-builder sqlbuilder database go go-sql golang mysql

go-sqlx's Introduction

SQL builder for Go 1.18+ Build Status GoDoc License

Package sqlx provides a set of flexible and powerful SQL builders, not ORM, which is inspired by go-sqlbuilder. The built result can be used by DB.Query() and DB.Exec()

Install

$ go get -u github.com/xgfone/go-sqlx

Usage

package main

import (
	"fmt"

	"github.com/xgfone/go-op"
	"github.com/xgfone/go-sqlx"
)

func main() {
	builder := sqlx.Select("*").From("table")
	builder.Where(op.Equal("id", 123), op.Between("age", 20, 30))

	// You can set the dialect by hand, which is DefaultDialect by default.
	// DefaultDialect is the MySQL dialect, but you can modify it.
	// builder.SetDialect(Sqlite3)

	sql, args := builder.Build()
	fmt.Println(sql)
	fmt.Println(args)

	// Output:
	// SELECT * FROM `table` WHERE (`id`=? AND `age` BETWEEN ? AND ?)
	// [123 20 30]
}

You can use sqlx.DB, which is the proxy of builder and sql.DB, it will automatically set the dialect by the sql driver name. For example,

// Set the dialect to MySQL.
db, _ := sqlx.Open("mysql", "user:password@tcp(127.0.0.1:3306)/db")
builder := db.Select("*").From("table").Where(op.Equal("id", 123))

sql, args := builder.Build()
rows, err := db.Query(sql, args...)

// Or
// rows, err := builder.Query()

Intercept SQL

package main

import (
	"fmt"

	"github.com/xgfone/go-op"
	"github.com/xgfone/go-sqlx"
)

func main() {
	// Open DB connecting the mysql server and set the dialect to MySQL.
	db, err := sqlx.Open("mysql", "user:password@tcp(127.0.0.1:3306)/db")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer db.Close()

	// Set the interceptor to print the sql statement.
	db.Interceptor = sqlx.InterceptorFunc(func(sql string, args []interface{}) (string, []interface{}, error) {
		fmt.Println(sql)
		return sql, args, nil
	})

	// Build the SELECT SQL statement
	builder := db.Select("*").From("table")
	builder.Where(op.Equal("id", 123))
	rows, err := builder.Query()
	// ...

	// Interceptor will output:
	// SELECT * FROM `table` WHERE `id`=?
}

go-sqlx's People

Contributors

xgfone avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mybigman

go-sqlx's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.