px

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 15 Imported by: 0

README

PGX

Simple PostgreSQL ORM extension for Golang

Install

go get -u github.com/stevenzack/px

Example

package main

import (
	"database/sql"
	"fmt"
	"log"
	"time"

	"github.com/stevenzack/px"
)

type User struct {
	Id          uint32                                               // serial not null primary key
	PhoneNumber sql.NullString    `limit:"36" index:"unique"` // varchar(36) unique index
	Info        map[string]string                                  // jsonb
	UpdateTime  time.Time         `index:""`
	CreateTime  time.Time         
}

func init() {
	log.SetFlags(log.Lshortfile)
}

func main() {

	urlExample := "postgres://stevenzack:@localhost:5432/langenius"
	c, e := px.NewBaseModel(urlExample, User{})
	if e != nil {
		log.Println(e)
		return
	}
	id, e := c.Insert(User{
		PhoneNumber: sql.NullString{Valid: true, String: "asd"},
	})
	if e != nil {
		log.Println(e)
		return
	}
	fmt.Println("inserted: ", id)

	v, e := c.Find(id)
	if e != nil {
		log.Println(e)
		return
	}
	fmt.Println(v)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	AutoSyncTableSchema  = false
	AutoDropRemoteColumn = false
)

Functions

func Bool

func Bool(b bool) *bool

func BoolCover

func BoolCover(b *bool) bool

func Float

func Float(f float64) *float64

func FormatDsn

func FormatDsn(m map[string]string) string

func Int

func Int(i int) *int

func Int32

func Int32(i int32) *int32

func Int64

func Int64(i int64) *int64

func ParseDsn

func ParseDsn(s string) (map[string]string, error)

func String

func String(s string) *string

func StringCover

func StringCover(s *string) string

func Time

func Time(t time.Time) *time.Time

func TimeCover

func TimeCover(t *time.Time) time.Time

func ToPostgreType

func ToPostgreType(t reflect.Type, dbTag string, length, limit int) (string, error)

func ToTableName

func ToTableName(s string) string

func Uint

func Uint(i uint) *uint

func Uint32

func Uint32(i uint32) *uint32

func Uint64

func Uint64(i uint64) *uint64

Types

type BaseModel

type BaseModel[T any] struct {
	Type      reflect.Type
	Dsn       string
	Pool      *pgxpool.Pool
	Database  string
	Schema    string
	TableName string
	// contains filtered or unexported fields
}

func MustNewBaseModel

func MustNewBaseModel[T any](dsn string) *BaseModel[T]

func NewBaseModel

func NewBaseModel[T any](dsn string) (*BaseModel[T], error)

func NewBaseModelWithCreated

func NewBaseModelWithCreated[T any](dsn string) (*BaseModel[T], bool, error)

func (*BaseModel[T]) Clear

func (b *BaseModel[T]) Clear() error

func (*BaseModel[T]) CountWhere

func (b *BaseModel[T]) CountWhere(where string, args ...any) (int64, error)

func (*BaseModel[T]) Delete

func (b *BaseModel[T]) Delete(id any) (int64, error)

func (*BaseModel[T]) DeleteWhere

func (b *BaseModel[T]) DeleteWhere(where string, args ...any) (int64, error)

func (*BaseModel[T]) Exists

func (b *BaseModel[T]) Exists(id any) (bool, error)

func (*BaseModel[T]) ExistsWhere

func (b *BaseModel[T]) ExistsWhere(where string, args ...any) (bool, error)

func (*BaseModel[T]) Find

func (b *BaseModel[T]) Find(id any) (*T, error)

Find finds a document (*struct type) by id

func (*BaseModel[T]) FindAndUpdateSet

func (b *BaseModel[T]) FindAndUpdateSet(where, sets string, args ...any) (*T, error)

func (*BaseModel[T]) FindWhere

func (b *BaseModel[T]) FindWhere(where string, args ...any) (*T, error)

FindWhere finds a document (*struct type) that matches 'where' condition

func (*BaseModel[T]) GetCreateTableSQL

func (b *BaseModel[T]) GetCreateTableSQL(primaryKeyModel *indexModel) string

func (*BaseModel[T]) GetIndexes

func (b *BaseModel[T]) GetIndexes() ([]IndexSchema, error)

func (*BaseModel[T]) GetInsertReturningSQL

func (b *BaseModel[T]) GetInsertReturningSQL() ([]int, string)

GetInsertReturningSQL returns insert SQL with returning id

func (*BaseModel[T]) GetInsertSQL

func (b *BaseModel[T]) GetInsertSQL() ([]int, string)

GetInsertSQL returns insert SQL without returning id

func (*BaseModel[T]) GetSelectFields

func (b *BaseModel[T]) GetSelectFields() ([]int, string)

id,name,create_at

func (*BaseModel[T]) GetSelectSQL

func (b *BaseModel[T]) GetSelectSQL() ([]int, string)

GetSelectSQL returns fieldIndexes, and select SQL

func (*BaseModel[T]) Insert

func (b *BaseModel[T]) Insert(v T) (any, error)

Insert inserts v (*struct or struct type)

func (*BaseModel[T]) Query

func (b *BaseModel[T]) Query(queryTrail string, args ...any) ([]T, error)

QueryWhere queries documents ([]struct type) that matches 'where' condition

func (*BaseModel[T]) QueryAndUpdateSet

func (b *BaseModel[T]) QueryAndUpdateSet(where, sets string, args ...any) ([]T, error)

func (*BaseModel[T]) QueryWhere

func (b *BaseModel[T]) QueryWhere(where string, args ...any) ([]T, error)

QueryWhere queries documents ([]struct type) that matches 'where' condition

func (*BaseModel[T]) Truncate

func (b *BaseModel[T]) Truncate() error

func (*BaseModel[T]) UpdateSet

func (b *BaseModel[T]) UpdateSet(where, sets string, args ...any) (int64, error)

type Column

type Column struct {
	ColumnName string `db:"column_name"`
	DataType   string `db:"data_type"`
	IsNullable string `db:"is_nullable"`
}

func DescTable

func DescTable(pool *pgxpool.Pool, database, schema, tableName string) ([]Column, error)

select column_name,data_type from information_schema.columns where table_catalog='langenius' and table_schema='public' and table_name='student'

type IndexSchema

type IndexSchema struct {
	SchemaName string `db:"schemaname"`
	TableName  string `db:"tablename"`
	IndexName  string `db:"indexname"`
	IndexDef   string `db:"indexdef"`
}

Jump to

Keyboard shortcuts

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