<commit_before>﻿using System;
using System.Data;
using System.Data.SqlClient;
using NServiceBus.Saga;

class SagaPersister : ISagaPersister
{
    string connectionString;
    string schema;
    string endpointName;

    public SagaPersister(string connectionString, string schema, string endpointName)
    {
        this.connectionString = connectionString;
        this.schema = schema;
        this.endpointName = endpointName;
    }

    public void Save(IContainSagaData data)
    {
        var type = data.GetType();
        var saveComand = string.Format(@"
INSERT INTO [{0}].[{1}.{2}] 
(
    Id, 
    Originator, 
    OriginalMessageId, 
    Data
) 
VALUES 
(
    @Id, 
    @Originator, 
    @OriginalMessageId, 
    @Data
)", schema, endpointName, SagaTableNameBuilder.GetTableSuffix(type));
        using (var connection = SqlHelpers.New(connectionString))
        using (var command = new SqlCommand(saveComand, connection))
        {
            command.AddParameter("Id", data.Id);
            command.AddParameter("OriginalMessageId", data.OriginalMessageId);
            command.AddParameter("Originator", data.Originator);
            command.AddParameter("Data", SagaSerializer.ToXml(data));
            command.ExecuteNonQuery();
        }
    }

    public void Update(IContainSagaData data)
    {
        var type = data.GetType();
        var saveComand = string.Format(@"
UPDATE [{0}].[{1}.{2}] 
SET
    Originator = @Originator, 
    OriginalMessageId = @OriginalMessageId, 
    Data = @Data
WHERE
    Id = @Id
", schema, endpointName, SagaTableNameBuilder.GetTableSuffix(type));
        using (var connection = SqlHelpers.New(connectionString))
        using (var command = new SqlCommand(saveComand, connection))
        {
            command.AddParameter("Id", data.Id);
            command.AddParameter("OriginalMessageId", data.OriginalMessageId);
            command.AddParameter("Originator", data.Originator);
            command.Parameters.AddWithValue("Data", SagaSerializer.ToXml(data));
            command.ExecuteNonQuery();
        }
    }

    public TSagaData Get<TSagaData>(Guid sagaId) where TSagaData : IContainSagaData
    {
        var getComand = string.Format(@"
SELECT
    Id,
    Originator,
    OriginalMessageId,
    Data
FROM  [{0}].[{1}.{2}] 
WHERE Id = @Id
", schema, endpointName, SagaTableNameBuilder.GetTableSuffix(typeof(TSagaData)));
        using (var connection = SqlHelpers.New(connectionString))
        using (var command = new SqlCommand(getComand, connection))
        {
            command.AddParameter("Id", sagaId);
            return GetSagaData<TSagaData>(command);
        }
    }

    public TSagaData Get<TSagaData>(string propertyName, object propertyValue) where TSagaData : IContainSagaData
    {
        var getComand = string.Format(@"
SELECT
    Id,
    Originator,
    OriginalMessageId,
    Data
FROM  [{0}].[{1}.{2}] 
WHERE [Data].exist('/Data/{3}[.= ""{4}""]') = 1
", schema, endpointName, SagaTableNameBuilder.GetTableSuffix(typeof(TSagaData)), propertyName, propertyValue);
        using (var connection = SqlHelpers.New(connectionString))
        using (var command = new SqlCommand(getComand, connection))
        {
            //command.AddParameter("Id", "");
            return GetSagaData<TSagaData>(command);
        }
    }



    static TSagaData GetSagaData<TSagaData>(SqlCommand command) where TSagaData : IContainSagaData
    {<commit_msg>parameterize xml propertyValue<commit_after>﻿using System;
using System.Data;
using System.Data.SqlClient;
using NServiceBus.Saga;

class SagaPersister : ISagaPersister
{
    string connectionString;
    string schema;
    string endpointName;

    public SagaPersister(string connectionString, string schema, string endpointName)
    {
        this.connectionString = connectionString;
        this.schema = schema;
        this.endpointName = endpointName;
    }

    public void Save(IContainSagaData data)
    {
        var type = data.GetType();
        var saveComand = string.Format(@"
INSERT INTO [{0}].[{1}.{2}] 
(
    Id, 
    Originator, 
    OriginalMessageId, 
    Data
) 
VALUES 
(
    @Id, 
    @Originator, 
    @OriginalMessageId, 
    @Data
)", schema, endpointName, SagaTableNameBuilder.GetTableSuffix(type));
        using (var connection = SqlHelpers.New(connectionString))
        using (var command = new SqlCommand(saveComand, connection))
        {
            command.AddParameter("Id", data.Id);
            command.AddParameter("OriginalMessageId", data.OriginalMessageId);
            command.AddParameter("Originator", data.Originator);
            command.AddParameter("Data", SagaSerializer.ToXml(data));
            command.ExecuteNonQuery();
        }
    }

    public void Update(IContainSagaData data)
    {
        var type = data.GetType();
        var saveComand = string.Format(@"
UPDATE [{0}].[{1}.{2}] 
SET
    Originator = @Originator, 
    OriginalMessageId = @OriginalMessageId, 
    Data = @Data
WHERE
    Id = @Id
", schema, endpointName, SagaTableNameBuilder.GetTableSuffix(type));
        using (var connection = SqlHelpers.New(connectionString))
        using (var command = new SqlCommand(saveComand, connection))
        {
            command.AddParameter("Id", data.Id);
            command.AddParameter("OriginalMessageId", data.OriginalMessageId);
            command.AddParameter("Originator", data.Originator);
            command.Parameters.AddWithValue("Data", SagaSerializer.ToXml(data));
            command.ExecuteNonQuery();
        }
    }

    public TSagaData Get<TSagaData>(Guid sagaId) where TSagaData : IContainSagaData
    {
        var getComand = string.Format(@"
SELECT
    Id,
    Originator,
    OriginalMessageId,
    Data
FROM  [{0}].[{1}.{2}] 
WHERE Id = @Id
", schema, endpointName, SagaTableNameBuilder.GetTableSuffix(typeof(TSagaData)));
        using (var connection = SqlHelpers.New(connectionString))
        using (var command = new SqlCommand(getComand, connection))
        {
            command.AddParameter("Id", sagaId);
            return GetSagaData<TSagaData>(command);
        }
    }

    public TSagaData Get<TSagaData>(string propertyName, object propertyValue) where TSagaData : IContainSagaData
    {
        var getComand = string.Format(@"
SELECT
    Id,
    Originator,
    OriginalMessageId,
    Data
FROM  [{0}].[{1}.{2}] 
WHERE [Data].exist('/Data/{3}[.= (sql:variable(""@propertyValue""))]') = 1
", schema, endpointName, SagaTableNameBuilder.GetTableSuffix(typeof(TSagaData)), propertyName);
        using (var connection = SqlHelpers.New(connectionString))
        using (var command = new SqlCommand(getComand, connection))
        {
            command.AddParameter("propertyValue", propertyValue);
            return GetSagaData<TSagaData>(command);
        }
    }


    static TSagaData GetSagaData<TSagaData>(SqlCommand command) where TSagaData : IContainSagaData
    {
{'fraction_non_alphanumeric': 0.1143394044521538, 'fraction_numerical': 0.004191962995085285, 'mean_word_length': 2.997111496244945, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4491293', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1993, 'n_words': 408}