package dobotaiops import ( "context" "time" ) // RecordQuerier and RecordTransaction keep the projection identical while an // explicitly selected reader supplies either source-ACL or local-snapshot transactions. type RecordRow = rowScanner type RecordQuerier = querier type RecordTransaction func(context.Context, func(context.Context, RecordQuerier) error) error type RecordsRepositoryOptions struct { BaseSQL string SummarySQL string QualityScopeSQL string Transaction RecordTransaction ValidateWindow func(Window) error ReadAsOf func(context.Context, RecordQuerier, *time.Time) error TenantKey string } type RecordsRepository struct { baseSQL string summarySQL string qualityScopeSQL string transact RecordTransaction validateQueryWindow func(Window) error readAsOf func(context.Context, RecordQuerier, *time.Time) error tenant string } func NewRecordsRepository(o RecordsRepositoryOptions) *RecordsRepository { asOf := o.ReadAsOf if asOf == nil { asOf = func(ctx context.Context, q RecordQuerier, t *time.Time) error { return q.QueryRow(ctx, "SELECT transaction_timestamp()").Scan(t) } } return &RecordsRepository{summarySQL: o.SummarySQL, qualityScopeSQL: o.QualityScopeSQL, baseSQL: o.BaseSQL, transact: o.Transaction, validateQueryWindow: o.ValidateWindow, readAsOf: asOf, tenant: o.TenantKey} } func (c *RecordsRepository) WithSummarySQL(sql string) *RecordsRepository { out := *c out.summarySQL = sql return &out } func (c *Client) recordRepository() *RecordsRepository { return NewRecordsRepository(RecordsRepositoryOptions{BaseSQL: recordsBaseSQL, Transaction: c.withReadOnlyTx, ValidateWindow: c.validateQueryWindow}) } func (c *Client) ListRecords(ctx context.Context, q RecordQuery) (RecordPage, error) { return c.recordRepository().ListRecords(ctx, q) } func (c *Client) GetRecord(ctx context.Context, tenant, id string) (Record, error) { return c.recordRepository().GetRecord(ctx, tenant, id) } func (c *Client) ReadUsageGroups(ctx context.Context, q RecordQuery) (UsageGroups, error) { return c.recordRepository().ReadUsageGroups(ctx, q) } func (c *Client) ReadQualityScope(ctx context.Context, q RecordQuery) (QualityScope, error) { return c.recordRepository().ReadQualityScope(ctx, q) } // DecodeMirrorPayload accepts only the source adapter's fixed safe projection; // credentials and arbitrary source tables are never part of this protocol. func DecodeMirrorPayload(raw string) (Record, error) { return decodeRecord(raw) }