Skip to content

Commit

Permalink
move parameters before sql query. Make sure sql string is properly de…
Browse files Browse the repository at this point in the history
…tected as SQL in IDE.
  • Loading branch information
PawelPawelec-RDX committed Oct 10, 2024
1 parent 8f2bfe3 commit 8cbaa73
Show file tree
Hide file tree
Showing 25 changed files with 859 additions and 664 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,16 @@
using RadixDlt.NetworkGateway.Abstractions.Model;
using RadixDlt.NetworkGateway.Abstractions.StandardMetadata;
using RadixDlt.NetworkGateway.PostgresIntegration.Models;
using RadixDlt.NetworkGateway.PostgresIntegration.Queries;
using RadixDlt.NetworkGateway.PostgresIntegration.Queries.CustomTypes;
using RadixDlt.NetworkGateway.PostgresIntegration.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using CoreModel = RadixDlt.CoreApiSdk.Model;
using GatewayModel = RadixDlt.NetworkGateway.GatewayApiSdk.Model;
using LedgerTransaction = RadixDlt.NetworkGateway.PostgresIntegration.Models.LedgerTransaction;
using NonFungibleIdType = RadixDlt.NetworkGateway.Abstractions.Model.NonFungibleIdType;
using PublicKeyType = RadixDlt.NetworkGateway.Abstractions.Model.PublicKeyType;
using ToolkitModel = RadixEngineToolkit;
using UserLedgerTransaction = RadixDlt.NetworkGateway.PostgresIntegration.Models.UserLedgerTransaction;

