WIP: Insert sets PK

This commit is contained in:
2026-05-20 23:16:42 +02:00
parent e98dbe3338
commit 4726204413
4 changed files with 79 additions and 18 deletions
+51 -2
View File
@@ -15,45 +15,94 @@ type Test struct {
StringField string `sql:"nn"`
}
func (t Test) IsInsertable() bool {
func (t *Test) IsInsertable() bool {
return t.ID == 0
}
func (t *Test) SetPk(pks ...any) {
t.ID = pks[0].(int)
}
type TestWithFk struct {
ID int `sql:"pk"`
TestID int `sql:"nn;fk=Test.ID"`
}
func (t *TestWithFk) IsInsertable() bool {
return t.ID == 0
}
func (t *TestWithFk) SetPk(pks ...any) {
t.ID = pks[0].(int)
}
type TestWithFkAndFkId struct {
ID int `sql:"pk"`
TestID int `sql:"nn;fk=Test.ID;fk_id=custom_fk"`
}
func (t *TestWithFkAndFkId) IsInsertable() bool {
return t.ID == 0
}
func (t *TestWithFkAndFkId) SetPk(pks ...any) {
t.ID = pks[0].(int)
}
type TestWithCompositePk struct {
ID int `sql:"nn;pk;"`
Name string `sql:"nn;pk"`
}
func (t TestWithCompositePk) IsInsertable() bool {
func (t *TestWithCompositePk) IsInsertable() bool {
return t.ID == 0 && t.Name == ""
}
func (t *TestWithCompositePk) SetPk(pks ...any) {
t.ID = pks[0].(int)
t.Name = pks[1].(string)
}
type TestWithCompositeFk struct {
ID int `sql:"nn;pk"`
CompositeFkId int `sql:"nn;fk=TestWithCompositePk.ID;"`
CompositeFkName string `sql:"nn;fk=TestWithCompositePk.Name;"`
}
func (t *TestWithCompositeFk) IsInsertable() bool {
return t.ID == 0
}
func (t *TestWithCompositeFk) SetPk(pks ...any) {
t.ID = pks[0].(int)
}
type TestWithBool struct {
ID int `sql:"pk"`
BoolField bool `sql:"nn"`
}
func (t *TestWithBool) IsInsertable() bool {
return t.ID == 0
}
func (t *TestWithBool) SetPk(pks ...any) {
t.ID = pks[0].(int)
}
type TestWithTime struct {
ID int `sql:"pk"`
TimeField int64 `sql:"nn"`
}
func (t *TestWithTime) IsInsertable() bool {
return t.ID == 0
}
func (t *TestWithTime) SetPk(pks ...any) {
t.ID = pks[0].(int)
}
func TestMain(m *testing.M) {
log.SetupLogs("Info")
+7 -7
View File
@@ -33,7 +33,7 @@ func TestRepoSelectAll(t *testing.T) {
return
}
_, err = insertExec.Execute(conn)
repo := repository.NewRepository[Test](conn, orm)
repo := repository.NewRepository[*Test](conn, orm)
// WHEN
res := repo.SelectAll()
@@ -59,7 +59,7 @@ func TestRepoSelectByPk(t *testing.T) {
return
}
_, err = insertExec.Execute(conn)
repo := repository.NewRepository[Test](conn, orm)
repo := repository.NewRepository[*Test](conn, orm)
// WHEN
res := repo.SelectByPk(1)
@@ -85,7 +85,7 @@ func TestRepoSelectByCompundPk(t *testing.T) {
return
}
_, err = insertExec.Execute(conn)
repo := repository.NewRepository[TestWithCompositePk](conn, orm)
repo := repository.NewRepository[*TestWithCompositePk](conn, orm)
// WHEN
res := repo.SelectByPk(42069, "Test")
@@ -104,10 +104,10 @@ func TestRepoSave(t *testing.T) {
defer cleanUp("test.db", conn)
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
repo := repository.NewRepository[Test](conn, orm)
repo := repository.NewRepository[*Test](conn, orm)
// WHEN
repo.Save(testObj)
repo.Save(&testObj)
// THEN
res := repo.SelectByPk(1)
@@ -124,9 +124,9 @@ func TestRepoDelete(t *testing.T) {
defer cleanUp("test.db", conn)
testObj := Test{Int64Field: 54, IntField: 12, StringField: "asdf"}
repo := repository.NewRepository[Test](conn, orm)
repo := repository.NewRepository[*Test](conn, orm)
repo.Save(testObj)
repo.Save(&testObj)
res := repo.SelectByPk(1)
if res == nil {