mirror of https://github.com/VinGarcia/ksql.git
112 lines
2.2 KiB
Go
112 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/vingarcia/kissorm"
|
|
"github.com/vingarcia/kissorm/nullable"
|
|
)
|
|
|
|
// User ...
|
|
type User struct {
|
|
ID int `kissorm:"id"`
|
|
Name string `kissorm:"name"`
|
|
Age int `kissorm:"age"`
|
|
}
|
|
|
|
// PartialUpdateUser ...
|
|
type PartialUpdateUser struct {
|
|
ID int `kissorm:"id"`
|
|
Name *string `kissorm:"name"`
|
|
Age *int `kissorm:"age"`
|
|
}
|
|
|
|
func main() {
|
|
ctx := context.Background()
|
|
db, err := kissorm.New("sqlite3", "/tmp/hello.sqlite", 1, "users")
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
err = db.Exec(ctx, `CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY,
|
|
age INTEGER,
|
|
name TEXT
|
|
)`)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
var alison = User{
|
|
Name: "Alison",
|
|
Age: 22,
|
|
}
|
|
err = db.Insert(ctx, &alison)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
fmt.Println("Alison ID:", alison.ID)
|
|
|
|
// Inserting inline:
|
|
err = db.Insert(ctx, &User{
|
|
Name: "Cristina",
|
|
Age: 27,
|
|
})
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
// Deleting Alison:
|
|
err = db.Delete(ctx, alison.ID)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
// Retrieving Cristina:
|
|
var cris User
|
|
err = db.QueryOne(ctx, &cris, "SELECT * FROM users WHERE name = ? ORDER BY id", "Cristina")
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
fmt.Printf("Cristina: %#v\n", cris)
|
|
|
|
// Updating all fields from Cristina:
|
|
cris.Name = "Cris"
|
|
err = db.Update(ctx, cris)
|
|
|
|
// Changing the age of Cristina but not touching any other fields:
|
|
|
|
// Partial update technique 1:
|
|
err = db.Update(ctx, struct {
|
|
ID int `kissorm:"id"`
|
|
Age int `kissorm:"age"`
|
|
}{ID: cris.ID, Age: 28})
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
// Partial update technique 2:
|
|
err = db.Update(ctx, PartialUpdateUser{
|
|
ID: cris.ID,
|
|
Age: nullable.Int(28),
|
|
})
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
// Listing first 10 users from the database
|
|
// (each time you run this example a new Cristina is created)
|
|
//
|
|
// Note: Using this function it is recommended to set a LIMIT, since
|
|
// not doing so can load too many users on your computer's memory or
|
|
// cause an Out Of Memory Kill.
|
|
var users []User
|
|
err = db.Query(ctx, &users, "SELECT * FROM users LIMIT 10")
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
fmt.Printf("Users: %#v\n", users)
|
|
}
|