GoooQo v0.2.3
HomeGitHubDemo
English
English
  • Introduction
  • Quickstart
  • API
    • Connection
    • Transaction
    • CRUD
    • Association Service
  • Entity Mapping
    • Entity Object
    • Related Entities
  • Query Mapping
    • Query Object
      • Predicate-Suffix Field
      • Logic-Suffix Field
      • Subquery Field
      • E-R Query Field
      • Custom Condition Field
    • Page Query
  • Aggregate Query
    • View Object
    • Having
    • Natural Join
    • Outer Join
    • Nested View
  • Advanced
    • Dialect
    • Locking
  • Related Resources
    • Articles
      • From ORM to OQM: An Object-Only SQL Construction Solution
      • Introduction to GoooQo
      • How to express `select * from user where id = ? or name = ? and age = ?` in GoooQo
Powered by GitBook
On this page
  • Example
  • Definition

Was this helpful?

Edit on GitHub
  1. Entity Mapping

Entity Object

Example

import (
	. "github.com/doytowin/goooqo"
)

type UserEntity struct {
	Int64Id
	Name  *string `json:"name,omitempty"`
	Score *int    `json:"score,omitempty"`
	Memo  *string `json:"memo,omitempty"`
}

func (u UserEntity) GetTableName() string {
	return "t_user"
}

Definition

An entity object is used to provide the table name and column names for CRUD statements construction in GoooQo.

The entity struct needs to implement the following interface:

package core

type Entity interface {
	GetId() any

	// SetId set id to self.
	// self: the pointer points to the current entity.
	// id: type could be int, int64, or string so far.
	SetId(self any, id any) error
}


package rdb

import "github.com/doytowin/goooqo/core"

type RdbEntity interface {
	core.Entity
	GetTableName() string
}
  • GetId is used to build a Update statement.

  • SetId is used to set the generated ID to an entity.

  • GetTableName is used to provide the table name corresponding to the entity.

  • Each field in the entity needs to correspond to a column in the table.

GoooQo provides two Entity implementations, IntId and Int64Id, to simplify entity definition.

The CRUD statements of the UserEntity in the example are:

SELECT id, name, score, memo FROM t_user;
INSERT INTO t_user (name, score, memo) VALUES (?, ?, ?)
UPDATE t_user SET name = ?, score = ?, memo = ? WHERE id = ?;
DELETE FROM t_user WHERE id = ?;

PreviousAssociation ServiceNextRelated Entities

Last updated 8 months ago

Was this helpful?