namespace RadixDlt.NetworkGateway.PostgresIntegration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
using RadixDlt.NetworkGateway.Abstractions.Extensions;
using RadixDlt.NetworkGateway.DataAggregator.Services;
using RadixDlt.NetworkGateway.PostgresIntegration.Models;
using RadixDlt.NetworkGateway.PostgresIntegration.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -142,7 +143,7 @@ FROM entities
public async Task<SequencesHolder> LoadSequences(CancellationToken token)
{
var sw = Stopwatch.GetTimestamp();
var cd = new CommandDefinition(
var cd = DapperExtensions.CreateCommandDefinition(
commandText: @"
SELECT
nextval('account_locker_entry_definition_id_seq') AS AccountLockerEntryDefinitionSequence,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
using RadixDlt.NetworkGateway.Abstractions.Model;
using RadixDlt.NetworkGateway.DataAggregator.Services;
using RadixDlt.NetworkGateway.PostgresIntegration.Models;
using RadixDlt.NetworkGateway.PostgresIntegration.Utils;
using RadixDlt.NetworkGateway.PostgresIntegration.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -200,7 +200,58 @@ public async Task UpdateSequences(SequencesHolder sequences, CancellationToken t
{
var sw = Stopwatch.GetTimestamp();

var cd = new CommandDefinition(
var parameters = new
{
accountLockerEntryDefinitionSequence = sequences.AccountLockerEntryDefinitionSequence,
accountLockerEntryResourceVaultDefinitionSequence = sequences.AccountLockerEntryResourceVaultDefinitionSequence,
accountLockerEntryTouchHistorySequence = sequences.AccountLockerEntryTouchHistorySequence,
accountDefaultDepositRuleHistorySequence = sequences.AccountDefaultDepositRuleHistorySequence,
accountResourcePreferenceRuleEntryHistorySequence = sequences.AccountResourcePreferenceRuleEntryHistorySequence,
accountResourcePreferenceRuleAggregateHistorySequence = sequences.AccountResourcePreferenceRuleAggregateHistorySequence,
stateHistorySequence = sequences.StateHistorySequence,
entitySequence = sequences.EntitySequence,
entityMetadataEntryHistorySequence = sequences.EntityMetadataEntryHistorySequence,
entityMetadataEntryDefinitionSequence = sequences.EntityMetadataEntryDefinitionSequence,
entityMetadataTotalsHistorySequence = sequences.EntityMetadataTotalsHistorySequence,
entityRoleAssignmentsAggregateHistorySequence = sequences.EntityRoleAssignmentsAggregateHistorySequence,
entityRoleAssignmentsEntryHistorySequence = sequences.EntityRoleAssignmentsEntryHistorySequence,
entityRoleAssignmentsOwnerRoleHistorySequence = sequences.EntityRoleAssignmentsOwnerRoleHistorySequence,
componentMethodRoyaltyEntryHistorySequence = sequences.ComponentMethodRoyaltyEntryHistorySequence,
componentMethodRoyaltyAggregateHistorySequence = sequences.ComponentMethodRoyaltyAggregateHistorySequence,
resourceEntitySupplyHistorySequence = sequences.ResourceEntitySupplyHistorySequence,
nonFungibleIdDataSequence = sequences.NonFungibleIdDefinitionSequence,
nonFungibleIdDataHistorySequence = sequences.NonFungibleIdDataHistorySequence,
nonFungibleIdLocationHistorySequence = sequences.NonFungibleIdLocationHistorySequence,
validatorPublicKeyHistorySequence = sequences.ValidatorPublicKeyHistorySequence,
validatorActiveSetHistorySequence = sequences.ValidatorActiveSetHistorySequence,
ledgerTransactionMarkerSequence = sequences.LedgerTransactionMarkerSequence,
packageBlueprintHistorySequence = sequences.PackageBlueprintHistorySequence,
packageCodeHistorySequence = sequences.PackageCodeHistorySequence,
schemaEntryDefinitionSequence = sequences.SchemaEntryDefinitionSequence,
schemaEntryAggregateHistorySequence = sequences.SchemaEntryAggregateHistorySequence,
keyValueStoreEntryDefinitionSequence = sequences.KeyValueStoreEntryDefinitionSequence,
keyValueStoreEntryHistorySequence = sequences.KeyValueStoreEntryHistorySequence,
validatorCumulativeEmissionHistorySequence = sequences.ValidatorCumulativeEmissionHistorySequence,
nonFungibleSchemaHistorySequence = sequences.NonFungibleSchemaHistorySequence,
keyValueSchemaHistorySequence = sequences.KeyValueSchemaHistorySequence,
packageBlueprintAggregateHistorySequence = sequences.PackageBlueprintAggregateHistorySequence,
packageCodeAggregateHistorySequence = sequences.PackageCodeAggregateHistorySequence,
accountAuthorizedDepositorEntryHistorySequence = sequences.AccountAuthorizedDepositorEntryHistorySequence,
accountAuthorizedDepositorAggregateHistorySequence = sequences.AccountAuthorizedDepositorAggregateHistorySequence,
unverifiedStandardMetadataAggregateHistorySequence = sequences.UnverifiedStandardMetadataAggregateHistorySequence,
unverifiedStandardMetadataEntryHistorySequence = sequences.UnverifiedStandardMetadataEntryHistorySequence,
resourceHoldersSequence = sequences.ResourceHoldersSequence,
entityResourceEntryDefinitionSequence = sequences.EntityResourceEntryDefinitionSequence,
entityResourceVaultEntryDefinitionSequence = sequences.EntityResourceVaultEntryDefinitionSequence,
entityResourceTotalsHistorySequence = sequences.EntityResourceTotalsHistorySequence,
entityResourceVaultTotalsHistorySequence = sequences.EntityResourceVaultTotalsHistorySequence,
entityResourceBalanceHistorySequence = sequences.EntityResourceBalanceHistorySequence,
vaultBalanceHistorySequence = sequences.VaultBalanceHistorySequence,
nonFungibleVaultEntryDefinitionSequence = sequences.NonFungibleVaultEntryDefinitionSequence,
nonFungibleVaultEntryHistorySequence = sequences.NonFungibleVaultEntryHistorySequence,
};

var cd = DapperExtensions.CreateCommandDefinition(
commandText: @"
SELECT
setval('account_locker_entry_definition_id_seq', @accountLockerEntryDefinitionSequence),
Expand Down Expand Up @@ -251,56 +302,7 @@ public async Task UpdateSequences(SequencesHolder sequences, CancellationToken t
setval('non_fungible_vault_entry_definition_id_seq', @nonFungibleVaultEntryDefinitionSequence),
setval('non_fungible_vault_entry_history_id_seq', @nonFungibleVaultEntryHistorySequence)
",
parameters: new
{
accountLockerEntryDefinitionSequence = sequences.AccountLockerEntryDefinitionSequence,
accountLockerEntryResourceVaultDefinitionSequence = sequences.AccountLockerEntryResourceVaultDefinitionSequence,
accountLockerEntryTouchHistorySequence = sequences.AccountLockerEntryTouchHistorySequence,
accountDefaultDepositRuleHistorySequence = sequences.AccountDefaultDepositRuleHistorySequence,
accountResourcePreferenceRuleEntryHistorySequence = sequences.AccountResourcePreferenceRuleEntryHistorySequence,
accountResourcePreferenceRuleAggregateHistorySequence = sequences.AccountResourcePreferenceRuleAggregateHistorySequence,
stateHistorySequence = sequences.StateHistorySequence,
entitySequence = sequences.EntitySequence,
entityMetadataEntryHistorySequence = sequences.EntityMetadataEntryHistorySequence,
entityMetadataEntryDefinitionSequence = sequences.EntityMetadataEntryDefinitionSequence,
entityMetadataTotalsHistorySequence = sequences.EntityMetadataTotalsHistorySequence,
entityRoleAssignmentsAggregateHistorySequence = sequences.EntityRoleAssignmentsAggregateHistorySequence,
entityRoleAssignmentsEntryHistorySequence = sequences.EntityRoleAssignmentsEntryHistorySequence,
entityRoleAssignmentsOwnerRoleHistorySequence = sequences.EntityRoleAssignmentsOwnerRoleHistorySequence,
componentMethodRoyaltyEntryHistorySequence = sequences.ComponentMethodRoyaltyEntryHistorySequence,
componentMethodRoyaltyAggregateHistorySequence = sequences.ComponentMethodRoyaltyAggregateHistorySequence,
resourceEntitySupplyHistorySequence = sequences.ResourceEntitySupplyHistorySequence,
nonFungibleIdDataSequence = sequences.NonFungibleIdDefinitionSequence,
nonFungibleIdDataHistorySequence = sequences.NonFungibleIdDataHistorySequence,
nonFungibleIdLocationHistorySequence = sequences.NonFungibleIdLocationHistorySequence,
validatorPublicKeyHistorySequence = sequences.ValidatorPublicKeyHistorySequence,
validatorActiveSetHistorySequence = sequences.ValidatorActiveSetHistorySequence,
ledgerTransactionMarkerSequence = sequences.LedgerTransactionMarkerSequence,
packageBlueprintHistorySequence = sequences.PackageBlueprintHistorySequence,
packageCodeHistorySequence = sequences.PackageCodeHistorySequence,
schemaEntryDefinitionSequence = sequences.SchemaEntryDefinitionSequence,
schemaEntryAggregateHistorySequence = sequences.SchemaEntryAggregateHistorySequence,
keyValueStoreEntryDefinitionSequence = sequences.KeyValueStoreEntryDefinitionSequence,
keyValueStoreEntryHistorySequence = sequences.KeyValueStoreEntryHistorySequence,
validatorCumulativeEmissionHistorySequence = sequences.ValidatorCumulativeEmissionHistorySequence,
nonFungibleSchemaHistorySequence = sequences.NonFungibleSchemaHistorySequence,
keyValueSchemaHistorySequence = sequences.KeyValueSchemaHistorySequence,
packageBlueprintAggregateHistorySequence = sequences.PackageBlueprintAggregateHistorySequence,
packageCodeAggregateHistorySequence = sequences.PackageCodeAggregateHistorySequence,
accountAuthorizedDepositorEntryHistorySequence = sequences.AccountAuthorizedDepositorEntryHistorySequence,
accountAuthorizedDepositorAggregateHistorySequence = sequences.AccountAuthorizedDepositorAggregateHistorySequence,
unverifiedStandardMetadataAggregateHistorySequence = sequences.UnverifiedStandardMetadataAggregateHistorySequence,
unverifiedStandardMetadataEntryHistorySequence = sequences.UnverifiedStandardMetadataEntryHistorySequence,
resourceHoldersSequence = sequences.ResourceHoldersSequence,
entityResourceEntryDefinitionSequence = sequences.EntityResourceEntryDefinitionSequence,
entityResourceVaultEntryDefinitionSequence = sequences.EntityResourceVaultEntryDefinitionSequence,
entityResourceTotalsHistorySequence = sequences.EntityResourceTotalsHistorySequence,
entityResourceVaultTotalsHistorySequence = sequences.EntityResourceVaultTotalsHistorySequence,
entityResourceBalanceHistorySequence = sequences.EntityResourceBalanceHistorySequence,
vaultBalanceHistorySequence = sequences.VaultBalanceHistorySequence,
nonFungibleVaultEntryDefinitionSequence = sequences.NonFungibleVaultEntryDefinitionSequence,
nonFungibleVaultEntryHistorySequence = sequences.NonFungibleVaultEntryHistorySequence,
},
parameters,
cancellationToken: token);

await _connection.ExecuteAsync(cd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,8 @@
using System.Numerics;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using RadixDlt.NetworkGateway.Abstractions.Model;
using RadixDlt.NetworkGateway.Abstractions.StandardMetadata;
using RadixDlt.NetworkGateway.PostgresIntegration;
using RadixDlt.NetworkGateway.PostgresIntegration.Models;

#nullable disable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@

using RadixDlt.NetworkGateway.Abstractions;
using RadixDlt.NetworkGateway.Abstractions.Model;
using RadixDlt.NetworkGateway.PostgresIntegration.LedgerExtension;
using RadixDlt.NetworkGateway.PostgresIntegration.LedgerExtension.Processors;
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@
* permissions under this License.
*/

using RadixDlt.NetworkGateway.Abstractions;
using RadixDlt.NetworkGateway.Abstractions.Model;
using RadixDlt.NetworkGateway.Abstractions.Numerics;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
* permissions under this License.
*/

using Dapper;
using Microsoft.EntityFrameworkCore;
using RadixDlt.NetworkGateway.Abstractions;
using RadixDlt.NetworkGateway.Abstractions.Model;
Expand Down Expand Up @@ -269,7 +268,23 @@ private static async Task<IDictionary<long, PerEntityQueryResultRow>> Execute(
throw new InvalidOperationException("Neither resource filter nor cursors can be used if executing against multiple entities.");
}

var cd = new CommandDefinition(
var parameters = new
{
entityIds = entityIds.ToList(),
resourceEntityId = resourceEntityId,
fungibleResourcesPerEntity = configuration.FungibleResourcesPerEntity == 0 ? 0 : configuration.FungibleResourcesPerEntity + 1,
vaultsPerResource = configuration.VaultsPerResource == 0 ? 0 : configuration.VaultsPerResource + 1,
useVaultAggregation = configuration.VaultsPerResource > 0,
useResourceCursor = configuration.ResourceCursor is not null,
resourceCursorStateVersion = configuration.ResourceCursor?.StateVersion,
resourceCursorId = configuration.ResourceCursor?.Id,
useVaultCursor = configuration.VaultCursor is not null,
vaultCursorStateVersion = configuration.VaultCursor?.StateVersion,
vaultCursorId = configuration.VaultCursor?.Id,
atLedgerState = configuration.AtLedgerState,
};

var cd = DapperExtensions.CreateCommandDefinition(
@"
WITH variables AS (
SELECT
Expand Down Expand Up @@ -394,21 +409,7 @@ ORDER BY from_state_version DESC
LIMIT 1
) vault_balance_history ON var.use_vault_aggregation
ORDER BY resource_definitions.cursor DESC, vault_entry_definitions.cursor DESC",
new
{
entityIds = entityIds.ToList(),
resourceEntityId = resourceEntityId,
fungibleResourcesPerEntity = configuration.FungibleResourcesPerEntity == 0 ? 0 : configuration.FungibleResourcesPerEntity + 1,
vaultsPerResource = configuration.VaultsPerResource == 0 ? 0 : configuration.VaultsPerResource + 1,
useVaultAggregation = configuration.VaultsPerResource > 0,
useResourceCursor = configuration.ResourceCursor is not null,
resourceCursorStateVersion = configuration.ResourceCursor?.StateVersion,
resourceCursorId = configuration.ResourceCursor?.Id,
useVaultCursor = configuration.VaultCursor is not null,
vaultCursorStateVersion = configuration.VaultCursor?.StateVersion,
vaultCursorId = configuration.VaultCursor?.Id,
atLedgerState = configuration.AtLedgerState,
},
parameters,
cancellationToken: token);

var result = new Dictionary<long, PerEntityQueryResultRow>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
* permissions under this License.
*/

using Dapper;
using Microsoft.EntityFrameworkCore;
using RadixDlt.NetworkGateway.Abstractions;
using RadixDlt.NetworkGateway.Abstractions.Model;
Expand Down Expand Up @@ -283,7 +282,23 @@ private static async Task<IDictionary<long, PerEntityQueryResultRow>> Execute(
throw new InvalidOperationException("Neither resource filter nor cursors can be used if executing against multiple entities.");
}

var cd = new CommandDefinition(
var parameters = new
{
entityIds = entityIds.ToList(),
resourceEntityId = resourceEntityId,
nonFungibleResourcesPerEntity = configuration.NonFungibleResourcesPerEntity == 0 ? 0 : configuration.NonFungibleResourcesPerEntity + 1,
vaultsPerResource = configuration.VaultsPerResource == 0 ? 0 : configuration.VaultsPerResource + 1,
useVaultAggregation = configuration.VaultsPerResource > 0,
useResourceCursor = configuration.ResourceCursor is not null,
resourceCursorStateVersion = configuration.ResourceCursor?.StateVersion,
resourceCursorId = configuration.ResourceCursor?.Id,
useVaultCursor = configuration.VaultCursor is not null,
vaultCursorStateVersion = configuration.VaultCursor?.StateVersion,
vaultCursorId = configuration.VaultCursor?.Id,
atLedgerState = configuration.AtLedgerState,
};

var cd = DapperExtensions.CreateCommandDefinition(
@"
WITH variables AS (
SELECT
Expand Down Expand Up @@ -409,21 +424,7 @@ ORDER BY from_state_version DESC
LIMIT 1
) vault_balance_history ON var.use_vault_aggregation
ORDER BY definitions.cursor DESC, vault_entry_definition.cursor DESC",
new
{
entityIds = entityIds.ToList(),
resourceEntityId = resourceEntityId,
nonFungibleResourcesPerEntity = configuration.NonFungibleResourcesPerEntity == 0 ? 0 : configuration.NonFungibleResourcesPerEntity + 1,
vaultsPerResource = configuration.VaultsPerResource == 0 ? 0 : configuration.VaultsPerResource + 1,
useVaultAggregation = configuration.VaultsPerResource > 0,
useResourceCursor = configuration.ResourceCursor is not null,
resourceCursorStateVersion = configuration.ResourceCursor?.StateVersion,
resourceCursorId = configuration.ResourceCursor?.Id,
useVaultCursor = configuration.VaultCursor is not null,
vaultCursorStateVersion = configuration.VaultCursor?.StateVersion,
vaultCursorId = configuration.VaultCursor?.Id,
atLedgerState = configuration.AtLedgerState,
},
parameters,
cancellationToken: token);

var result = new Dictionary<long, PerEntityQueryResultRow>();
Expand Down
Loading

0 comments on commit 8cbaa73

Please sign in to comment.