query

package
v0.1.1-beta.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultQuerier = Querier{}

Functions

func BindRelation

func BindRelation(content contenttype.ContentTyper) error

FinishBind sets related data after data binding. It will be better if SQLBoiler support interface for customized binding for struct.

func Children

func Children(ctx context.Context, userID int, parentContent contenttype.ContentTyper, contenttype string, cond db.Condition) ([]contenttype.ContentTyper, int, error)

Children fetches children content directly under the given parentContent

Example
rootContent, _ := FetchByID(context.Background(), 1)

//fetch all folders directly under root. Can filter if valid condition parameter is provided.
children, _, _ := Children(context.Background(), 1, rootContent, "folder", db.EmptyCond())

fmt.Println(len(children) > 0)
Output:

true

func Fetch

func Fetch(ctx context.Context, contentType string, condition db.Condition) (contenttype.ContentTyper, error)

Fetch fetches first content based on condition.

Example
//Fetch folder whose name is "Content", only first will be returned
content, err := Fetch(context.Background(), "folder", db.Cond("l.name", "Content"))
if err == nil && content != nil {
	fmt.Println(content.(*entity.Folder).ID)
}
Output:

1

func FetchByCID

func FetchByCID(ctx context.Context, contentType string, contentID int) (contenttype.ContentTyper, error)

FetchByCID fetches a content by content id.

Example
//Fetch folder which is content id(cid) 1
content, err := FetchByCID(context.Background(), "folder", 1)
if err == nil && content != nil {
	fmt.Println(content.(*entity.Folder).ContentID)
}
Output:

1

func FetchByCUID

func FetchByCUID(ctx context.Context, contentType string, cuid string) (contenttype.ContentTyper, error)

FetchByCUID fetches a content by content's uid(cuid)

func FetchByID

func FetchByID(ctx context.Context, contentType string, contentID int) (contenttype.ContentTyper, error)

FetchByCID is duplicate of FetchByCID

func FetchByLID

func FetchByLID(ctx context.Context, locationID int) (contenttype.ContentTyper, error)

FetchByLID fetches content by location id. If no location found. it will return nil and error message.

Example
//Fetch folder which has location id 1
content, err := FetchByLID(context.Background(), 1)
if err == nil && content != nil {
	fmt.Println(content.(*entity.Folder).ContentID)
}
Output:

1

func FetchByPath

func FetchByPath(ctx context.Context, path string) (contenttype.ContentTyper, error)

func FetchByUID

func FetchByUID(ctx context.Context, uid string) (contenttype.ContentTyper, error)

FetchByUID fetches content by unique id

func FetchLocation

func FetchLocation(ctx context.Context, cond db.Condition) (contenttype.Location, error)

func FetchLocationByID

func FetchLocationByID(ctx context.Context, locationID int) (contenttype.Location, error)

func FetchUserRoles

func FetchUserRoles(ctx context.Context, userID int) ([]contenttype.ContentTyper, error)

func GetContentAuthor

func GetContentAuthor(content contenttype.ContentTyper) (contenttype.ContentTyper, error)

func GetUser

func GetUser(id int) (contenttype.ContentTyper, error)

func List

func List(ctx context.Context, contentType string, condition db.Condition) ([]contenttype.ContentTyper, int, error)

List without considering permission

Example
//fetch list of folders where id larger than 2
list, _, _ := List(context.Background(), "folder", db.Cond("l.id>", 2))
fmt.Println(list[0].GetLocation().ID)
Output:

3
Example (SortLimit)
//fetch list ordered by modified desc, limit 0,4
list, _, _ := List(context.Background(), "folder", db.EmptyCond().Sortby("modified desc").Limit(0, 4))

fmt.Println(len(list))
Output:

4

func ListWithUser

func ListWithUser(ctx context.Context, userID int, contentType string, condition db.Condition) ([]contenttype.ContentTyper, int, error)

