Help us shape the future of Go! Take the 2026 Go Developer Survey

stst

package module
v0.0.0-...-a78b224 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2021 License: MIT Imports: 8 Imported by: 0

README

stst

Notice : This project is still a work in progress.

Stst is an code generator which generates Go structs from SELECT statements.

How to use

Provide a SELECT statement.

-- sql/example.sql
SELECT
    foo_id
    , bar_name
FROM
    awesome_table
LIMIT 1;

Generate a Go source code from the sql file.

$ go run github.com/uhey22e/stst/cmd/stst -i sql/example.sql -p models -n Example -o models/example.go
$ less models/example.go

package models

type Example struct {
    FooID   int64
    BarName string
}
...

Use the generated struct and helper functions for the database/sql package in your application.

// your_app.go

rows, err := db.Query(models.ExampleQuery)
for rows.Next() {
    var m models.Example
    err := rows.Scan(m.GetScanDests()...)
    fmt.Printf("%#v", m)
}

For more examples, please see demo project.

Options

To be written...

Tests

A runnning PostgreSQL server is required for tests.

Run local PostgreSQL server using docker:

$ cd testdb
$ docker-compose up -d

Run tests:

$ go test -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColInfo

type ColInfo struct {
	Name        string
	GoTypeName  string
	PackagePath string
}

ColInfo is a concrete representation of a column and a member of struct.

type DBConf

type DBConf struct {
	Host     string `env:"DB_HOST" envDefault:"127.0.0.1"`
	Port     int    `env:"DB_PORT" envDefault:"5432"`
	DBName   string `env:"DB_DBNAME" envDefault:"postgres"`
	Username string `env:"DB_USERNAME" envDefault:"postgres"`
	Password string `env:"DB_PASSWORD" envDefault:"postgres"`
	SSLMode  string `env:"DB_SSLMODE" envDefault:"disable"`
}

DBConf contains configurations connecting to database.

type MemberCustomizer

type MemberCustomizer func(*jen.Statement, *ColInfo)

func AppendColNameTag

func AppendColNameTag(key string) MemberCustomizer

AppendColNameTag is an option for GenerateStruct()

type PsqlTypemap

type PsqlTypemap struct{}

PsqlTypemap is an implementation of Typemap for PostgreSQL.

func (PsqlTypemap) GetGoType

func (p PsqlTypemap) GetGoType(databaseTypeName string) (string, bool)

GetGoType .

type Stst

type Stst struct {
	DB      *sql.DB
	Typemap Typemap
}

Stst .

func NewPsql

func NewPsql(db *sql.DB) *Stst

NewPsql is a constructor

func (*Stst) GenerateGetScanDestsFunc

func (s *Stst) GenerateGetScanDestsFunc(structName string, cols []ColInfo) (*jen.Statement, error)

GenerateGetScanDestsFunc assembles the GetScanDests() function from ColInfo.

func (*Stst) GenerateQueryVar

func (s *Stst) GenerateQueryVar(name, query string) (*jen.Statement, error)

GenerateQueryVar assembles the variable definition of the query.

func (*Stst) GenerateStruct

func (s *Stst) GenerateStruct(name string, cols []ColInfo, memberCustomizer ...MemberCustomizer) (*jen.Statement, error)

GenerateStruct assembles the struct from ColInfo.

func (*Stst) GetMeta

func (s *Stst) GetMeta(query string) ([]ColInfo, error)

GetMeta returns metadata of columns

func (*Stst) Package

func (s *Stst) Package(w io.Writer, name string, codes []jen.Code, pkgComments []string) error

Package .

type Typemap

type Typemap interface {
	// GetGoType returns a golang type name mapped from the database type name
	GetGoType(databaseTypeName string) (string, bool)
}

Typemap is an interface

Directories

Path Synopsis
cmd
stst command
models
Code generated by stst; DO NOT EDIT.
Code generated by stst; DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL