Search code examples
postgresqlsqlkata

SqlKata Values for AsUpdate as column names


I have the following query generated by SqlKata:

UPDATE "temp"."dbe01_measures" SET "category2name" = 'category4name'.

The expected query is:

UPDATE "temp"."dbe01_measures" SET "category2name" = "category4name"

I want to use category4name as column name, not as value.

The code for this is:

var query = new Query(dimensionDataSource.Object)

var valuesToUpdateForQ1 = new Dictionary<string, object>
{
   {toOptionDataSourceMetadata.NameColumn, fromDimension.DimensionDisplayName}
};

query.AsUpdate(new ReadOnlyDictionary<string, object>(valuesToUpdateForQ1));

var sqlQueryString1 = new QueryFactory { Compiler = new PostgresCompiler() }.Compiler.Compile(query1).ToString();

Solution

  • You have to use the UnsafeLiteral to instruct the compiler to consume the value without any processing.

    var q = new Query("Table").AsUpdate(new Dictionary<string, object> {
        {"Id", 1},
        {"Value", Expressions.UnsafeLiteral("\"Col\"")}
    });
    

    This will produce the following:

    UPDATE "Table" SET "Id" = 1, "Value" = "Col"