# Quickstart

### Init Project

First, use `go mod init` to initialize the project and add GoooQo by:

```
go get -u github.com/doytowin/goooqo/rdb
```

Then, initialize the database connection and transaction manager as follows:

```go
package main

import (
	"database/sql"
	"github.com/doytowin/goooqo/rdb"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db := rdb.Connect("app.properties")
	defer rdb.Disconnect(db)
	tm := rdb.NewTransactionManager(db)

	//...
}
```

### Build DataAccess

Suppose we have the following user table in `test.db`:

<table><thead><tr><th>id</th><th>name</th><th data-type="number">score</th><th>memo</th></tr></thead><tbody><tr><td>1</td><td>Alley</td><td>80</td><td>Good</td></tr><tr><td>2</td><td>Dave</td><td>75</td><td>Well</td></tr><tr><td>3</td><td>Bob</td><td>60</td><td></td></tr><tr><td>4</td><td>Tim</td><td>92</td><td>Great</td></tr><tr><td>5</td><td>Emy</td><td>100</td><td>Great</td></tr></tbody></table>

We define an entity object and a query object for the table:

{% code title="user.go" %}

```go
package main

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

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

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

type UserQuery struct {
	PageQuery
	ScoreLt   *int
	MemoStart *string
	// ...
}
```

{% endcode %}

The fields of the entity object correspond to the columns of the table, and the fields of the query object are based on the query conditions of the requirements.

Then we define a `userDataAccess` to access the table:

```go
userDataAccess := rdb.NewTxDataAccess[UserEntity](tm)

userQuery := UserQuery{PageQuery: PageQuery{Page: 5, Size: 10}, ScoreLt: P(80)}
userEntities, err := userDataAccess.Query(ctx, userQuery)
```

This will generate and execute the following SQL:

```sql
SELECT id, name, score, memo FROM t_user WHERE score < ? LIMIT 10 OFFSET 40
```

### Related Documents

<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td></td><td>Define an entity object</td><td></td><td><a href="/pages/OUsrtuabLBHBSIEnCOGx">/pages/OUsrtuabLBHBSIEnCOGx</a></td></tr><tr><td></td><td>Define a query object</td><td></td><td><a href="/pages/DbcpkkQ6xQmUBzjz98zT">/pages/DbcpkkQ6xQmUBzjz98zT</a></td></tr><tr><td></td><td>Create DataAccess</td><td></td><td><a href="/pages/81sqmRGj2KUyCouwhl3f">/pages/81sqmRGj2KUyCouwhl3f</a></td></tr><tr><td></td><td>Database Connection</td><td></td><td><a href="/pages/s9zsiS7aIsQ0s5nXzn76">/pages/s9zsiS7aIsQ0s5nXzn76</a></td></tr><tr><td></td><td>Transaction</td><td></td><td><a href="/pages/f0VRvrkPkifAppOc5FXp">/pages/f0VRvrkPkifAppOc5FXp</a></td></tr></tbody></table>


---

# 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/quickstart.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.
