dgwlib

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dgwlib

based on kanmu/tools/dgw

Documentation

Overview

go:generate go-bindata -o bindata.go template mapconfig

Index

Constants

This section is empty.

Variables

View Source
var AutoGenKeyCfg = &AutoKeyMap{
	Types: []string{"smallserial", "serial", "bigserial", "autogenuuid"},
}

AutoGenKeyCfg ...

Functions

func CreateInsertParams

func CreateInsertParams(st *Struct) string

CreateInsertParams ...

func CreateInsertSQL

func CreateInsertSQL(st *Struct) string

CreateInsertSQL ...

func CreateInsertScan

func CreateInsertScan(st *Struct) string

CreateInsertScan ...

func CreateSelectByPkFuncParams

func CreateSelectByPkFuncParams(st *Struct) string

CreateSelectByPkFuncParams ...

func CreateSelectByPkSQL

func CreateSelectByPkSQL(st *Struct) string

CreateSelectByPkSQL ...

func CreateSelectByPkSQLParams

func CreateSelectByPkSQLParams(st *Struct) string

CreateSelectByPkSQLParams ...

func CreateSelectByPkScan

func CreateSelectByPkScan(st *Struct) string

CreateSelectByPkScan ...

func OpenDB

func OpenDB(connStr string) (*sql.DB, error)

OpenDB opens database connection

func PgConvertType

func PgConvertType(col *PgColumn, typeCfg *PgTypeMapConfig) string

PgConvertType converts type

func PgExecuteCustomTemplate

func PgExecuteCustomTemplate(st *StructTemplate, customTmpl string) ([]byte, error)

PgExecuteCustomTemplate execute custom template

Types

type AutoKeyMap

type AutoKeyMap struct {
	Types []string `toml:"db_types"`
}

AutoKeyMap auto generating key config

type PgColumn

type PgColumn struct {
	FieldOrdinal int
	Name         string
	DataType     string
	DDLType      string
	NotNull      bool
	DefaultValue sql.NullString
	IsPrimaryKey bool
}

PgColumn postgres columns

func PgLoadColumnDef

func PgLoadColumnDef(db Queryer, schema string, table string) ([]*PgColumn, error)

PgLoadColumnDef load Postgres column definition

type PgTable

type PgTable struct {
	Schema      string
	Name        string
	DataType    string
	AutoGenPk   bool
	PrimaryKeys []*PgColumn
	Columns     []*PgColumn
}

PgTable postgres table

func PgLoadTableDef

func PgLoadTableDef(db Queryer, schema string) ([]*PgTable, error)

PgLoadTableDef load Postgres table definition

type PgTypeMapConfig

type PgTypeMapConfig map[string]TypeMap

PgTypeMapConfig go/db type map struct toml config

var DefaultTypeMapCfg PgTypeMapConfig = PgTypeMapConfig{
	"string": TypeMap{
		DBTypes:        []string{"character", "character varying", "text", "money"},
		NotNullGoType:  "string",
		NullableGoType: "sql.NullString",
	},
	"time": TypeMap{
		DBTypes:        []string{"time with time zone", "time without time zone", "timestamp without time zone", "timestamp with time zone", "date"},
		NotNullGoType:  "time.Time",
		NullableGoType: "sql.NullTime",
	},
	"bool": TypeMap{
		DBTypes:        []string{"boolean"},
		NotNullGoType:  "bool",
		NullableGoType: "sql.NullBool",
	},
	"smallint": TypeMap{
		DBTypes:        []string{"smallint"},
		NotNullGoType:  "int16",
		NullableGoType: "sql.NullInt64",
	},
	"integer": TypeMap{
		DBTypes:        []string{"integer"},
		NotNullGoType:  "int",
		NullableGoType: "sql.NullInt64",
	},
	"bigint": TypeMap{
		DBTypes:        []string{"bigint"},
		NotNullGoType:  "int64",
		NullableGoType: "sql.NullInt64",
	},
	"smallserial": TypeMap{
		DBTypes:        []string{"smallserial"},
		NotNullGoType:  "uint16",
		NullableGoType: "sql.NullInt64",
	},
	"serial": TypeMap{
		DBTypes:        []string{"serial"},
		NotNullGoType:  "uint32",
		NullableGoType: "sql.NullInt64",
	},
	"real": TypeMap{
		DBTypes:        []string{"real"},
		NotNullGoType:  "float32",
		NullableGoType: "sql.NullFloat64",
	},
	"numeric": TypeMap{
		DBTypes:        []string{"numeric", "double precision"},
		NotNullGoType:  "float64",
		NullableGoType: "sql.NullFloat64",
	},
	"bytea": TypeMap{
		DBTypes:        []string{"bytea"},
		NotNullGoType:  "[]byte",
		NullableGoType: "[]byte",
	},
	"json": TypeMap{
		DBTypes:        []string{"json", "jsonb"},
		NotNullGoType:  "[]byte",
		NullableGoType: "[]byte",
	},
	"xml": TypeMap{
		DBTypes:        []string{"xml"},
		NotNullGoType:  "[]byte",
		NullableGoType: "[]byte",
	},
	"interval": TypeMap{
		DBTypes:        []string{"interval"},
		NotNullGoType:  "time.Duration",
		NullableGoType: "*time.Duration",
	},
	"default": TypeMap{
		DBTypes:        []string{"*"},
		NotNullGoType:  "interface{}",
		NullableGoType: "interface{}",
	},
}

DefaultTypeMapCfg is the default type map configuration

func PgLoadTypeMapFromFile

func PgLoadTypeMapFromFile(filePath string) (*PgTypeMapConfig, error)

PgLoadTypeMapFromFile load type map from toml file

type Queryer

type Queryer interface {
	Exec(string, ...interface{}) (sql.Result, error)
	Query(string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

Queryer database/sql compatible query interface

type Struct

type Struct struct {
	Name    string
	Table   *PgTable
	Comment string
	Fields  []*StructField
}

Struct go struct

func PgTableToStruct

func PgTableToStruct(t *PgTable, typeCfg *PgTypeMapConfig, keyConfig *AutoKeyMap) (*Struct, error)

PgTableToStruct converts table def to go struct

type StructField

type StructField struct {
	Name   string
	Type   string
	Tag    string
	Column *PgColumn
}

StructField go struct field

func PgColToField

func PgColToField(col *PgColumn, typeCfg *PgTypeMapConfig) (*StructField, error)

PgColToField converts pg column to go struct field

type StructTemplate

type StructTemplate struct {
	Struct *Struct
}

StructTemplate go struct passed to template

type TypeMap

type TypeMap struct {
	DBTypes        []string `toml:"db_types"`
	NotNullGoType  string   `toml:"notnull_go_type"`
	NullableGoType string   `toml:"nullable_go_type"`
}

TypeMap go/db type map struct

Jump to

Keyboard shortcuts

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