question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExpressionQueryImpl<TElement>.ToString() doesn't update SQL text when parameter's values changes

See original GitHub issue

Hello, when we build queries with linq2db we get an ExpressionQueryImpl<TElement> object that implements IQueryable<T> interface. For example:

...
DataConnection.DefaultSettings = new MySettings();
Configuration.Sql.GenerateFinalAliases = true;

using var db = new WipDB("Wip");
var wipCte = new WipCte(db);

var ncCodeBo = "NCCodeBO:8110,SETUP_OSCILLOSCO";

var result = from item in wipCte.AllowedNcCode() where item.NcCodeBo == ncCodeBo select item;
var sql = result.ToString();
Console.WriteLine(sql);
..

Output:

-- Wip Oracle.Managed Oracle
DECLARE @ncCodeBo_1 Varchar2(30) -- String
SET     @ncCodeBo_1 = 'NCCodeBO:8110,SETUP_OSCILLOSCO'

WITH AllowedNcCode (Handle, NcCodeColumn, Description)
AS
(
        SELECT DISTINCT
                ncCode.HANDLE as NcCodeBo,
                ncCode.NC_CODE as NcCode,
                ncCode.DESCRIPTION as NcCodeDescription
        FROM
                WIPUSER.NC_CODE ncCode
                        INNER JOIN WIPUSER.NC_GROUP_MEMBER ncGroupMember ON ncCode.HANDLE = ncGroupMember.NC_CODE_OR_GROUP_GBO
        WHERE
                ((ncGroupMember.NC_GROUP_BO IS NOT NULL AND ncGroupMember.NC_GROUP_BO = 'NCGroupBO:' || ncCode.SITE || ',CATAN_AUTO' OR ncGroupMember.NC_GROUP_BO IS NOT NULL AND ncGroupMember.NC_GROUP_BO = 'NCGroupBO:' || ncCode.SITE || ',CATAN_MAN') OR ncGroupMember.NC_GROUP_BO IS NOT NULL AND ncGroupMember.NC_GROUP_BO = 'NCGroupBO:' || ncCode.SITE || ',CATAN_ALL')
)
SELECT
        item.Handle,
        item.NcCodeColumn,
        item.Description
FROM
        AllowedNcCode item
WHERE
        item.Handle = :ncCodeBo_1

If I change ncCodeBo and I print the SQL, I get the previous one because the SQL query is built when I assign the query to the variable result:

ncCodeBo = "NCCodeBO:8110,SETUP_OSCILLOSCO2";
sql = result.ToString();
Console.WriteLine(sql);
-- Wip Oracle.Managed Oracle
DECLARE @ncCodeBo_1 Varchar2(30) -- String
SET     @ncCodeBo_1 = 'NCCodeBO:8110,SETUP_OSCILLOSCO' -- It should change to "NCCodeBO:8110,SETUP_OSCILLOSCO2"

WITH AllowedNcCode (Handle, NcCodeColumn, Description)
AS
...

My question is: is there a way to get an ExpressionQueryImpl<TElement> with a method to call to set only the changing part of the query? For example the parameter ncCodeBo?

If the query is really huge, rebuilding the query only to change few parameters is time consuming.

Thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Feb 6, 2020

It is a bug in ToString(), we have to repopulate parameters for this call. Will fix that. First query is slow because we parse Expression Tree and provide optimizations. Second query just reuse parsed AST and generated SQL.

0reactions
filippobottegacommented, Feb 6, 2020

Hello @sdanyliv , Sorry, I don’t really need to log “all queries”, and sometime I need the SQL query without running the query. I have created this workaround that works very well:

DataConnection.DefaultSettings = new MySettings();
Configuration.Sql.GenerateFinalAliases = true;

using var db = new WipDB("Wip");
var wipCte = new WipCte(db);

var ncCodeBo = "NCCodeBO:8110,SETUP_OSCILLOSCO";

var queryBuilder = (Func<IQueryable<AllowedNcCodeOutput>>) (() =>  from item in wipCte.AllowedNcCode() where item.NcCodeBo == ncCodeBo select item);
var result = queryBuilder.Invoke();
var sql = result.ToString(); // Very slow...
Console.WriteLine(sql);

ncCodeBo = "NCCodeBO:8110,SETUP_OSCILLOSCO2";
result = queryBuilder.Invoke();
sql = result.ToString(); // Very fast!
Console.WriteLine(sql);

I have discovered that only the first call to result.ToString() is slow but the second call is fast and gives me the right result. You should probably update the documentation to explain the ToString() behavior and to show how to update the ToString() result if parameters change.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vb.net - Parameterized SQL update query doesn't work
My code doesn't work in updating student records using parameterized SQL query in VB.Net. Dim result As Integer cmd = New OleDbCommand("UPDATE ......
Read more >
SSRS cascading parameter doesn't change value when ...
I overtyped it with the value I want to use instead and run the report where the revised text appears. Screenshot3. I then...
Read more >
Unable to Update Record in Access Database Using C# ...
Simplified the UPDATE query to only update one field; Changed the order of the parameters to match the order in the SQL statement....
Read more >
Using multi-value parameters in SSRS
This article mentions usage of the multi-value parameter in the SSRS and it also include how to design a report with Report Builder....
Read more >
Using Prepared Statements - JDBC Basics
This JDBC Java tutorial describes how to use JDBC API to create, insert into, update, and query tables. You will also learn how...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found