GSQL is a structured query language code builder for golang.

Overview

GSQL

GSQL is a structured query language code builder for golang.

Genreate SQL

= '100' err, s := gsql.Select("name", "age", "money"). From("user_info").Where("money >= ?", "100"). Build() t.Log(err) t.Log(s) } func TestSelectTag(t *testing.T) { type UserInfo struct { Name string `sql:"name"` Age int `sql:"age"` } // SELECT name, age FROM user_info WHERE money >= 999.90 AND age = 18 err, s := gsql.Select(UserInfo{}).From("user_info").Where("money >= ? AND age = ?", 999.9, 18).Build() t.Log(err) t.Log(s) } func TestSelectMix(t *testing.T) { //sql_test.go:36: missing parameters: where syntax lack of conditions //sql_test.go:37: SELECT name, age, money FROM user_info err, s := gsql.Select("name", 3.1415827, "age", 112, "money"). From("user_info"). Where("money >= ?", "100", "1").Build() t.Log(err) t.Log(s) } func TestSelectToStr(t *testing.T) { // SELECT name, age, money FROM user_info WHERE name = 'Leon Ding' sql := gsql.Select("name", "age", "money"). From("user_info"). Where("name = ?", "Leon Ding").String() t.Log(sql) } func TestSelectAsName(t *testing.T) { // SELECT name, age, money AS '余额' FROM user_info WHERE name = 'Leon Ding' LIMIT 1 sql := gsql.Select("name", "age", syntax.As("money", "余额")). From("user_info"). Where("name = ?", "Leon Ding").Limit(1).String() t.Log(sql) } func TestSelectFilter(t *testing.T) { // offset=true SELECT name, age, money AS '余额' FROM user_info LIMIT 3 OFFSET 1 // offset=false SELECT name, age, money AS '余额' FROM user_info LIMIT 1,3 syntaxSql := gsql.Select("name", "age", syntax.As("money", "余额")). From("user_info") // SELECT name, age, money AS '余额' FROM user_info LIMIT 3 OFFSET 1 sql := syntax.Limit(syntaxSql, true, 1, 3).String() t.Log(sql) } func TestSelectAlias(t *testing.T) { type UserInfo struct { Name string `sql:"name"` Age int `sql:"age"` Money float64 `sql:"money"` } sql := gsql.SelectAs(syntax.Alias(UserInfo{}, map[string]string{ "name": "用户名", "money": "金钱", })). From("user_info"). Where("name = ?", "Leon Ding").Limit(1).String() t.Log(sql) syntaxSql := gsql.Select(UserInfo{}).From("user_info").Limit(2) err, s := syntax.Limit(syntaxSql, true, 1, 3).Build() t.Log(err) t.Log(s) //=== RUN TestSelectAlias //sql_test.go:92: SELECT name AS '用户名', age, money AS '金钱' FROM user_info WHERE name = 'Leon Ding' LIMIT 1 //sql_test.go:98: limit syntax recurring //sql_test.go:99: SELECT name, age, money FROM user_info LIMIT 2 //--- PASS: TestSelectAlias (0.00s) //PASS } func TestSqlSelectOrderBy(t *testing.T) { type UserInfo struct { Name string `sql:"name"` Age int `sql:"age"` Money float64 `sql:"money"` } syntaxSql := gsql.SelectAs(syntax.Alias(UserInfo{}, map[string]string{ "name": "用户名", })).From("user_info") // SELECT name AS '用户名', age, money FROM user_info ORDER BY money DESC, age ASC sql := syntax.OrderBy(syntaxSql, []syntax.OrderRow{ {"money", syntax.DESC}, {"age", syntax.ASC}, }) s := syntax.Limit(sql, true, 1, 3).String() t.Log(s) } func TestCol(t *testing.T) { // name = 'Leon Ding' AND age = 19 sql := syntax.Condition(syntax.Col("name").Equal("'Leon Ding'")). AND(syntax.Col("age").Equal(19)) t.Log(sql) } func TestIN(t *testing.T) { // name IN ('Jaco','Kimi') sql := syntax.Condition(syntax.Col("name").In([]string{"Jaco", "Kimi"})).String() t.Log(sql) } func TestLike(t *testing.T) { // name LIKE '%Di%' sql := syntax.Condition(syntax.Col("name").Like("%Di%")).String() t.Log(sql) } func TestBetween(t *testing.T) { // SELECT name, age, money AS '余额' FROM user_info // WHERE created_at BETWEEN '2000-01-08 00:00:00' // AND '2021-09-07 20:38:52' // AND age BETWEEN 10 AND 21 err, left := syntax.Col("created_at").Between([]interface{}{ "'2000-01-08 00:00:00'", fmt.Sprintf("'%v'", time.Now().Format("2006-01-02 15:04:05")), }) err, right := syntax.Col("age").Between([]interface{}{10, 21}) sql := syntax.Condition(left).AND(right) syntaxSql := gsql.Select("name", "age", syntax.As("money", "余额")). From("user_info").WhereBind(sql).String() if err != nil { t.Error(err) } t.Log(syntaxSql) } ">
package gsql_test

import (
	"fmt"
	"github.com/auula/gsql"
	"github.com/auula/gsql/syntax"
	"testing"
	"time"
)

func TestSelectString(t *testing.T) {
	// SELECT name, age, money FROM user_info WHERE money >= '100'
	err, s := gsql.Select("name", "age", "money").
		From("user_info").Where("money >= ?", "100").
		Build()

	t.Log(err)
	t.Log(s)
}

func TestSelectTag(t *testing.T) {

	type UserInfo struct {
		Name string `sql:"name"`
		Age  int    `sql:"age"`
	}
	// SELECT name, age FROM user_info WHERE money >= 999.90 AND age = 18
	err, s := gsql.Select(UserInfo{}).From("user_info").Where("money >= ? AND age = ?", 999.9, 18).Build()
	t.Log(err)
	t.Log(s)
}

func TestSelectMix(t *testing.T) {

	//sql_test.go:36: missing parameters: where syntax lack of conditions
	//sql_test.go:37: SELECT name, age, money FROM user_info
	err, s := gsql.Select("name", 3.1415827, "age", 112, "money").
		From("user_info").
		Where("money >= ?", "100", "1").Build()

	t.Log(err)
	t.Log(s)
}

func TestSelectToStr(t *testing.T) {

	// SELECT name, age, money FROM user_info WHERE name = 'Leon Ding'
	sql := gsql.Select("name", "age", "money").
		From("user_info").
		Where("name = ?", "Leon Ding").String()

	t.Log(sql)
}

func TestSelectAsName(t *testing.T) {

	// SELECT name, age, money AS '余额' FROM user_info WHERE name = 'Leon Ding' LIMIT 1
	sql := gsql.Select("name", "age", syntax.As("money", "余额")).
		From("user_info").
		Where("name = ?", "Leon Ding").Limit(1).String()

	t.Log(sql)
}

func TestSelectFilter(t *testing.T) {

	// offset=true SELECT name, age, money AS '余额' FROM user_info LIMIT 3 OFFSET 1
	// offset=false SELECT name, age, money AS '余额' FROM user_info LIMIT 1,3

	syntaxSql := gsql.Select("name", "age", syntax.As("money", "余额")).
		From("user_info")

	// SELECT name, age, money AS '余额' FROM user_info LIMIT 3 OFFSET 1
	sql := syntax.Limit(syntaxSql, true, 1, 3).String()
	t.Log(sql)

}

func TestSelectAlias(t *testing.T) {

	type UserInfo struct {
		Name  string  `sql:"name"`
		Age   int     `sql:"age"`
		Money float64 `sql:"money"`
	}

	sql := gsql.SelectAs(syntax.Alias(UserInfo{}, map[string]string{
		"name":  "用户名",
		"money": "金钱",
	})).
		From("user_info").
		Where("name = ?", "Leon Ding").Limit(1).String()
	t.Log(sql)

	syntaxSql := gsql.Select(UserInfo{}).From("user_info").Limit(2)

	err, s := syntax.Limit(syntaxSql, true, 1, 3).Build()

	t.Log(err)
	t.Log(s)

	//=== RUN   TestSelectAlias
	//sql_test.go:92: SELECT name AS '用户名', age, money AS '金钱' FROM user_info WHERE name = 'Leon Ding' LIMIT 1
	//sql_test.go:98: limit syntax recurring
	//sql_test.go:99: SELECT name, age, money FROM user_info LIMIT 2
	//--- PASS: TestSelectAlias (0.00s)
	//PASS

}

func TestSqlSelectOrderBy(t *testing.T) {

	type UserInfo struct {
		Name  string  `sql:"name"`
		Age   int     `sql:"age"`
		Money float64 `sql:"money"`
	}

	syntaxSql := gsql.SelectAs(syntax.Alias(UserInfo{}, map[string]string{
		"name": "用户名",
	})).From("user_info")

	// SELECT name AS '用户名', age, money FROM user_info ORDER BY  money DESC, age ASC
	sql := syntax.OrderBy(syntaxSql, []syntax.OrderRow{
		{"money", syntax.DESC},
		{"age", syntax.ASC},
	})

	s := syntax.Limit(sql, true, 1, 3).String()

	t.Log(s)
}

func TestCol(t *testing.T) {
	// name = 'Leon Ding' AND age = 19
	sql := syntax.Condition(syntax.Col("name").Equal("'Leon Ding'")).
		AND(syntax.Col("age").Equal(19))
	t.Log(sql)
}

func TestIN(t *testing.T) {
	// name IN ('Jaco','Kimi')
	sql := syntax.Condition(syntax.Col("name").In([]string{"Jaco", "Kimi"})).String()
	t.Log(sql)
}

func TestLike(t *testing.T) {
	// name LIKE '%Di%'
	sql := syntax.Condition(syntax.Col("name").Like("%Di%")).String()
	t.Log(sql)
}

func TestBetween(t *testing.T) {

	// SELECT name, age, money AS '余额' FROM user_info
	// WHERE created_at BETWEEN '2000-01-08 00:00:00'
	// AND '2021-09-07 20:38:52'
	// AND age BETWEEN 10 AND 21

	err, left := syntax.Col("created_at").Between([]interface{}{
		"'2000-01-08 00:00:00'",
		fmt.Sprintf("'%v'", time.Now().Format("2006-01-02 15:04:05")),
	})

	err, right := syntax.Col("age").Between([]interface{}{10, 21})
	sql := syntax.Condition(left).AND(right)

	syntaxSql := gsql.Select("name", "age", syntax.As("money", "余额")).
		From("user_info").WhereBind(sql).String()

	if err != nil {
		t.Error(err)
	}

	t.Log(syntaxSql)
}
You might also like...
Data-builder - Data builder with golang

databuilder import "github.com/go-coldbrew/data-builder" Index Variables func Is

Type safe SQL builder with code generation and automatic query result data mapping
Type safe SQL builder with code generation and automatic query result data mapping

Jet Jet is a complete solution for efficient and high performance database access, consisting of type-safe SQL builder with code generation and automa

SQL builder and query library for golang

__ _ ___ __ _ _ _ / _` |/ _ \ / _` | | | | | (_| | (_) | (_| | |_| | \__, |\___/ \__, |\__,_| |___/ |_| goqu is an expressive SQL bu

Go database query builder library for PostgreSQL

buildsqlx Go Database query builder library Installation Selects, Ordering, Limit & Offset GroupBy / Having Where, AndWhere, OrWhere clauses WhereIn /

SQL query builder for Go

GoSQL Query builder with some handy utility functions. Documentation For full documentation see the pkg.go.dev or GitBook. Examples // Open database a

Type safe SQL query builder and struct mapper for Go

sq (Structured Query) 🎯 🏆 sq is a code-generated, type safe query builder and struct mapper for Go. 🏆 🎯 Documentation • Reference • Examples This

Fast SQL query builder for Go

sqlf A fast SQL query builder for Go. sqlf statement builder provides a way to: Combine SQL statements from fragments of raw SQL and arguments that ma

Simple query builder for MongoDB

🌱 greenleaf - simple, type safe and easy to use query builder for MongoDB Installation To install use: go get github.com/slavabobik/greenleaf Quick

A Go SQL query builder and struct mapper.

godb - a Go query builder and struct mapper godb is a simple Go query builder and struct mapper, not a full-featured ORM. godb does not manage relatio

An idiomatic Go query builder for ElasticSearch

esquery A non-obtrusive, idiomatic and easy-to-use query and aggregation builder for the official Go client for ElasticSearch. Table of Contents Descr

dqlx is a fully featured DGraph Schema and Query Builder for Go.

dqlx is a fully featured DGraph Schema and Query Builder for Go. It aims to simplify the interaction with the awesome Dgraph database allowing you to fluently compose any queries and mutations of any complexity. It also comes with a rich Schema builder to easily develop and maintain your Dgraph schema.

gosq is a parsing engine for a simplicity-focused, template-based SQL query builder for Go.

gosq is a parsing engine for a simplicity-focused, template-based SQL query builder for Go.

BQB is a lightweight and easy to use query builder that works with sqlite, mysql, mariadb, postgres, and others.

Basic Query Builder Why Simple, lightweight, and fast Supports any and all syntax by the nature of how it works Doesn't require learning special synta

Floppa programming language inspired by the brainf*ck programming language. Created just for fun and you can convert your brainf*ck code to floppa code.

Floppa Programming Language Created just for fun. But if you want to contribute, why not? Floppa p.l. inspired by the brainf*ck programming language.

This is the code example how to use SQL to query data from any relational databases in Go programming language.

Go with SQL example This is the code example how to use SQL to query data from any relational databases in Go programming language. To start, please m

🔥 Hugo website builder, Hugo themes & Hugo CMS. No code, build with widgets!
🔥 Hugo website builder, Hugo themes & Hugo CMS. No code, build with widgets!

Wowchemy: the website builder for Hugo Join 750,000+ Sites. No Code. Easily Create Future-Proof Websites ✏️ 📰 🚀 🔥 1. Create any kind of website

x-crafter is used to quickly create templates from your prototype, also come with a builder to quickly regenerate your code

XCrafter 😄 x-crafter is used to quickly create templates from your prototype, also come with a builder to quickly regenerate your code. Install Using

`go-redash-query` is a simple library to get structed data from `redash query` sources

go-redash-query go-redash-query is a simple library to get structed data from redash query sources Example Source table id name email 1 Dannyhann rhrn

Go-linq - A powerful language integrated query (LINQ) library for Golang

go-linq A powerful language integrated query (LINQ) library for Go. Written in v

Owner
Leon Ding
Jokes are truth.
Leon Ding
Simple filter query language parser so that you can build SQL, Elasticsearch, etc. queries safely from user input.

fexpr fexpr is a filter query language parser that generates extremely easy to work with AST structure so that you can create safely SQL, Elasticsearc

Gani Georgiev 35 Nov 20, 2022
JSON query expression library in Golang.

jsonql JSON query expression library in Golang. This library enables query against JSON. Currently supported operators are: (precedences from low to h

Qian Chen 256 Sep 27, 2022
Query, update and convert data structures from the command line. Comparable to jq/yq but supports JSON, TOML, YAML, XML and CSV with zero runtime dependencies.

dasel Dasel (short for data-selector) allows you to query and modify data structures using selector strings. Comparable to jq / yq, but supports JSON,

Tom Wright 3.8k Nov 20, 2022
A simple Go package to Query over JSON/YAML/XML/CSV Data

A simple Go package to Query over JSON Data. It provides simple, elegant and fast ODM like API to access, query JSON document Installation Install the

Saddam H 2k Nov 27, 2022
Query Parser for REST

Query Parser for REST Query Parser is a library for easy building dynamic SQL queries to Database. It provides a simple API for web-applications which

Tim S. 47 Nov 20, 2022
An implementation of GraphQL for Go / Golang

graphql An implementation of GraphQL in Go. Follows the official reference implementation graphql-js. Supports: queries, mutations & subscriptions. Do

null 8.9k Nov 23, 2022
Convert Golang Struct To GraphQL Object On The Fly

Straf Convert Golang Struct To GraphQL Object On The Fly Easily Create GraphQL Schemas Example Converting struct to GraphQL Object type UserExtra stru

Roshan Mehta 34 Oct 26, 2022
Tools to write high performance GraphQL applications using Go/Golang.

graphql-go-tools Sponsors WunderGraph Are you looking for a GraphQL e2e data fetching solution? Supports frameworks like NextJS, type safety with gene

Jens Neuse 414 Nov 20, 2022
A GraphQL complete example using Golang And PostgreSQL

GraphQL with Golang A GraphQL complete example using Golang & PostgreSQL Installation Install the dependencies go get github.com/graphql-go/graphql go

Santo Shakil 2 Aug 25, 2022
Go-Postgresql-Query-Builder - A query builder for Postgresql in Go

Postgresql Query Builder for Go This query builder aims to make complex queries

Samuel Banks 7 Nov 17, 2022