@@ -35,10 +35,33 @@ type Interface[T any] interface {
3535}
3636
3737type CreateInterface [T any ] interface {
38- ChainInterface [T ]
38+ ExecInterface [T ]
39+ // chain methods available at start; return ChainInterface
40+ Scopes (scopes ... func (db * Statement )) ChainInterface [T ]
41+ Where (query interface {}, args ... interface {}) ChainInterface [T ]
42+ Not (query interface {}, args ... interface {}) ChainInterface [T ]
43+ Or (query interface {}, args ... interface {}) ChainInterface [T ]
44+ Limit (offset int ) ChainInterface [T ]
45+ Offset (offset int ) ChainInterface [T ]
46+ Joins (query clause.JoinTarget , on func (db JoinBuilder , joinTable clause.Table , curTable clause.Table ) error ) ChainInterface [T ]
47+ Preload (association string , query func (db PreloadBuilder ) error ) ChainInterface [T ]
48+ Select (query string , args ... interface {}) ChainInterface [T ]
49+ Omit (columns ... string ) ChainInterface [T ]
50+ MapColumns (m map [string ]string ) ChainInterface [T ]
51+ Distinct (args ... interface {}) ChainInterface [T ]
52+ Group (name string ) ChainInterface [T ]
53+ Having (query interface {}, args ... interface {}) ChainInterface [T ]
54+ Order (value interface {}) ChainInterface [T ]
55+ Build (builder clause.Builder )
56+
57+ Delete (ctx context.Context ) (rowsAffected int , err error )
58+ Update (ctx context.Context , name string , value any ) (rowsAffected int , err error )
59+ Updates (ctx context.Context , t T ) (rowsAffected int , err error )
60+
3961 Table (name string , args ... interface {}) CreateInterface [T ]
4062 Create (ctx context.Context , r * T ) error
4163 CreateInBatches (ctx context.Context , r * []T , batchSize int ) error
64+ Set (assignments ... clause.Assignment ) SetCreateOrUpdateInterface [T ]
4265}
4366
4467type ChainInterface [T any ] interface {
@@ -58,15 +81,28 @@ type ChainInterface[T any] interface {
5881 Group (name string ) ChainInterface [T ]
5982 Having (query interface {}, args ... interface {}) ChainInterface [T ]
6083 Order (value interface {}) ChainInterface [T ]
84+ Set (assignments ... clause.Assignment ) SetUpdateOnlyInterface [T ]
6185
6286 Build (builder clause.Builder )
6387
88+ Table (name string , args ... interface {}) ChainInterface [T ]
6489 Delete (ctx context.Context ) (rowsAffected int , err error )
6590 Update (ctx context.Context , name string , value any ) (rowsAffected int , err error )
6691 Updates (ctx context.Context , t T ) (rowsAffected int , err error )
6792 Count (ctx context.Context , column string ) (result int64 , err error )
6893}
6994
95+ // SetUpdateOnlyInterface is returned by Set after chaining; only Update is allowed
96+ type SetUpdateOnlyInterface [T any ] interface {
97+ Update (ctx context.Context ) (rowsAffected int , err error )
98+ }
99+
100+ // SetCreateOrUpdateInterface is returned by Set at start; Create or Update are allowed
101+ type SetCreateOrUpdateInterface [T any ] interface {
102+ Create (ctx context.Context ) error
103+ Update (ctx context.Context ) (rowsAffected int , err error )
104+ }
105+
70106type ExecInterface [T any ] interface {
71107 Scan (ctx context.Context , r interface {}) error
72108 First (context.Context ) (T , error )
@@ -163,6 +199,12 @@ func (c createG[T]) Table(name string, args ...interface{}) CreateInterface[T] {
163199 })}
164200}
165201
202+ func (c createG [T ]) Set (assignments ... clause.Assignment ) SetCreateOrUpdateInterface [T ] {
203+ assigns := make ([]clause.Assignment , len (assignments ))
204+ copy (assigns , assignments )
205+ return setCreateOrUpdateG [T ]{c : c .chainG , assigns : assigns }
206+ }
207+
166208func (c createG [T ]) Create (ctx context.Context , r * T ) error {
167209 return c .g .apply (ctx ).Create (r ).Error
168210}
@@ -189,6 +231,12 @@ func (c chainG[T]) with(v op) chainG[T] {
189231 }
190232}
191233
234+ func (c chainG [T ]) Table (name string , args ... interface {}) ChainInterface [T ] {
235+ return c .with (func (db * DB ) * DB {
236+ return db .Table (name , args ... )
237+ })
238+ }
239+
192240func (c chainG [T ]) Scopes (scopes ... func (db * Statement )) ChainInterface [T ] {
193241 return c .with (func (db * DB ) * DB {
194242 for _ , fc := range scopes {
@@ -198,12 +246,6 @@ func (c chainG[T]) Scopes(scopes ...func(db *Statement)) ChainInterface[T] {
198246 })
199247}
200248
201- func (c chainG [T ]) Table (name string , args ... interface {}) ChainInterface [T ] {
202- return c .with (func (db * DB ) * DB {
203- return db .Table (name , args ... )
204- })
205- }
206-
207249func (c chainG [T ]) Where (query interface {}, args ... interface {}) ChainInterface [T ] {
208250 return c .with (func (db * DB ) * DB {
209251 return db .Where (query , args ... )
@@ -390,6 +432,12 @@ func (c chainG[T]) MapColumns(m map[string]string) ChainInterface[T] {
390432 })
391433}
392434
435+ func (c chainG [T ]) Set (assignments ... clause.Assignment ) SetUpdateOnlyInterface [T ] {
436+ assigns := make ([]clause.Assignment , len (assignments ))
437+ copy (assigns , assignments )
438+ return setCreateOrUpdateG [T ]{c : c , assigns : assigns }
439+ }
440+
393441func (c chainG [T ]) Distinct (args ... interface {}) ChainInterface [T ] {
394442 return c .with (func (db * DB ) * DB {
395443 return db .Distinct (args ... )
@@ -557,6 +605,26 @@ func (c chainG[T]) Build(builder clause.Builder) {
557605 }
558606}
559607
608+ type setCreateOrUpdateG [T any ] struct {
609+ c chainG [T ]
610+ assigns []clause.Assignment
611+ }
612+
613+ func (s setCreateOrUpdateG [T ]) Update (ctx context.Context ) (rowsAffected int , err error ) {
614+ var r T
615+ res := s .c .g .apply (ctx ).Model (r ).Clauses (clause .Set (s .assigns )).Updates (map [string ]interface {}{})
616+ return int (res .RowsAffected ), res .Error
617+ }
618+
619+ func (s setCreateOrUpdateG [T ]) Create (ctx context.Context ) error {
620+ var r T
621+ data := make (map [string ]interface {}, len (s .assigns ))
622+ for _ , a := range s .assigns {
623+ data [a .Column .Name ] = a .Value
624+ }
625+ return s .c .g .apply (ctx ).Model (r ).Create (data ).Error
626+ }
627+
560628type execG [T any ] struct {
561629 g * g [T ]
562630}
0 commit comments