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)
}
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 3 Feb 15, 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 252 Jun 22, 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.4k Jul 3, 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 1.9k Jun 24, 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. 39 Jun 6, 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.6k Jun 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 32 Jun 23, 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 357 Jul 3, 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 1 Jan 7, 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 4 May 24, 2022
Data-builder - Data builder with golang

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

Coldbrew 0 Feb 5, 2022
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

null 723 Jun 24, 2022
SQL builder and query library for golang

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

Doug Martin 1.6k Jun 24, 2022
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 /

Arthur Kushman 63 Jun 12, 2022
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

Travis Harmon 23 Apr 25, 2022
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

null 162 Jun 24, 2022
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

Vlad Glushchuk 70 Jun 24, 2022
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

Slava 72 Jun 22, 2022
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

Samuel GAY 715 Jun 15, 2022
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

Aqua Security 239 Jun 20, 2022
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.

Fabrizio 44 Jun 26, 2022
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.

Sang-gon Lee 49 Apr 26, 2022
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

Aaron M 37 Jun 11, 2022
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.

null 18 Apr 26, 2022
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

Muhammad Uzair Mohd Faizul 1 Mar 12, 2022
🔥 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

Wowchemy 6.5k Jun 30, 2022
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

Chi-Tai Vong 3 Nov 29, 2021
`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

danny han 3 May 22, 2022
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

Ahmet Alp Balkan 3k Jul 1, 2022