@ -175,7 +175,7 @@ func Exec(ctx context.Context, sqlAndArgs ...any) (sql.Result, error) {
func Get [ T any ] ( ctx context . Context , cond builder . Cond ) ( object * T , exist bool , err error ) {
func Get [ T any ] ( ctx context . Context , cond builder . Cond ) ( object * T , exist bool , err error ) {
if ! cond . IsValid ( ) {
if ! cond . IsValid ( ) {
return nil , false , ErrConditionRequired { }
panic ( "cond is invalid in db.Get(ctx, cond). This should not be possible." )
}
}
var bean T
var bean T
@ -201,7 +201,7 @@ func GetByID[T any](ctx context.Context, id int64) (object *T, exist bool, err e
func Exist [ T any ] ( ctx context . Context , cond builder . Cond ) ( bool , error ) {
func Exist [ T any ] ( ctx context . Context , cond builder . Cond ) ( bool , error ) {
if ! cond . IsValid ( ) {
if ! cond . IsValid ( ) {
return false , ErrConditionRequired { }
panic ( "cond is invalid in db.Exist(ctx, cond). This should not be possible." )
}
}
var bean T
var bean T
@ -213,16 +213,36 @@ func ExistByID[T any](ctx context.Context, id int64) (bool, error) {
return GetEngine ( ctx ) . ID ( id ) . NoAutoCondition ( ) . Exist ( & bean )
return GetEngine ( ctx ) . ID ( id ) . NoAutoCondition ( ) . Exist ( & bean )
}
}
// DeleteByID deletes the given bean with the given ID
func DeleteByID [ T any ] ( ctx context . Context , id int64 ) ( int64 , error ) {
var bean T
return GetEngine ( ctx ) . ID ( id ) . NoAutoCondition ( ) . NoAutoTime ( ) . Delete ( & bean )
}
func DeleteByIDs [ T any ] ( ctx context . Context , ids ... int64 ) error {
if len ( ids ) == 0 {
return nil
}
var bean T
_ , err := GetEngine ( ctx ) . In ( "id" , ids ) . NoAutoCondition ( ) . NoAutoTime ( ) . Delete ( & bean )
return err
}
func Delete [ T any ] ( ctx context . Context , opts FindOptions ) ( int64 , error ) {
if opts == nil || ! opts . ToConds ( ) . IsValid ( ) {
panic ( "opts are empty or invalid in db.Delete(ctx, opts). This should not be possible." )
}
var bean T
return GetEngine ( ctx ) . Where ( opts . ToConds ( ) ) . NoAutoCondition ( ) . NoAutoTime ( ) . Delete ( & bean )
}
// DeleteByBean deletes all records according non-empty fields of the bean as conditions.
// DeleteByBean deletes all records according non-empty fields of the bean as conditions.
func DeleteByBean ( ctx context . Context , bean any ) ( int64 , error ) {
func DeleteByBean ( ctx context . Context , bean any ) ( int64 , error ) {
return GetEngine ( ctx ) . Delete ( bean )
return GetEngine ( ctx ) . Delete ( bean )
}
}
// DeleteByID deletes the given bean with the given ID
func DeleteByID ( ctx context . Context , id int64 , bean any ) ( int64 , error ) {
return GetEngine ( ctx ) . ID ( id ) . NoAutoCondition ( ) . NoAutoTime ( ) . Delete ( bean )
}
// FindIDs finds the IDs for the given table name satisfying the given condition
// FindIDs finds the IDs for the given table name satisfying the given condition
// By passing a different value than "id" for "idCol", you can query for foreign IDs, i.e. the repo IDs which satisfy the condition
// By passing a different value than "id" for "idCol", you can query for foreign IDs, i.e. the repo IDs which satisfy the condition
func FindIDs ( ctx context . Context , tableName , idCol string , cond builder . Cond ) ( [ ] int64 , error ) {
func FindIDs ( ctx context . Context , tableName , idCol string , cond builder . Cond ) ( [ ] int64 , error ) {