customorm

package module
v0.0.0-...-26ea5c4 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2022 License: MIT Imports: 9 Imported by: 0

README

customorm

Not a real ORM, just a bunch of functions to generate and run SQL queries by specific structs. Functions receive and return interfaces and can panic. Focused to work with PostgreSQL.

examples
structs
  • fields Id is mandatory (with pkey tag)
  • GetTableName method can be used to rewrite table name
type Domain struct {
	Id          int64   `json:"id" customsql:"pkey:id;check(id <> 0)"`
	Enabled     bool    `json:"enabled" customsql:"enabled;default=TRUE"`
	Name        string  `json:"name" customsql:"name;unique;check(name <> '')"`
}

func (w *Domain) GetTableName() string {
    return "domains"
}

type DomainUser struct {
	Id          int64   `json:"id" customsql:"pkey:id;check(id <> 0)"`
	Position    int64   `json:"position" customsql:"position;position"`
	Enabled     bool    `json:"enabled" customsql:"enabled;default=TRUE"`
	Name        string  `json:"name" customsql:"name;unique;check(name <> '')"`
	Parent      *Domain `json:"parent" customsql:"fkey:parent_id;unique;check(parent_id <> 0)"`
}

func (w *DomainUser) GetTableName() string {
    return "domain_users"
}
create table
corm := customorm.Init(db)
corm.CreateTable(&Domain{}) // return bool
corm.CreateTable(&DomainUser{}) // return bool
insert row
corm := customorm.Init(db)
corm.InsertRow(&DomainUser{
    Name: "UserName",
    Parent: &altStruct.Domain{Id: parentId},
    Enabled: true,
}) // return int64, error
update row/rows
corm := customorm.Init(db)
corm.UpdateRow(&DomainUser{
	    Id: 1,
	    Name: "NewUserName"),
    },
    true,
    map[string]bool{"Name": true},
) // return error
delete row/rows
corm := customorm.Init(db)
corm.DeleteRowById(&DomainUser{Id: 1}) // return error
corm.DeleteRowByArgId(&DomainUser{}, 1) // return error
corm.DeleteRows(&DomainUser{Enabled: false}, map[string]bool{"Enabled": true}) // return error
select row/rows
corm := customorm.Init(db)
corm.GetDataAll(&DomainUser{}, true) // return interface{}, error

filter := customOrm.Filters{
            Fields map[string]FilterFields{
                "Name": {
                    Flag: true,             // use this filter
                    UseValue false,         // use value from struct or from value field below
                    Value nil,              // can use this value instead value from struct
                    Operand: "",            // operand to compare with value of (<,>,=,CONTAINS,IN) default "="
                },
            },
            Order customOrm.Order{
                Desc: true,                 // sort DESC
                Fields: []string{"Id"}      // sort by fields names
            }
            Limit: 10,
            Offset: 0,
            Count: false,                   // if true returns count(*) instead of fields
}

corm.GetDataByValue(
    &DomainUser{Id: 1},
    filter, 
    false,                                  // return interface will contain slice of results(map if true)
) // return interface{}, error

Documentation

Index

Constants

View Source
const (
	OperandEqual    = "="
	OperandMore     = ">"
	OperandLess     = "<"
	OperandNotEqual = "<>"
	OperandContains = "CONTAINS"
	OperandIn       = "IN"
)

Variables

This section is empty.

Functions

func GetTableName

func GetTableName(i interface{}) string

func ToSnakeCase

func ToSnakeCase(str string) string

func ValuesEqualPlaceholders

func ValuesEqualPlaceholders(sl []string) string

func ValuesEqualPlaceholdersAnd

func ValuesEqualPlaceholdersAnd(sl []string) string

func ValuesPlaceholders

func ValuesPlaceholders(sl []string) string

Types

type CORM

type CORM struct {
	// contains filtered or unexported fields
}

func Init

func Init(db *sql.DB) *CORM

func (*CORM) CreateTable

func (c *CORM) CreateTable(s interface{}) bool

func (*CORM) DeleteRowByArgId

func (c *CORM) DeleteRowByArgId(s interface{}, id int64) error

func (*CORM) DeleteRowById

func (c *CORM) DeleteRowById(s interface{}) error

func (*CORM) DeleteRows

func (c *CORM) DeleteRows(s interface{}, fieldNames map[string]bool) error

func (*CORM) GetDataAll

func (c *CORM) GetDataAll(s interface{}, asMap bool) (interface{}, error)

rarely used

func (*CORM) GetDataById

func (c *CORM) GetDataById(s interface{}, id int64) (interface{}, error)

func (*CORM) GetDataByValue

func (c *CORM) GetDataByValue(s interface{}, filter Filters, asMap bool) (interface{}, error)

func (*CORM) GetTable

func (c *CORM) GetTable(s interface{}) (Table, error)

func (*CORM) InsertRow

func (c *CORM) InsertRow(s interface{}) (int64, error)

func (*CORM) MovePosition

func (c *CORM) MovePosition(table Table) error

func (*CORM) UpdateRow

func (c *CORM) UpdateRow(s interface{}, onlyFields bool, fieldNames map[string]bool) error

type Column

type Column struct {
	Name       string
	FieldName  string
	Value      interface{}
	Type       string
	Attr       string
	IsPosition bool
	Default    string
	Check      string
}

type FKey

type FKey struct {
	ColumnName      string
	ColumnValue     interface{}
	TableName       string
	TableColumnName string
	Type            reflect.Type
	FieldName       string
	IsNull          bool
}

type FilterFields

type FilterFields struct {
	Flag     bool
	UseValue bool
	Value    interface{}
	Operand  string
}

type Filters

type Filters struct {
	Fields map[string]FilterFields
	Order  Order
	Limit  int
	Offset int
	Count  bool
}

type Order

type Order struct {
	Desc   bool     `json:"desc"`
	Fields []string `json:"fields"`
}

type Row

type Row interface {
	GetTableName() string
}

type Table

type Table struct {
	Name     string
	Columns  []Column
	FKeys    []FKey
	Uniq     []string
	Instance interface{}
}

func (*Table) ImportTableData

func (table *Table) ImportTableData()

func (*Table) Test

func (table *Table) Test(s interface{})

Jump to

Keyboard shortcuts

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