ListWithUser fetches a list of content which the user has read permission to. Note: If you have parent condition, use SubList, because this will not optimize 'under' policy and parent paramter

Example
//fetch list of folders where id larger than 2
list, _, _ := ListWithUser(context.Background(), 1, "folder", db.Cond("l.id>", 2))
fmt.Println(list[0].GetLocation().ID)
Output:

3

func LocationList

func LocationList(condition db.Condition) ([]contenttype.Location, int, error)

func Output

Output converts content into output format.(eg. add text to select, name in relation, etc)

func OutputField

func OutputField(ctx context.Context, content contenttype.ContentTyper, field string) (interface{}, error)

func OutputList

func OutputList(ctx context.Context, contentList []contenttype.ContentTyper) ([]contenttype.ContentMap, error)

OutputList converts contents into output format, see Output for single content.

func RelationOptions

func RelationOptions(ctx context.Context, ctype string, identifier string, limit []int, sortby []string, hasCount bool) ([]contenttype.ContentTyper, int, error)

GetRelationOptions get content list based on relation parameters

func SubList

func SubList(ctx context.Context, userID int, rootContent contenttype.ContentTyper, contentType string, depth int, condition db.Condition) ([]contenttype.ContentTyper, int, error)

SubList fetches content list with permission considered(only return contents the user has access to).

Example
rootContent, _ := FetchByID(context.Background(), 1)
//Fetch articles(level in 3) under root
list, _, _ := SubList(context.Background(), 1, rootContent, "article", 3, db.EmptyCond())

fmt.Println(len(list) > 0)
Output:

true
Example (WithSortLimit)
rootContent, _ := FetchByID(context.Background(), 1)
//Fetch articles(level in 3) under root

list, _, _ := SubList(context.Background(), 1, rootContent, "article", 3, db.EmptyCond().Sortby("c.modified desc").Limit(0, 3))

fmt.Println(len(list))
Output:

3

func Version

func Version(ctx context.Context, contentType string, condition db.Condition) (contenttype.Version, contenttype.ContentTyper, error)

return a version content

Types

type Querier

type Querier struct {
}

func (Querier) Fetch

func (q Querier) Fetch(ctx context.Context, contentType string, condition db.Condition) (contenttype.ContentTyper, error)

func (Querier) List

func (q Querier) List(ctx context.Context, contentType string, condition db.Condition) ([]contenttype.ContentTyper, int, error)

List fetches a list of content based on conditions. This is a database level 'list' WITHOUT permission check.

For permission included, use query.ListWithUser For list under a tree root, use query.SubList

Condition example:

db.Cond("l.parent_id", 4).Cond("author", 1).Cond("modified >", "2020-03-13")

where content field can be used directly or with c. as prefix(eg. "c.author"), but location field need a l. prefix(eg. l.parent_id)

type TreeNode

type TreeNode struct {
	*contenttype.Location
	Name     string                   `json:"name"`
	Fields   interface{}              `json:"fields"` //todo: maybe more generaic attributes instead of hard coded 'Fields' here, or use custom MarshalJSON(remove *Locatoin then)?
	Content  contenttype.ContentTyper `json:"content"`
	Children []TreeNode               `json:"children"`
}

TreeNode is a query result when querying SubTree

func SubTree

func SubTree(ctx context.Context, userID int, rootContent contenttype.ContentTyper, depth int, contentTypes string, sortby []string) (TreeNode, error)

SubTree fetches content and return a tree result under rootContent, permission considered. See TreeNode for the tree structure

Example
rootContent, _ := FetchByID(context.Background(), 1)
//fetch trees of root for 3 levels
treenode, _ := SubTree(context.Background(), 1, rootContent, 3, "folder,article", []string{})

level := 0
treenode.Iterate(func(node *TreeNode) {
	level++
})
fmt.Println(level > 3)
Output:

true

func (*TreeNode) Iterate

func (tn *TreeNode) Iterate(operation func(node *TreeNode))

Iterate loops all nodes in the tree

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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