Coder Social home page Coder Social logo

rel's Introduction

rel

๐Ÿšง no stable api, just a playground for go database client alternatives

Go database helper that maps structs to tuples borrowing method names from Tutorial D syntax

Usage example:

import "github.com/peter-mueller/rel"

type Person struct {
	Name string `db:"name"`
	Age  int    `db:"age"`
}

var person = rel.NewVar[Person]("person")

func Insert(p Person) (stmt string, params []any) {
	stmt += fmt.Sprintf("INSERT INTO person (%s)\n", strings.Join(person.AttributeRef(), ", "))
	stmt += fmt.Sprintf("VALUES (%s)\n", strings.Join(person.Params("?"), ","))
	params = person.Tuple(p)
	return stmt, params
}

func InsertGeneric[T any](v *rel.Var[T], value T) (stmt string, params []any) {
	stmt += fmt.Sprintf("INSERT INTO %s (%s)\n", v.Name(), strings.Join(v.AttributeRef(), ", "))
	stmt += fmt.Sprintf("VALUES (%s)\n", strings.Join(v.Params("?"), ","))
	params = v.Tuple(value)
	return stmt, params
}

p := Person{Name: "Hans", Age: 33}
stmt, params := Insert(p)
// INSERT INTO person (name, age) 
// VALUES (?,?)
// ["Hans", 33]
res, err := db.ExecContext(ctx, stmt, params...)

sqlwriter example:

p := Person{
	Name: "Franz",
	Age:  23,
}

var w sqlwriter.PostgresSQLWriter
w.InsertInto(person.Name(), person.AttributeRef())
w.Values(person.Tuple(p))
res, err := db.ExecContext(ctx, w.Stmt(), w.Params...)
var p Person

var w sqlwriter.PostgresSQLWriter
w.SelectFrom(person.AttributeRef(), person.Dests(&p), person.Name())
w.Where("age > ?", 3)
res, err := db.ExecContext(ctx, w.Stmt(), w.Params...)
// Scan(w.Dests...)

rel's People

Contributors

peter-mueller avatar

Watchers

 avatar

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.