feat: SelectDataset 新增 Exists/ExistsContext

生成 SELECT 1 FROM ... LIMIT 1 判断是否存在符合条件的行,不用调用方自己拼
COUNT(*) 或者查完整行再判断长度。
This commit is contained in:
2026-08-20 17:09:48 +08:00
parent 6ed2803158
commit 9c6bb5a835
2 changed files with 63 additions and 0 deletions
+11
View File
@@ -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())
}