I have two models:
type MainFields struct {
Id int `orm:"auto"`
Created time.Time `orm:"auto_now_add;type(datetime)"`
Updated time.Time `orm:"auto_now;type(datetime)"`
}
type Game struct {
MainFields
Players []*Player `orm:"rel(m2m)"`
}
type Player struct {
MainFields
Games []*Game `orm:"reverse(many)"`
NickName string
}
And with this code i`am trying to create new game with one player:
func insertTestData() {
var playerA models.Player
playerA.NickName = "CoolDude"
id, err := models.ORM.Insert(&playerA)
if err != nil {
log.Printf(err.Error())
} else {
log.Printf("Player ID: %v", id)
}
var game models.Game
game.Players = []*models.Player{&playerA}
id, err = models.ORM.Insert(&game)
if err != nil {
log.Printf(err.Error())
} else {
log.Printf("Game ID: %v", id)
}
}
But it just create two inserts for game and player without rel-connection through "game_players" table which created automatically with orm.RunSyncdb().
2016/09/29 22:19:59 Player ID: 1
[ORM]2016/09/29 22:19:59 -[Queries/default] - [ OK / db.QueryRow / 11.0ms] - [INSERT INTO "player" ("created", "updated", "nick_name") VALUES ($1, $2, $3) RETURNING "id"] - `2016-09-29 22:19:59.8615846 +1000 VLAT`, `2016-09-29 22:19:59.8615846 +1000 VLAT`, `CoolDude`
2016/09/29 22:19:59 Game ID: 1
[ORM]2016/09/29 22:19:59 -[Queries/default] - [ OK / db.QueryRow / 11.0ms] - [INSERT INTO "game" ("created", "updated") VALUES ($1, $2) RETURNING "id"] - `2016-09-29 22:19:59.8725853 +1000 VLAT`, `2016-09-29 22:19:59.8725853 +1000 VLAT`
I can`t find any special rules for working with m2m-models in docs and ask for help to community. How should i insert new row in table?
According to this, you have to make a m2m object, after creating object game, like this:
m2m := models.ORM.QueryM2M(&game, "Players")
And instead of game.Players = []*models.Player{&playerA}, you write:
num, err := m2m.Add(playerA)
So, your function must look like this:
func insertTestData() {
var playerA models.Player
playerA.NickName = "CoolDude"
id, err := models.ORM.Insert(&playerA)
if err != nil {
log.Printf(err.Error())
} else {
log.Printf("Player ID: %v", id)
}
var game models.Game
id, err = models.ORM.Insert(&game)
if err != nil {
log.Printf(err.Error())
} else {
log.Printf("Game ID: %v", id)
}
m2m := o.QueryM2M(&game, "Players")
num, err := m2m.Add(playerA)
if err == nil {
log.Printf("Added nums: %v", num)
}
}
I hope this helps.
P.S.: BTW, you were right, It wasn't necessary to specify the name of the m2m table.
Related
I have the following models
type Instance struct {
gorm.Model
Name string `gorm:"index:idx_name_and_group,unique"`
UserID uint
GroupID uint `gorm:"index:idx_name_and_group,unique"`
...
}
type Group struct {
gorm.Model
Name string `gorm:"unique;"`
Instances []Instance
...
}
I'm trying to get an instance by name and group name.
I can do it using the following code
func (r instanceRepository) FindByName(groupName string, instanceName string) (*model.Instance, error) {
var instance *model.Instance
var group *model.Group
err := r.db.
First(&group, "name = ?", groupName).Error
if err != nil {
return nil, err
}
err = r.db.
Where("name = ? and group_id = ?", instanceName, group.ID).
First(&instance).Error
return instance, err
}
But I'd like to turn it into one query. Any ideas about how to achieve that?
You can always use the Joins function to do SQL joins. Something like this:
func (r instanceRepository) FindByName(groupName string, instanceName string) (*model.Instance, error) {
var instance *model.Instance
err := r.db.
Joins("INNER JOIN groups g ON g.id = instances.group_id").
Where("g.name = ? AND instances.name = ?", groupName, instanceName).
First(&instance).Error
if err != nil {
return nil, err
}
return instance, err
}
I have database store function:
func (p *ProductsRep) FindAll(PageNumber int, PaginationSize int, Query string) []*postgresmodels.Product {
Also I have SQL query look like this:
SELECT * FROM table_name.
Then I want to concat conditional action like WHERE some_value=3 if some value (in this case Query) exists then I want to get SELECT * FROM table_name WHERE some_value=3.
I tried to use fmt.Sprintf to concat, or strings.Join, or bytes.Buffer.WriteString. But everytime I getting this error:
I replace real value for understanding:
pq: column "Some value" does not exist.
How can I do "adaptive" queries, which depends on inputed function values.
I believe you are trying to query rows in the database by using parameters.
You need to make sure you don't pass this data in as RAW values, due to the potential risk of SQL injection. You can make queries by using store procedures
You can use the function Query to pass in your query with your parameters. In the example case this is $1. If you wanted to you could add $2, $3... etc depending on how many parameters you wanted to query
Here is two examples
Postgres
using "github.com/jackc/pgx/v4" driver
ctx := context.Background()
type Bar struct {
ID int64
SomeValue string
}
rows, err := conn.Query(ctx, `SELECT * FROM main WHERE some_value=$1`, "foo")
if err != nil {
fmt.Println("ERRO")
panic(err) // handle error
}
defer rows.Close()
var items []Bar
for rows.Next() {
var someValue string
var id int64
if err := rows.Scan(&id, &someValue); err != nil {
log.Fatal(err) // handle error
}
item := Bar{
ID: id,
SomeValue: someValue,
}
items = append(items, item)
}
fmt.Println(items)
MySQL Driver
https://golang.org/pkg/database/sql/#DB.QueryRow
type Bar struct {
ID int64
SomeValue string
}
rows, err := conn.Query(`SELECT * FROM main WHERE some_value=$1`, "foo")
if err != nil {
fmt.Println("ERRO")
panic(err) // handle error
}
defer rows.Close()
var items []Bar
for rows.Next() {
var someValue string
var id int64
if err := rows.Scan(&id, &someValue); err != nil {
log.Fatal(err) // handle error
}
item := Bar{
ID: id,
SomeValue: someValue,
}
items = append(items, item)
}
fmt.Println(items)
I want to create a function that takes string argument, and depending of string returns different types
In my current project I am using Go version 1.11.4 for Rest API, PostgreSQL 11 for storing data.
The function is for avoiding the code redundancy, I just need one function for handling the requests, whithout this function I would be write more many request handlers.
Functions is:
func GetArrayOfModelStructs(table_name string) interface{} {
if table_name == "prefs_n_cities" {
Prefs_n_cities_array := &models.Prefs_n_cities_array{}
return Prefs_n_cities_array
} else if table_name == "position_owner" {
Position_owners := &models.Position_owners{}
return Position_owners
} else if table_name == "region" {
Regions := &models.Regions{}
return Regions
} else if table_name == "district" {
Districts := &models.Districts{}
return Districts
} else if table_name == "locality" {
Localities := &models.Localities{}
return Localities
} else if table_name == "locality_type" {
Locality_types := &models.Locality_types{}
return Locality_types
} else if table_name == "population" {
Populations := &models.Populations{}
return Populations
}
return nil
}
func GetModelStructs(table_name string) interface{} {
if table_name == "prefs_n_cities" {
return models.Prefs_n_cities{}
} else if table_name == "position_owner" {
return models.Position_owner{}
} else if table_name == "region" {
return models.Regions{}
} else if table_name == "district" {
return models.District{}
} else if table_name == "locality" {
return models.Locality{}
} else if table_name == "locality_type" {
return models.Locality_type{}
} else if table_name == "population" {
return models.Population{}
}
return nil
}
My structs example like:
type Prefs_n_cities struct {
id int32
Name string
Description string
Region_id int32
}
type Prefs_n_cities_array struct {
Array []Prefs_n_cities
}
And all of this I trying to use in another package for handling requests:
var GetData = func(res http.ResponseWriter, req *http.Request) {
// u.Logging(req, false)
db_name := req.URL.Query().Get("table_name")
data := u.GetArrayOfModelStructs(db_name)
spew.Dump(data)
sqlString := `SELECT * FROM "new_rollout_managment".` + db_name
rows, err := database.DB.Query(sqlString)
if err != nil {
http.Error(res, err.Error(), 400)
return
}
defer rows.Close()
for rows.Next() {
model := u.GetModelStructs(db_name)
err = rows.Scan(&model)
if err != nil {
http.Error(res, err.Error(), 400)
return
}
data.Array = append(data.Array, model)
}
err = rows.Err()
if err != nil {
http.Error(res, err.Error(), 400)
return
}
out, err := json.Marshal(models)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
resp := u.Message(true, "Array of dictionaries")
resp["Dictionaries"] = out
u.Respond(res, resp)
}
My router is:
func Use(router *mux.Router) {
router.HandleFunc("/api/test/new", controllers.TestFunc).Methods("POST")
router.HandleFunc("/api/dictionaries/getdata", controllers.GetData).Methods("GET")
}
But in the GetData handler I have an error:
data.Array undefined (type interface {} is interface with no methods)
You can't do that. But you have two options:
Return an interface{}, as you are doing, then convert to a concrete type. This probably defeats your purpose.
Pass in your destination as a pointer, and modify it in place. So something like:
func GetArrayOfModelStructs(table_name string, dst interface{}) error {
Then have your function put the value in dst. This does require that the value passed as dst is a pointer. This is a very common pattern, seen in json.Unmarshal(), in sql's Rows.Scan, and many other places in the standard library and elsewhere.
I'm using go 1.10.3 and I'm trying to use the sqlx package to get one row and enter it to a struct with Get(), or get several rows and enter them to a slice with Select().
lets start with getting one row into a struct.
I created the following struct:
type PsqlProduct struct {
Id int64 `db:"product_id"`
Name string `db:"product_name"`
Desc sql.NullString `db:"product_desc"`
YearManufacture sql.NullInt64 `db:"year_manufacture"`
Quantity sql.NullInt64 `db:"quantity"`
}
for the query:
QUERY_SELECT_PRODUCT = `select wd.product.id as product_id,
trans_p_name.text as product_name,
trans_p_desc.text as product_desc,
wd.product.year_manufacture, wd.product.quantity
from wd.product
join wd.text_translation as trans_p_name
on trans_p_name.text_id = wd.product.product_name_trans_id and trans_p_name.lang_id=1
left join wd.text_translation as trans_p_desc
on trans_p_desc.text_id = wd.product.product_desc_trans_id and trans_p_desc.lang_id=1
where wd.product.id = $1
`
and I created the following function to get product by id:
func PsqlGetProductById(productId int) *Product {
product := new(PsqlProduct)
err := Psqldb.Get(&product, QUERY_SELECT_PRODUCT,productId)
if err != nil {
log.Fatalf("error: %v",err)
return nil
} else {
newp := Product{
ID: uint(product.Id),
Name: product.Name,
}
if product.Quantity.Valid {
newp.Quantity = uint16(product.Quantity.Int64)
}
if product.YearManufacture.Valid {
newp.YearManufacture = uint16(product.YearManufacture.Int64)
}
if product.Desc.Valid {
newp.Desc = product.Desc.String
}
return &newp
}
}
and I got the error
error: scannable dest type ptr with >1 columns (5) in result
it's as if Get() function is only for one column.. but the documentation clearly states it's not!
if I change the Get() function call to Psqldb.QueryRowx(QUERY_SELECT_PRODUCT, productId).StructScan(product)
then it does work.. but still.. trying to find out why Get() doesn't work.
next.. Select()
so this is the struct
type PsqlCategory struct {
Id int64 `db:"category_id"`
Name string `db:"category_name"`
ParentCategoryId sql.NullInt64 `db:"parent_category_id"`
}
sql query:
QUERY_SELECT_CATEGORIES = `
select category.id as category_id,
text_translation.text as category_name,
category.parent_category_id
from category
join text_translation on text_translation.text_id=category.category_name_trans_id
and text_translation.lang_id = 1`
and the function
func PsqlGetCategories() []Category {
categories := []PsqlCategory{}
err := Psqldb.Select(&categories, QUERY_SELECT_CATEGORIES)
if err != nil {
log.Fatalf("could not parse categories: %v", err)
return nil
}
var nCategories []Category
for _, cat := range categories {
newCat := Category{
Id: cat.Id,
Name: cat.Name,
}
if cat.ParentCategoryId.Valid {
newCat.ParentCategoryId = cat.ParentCategoryId.Int64
}
nCategories = append(nCategories, newCat)
}
return nCategories
}
and this is the error
could not parse categories: pq: relation "category" does not exist
it's like I totally misunderstood the usage of the sqlx library or I'm missing something..
any information regarding the issue would be greatly appreciated.
The problem arises because you're passing **PsqlProduct to Get which thinks that you want to scan the query result into the pointed to pointer, hence "... dest type ptr with >1 columns ...".
Just change:
err := Psqldb.Get(&product, QUERY_SELECT_PRODUCT,productId)
to:
err := Psqldb.Get(product, QUERY_SELECT_PRODUCT,productId)
I have created an object mapping in Go that is not relational, it is very simple.
I have several structs that looks like this:
type Message struct {
Id int64
Message string
ReplyTo sql.NullInt64 `db:"reply_to"`
FromId int64 `db:"from_id"`
ToId int64 `db:"to_id"`
IsActive bool `db:"is_active"`
SentTime int64 `db:"sent_time"`
IsViewed bool `db:"is_viewed"`
Method string `db:"-"`
AppendTo int64 `db:"-"`
}
To create a new message I just run this function:
func New() *Message {
return &Message{
IsActive: true,
SentTime: time.Now().Unix(),
Method: "new",
}
}
And then I have a message_crud.go file for this struct that looks like this:
To find a message by a unique column (for example by id) I run this function:
func ByUnique(column string, value interface{}) (*Message, error) {
query := fmt.Sprintf(`
SELECT *
FROM message
WHERE %s = ?
LIMIT 1;
`, column)
message := &Message{}
err := sql.DB.QueryRowx(query, value).StructScan(message)
if err != nil {
return nil, err
}
return message, nil
}
And to save a message (insert or update in the database) I run this method:
func (this *Message) save() error {
s := ""
if this.Id == 0 {
s = "INSERT INTO message SET %s;"
} else {
s = "UPDATE message SET %s WHERE id=:id;"
}
query := fmt.Sprintf(s, sql.PlaceholderPairs(this))
nstmt, err := sql.DB.PrepareNamed(query)
if err != nil {
return err
}
res, err := nstmt.Exec(*this)
if err != nil {
return err
}
if this.Id == 0 {
lastId, err := res.LastInsertId()
if err != nil {
return err
}
this.Id = lastId
}
return nil
}
The sql.PlaceholderPairs() function looks like this:
func PlaceholderPairs(i interface{}) string {
s := ""
val := reflect.ValueOf(i).Elem()
count := val.NumField()
for i := 0; i < count; i++ {
typeField := val.Type().Field(i)
tag := typeField.Tag
fname := strings.ToLower(typeField.Name)
if fname == "id" {
continue
}
if t := tag.Get("db"); t == "-" {
continue
} else if t != "" {
s += t + "=:" + t
} else {
s += fname + "=:" + fname
}
s += ", "
}
s = s[:len(s)-2]
return s
}
But every time I create a new struct, for example a User struct I have to copy paste the "crud section" above and create a user_crud.go file and replace the words "Message" with "User", and the words "message" with "user". I repeat alot of code and it is not very dry. Is there something I could do to not repeat this code for things I would reuse? I always have a save() method, and always have a function ByUnique() where I can return a struct and search by a unique column.
In PHP this was easy because PHP is not statically typed.
Is this possible to do in Go?
Your ByUnique is almost generic already. Just pull out the piece that varies (the table and destination):
func ByUnique(table string, column string, value interface{}, dest interface{}) error {
query := fmt.Sprintf(`
SELECT *
FROM %s
WHERE %s = ?
LIMIT 1;
`, table, column)
return sql.DB.QueryRowx(query, value).StructScan(dest)
}
func ByUniqueMessage(column string, value interface{}) (*Message, error) {
message := &Message{}
if err := ByUnique("message", column, value, &message); err != nil {
return nil, err
}
return message, error
}
Your save is very similar. You just need to make a generic save function along the lines of:
func Save(table string, identifier int64, source interface{}) { ... }
Then inside of (*Message)save, you'd just call the general Save() function. Looks pretty straightforward.
Side notes: do not use this as the name of the object inside a method. See the link from #OneOfOne for more on that. And do not get obsessed about DRY. It is not a goal in itself. Go focuses on code being simple, clear, and reliable. Do not create something complicated and fragile just to avoid typing a simple line of error handling. This doesn't mean that you shouldn't extract duplicated code. It just means that in Go it is usually better to repeat simple code a little bit rather than create complicated code to avoid it.
EDIT: If you want to implement Save using an interface, that's no problem. Just create an Identifier interface.
type Ider interface {
Id() int64
SetId(newId int64)
}
func (msg *Message) Id() int64 {
return msg.Id
}
func (msg *Message) SetId(newId int64) {
msg.Id = newId
}
func Save(table string, source Ider) error {
s := ""
if source.Id() == 0 {
s = fmt.Sprintf("INSERT INTO %s SET %%s;", table)
} else {
s = fmt.Sprintf("UPDATE %s SET %%s WHERE id=:id;", table)
}
query := fmt.Sprintf(s, sql.PlaceholderPairs(source))
nstmt, err := sql.DB.PrepareNamed(query)
if err != nil {
return err
}
res, err := nstmt.Exec(source)
if err != nil {
return err
}
if source.Id() == 0 {
lastId, err := res.LastInsertId()
if err != nil {
return err
}
source.SetId(lastId)
}
return nil
}
func (msg *Message) save() error {
return Save("message", msg)
}
The one piece that might blow up with this is the call to Exec. I don't know what package you're using, and it's possible that Exec won't work correctly if you pass it an interface rather than the actual struct, but it probably will work. That said, I'd probably just pass the identifier rather than adding this overhead.
You probably want to use an ORM.
They eliminate a lot of the boilerplate code you're describing.
See this question for "What is an ORM?"
Here is a list of ORMs for go: https://github.com/avelino/awesome-go#orm
I have never used one myself, so I can't recommend any. The main reason is that an ORM takes a lot of control from the developer and introduces a non-negligible performance overhead. You need to see for yourself if they fit your use-case and/or if you are comfortable with the "magic" that's going on in those libraries.
I don't recommend doing this, i personally would prefer being explicit about scanning into structs and creating queries.
But if you really want to stick to reflection you could do:
func ByUnique(obj interface{}, column string, value interface{}) error {
// ...
return sql.DB.QueryRowx(query, value).StructScan(obj)
}
// Call with
message := &Message{}
ByUnique(message, ...)
And for your save:
type Identifiable interface {
Id() int64
}
// Implement Identifiable for message, etc.
func Save(obj Identifiable) error {
// ...
}
// Call with
Save(message)
The approach i use and would recommend to you:
type Redirect struct {
ID string
URL string
CreatedAt time.Time
}
func FindByID(db *sql.DB, id string) (*Redirect, error) {
var redirect Redirect
err := db.QueryRow(
`SELECT "id", "url", "created_at" FROM "redirect" WHERE "id" = $1`, id).
Scan(&redirect.ID, &redirect.URL, &redirect.CreatedAt)
switch {
case err == sql.ErrNoRows:
return nil, nil
case err != nil:
return nil, err
}
return &redirect, nil
}
func Save(db *sql.DB, redirect *Redirect) error {
redirect.CreatedAt = time.Now()
_, err := db.Exec(
`INSERT INTO "redirect" ("id", "url", "created_at") VALUES ($1, $2, $3)`,
redirect.ID, redirect.URL, redirect.CreatedAt)
return err
}
This has the advantage of using the type system and mapping only things it should actually map.