# Page Query

## Definition

`PageQuery` is a ​**​generic pagination query parameter structure​**​ designed to standardize common list query requirements such as pagination and sorting. `PageQuery` has three fields, `PageNumber` and `PageSize` is used to build the paging clause, and `Sort` is used to build the sorting clause. `PageQuery` implements the `Query` interface.

```go
package core

type PageQuery struct {
	Page int    `json:"page,omitempty"`
	Size int    `json:"size,omitempty"`
	Sort string `json:"sort,omitempty"`
}
```

## Usage

When defining query objects, embed the `PageQuery` struct through composition to reuse standardized pagination and sorting functionality.

```go
type UserQuery struct {
    PageQuery
    ScoreLt  *int
    MemoNull *bool
    Deleted  *bool
}
```

## Paging

```go
userQuery := UserQuery{PageQuery: PageQuery{}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User

userQuery := UserQuery{PageQuery: PageQuery{Size: 20}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User LIMIT 20 OFFSET 0

// When only PageNumber is set, PageSize will be set to 10
userQuery := UserQuery{PageQuery: PageQuery{Page: 5}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User LIMIT 10 OFFSET 40

userQuery := UserQuery{PageQuery: PageQuery{Page: 3, Size: 50}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User LIMIT 50 OFFSET 100	
```

## Sorting

The `Sort` string should follow `regexp.MustCompile("(?i)(\w+)(,(asC|dEsc))?;?")`

```go
userQuery := UserQuery{PageQuery: PageQuery{Sort: "id,desc;score,asc;memo"}}
users, err := userDataAccess.Query(ctx, userQuery)
//SELECT id, score, memo FROM User ORDER BY id DESC, score ASC, memo
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://goooqo.docs.doyto.win/query-mapping/page-query.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
