@@ -26,20 +26,18 @@ func (h accountsResourceHandler) Schema() common.EntitySchema {
2626}
2727
2828func (h accountsResourceHandler ) BuildDataset (opts common.RepositoryHandlerBuildContext [any ]) (* bun.SelectQuery , error ) {
29- ret := h .store .db . NewSelect ().
29+ ret := h .store .newScopedSelect ().
3030 ModelTableExpr (h .store .GetPrefixedRelationName ("accounts" )).
31- Column ("address" , "address_array" , "first_usage" , "insertion_date" , "updated_at" ).
32- Where ("ledger = ?" , h .store .ledger .Name )
31+ Column ("address" , "address_array" , "first_usage" , "insertion_date" , "updated_at" )
3332
3433 if opts .PIT != nil && ! opts .PIT .IsZero () {
3534 ret = ret .Where ("accounts.first_usage <= ?" , opts .PIT )
3635 }
3736
3837 if h .store .ledger .HasFeature (features .FeatureAccountMetadataHistory , "SYNC" ) && opts .PIT != nil && ! opts .PIT .IsZero () {
39- selectDistinctAccountMetadataHistories := h .store .db . NewSelect ().
38+ selectDistinctAccountMetadataHistories := h .store .newScopedSelect ().
4039 DistinctOn ("accounts_address" ).
4140 ModelTableExpr (h .store .GetPrefixedRelationName ("accounts_metadata" )).
42- Where ("ledger = ?" , h .store .ledger .Name ).
4341 Column ("accounts_address" ).
4442 ColumnExpr ("first_value(metadata) over (partition by accounts_address order by revision desc) as metadata" ).
4543 Where ("date <= ?" , opts .PIT )
@@ -65,9 +63,8 @@ func (h accountsResourceHandler) ResolveFilter(opts common.ResourceQuery[any], o
6563 return fmt .Sprintf ("%s %s ?" , property , common .ConvertOperatorToSQL (operator )), []any {value }, nil
6664 case balanceRegex .MatchString (property ) || property == "balance" :
6765
68- selectBalance := h .store .db .NewSelect ().
69- Where ("accounts_address = dataset.address" ).
70- Where ("ledger = ?" , h .store .ledger .Name )
66+ selectBalance := h .store .newScopedSelect ().
67+ Where ("accounts_address = dataset.address" )
7168
7269 if opts .PIT != nil && ! opts .PIT .IsZero () {
7370 if ! h .store .ledger .HasFeature (features .FeatureMovesHistory , "ON" ) {
@@ -122,14 +119,13 @@ func (h accountsResourceHandler) Expand(opts common.ResourceQuery[any], property
122119 }
123120 }
124121
125- selectRowsQuery := h .store .db . NewSelect ().
122+ selectRowsQuery := h .store .newScopedSelect ().
126123 Where ("accounts_address in (select address from dataset)" )
127124 if opts .UsePIT () {
128125 selectRowsQuery = selectRowsQuery .
129126 ModelTableExpr (h .store .GetPrefixedRelationName ("moves" )).
130127 DistinctOn ("accounts_address, asset" ).
131- Column ("accounts_address" , "asset" ).
132- Where ("ledger = ?" , h .store .ledger .Name )
128+ Column ("accounts_address" , "asset" )
133129 if property == "volumes" {
134130 selectRowsQuery = selectRowsQuery .
135131 ColumnExpr ("first_value(post_commit_volumes) over (partition by (accounts_address, asset) order by seq desc) as volumes" ).
@@ -143,16 +139,15 @@ func (h accountsResourceHandler) Expand(opts common.ResourceQuery[any], property
143139 selectRowsQuery = selectRowsQuery .
144140 ModelTableExpr (h .store .GetPrefixedRelationName ("accounts_volumes" )).
145141 Column ("asset" , "accounts_address" ).
146- ColumnExpr ("(input, output)::" + h .store .GetPrefixedRelationName ("volumes" )+ " as volumes" ).
147- Where ("ledger = ?" , h .store .ledger .Name )
142+ ColumnExpr ("(input, output)::" + h .store .GetPrefixedRelationName ("volumes" )+ " as volumes" )
148143 }
149144
150145 return h .store .db .NewSelect ().
151- With ("rows" , selectRowsQuery ).
152- ModelTableExpr ("rows" ).
153- Column ("accounts_address" ).
154- ColumnExpr ("public.aggregate_objects(json_build_object(asset, json_build_object('input', (volumes).inputs, 'output', (volumes).outputs))::jsonb) as " + strcase .SnakeCase (property )).
155- Group ("accounts_address" ), & common.JoinCondition {
146+ With ("rows" , selectRowsQuery ).
147+ ModelTableExpr ("rows" ).
148+ Column ("accounts_address" ).
149+ ColumnExpr ("public.aggregate_objects(json_build_object(asset, json_build_object('input', (volumes).inputs, 'output', (volumes).outputs))::jsonb) as " + strcase .SnakeCase (property )).
150+ Group ("accounts_address" ), & common.JoinCondition {
156151 Left : "address" ,
157152 Right : "accounts_address" ,
158153 }, nil
0 commit comments