本文整理汇总了Golang中github.com/stellar/gateway/db/entities.Entity类的典型用法代码示例。如果您正苦于以下问题:Golang Entity类的具体用法?Golang Entity怎么用?Golang Entity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Entity类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: Persist
// Persist persists an object in DB.
//
// If `object.IsNew()` equals true object will be inserted.
// Otherwise, it will found using `object.GetId()` and updated.
func (em EntityManager) Persist(object entities.Entity) (err error) {
if object.IsNew() {
_, err = em.driver.Insert(object)
} else {
err = em.driver.Update(object)
}
return
}
开发者ID:FihlaTV,项目名称:bridge-server,代码行数:12,代码来源:entity_manager.go
示例2: Insert
// Insert inserts the entity to a DB
func (d *Driver) Insert(object entities.Entity) (id int64, err error) {
value, tableName, err := getTypeData(object)
if err != nil {
return 0, err
}
fieldsCount := value.NumField()
var fieldNames []string
var fieldValues []string
for i := 0; i < fieldsCount; i++ {
field := value.Field(i)
tag := field.Tag.Get("db")
if tag == "" {
continue
}
fieldNames = append(fieldNames, tag)
fieldValues = append(fieldValues, ":"+tag)
}
query := "INSERT INTO " + tableName + " (" + strings.Join(fieldNames, ", ") + ") VALUES (" + strings.Join(fieldValues, ", ") + ");"
var result sql.Result
switch object := object.(type) {
case *entities.AuthorizedTransaction:
result, err = d.database.NamedExec(query, object)
case *entities.AllowedFi:
result, err = d.database.NamedExec(query, object)
case *entities.AllowedUser:
result, err = d.database.NamedExec(query, object)
case *entities.SentTransaction:
result, err = d.database.NamedExec(query, object)
case *entities.ReceivedPayment:
result, err = d.database.NamedExec(query, object)
}
if err != nil {
return
}
id, err = result.LastInsertId()
if id == 0 {
// Not autoincrement
if object.GetID() == nil {
return 0, fmt.Errorf("Not autoincrement but ID nil")
}
id = *object.GetID()
}
if err == nil {
object.SetID(id)
object.SetExists()
}
return
}
开发者ID:stellar,项目名称:bridge-server,代码行数:59,代码来源:main.go
示例3: GetOne
// GetOne returns a single entity based on a seach conditions
func (d *Driver) GetOne(object entities.Entity, where string, params ...interface{}) (entities.Entity, error) {
_, tableName, err := getTypeData(object)
if err != nil {
return nil, err
}
err = d.database.Get(object, "SELECT * FROM "+tableName+" WHERE "+where+" LIMIT 1;", params...)
if err != nil {
if err.Error() == "sql: no rows in result set" {
return nil, nil
}
return nil, err
}
object.SetExists() // Mark this entity as existing
return object, err
}
开发者ID:stellar,项目名称:bridge-server,代码行数:17,代码来源:main.go
示例4: Insert
// Insert inserts the entity to a DB
func (d *Driver) Insert(object entities.Entity) (id int64, err error) {
value, tableName, err := getTypeData(object)
if err != nil {
return 0, err
}
fieldsCount := value.NumField()
var fieldNames []string
var fieldValues []string
for i := 0; i < fieldsCount; i++ {
field := value.Field(i)
tag := field.Tag.Get("db")
if tag == "" {
continue
}
if tag == "id" && object.GetID() == nil {
// To handle error:
// null value in column "id" violates not-null constraint
continue
}
fieldNames = append(fieldNames, tag)
fieldValues = append(fieldValues, ":"+tag)
}
query := "INSERT INTO " + tableName + " (" + strings.Join(fieldNames, ", ") + ") VALUES (" + strings.Join(fieldValues, ", ") + ") RETURNING id;"
// TODO cache prepared statement
stmt, err := d.database.PrepareNamed(query)
if err != nil {
return
}
switch object := object.(type) {
case *entities.AuthorizedTransaction:
err = stmt.Get(&id, object)
case *entities.SentTransaction:
err = stmt.Get(&id, object)
case *entities.ReceivedPayment:
err = stmt.Get(&id, object)
}
if err != nil {
return
}
if id == 0 {
// Not autoincrement
if object.GetID() == nil {
return 0, fmt.Errorf("Not autoincrement but ID nil")
}
id = *object.GetID()
}
if err == nil {
object.SetID(id)
object.SetExists()
}
return
}
开发者ID:FihlaTV,项目名称:bridge-server,代码行数:65,代码来源:main.go
注:本文中的github.com/stellar/gateway/db/entities.Entity类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论