Forgot that squirrel needs the =? on Where so switched to using sq.Eq all the time.
This commit is contained in:
parent
d59e29fdc1
commit
d481c260c5
3 changed files with 124 additions and 111 deletions
96
admin/db.go
Normal file
96
admin/db.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"fmt"
|
||||
"zedshaw.games/webapp/data"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
)
|
||||
|
||||
func SelectTable(table string, the_type reflect.Type, limit uint64, offset uint64) ([]reflect.Value, error) {
|
||||
var results []reflect.Value
|
||||
sql_query, args, err := sq.Select("*").Limit(limit).Offset(offset).From(table).ToSql()
|
||||
if err != nil { return results, err }
|
||||
|
||||
rows, err := data.DB.Queryx(sql_query, args...)
|
||||
if err != nil { return results, err }
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
the_data := reflect.New(the_type)
|
||||
err = rows.StructScan(the_data.Interface())
|
||||
if err != nil { return results, err }
|
||||
|
||||
results = append(results, the_data.Elem())
|
||||
}
|
||||
|
||||
return results, rows.Err()
|
||||
}
|
||||
|
||||
func Get(table string, the_type reflect.Type, id int64) (reflect.Value, error) {
|
||||
sql_query, args, err := sq.Select("*").From(table).Where(sq.Eq{"id": id}).ToSql()
|
||||
if err != nil { return reflect.New(nil), err }
|
||||
|
||||
the_data := reflect.New(the_type)
|
||||
err = data.DB.Get(the_data.Interface(), sql_query, args...)
|
||||
|
||||
// BUG: not sure if Elem or returning the reflect.New is better
|
||||
return the_data.Elem(), err
|
||||
}
|
||||
|
||||
func Delete(table string, id int64) error {
|
||||
sql_query, args, err := sq.Delete(table).Where(sq.Eq{"id": id}).ToSql()
|
||||
if err != nil { return err }
|
||||
|
||||
_, err = data.DB.Exec(sql_query, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
func Insert(table string, value reflect.Value) error {
|
||||
type_of := value.Type()
|
||||
field_num := value.NumField()
|
||||
var columns []string
|
||||
var values []interface{}
|
||||
|
||||
for i := 0; i < field_num; i++ {
|
||||
field := value.Field(i)
|
||||
tag := type_of.Field(i).Tag.Get("db")
|
||||
if tag == "id" { continue }
|
||||
columns = append(columns, tag)
|
||||
values = append(values, field.Interface())
|
||||
}
|
||||
|
||||
builder := sq.Insert(table).Columns(columns...).Values(values...)
|
||||
sql_query, args, err := builder.ToSql()
|
||||
|
||||
_, err = data.DB.Exec(sql_query, args...)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func Update(table string, value reflect.Value) error {
|
||||
builder := sq.Update(table)
|
||||
|
||||
type_of := value.Type()
|
||||
field_num := value.NumField()
|
||||
|
||||
for i := 0; i < field_num; i++ {
|
||||
field := value.Field(i)
|
||||
tag := type_of.Field(i).Tag.Get("db")
|
||||
|
||||
// skip update of id to avoid replacing it
|
||||
if tag == "id" { continue }
|
||||
|
||||
builder = builder.Set(tag, field.Interface())
|
||||
}
|
||||
|
||||
builder.Where(sq.Eq{"id": value.FieldByName("Id").Interface()})
|
||||
sql_query, args, err := builder.ToSql()
|
||||
|
||||
fmt.Println("UPDATE QUERY", sql_query, args)
|
||||
if err != nil { return err}
|
||||
|
||||
_, err = data.DB.Exec(sql_query, args...)
|
||||
return err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue