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.

Merge Scripting and Parameterization phases in dynamic sql

See original GitHub issue

Currently dynamic sql is managed in two phases, one for scripting and one for sql parameterization. This leads to two different languages in a dynamic sql at the same time and is a common source of mistakes.

For example, using velocity scripting we can do something like:

SELECT * from mytable Where mycolumn = '$_parameter.mymethod($_parameter.value)'

but we can’t do:

SELECT * from mytable Where mycolumn = @{$_parameter.mymethod($_parameter.value)}

Because SQL parameter binding language is not velocity, but a propietary binding language.

The problem goes worst in loops, because the scope of the scripting variables and sql parameter are not the same. So <bind /> will not work in loops as expected.

My proposal is to extract any sql parameter in the scripting phase and cache it in the BoundSql object as proposed some time ago by Eduardo, then the sql parameterization will use the already extracted values directly by index, so we can use arbitrary scripting expressions.

For example:

SELECT * from mytable Where mycolumn = $sql.bind($_parameter.mymethod($_parameter.value))

the hypothetical $sql.bind method should support more parameters like type (IN,OUT,INOUT), jsdbType, etc…

Also in the case of velocity, the method can be replaced by a more friendly syntax (macro):

#set ($p = $_parameter)
SELECT * from mytable Where mycolumn = #param( $p.mymethod( $p.value ) )

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Drizzt321commented, May 26, 2016

Ok, for those who find this, I solved my problem by having a utility method which returns a Map with the dynamic field name/value set, and iterated through the map. Here’s a mapper fragement. getModifiedFields() returns a Map<String,Object>. I have it with a bind so that I can be sure that it iterates the same way for each for loop. For actual implementation, I’m using a TreeMap, but any kind of map with a consistent iteration should work fine.

INSERT INTO ${tableName}
<bind name="data" value="entity.getModifiedFields()"/>
<foreach item="value" index="name" collection="data" open="(" separator="," close=")">
    ${name}
</foreach>
VALUES
<foreach item="value" index="name" collection="data" open="(" separator="," close=")">
    #{value}
</foreach>
0reactions
Drizzt321commented, May 21, 2016

Ah, I see. That’s too bad. I’m actually a little surprised this hasn’t come up earlier than now. Maybe I’ll end up having to change my Mapper configs to try and work around it so I won’t have this bind problem so I can properly use prepared statement output.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic SQL in SQL Server
In this article, we will review how to construct and execute dynamic SQL statements in SQL Server with different examples.
Read more >
SA0172 : The dynamic SQL is constructed using external ...
The topic describes the SA0172 analysis rule. Message. The dynamic SQL is constructed using external parameters, which is not ensured to be safe....
Read more >
How to use Dynamic SQL in BigQuery - Towards Data Science
You can run a dynamic SQL statement using EXECUTE IMMEDIATE. ... We can combine the above three ideas — INFORMATION_SCHEMA, scripting, ...
Read more >
Replication Merge Agent - SQL Server - Microsoft Learn
This parameter is only valid if the publication is set to always have a snapshot available for new or reinitialized subscriptions. -Subscriber ...
Read more >
Dynamic SQL Merge - SQLServerCentral
Most seasoned database professionals have a "bag of tricks" collection of useful SQL scripts and stored procedures which are utilized to quickly ...
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