mk3008 / Carbunql

Carbunql provides query parsing and building functionality.
https://mk3008.github.io/Carbunql/
MIT License
40 stars 3 forks source link

Column name conversion does not work when parsing an Expression #448

Closed mk3008 closed 3 weeks ago

mk3008 commented 3 weeks ago

When parsing an Expression, the property names are used as column names.

mk3008 commented 3 weeks ago

Model

public record StoreData : IDataRow
{
    public int StoreDataId { get; set; }
    public string StoreName { get; set; } = string.Empty;
    // interface property
    IDataSet IDataRow.DataSet { get; set; } = null!;
}

Test

[Fact]
public void SnakeCase_SelectColumn()
{
    var sd = Sql.DefineDataSet<StoreData>();

    var query = Sql.From(() => sd)
        .Select(() => new
        {
            sd.StoreDataId,
            sd.StoreName,
        });

    var actual = query.ToText();
    Output.WriteLine(actual);

    var expect = @"SELECT
sd.store_data_id,
sd.store_name
FROM
store_data AS sd";

    Assert.Equal(expect, actual, true, true, true);
}

Result

SELECT
    sd.StoreDataId,
    sd.StoreName
FROM
    store_data AS sd    

Only the table names are in snake case. Column names are not converted to snake case.

mk3008 commented 3 weeks ago

This symptom itself can be solved, but the handling of alias names becomes very complicated.

For example,

new {
a.SaleDataId
}

can be interpreted as

a.sales_data_id

, but if an alias like

new {
SalesDataId2 = a.SaleDataId
}

is used, it is unclear whether it should be interpreted as

a.sales_data_id as sales_data_id2

or

a.sales_data_id as SalesDataId2

It is possible to uniformly use snake case, but this does not support cases where you do not want to do so.

So, unfortunately, I will stop "processing property names to use as column names" altogether.