Skip to content

Add support for numbered placeholders#725

Open
alex3d wants to merge 3 commits into
WiseLibs:masterfrom
alex3d:master
Open

Add support for numbered placeholders#725
alex3d wants to merge 3 commits into
WiseLibs:masterfrom
alex3d:master

Conversation

@alex3d

@alex3d alex3d commented Nov 15, 2021

Copy link
Copy Markdown

This PR should add support for numbered placeholders (?1, ?2, ?3).

PS What lzz is assumed to be used with this project? https://github.com/mjspncr/lzz3? https://github.com/driedfruit/lzz? Probably this info could be added to the readme.

@alex3d
alex3d requested a review from JoshuaWise as a code owner November 15, 2021 08:32
@Prinzhorn

Copy link
Copy Markdown
Contributor

Related to #576

Could you add some more tests? What if there are gaps, e.g. SELECT ?3, ?15? How do you bind that? With an array or object? Or do both work?

See my comment here #576 (comment)

@alex3d

alex3d commented Nov 25, 2021

Copy link
Copy Markdown
Author

@Prinzhorn I've added a couple of new testcases.

How do you bind that? With an array or object? Or do both work?

Semantically you should treat numbered placeholders the same as unnamed placeholders (?). It is just a shortcut to reuse the same bind values. For example, INSERT INTO payments(payment_type, amount) VALUES (?1, ?), (?1, ?) + bind values ['cash', 100, 200].
? === increment current_index and insert anonymous bind value with index current_index
?n === insert anonymous bind value with index n and set current_index = max(current_index, n)

@7nik

7nik commented Apr 15, 2023

Copy link
Copy Markdown

wow, it is still open

I need to use the parameter multiple times, and it is a single parameter:
WHERE instr(lower(card.name), ?1) OR instr(lower(card.description), ?1)

It's stupid to wrap it into an object or pass it twice. But currently, I have no choice.

@takeseem

takeseem commented Jul 15, 2026

Copy link
Copy Markdown

SQLite ?NNN allows for adjusting the binding locations of data, enabling reusability and flexible binding. ?NNN supported since 2004 v3.0.7.

better-sqlite3 initially didn’t know about this feature, so it might be difficult to implement it properly, especially in scenarios where mixing ?, ?NNN, @, :, $.

However, this is also my first time encountering $NNN. When writing backend JDBC code before, I really wished there was such a feature (which allows adjusting the binding locations) so that we wouldn’t have to wrap arrays again to adapt them for another SQL query.

But this is not a big problem. At worst, you can repackage an array or use named parameters directly.


SQLite ?NNN 允许调整数据的绑定位置,这样可以复用数据并灵活绑定。?NNN 从 2004-09-18 (3.0.7) 开始支持

better-sqlite3 主要是一开始不知道这个特性,估计要支持很麻烦,特别是实现混编 ?、?NNN 和 命名参数 的场景。

不过我也是第一次知道 $NNN,以前写后端 jdbc 时就特别希望有这个特性(能调整绑定位置)这样就不用再封装一次数组以适应另一个 sql。

不过这也不是什么大问题,大不了重新包装一个数组,或者直接使用命名参数

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants