Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Columns that contain dots are unexpectedly escaped #596

Closed
jordy758 opened this issue Aug 17, 2022 · 1 comment
Closed

Columns that contain dots are unexpectedly escaped #596

jordy758 opened this issue Aug 17, 2022 · 1 comment

Comments

@jordy758
Copy link

jordy758 commented Aug 17, 2022

Whenever a table has a column that contains a dot, it's escaped in way I don't want it to be escaped. Note: I am using the SqlServerCompiler class and have not tested it for other drivers.

Example, your table is products and it contains the column product.id and you use the query builder to build an insert query like this.

_queryFactory.Query("products").AsInsert(new { "product.id" = 5 })

It will generate a SQL query like: INSERT INTO products ([product].[id]) VALUES (5) instead of the (in my case) desired INSERT INTO products ([product.id]) VALUES (5). The escaping happens in the Wrap method of the compiler.

It's a difficult issue, because in some cases you actually want the generated query and in some cases the desired query. I've added a workaround like this.

public class CustomSqlServerCompiler : SqlServerCompiler
{
    public bool EscapeDots { get; set; } = false;

    public override string Wrap(string value)
    {
        if (!EscapeDots) return base.Wrap(value);

        value = value.Replace(".", "_______");
        value = base.Wrap(value);
        return value.Replace("_______", ".");
    }
}

This way I can prevent the behaviour when I want it to by changing the property EscapeDots. However it's quite a dirty solution (if a table would have the ___'s in their table it would do unexpected things). Does anyone know a better approach? And are there plans to implement a 'fix' in the project itself?

@jordy758 jordy758 changed the title Columns that contain dots are escaped Columns that contain dots are unexpectedly escaped Aug 17, 2022
@ahmad-moussawi
Copy link
Contributor

Since this is an uncommon thing, the best way is what you've did, to create a CustomCompiler and override the Wrap method as per your need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants