Categorygithub.com/volts-dev/orm
modulepackage
0.0.0-20241214184204-de246cbee03a
Repository: https://github.com/volts-dev/orm.git
Documentation: pkg.go.dev

# README

Warning: This package is a work in progress. After some preliminary testing, I think I might change a lot of how this package is structured. The Volts'ORM library for Golang, aims to be developer friendly.

Overview

  • Domain Parser (String type filter)
  • Dataset (Full data type covert interface)
  • Developer Friendly

可扩展定制范围

  • 支持Model扩展 (扩展处理方法和接口等,让model可带有其他中间件等特殊功能)
  • 支持Tag扩展 (id,int,many2many...)
  • 支持数据库扩展 (目前只有postgresql)
  • 支持字段格式转换扩展 (字段读写数据库时格式化)

1.定义数据表模型

type (
	// PartnerModel save all the records about a company/person/group
	PartnerModel struct {
		orm.TModel `table:"name('partner_model')"`
		Id         int64  `field:"pk autoincr title('ID') index"`
		Name       string `field:"char() unique index required"`
		Homepage   string `field:"char() size(25)"`
	}

	// CompanyModel save all the records about a shop/subcompany,
	// main details will relate to PartnerModel and mapping all field of PartnerModel
	CompanyModel struct {
		orm.TModel   `table:"name('company_model')"`
		PartnerModel `field:"relate(PartnerId)"`
		PartnerId    int64         `field:"one2one(partner_model)"`
		Id           int64         `field:"pk autoincr title('ID') index"`
		Name         string        `field:"char() unique index required"`
		OneToMany    []interface{} `field:"one2many(user_model,many_to_one) title('Test Title') domain([('active','=',True)])"`
	}

	UserModel struct {
		orm.TModel   `table:""`
		PartnerModel `field:"relate(PartnerId)"`
		PartnerId    int64     `field:"one2one(partner_model)"`
		CompanyId    int64     `field:"many2one(company_model)"` //-- Company
		Id           int64     `field:"pk autoincr type(char) title('ID') index"`
		Uid          int64     `field:"Id() pk  title('ID') index"`
		CreateTime   time.Time `field:"datetime() created"`
		WriteTime    time.Time `field:"datetime() updated"`

		Name  string `field:"char() size(15) unique index required"`
		Title string `field:"title('Test Title.')"`
		Help  string `field:"help('Technical field, used only to display a help string using multi-rows. 
				 test help 1\"
                 test help 2''
                 test help 3''''
                 test help 4.')"`

		// all data types
		Int        int           `field:"int() default(1)"`    // --
		Bool       bool          `field:"bool default(true)"`  // --
		Text       string        `field:"text"`                //
		Float      float32       `field:"float"`               //
		Bin        []byte        `field:"binary() attachment"` //
		Selection  string        `field:"selection('{\"person\":\"Individual\",\"company\":\"Company\"}')"`
		Func       string        `field:"selection(TestSelection)"`
		ManyToMany []interface{} `field:"many2many(company_model,company_id,user_id)"`
	}
)

2.同步映射模型到ORM

这里不需要当心同步模型的顺序,"test"只是区分这些模型在不同包或者文件夹的标志,可以是任何字符串

	Orm, err = orm.NewOrm(DataSource)
	if err != nil {
		// todo ...
	}
	
	Orm.SyncModel("test",
		new(PartnerModel),
		new(CompanyModel),
		new(UserModel),
	)

3.获取表模型

这里几乎可以在任何包里调用并获取模型,而不必担心golang包的交叉引用限制.参数"test"可以省略

	model, err := Orm.GetModel("user_model","test")
	if err != nil {
		// todo ...
	}

4.数据查询修改

	dataset, err := model.Records().Read()
	if err != nil {
		// todo ...
	}

	// 获取数据量
	if dataset.Count() == 0 {
		self.Fatal("please add some record first!")
	}
	
	// 遍历数据集
	dataset.Frist()
	if !dataset.Eof(){
		name:=dataset.Record().FieldByName("name").AsString("123")
	   	fmt.Println(name)	
		
		dataset.Next()
	}
	
	// 获取当前字段值
	id:=dataset.FieldByName("id").AsString() // the value is string
	id:=dataset.FieldByName("id").AsInteger() // the value is int
	id:=dataset.FieldByName("id").AsInterface() // the value is interface{}
	
	// 修改数据
	dataset.FieldByName("id").AsString("123") // the value is string
	
	// 写回数据库
	model.Records().Write(dataset.Record().AsItfMap())
	
	更多请查询Test目录测试例子
	....

扩展模型方法

数据表模型可以通过继承IModel(接口)/TModel(实现)达到为每个数据模型扩展方法的基模型

	IBaseModel interface{}{
		orm.IModel
		ReadFrist100()*TDataset,err
		...
	}
	
	TBaseModel struct{
		orm.TModel
		...
	}
	
	func(self *TBaseModel)ReadFrist100()*TDataset,err{
		// todo...
	}
	
	// 新接口获取模型
	GetModel(name string)IBaseModel{
		model,_:=orm.GetModel(modelName, module_name)
		
		return model.(IBaseModel)
	}

