feat: SelectDataset 新增 Exists/ExistsContext
生成 SELECT 1 FROM ... LIMIT 1 判断是否存在符合条件的行,不用调用方自己拼 COUNT(*) 或者查完整行再判断长度。
This commit is contained in:
@@ -701,6 +701,17 @@ func (sd *SelectDataset) ScanValContext(ctx context.Context, i interface{}) (boo
|
||||
}
|
||||
|
||||
// Generates the SELECT COUNT(*) sql for this dataset and uses Exec#ScanVal to scan the result into an int64.
|
||||
// Generates SELECT 1 FROM ... LIMIT 1 and returns true if any row exists.
|
||||
func (sd *SelectDataset) Exists() (bool, error) {
|
||||
return sd.ExistsContext(context.Background())
|
||||
}
|
||||
|
||||
// Generates SELECT 1 FROM ... LIMIT 1 and returns true if any row exists.
|
||||
func (sd *SelectDataset) ExistsContext(ctx context.Context) (bool, error) {
|
||||
var v int64
|
||||
return sd.Select(L("1")).Limit(1).ScanValContext(ctx, &v)
|
||||
}
|
||||
|
||||
func (sd *SelectDataset) Count() (int64, error) {
|
||||
return sd.CountContext(context.Background())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user