数据表模型继承基模型

	UserModel struct{
		TBaseModel
		...
	}
	
	// 映射注册
	orm.SyncModel("",new(UserModel))

获取模型

	user:=GetModel("user_model")
	ds,_:=user.ReadFrist100()

#自定义字段类型 #自定义返回TDataset 数据集

一个model表示一系列的函数接口的封装 一个model对应一些基本的功能,一般地,对应与一张表的操作。 传入参数,传出字典(而不是包装后的类)。即全部操作直接对应model里面的方法。 model最后统一在manager里面实例化,需要被调用的model绑定到manager里面。 一些高层的功能另外用model来揉合(调用胶水层)

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

ColumnString generate column description string according dialect.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Type2SQLType generate SQLType acorrding Go's type.
No description provided by the author
FIXME 优化.
No description provided by the author
TODO 使用函数配置参数 create a new ORM instance */.
No description provided by the author
No description provided by the author
Initialize the ExtendedLeaf :attr [string, tuple] leaf: operator or tuple-formatted domain expression :attr obj model: current working model :attr list models: list of chained models, updated when adding joins :attr list join_context: list of join contexts.
sqlType:接受数据类型SQLType/string.
No description provided by the author
No description provided by the author
TODO 改名外键.
No description provided by the author
No description provided by the author
QueryDialect query if registed database dialect.
No description provided by the author
RegisterDialect register database dialect.
No description provided by the author
Register makes a log provide available by the provided name.
Register makes a log provide available by the provided name.
default sql type change to go types.
"HelloWorld" to "hello_world" to "HW".
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ERROR.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
maximum number of prefetched records.
TODO remove.
No description provided by the author
No description provided by the author
#使用集中存储二进制模式 可以是表/目录/云上.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
#经典模式.
#经典模式.
No description provided by the author
#日期.
No description provided by the author
TODO.
.
No description provided by the author
# 函数赋值.
#字段描述.
type.
#索引字段.
忽略某些继承者成员.
#该字段继承来自X表X字段名称 //name = openerp.fields.Char(related='partner_id.name', inherited=True).
#postgres 的继承功能.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
#字段名称.
No description provided by the author
#将被更换的名称.
TODO.
No description provided by the author
No description provided by the author
TODO.
No description provided by the author
No description provided by the author
关系表 用于多对多等.
No description provided by the author
#select=True (在外键字段上创建了一个索引).
.
No description provided by the author
# 函数赋值.
No description provided by the author
No description provided by the author
.
No description provided by the author
TODO.
******* table tags ********.
No description provided by the author
No description provided by the author
No description provided by the author
#完整时间 包含时区.
#字段显示名称.
TODO.
No description provided by the author
#保持唯一.
No description provided by the author
No description provided by the author
TODO.
No description provided by the author
No description provided by the author
No description provided by the author
字段读写模式.
No description provided by the author
enumerates all columns types.

# Variables

No description provided by the author
ORM,MYSQL,PG.
PG.
MYSQL.
MYSQL.
No description provided by the author
No description provided by the author
ORM,MYSQL.
ORM MYSQL.
PG.
No description provided by the author
PG.
No description provided by the author
No description provided by the author
ORM,MYSQL,PG.
.
No description provided by the author
No description provided by the author
ORM,MYSQL,PG.
ORM,MYSQL.
MYSQL,PG.
DefaultPostgresSchema default postgres schema.
ORM,MYSQL,PG.
MYSQL.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ORM,MYSQL.
No description provided by the author
No description provided by the author
No description provided by the author
ORM,MYSQL.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PG.
GOLANG DATATYPE TYPE.
ITF_TYPE 作为Scan从数据库扫描存储的数据容器.
ORM.
No description provided by the author
special columns automatically created by the ORM.
MYSQL.
.
No description provided by the author
MYSQL.
ORM,MYSQL.
.
PG.
.
.
PG.
.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
GOLANG DATATYPE TYPE PTR.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
PG.
PG.
MYSQL.
No description provided by the author
ORM,MYSQL,PG.
.
PG.
No description provided by the author
No description provided by the author
.
ORM,MYSQL,PG.
MYSQL,PG.
MYSQL,PG.
PG.
No description provided by the author
MYSQL.
ORM,MYSQL.
.
ORM.
ORM.
ORM.
ORM.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
.
ORM.
ORM.
ORM.
ORM.
ORM.
PG.
MYSQL.
ORM,MYSQL,PG.
object types */.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
IFmter is an interface to Formatter SQL.
IFmter is an interface to Formatter SQL.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
IFmter is an interface to Formatter SQL.
No description provided by the author
SQL types.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
IFmter is an interface to Formatter SQL.
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
* 字段Tag * */.
No description provided by the author
No description provided by the author
No description provided by the author